void main_loop(void)
{
    int r;

#ifdef CONFIG_HAX
    if (hax_enabled())
        hax_sync_vcpus();
#endif

    for (;;) {
        do {
#ifdef CONFIG_PROFILER
            int64_t ti;
#endif
            tcg_cpu_exec();
#ifdef CONFIG_PROFILER
            ti = profile_getclock();
#endif
            main_loop_wait(qemu_calculate_timeout());
#ifdef CONFIG_PROFILER
            dev_time += profile_getclock() - ti;
#endif

            qemu_log_rotation_poll();

        } while (vm_can_run());

        if (qemu_debug_requested())
            vm_stop(EXCP_DEBUG);
        if (qemu_shutdown_requested()) {
            if (no_shutdown) {
                vm_stop(0);
                no_shutdown = 0;
            } else {
                if (savevm_on_exit != NULL) {
                  /* Prior to saving VM to the snapshot file, save HW config
                   * settings for that VM, so we can match them when VM gets
                   * loaded from the snapshot. */
                  snaphost_save_config(savevm_on_exit);
                  do_savevm(cur_mon, savevm_on_exit);
                }
                break;
            }
        }
        if (qemu_reset_requested()) {
            pause_all_vcpus();
            qemu_system_reset();
            resume_all_vcpus();
        }
        if (qemu_powerdown_requested())
            qemu_system_powerdown();
        if ((r = qemu_vmstop_requested()))
            vm_stop(r);
    }
    pause_all_vcpus();
}
Ejemplo n.º 2
0
void do_savevm(const char *name)
{
    QEMUFile *f;
    int saved_vm_running, ret;

    f = qemu_fopen(name, "wb");
    
    /* ??? Should this occur after vm_stop?  */
    qemu_aio_flush();

    saved_vm_running = vm_running;
    vm_stop(0);

    if (!f) {
        fprintf(logfile, "Failed to open savevm file '%s'\n", name);
        goto the_end;
    }
    
    ret = qemu_savevm_state(f);
    qemu_fclose(f);

    if (ret < 0)
        fprintf(logfile, "Error %d while writing VM to savevm file '%s'\n",
                ret, name);

 the_end:
    if (saved_vm_running)
        vm_start();

    return;
}
Ejemplo n.º 3
0
static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
    int is_read)
{
    BlockErrorAction action = bdrv_get_on_error(req->dev->bs, is_read);
    VirtIOBlock *s = req->dev;

    if (action == BLOCK_ERR_IGNORE) {
        bdrv_emit_qmp_error_event(s->bs, BDRV_ACTION_IGNORE, is_read);
        return 0;
    }

    if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
            || action == BLOCK_ERR_STOP_ANY) {
        req->next = s->rq;
        s->rq = req;
        bdrv_emit_qmp_error_event(s->bs, BDRV_ACTION_STOP, is_read);
        vm_stop(RUN_STATE_IO_ERROR);
        bdrv_iostatus_set_err(s->bs, error);
    } else {
        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
        bdrv_acct_done(s->bs, &req->acct);
        g_free(req);
        bdrv_emit_qmp_error_event(s->bs, BDRV_ACTION_REPORT, is_read);
    }

    return 1;
}
Ejemplo n.º 4
0
void migrate_fd_put_ready(void *opaque)
{
    FdMigrationState *s = opaque;

    if (s->state != MIG_STATE_ACTIVE) {
        DPRINTF("put_ready returning because of non-active state\n");
        return;
    }

    DPRINTF("iterate\n");
    if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
        int state;
        int old_vm_running = vm_running;

        DPRINTF("done iterating\n");
        vm_stop(VMSTOP_MIGRATE);

        if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
            if (old_vm_running) {
                vm_start();
            }
            state = MIG_STATE_ERROR;
        } else {
            state = MIG_STATE_COMPLETED;
        }
        if (migrate_fd_cleanup(s) < 0) {
            if (old_vm_running) {
                vm_start();
            }
            state = MIG_STATE_ERROR;
        }
        s->state = state;
        notifier_list_notify(&migration_state_notifiers);
    }
}
int exec_start_incoming_migration(const char *command)
{
    int ret;
    QEMUFile *f;

    dprintf("Attempting to start an incoming migration\n");
    f = qemu_popen_cmd(command, "r");
    if(f == NULL) {
        dprintf("Unable to apply qemu wrapper to popen file\n");
        return -errno;
    }
    vm_stop(0); /* just in case */
    ret = qemu_loadvm_state(f);
    if (ret < 0) {
        fprintf(stderr, "load of migration failed\n");
        goto err;
    }
    qemu_announce_self();
    dprintf("successfully loaded vm state\n");
    vm_start();
    qemu_fclose(f);
    return 0;

err:
    qemu_fclose(f);
    return -errno;
}
Ejemplo n.º 6
0
void migrate_fd_put_ready(void *opaque)
{
    FdMigrationState *s = opaque;

    if (s->state != MIG_STATE_ACTIVE) {
        dprintf("put_ready returning because of non-active state\n");
        return;
    }

    dprintf("iterate\n");
    if (qemu_savevm_state_iterate(s->file) == 1) {
        int state;
        dprintf("done iterating\n");
        vm_stop(0);

        bdrv_flush_all();
        if ((qemu_savevm_state_complete(s->file)) < 0) {
            vm_start();
            state = MIG_STATE_ERROR;
        } else {
            state = MIG_STATE_COMPLETED;
        }
        migrate_fd_cleanup(s);
        s->state = state;
    }
}
Ejemplo n.º 7
0
/* Free resources used by a router instance */
static int c3725_delete_instance(vm_instance_t *vm)
{
   c3725_t *router = VM_C3725(vm);
   int i;

   /* Stop all CPUs */
   if (vm->cpu_group != NULL) {
      vm_stop(vm);
      
      if (cpu_group_sync_state(vm->cpu_group) == -1) {
         vm_error(vm,"unable to sync with system CPUs.\n");
         return(FALSE);
      }
   }

   /* Remove NIO bindings */
   for(i=0;i<vm->nr_slots;i++)
      vm_slot_remove_all_nio_bindings(vm,i);

   /* Shutdown all Network Modules */
   vm_slot_shutdown_all(vm);

   /* Free mainboard EEPROM */
   cisco_eeprom_free(&router->mb_eeprom);

   /* Free all resources used by VM */
   vm_free(vm);

   /* Free the router structure */
   free(router);
   return(TRUE);
}
Ejemplo n.º 8
0
/* does a state transition even if the VM is already stopped,
   current state is forgotten forever */
void vm_stop_force_state(RunState state)
{
    if (runstate_is_running()) {
        vm_stop(state);
    } else {
        runstate_set(state);
    }
}
Ejemplo n.º 9
0
void qmp_stop(Error **errp)
{
    if (runstate_check(RUN_STATE_INMIGRATE)) {
        autostart = 0;
    } else {
        vm_stop(RUN_STATE_PAUSED);
    }
}
Ejemplo n.º 10
0
static void migrate_ft_trans_error(FdMigrationState *s)
{
    ft_mode = FT_ERROR;
    qemu_savevm_state_cancel(s->mon, s->file);
    migrate_fd_error(s);
    /* we need to set vm running to avoid assert in virtio-net */
    vm_start();
    event_tap_unregister();
    vm_stop(0);
}
Ejemplo n.º 11
0
/* does a state transition even if the VM is already stopped,
   current state is forgotten forever */
void vm_stop_force_state(RunState state)
{
    if (runstate_is_running()) {
        vm_stop(state);
    } else {
        //if(!mthread){
	  runstate_set(state);
	//}
    }
}
Ejemplo n.º 12
0
/* does a state transition even if the VM is already stopped,
   current state is forgotten forever */
int vm_stop_force_state(RunState state)
{
    if (runstate_is_running()) {
        return vm_stop(state);
    } else {
        runstate_set(state);
        /* Make sure to return an error if the flush in a previous vm_stop()
         * failed. */
        return bdrv_flush_all();
    }
}
Ejemplo n.º 13
0
Archivo: cpus.c Proyecto: linuxcer/qemu
bool cpu_exec_all(void)
{
    int r;

    /* Account partial waits to the vm_clock.  */
    qemu_clock_warp(vm_clock);

    if (next_cpu == NULL) {
        next_cpu = first_cpu;
    }
    for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
        CPUState *env = next_cpu;

        qemu_clock_enable(vm_clock,
                          (env->singlestep_enabled & SSTEP_NOTIMER) == 0);

#ifndef CONFIG_IOTHREAD
        if (qemu_alarm_pending()) {
            break;
        }
#endif
        if (cpu_can_run(env)) {
            if (kvm_enabled()) {
                r = kvm_cpu_exec(env);
                qemu_kvm_eat_signals(env);
            } else {
                r = tcg_cpu_exec(env);
            }
            if (r == EXCP_TRIPLE) {
                cpu_dump_state(env, stderr, fprintf, 0);
                fprintf(stderr, "Triple fault.  Halting for inspection via"
                        " QEMU monitor.\n");
                if (gdbserver_running())
                    r = EXCP_DEBUG;
                else {
                    vm_stop(0);
                    break;
                }
            }
            if (r == EXCP_DEBUG) {
                cpu_handle_guest_debug(env);
                break;
            }
        } else if (env->stop || env->stopped) {
            break;
        }
    }
    exit_request = 0;
    return !all_cpu_threads_idle();
}
Ejemplo n.º 14
0
static void handle_event(int event)
{
    static bool logged;

    if (event & ~PVPANIC_PANICKED && !logged) {
        qemu_log_mask(LOG_GUEST_ERROR, "pvpanic: unknown event %#x.\n", event);
        logged = true;
    }

    if (event & PVPANIC_PANICKED) {
        panicked_mon_event("pause");
        vm_stop(RUN_STATE_GUEST_PANICKED);
        return;
    }
}
Ejemplo n.º 15
0
void qmp_stop(Error **errp)
{
    /* if there is a dump in background, we should wait until the dump
     * finished */
    if (dump_in_progress()) {
        error_setg(errp, "There is a dump in process, please wait.");
        return;
    }

    if (runstate_check(RUN_STATE_INMIGRATE)) {
        autostart = 0;
    } else {
        vm_stop(RUN_STATE_PAUSED);
    }
}
Ejemplo n.º 16
0
/* Free resources used by a test instance */
static int ppc32_vmtest_delete_instance(vm_instance_t *vm)
{
   /* Stop all CPUs */
   if (vm->cpu_group != NULL) {
      vm_stop(vm);
      
      if (cpu_group_sync_state(vm->cpu_group) == -1) {
         vm_error(vm,"unable to sync with system CPUs.\n");
         return(FALSE);
      }
   }

   /* Free all resources used by VM */
   vm_free(vm);
   return(TRUE);
}
Ejemplo n.º 17
0
/* Send a gdb syscall request.
   This accepts limited printf-style format specifiers, specifically:
    %x  - target_ulong argument printed in hex.
    %lx - 64-bit argument printed in hex.
    %s  - string pointer (target_ulong) and length (int) pair.  */
void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
{
    va_list va;
    char *p;
    char *p_end;
    target_ulong addr;
    uint64_t i64;
    GDBState *s;

    s = gdbserver_state;
    if (!s)
        return;
    s->current_syscall_cb = cb;
#ifndef CONFIG_USER_ONLY
    vm_stop(RUN_STATE_DEBUG);
#endif
    va_start(va, fmt);
    p = s->syscall_buf;
    p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
    *(p++) = 'F';
    while (*fmt) {
        if (*fmt == '%') {
            fmt++;
            switch (*fmt++) {
            case 'x':
                addr = va_arg(va, target_ulong);
                p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
                break;
            case 'l':
                if (*(fmt++) != 'x')
                    goto bad_format;
                i64 = va_arg(va, uint64_t);
                p += snprintf(p, p_end - p, "%" PRIx64, i64);
                break;
            case 's':
                addr = va_arg(va, target_ulong);
                p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
                              addr, va_arg(va, int));
                break;
            default:
            bad_format:
                fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
                        fmt - 1);
                break;
            }
        } else {
static void tcp_accept_incoming_migration(void *opaque)
{
    struct sockaddr_in addr;
    socklen_t addrlen = sizeof(addr);
    int s = (unsigned long)opaque;
    QEMUFile *f;
    int c, ret;

    do {
        c = accept(s, (struct sockaddr *)&addr, &addrlen);
    } while (c == -1 && socket_error() == EINTR);

    dprintf("accepted migration\n");

    if (c == -1) {
        fprintf(stderr, "could not accept migration connection\n");
        return;
    }

    f = qemu_fopen_socket(c);
    if (f == NULL) {
        fprintf(stderr, "could not qemu_fopen socket\n");
        goto out;
    }

    vm_stop(0); /* just in case */
    ret = qemu_loadvm_state(f);
    if (ret < 0) {
        fprintf(stderr, "load of migration failed\n");
        goto out_fopen;
    }
    qemu_announce_self();
    dprintf("successfully loaded vm state\n");

    /* we've successfully migrated, close the server socket */
    qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
    close(s);

    vm_start();

out_fopen:
    qemu_fclose(f);
out:
    close(c);
}
Ejemplo n.º 19
0
/* Generic signal handler */
void signal_gen_handler(int sig)
{
    switch(sig) {
    case SIGHUP:
        /* For future use */
        break;

    case SIGQUIT:
        /* save VM context */
        vm_save_state = TRUE;
        break;

    /* Handle SIGPIPE by ignoring it */
    case SIGPIPE:
        fprintf(stderr,"Error: unwanted SIGPIPE.\n");
        break;

    case SIGINT:
        /* CTRL+C has been pressed */
        if (hypervisor_mode)
            hypervisor_stopsig();
        else {
            /* In theory, this shouldn't happen thanks to VTTY settings */
            vm_instance_t *vm;

            if ((vm = vm_acquire("default")) != NULL) {
                /* Only forward ctrl-c if user has requested local terminal */
                if (vm->vtty_con_type == VTTY_TYPE_TERM) {
                    vtty_store_ctrlc(vm->vtty_con);
                } else {
                    vm_stop(vm);
                }
                vm_release(vm);
            } else {
                fprintf(stderr,"Error: Cannot acquire instance handle.\n");
            }
        }
        break;

    default:
        fprintf(stderr,"Unhandled signal %d\n",sig);
    }
}
Ejemplo n.º 20
0
/* Stop a test instance */
static int ppc32_vmtest_stop_instance(vm_instance_t *vm)
{
   printf("\nPPC32_VMTEST '%s': stopping simulation.\n",vm->name);
   vm_log(vm,"PPC32_VMTEST_STOP","stopping simulation.\n");

   /* Stop all CPUs */
   if (vm->cpu_group != NULL) {
      vm_stop(vm);
      
      if (cpu_group_sync_state(vm->cpu_group) == -1) {
         vm_error(vm,"unable to sync with system CPUs.\n");
         return(-1);
      }
   }

   /* Free resources that were used during execution to emulate hardware */
   vm_hardware_shutdown(vm);
   return(0);
}
Ejemplo n.º 21
0
static int virtio_blk_handle_write_error(VirtIOBlockReq *req, int error)
{
    BlockInterfaceErrorAction action = drive_get_onerror(req->dev->bs);
    VirtIOBlock *s = req->dev;

    if (action == BLOCK_ERR_IGNORE)
        return 0;

    if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
            || action == BLOCK_ERR_STOP_ANY) {
        req->next = s->rq;
        s->rq = req;
        vm_stop(0);
    } else {
        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
    }

    return 1;
}
Ejemplo n.º 22
0
static void exec_accept_incoming_migration(void *opaque)
{
    QEMUFile *f = opaque;
    int ret;

    vm_stop(0); /* just in case */
    ret = qemu_loadvm_state(f);
    if (ret < 0) {
        fprintf(stderr, "load of migration failed\n");
        goto err;
    }
    qemu_announce_self();
    dprintf("successfully loaded vm state\n");
    /* we've successfully migrated, close the fd */
    qemu_set_fd_handler2(qemu_popen_fd(f), NULL, NULL, NULL, NULL);
    vm_start();

err:
    qemu_fclose(f);
}
Ejemplo n.º 23
0
static void tcp_accept_incoming_migration(void *opaque)
{
    SockAddress  addr;
    int s = (unsigned long)opaque;
    QEMUFile *f;
    int c, ret;

    c = socket_accept(s, &addr);
    dprintf("accepted migration\n");

    if (c == -1) {
        fprintf(stderr, "could not accept migration connection\n");
        return;
    }

    f = qemu_fopen_socket(c);
    if (f == NULL) {
        fprintf(stderr, "could not qemu_fopen socket\n");
        goto out;
    }

    vm_stop(0); /* just in case */
    ret = qemu_loadvm_state(f);
    if (ret < 0) {
        fprintf(stderr, "load of migration failed\n");
        goto out_fopen;
    }
    qemu_announce_self();
    dprintf("successfully loaded vm state\n");

    /* we've successfully migrated, close the server socket */
    qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
    socket_close(s);

    vm_start();

out_fopen:
    qemu_fclose(f);
out:
    socket_close(c);
}
Ejemplo n.º 24
0
void do_loadvm(const char *name)
{
    QEMUFile *f;
    int saved_vm_running, ret;

    /* Flush all IO requests so they don't interfere with the new state.  */
    qemu_aio_flush();

    saved_vm_running = vm_running;
    vm_stop(0);

    /* restore the VM state */
    f = qemu_fopen(name, "rb");
    if (!f) {
        fprintf(logfile, "Could not open VM state file\n");
        goto the_end;
    }

    ret = qemu_loadvm_state(f);
    qemu_fclose(f);
    if (ret < 0) {
        fprintf(logfile, "Error %d while loading savevm file '%s'\n",
                ret, name);
        goto the_end; 
    }

#if 0 
    /* del tmp file */
    if (unlink(name) == -1)
        fprintf(stderr, "delete tmp qemu state file failed.\n");
#endif


 the_end:
    if (saved_vm_running)
        vm_start();
}
Ejemplo n.º 25
0
static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
    int is_read)
{
    BlockErrorAction action = bdrv_get_on_error(req->dev->bs, is_read);
    VirtIOBlock *s = req->dev;

    if (action == BLOCK_ERR_IGNORE) {
        bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
        return 0;
    }

    if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
            || action == BLOCK_ERR_STOP_ANY) {
        req->next = s->rq;
        s->rq = req;
        bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
        vm_stop(0);
    } else {
        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
        bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
    }

    return 1;
}
Ejemplo n.º 26
0
/* Stop a Cisco 3725 instance */
static int c3725_stop_instance(vm_instance_t *vm)
{
   printf("\nC3725 '%s': stopping simulation.\n",vm->name);
   vm_log(vm,"C3725_STOP","stopping simulation.\n");

   /* Stop all CPUs */
   if (vm->cpu_group != NULL) {
      vm_stop(vm);
      
      if (cpu_group_sync_state(vm->cpu_group) == -1) {
         vm_error(vm,"unable to sync with system CPUs.\n");
         return(-1);
      }
   }

   /* Free resources that were used during execution to emulate hardware */
   vm_slot_shutdown_all(vm);
   vm_hardware_shutdown(vm);

   /* Cleanup */   
   VM_C3725(vm)->iofpga_data = NULL;
   VM_C3725(vm)->gt_data = NULL;
   return(0);
}
Ejemplo n.º 27
0
Archivo: ipl.c Proyecto: nikunjad/qemu
void s390_ipl_prepare_cpu(S390CPU *cpu)
{
    S390IPLState *ipl = get_ipl_device();
    Error *err = NULL;

    cpu->env.psw.addr = ipl->start_addr;
    cpu->env.psw.mask = IPL_PSW_MASK;

    if (!ipl->kernel || ipl->iplb_valid) {
        cpu->env.psw.addr = ipl->bios_start_addr;
        if (!ipl->iplb_valid) {
            ipl->iplb_valid = s390_gen_initial_iplb(ipl);
        }
    }
    if (ipl->netboot) {
        if (load_netboot_image(&err) < 0) {
            error_report_err(err);
            vm_stop(RUN_STATE_INTERNAL_ERROR);
        }
        ipl->qipl.netboot_start_addr = cpu_to_be64(ipl->start_addr);
    }
    s390_ipl_set_boot_menu(ipl);
    s390_ipl_prepare_qipl(cpu);
}
Ejemplo n.º 28
0
void process(const std::string& line)
{
  if (line.empty())
    {
      vm_stop(con);
      return;
    }
  const std::wstring s = IO2WString(line);
  if (s.length() == 1)
    {
      vm_letter(con, (char*)encodeUTF8(s).c_str());
      return;
    }
  std::wstring p;
  if (stringBegins(s, IO2WString(VOLUME_HEAD), p))
    {
      p = trim(p);
      if (!checkTypeUnsignedInt(p))
	{
	  std::cout << ERROR_PREFIX << "\'" << p << "\' is not a valid volume value" << std::endl;
	  return;
	}
      const size_t pp = parseAsUnsignedInt(p);
      if (pp > 100)
	{
	  std::cout << ERROR_PREFIX << "argument must be a value from 0 to 100" << std::endl;
	  return;
	}
      vm_volume(con, (unsigned char)pp);
      return;
    }
  if (stringBegins(s, IO2WString(PITCH_HEAD), p))
    {
      p = trim(p);
      if (!checkTypeUnsignedInt(p))
	{
	  std::cout << ERROR_PREFIX << "\'" << p << "\' is not a valid pitch value" << std::endl;
	  return;
	}
      const size_t pp = parseAsUnsignedInt(p);
      if (pp > 100)
	{
	  std::cout << ERROR_PREFIX << "argument must be a value from 0 to 100" << std::endl;
	  return;
	}
      vm_pitch(con, (unsigned char)pp);
      return;
    }
  if (stringBegins(s, IO2WString(RATE_HEAD), p))
    {
      p = trim(p);
      if (!checkTypeUnsignedInt(p))
	{
	  std::cout << ERROR_PREFIX << "\'" << p << "\' is not a valid rate value" << std::endl;
	  return;
	}
      const size_t pp = parseAsUnsignedInt(p);
      if (pp > 100)
	{
	  std::cout << ERROR_PREFIX << "argument must be a value from 0 to 100" << std::endl;
	  return;
	}
      vm_rate(con, (unsigned char)pp);
      return;
    }
  if (stringBegins(s, IO2WString(PUNC_HEAD), p))
    {
      p = trim(toLower(p));
      if (p == L"all")
	vm_procmode(con, VOICEMAN_PROCMODE_ALL); else
      if (p == L"some")
	vm_procmode(con, VOICEMAN_PROCMODE_SOME); else
      if (p == L"none")
	vm_procmode(con, VOICEMAN_PROCMODE_NONE); else
	{
	  std::cout << ERROR_PREFIX << "\'" << p << "\' is not a valid punctuation mode. Please use \'all\', \'some\' or \'none\' values" << std::endl;
	  return;
	}
      return;
    }
  if (stringBegins(s, IO2WString(FAMILY_HEAD), p))
    {
      p = trim(p);
      if (p.empty ())
	{
	  std::cout << ERROR_PREFIX << "this command requires an argument" << std::endl;
	  return;
	}
      vm_family(con, VOICEMAN_LANG_NONE, (char*)encodeUTF8(p).c_str());
      return;
    }
  vm_text(con, (char*)encodeUTF8(s).c_str());
}
Ejemplo n.º 29
0
int main(int argc, char* argv[])
{
  if (!cmdLine.parse(argc, argv))
    return 1;
  if (cmdLine.used("help"))
    {
    printHelp();
    return 0;
    }

  //Establishing connection;
  if (cmdLine.used("host"))
    {
      const std::string param = cmdLine["host"];
      std::string h=getDelimitedSubStr(param, 0, ':');
      const std::string p=getDelimitedSubStr(param, 1, ':');
      if (trim(h).empty())
	h="localhost";
      size_t port;
      if (!trim(p).empty())
	{
	  if (!checkTypeUnsignedInt(p))
	    {
	      std::cerr << ERROR_PREFIX << "\'" << trim(p) << "\' is not a valid port number." << std::endl;
	      return 1;
	    }
	  port = parseAsUnsignedInt(p);
	} else 
	port=VOICEMAN_DEFAULT_PORT;
      con = vm_connect_inet((char*)h.c_str(), port);
      if (con == VOICEMAN_BAD_CONNECTION)
	{
	  std::cerr << ERROR_PREFIX << "ERROR:Could not connect to host \'" << trim(h) << "\' with port " << port << "." << std::endl;
	  return 1;
	}
    } else
    if (cmdLine.used("socket"))
      {
	std::string p = cmdLine["socket"];
	if (trim(p).empty())
	  {
	    std::cerr << ERROR_PREFIX << "Missed name of UNIX domain socket." << std::endl;
	    return 1;
	  }
	if (trim(p) == "-")
	  p = VOICEMAN_DEFAULT_SOCKET;
	con = vm_connect_unix((char*)p.c_str());
	if (con == VOICEMAN_BAD_CONNECTION)
	  {
	    std::cerr << ERROR_PREFIX << "Could not connect to server via UNIX domain socket \'" << p << "\'." << std::endl;
	    return 1;
	  }
      } else
      {
	con = vm_connect();
	if (con == VOICEMAN_BAD_CONNECTION)
	  {
	    std::cerr << ERROR_PREFIX << "Could not connect to voicemand with default settings." << std::endl;
	    return 1;
	  }
      }

  ConnectionAutoClosing autoClosing(con);
  struct sigaction sa;
  sigaction(SIGPIPE, NULL, &sa);
  sa.sa_handler = sigPipeHandler;
  sa.sa_flags |= SA_RESTART;
  sigaction(SIGPIPE, &sa, NULL);



  //INitial connection parameters;
  assert(con != VOICEMAN_BAD_CONNECTION);
  if (cmdLine.used("stop"))
    vm_stop(con);
  if (cmdLine.used("pitch"))
    {
      const std::string value = trim(cmdLine["pitch"]);
      if (!checkTypeUnsignedInt(value))
	{
	  std::cerr << ERROR_PREFIX << "\'" << value << "\' is not a valid pitch value" << std::endl;
	  return 1;
	}
      vm_pitch(con, parseAsUnsignedInt(value));
    }
  if (cmdLine.used("rate"))
    {
      const std::string value = trim(cmdLine["rate"]);
      if (!checkTypeUnsignedInt(value))
	{
	  std::cerr << ERROR_PREFIX << "\'" << value << "\' is not a valid rate value" << std::endl;
	  return 1;
	}
      vm_rate(con, parseAsUnsignedInt(value));
    }
  if (cmdLine.used("volume"))
    {
      const std::string value = trim(cmdLine["volume"]);
      if (!checkTypeUnsignedInt(value))
	{
	  std::cerr << ERROR_PREFIX << "\'" << value << "\' is not a valid volume value" << std::endl;
	  return 1;
	}
      vm_volume(con, parseAsUnsignedInt(value));
    }
  if (cmdLine.used("family"))
    {
      //Neither trim() nor toLower() functions must be applied to teh value;
      //Let do it by the server itself;
      const std::string value = cmdLine["family"];
      if (trim(value).empty())
	{
	  std::cerr << ERROR_PREFIX << "voice family specification has an empty string value" << std::endl;
	  return 1;
	}
      for(std::string::size_type i = 0;i < value.length();i++)
	if (value[i] == ':' || value[i] == '\n')
	  {
	    std::cerr << ERROR_PREFIX << "voice family specification cannot contain \':\' and new line characters" << std::endl;
	    return 1;
	  }
      vm_family(con, VOICEMAN_LANG_NONE, (char*)value.c_str());
    }
  if (cmdLine.used("punc"))
    {
      const std::string value = trim(toLower(cmdLine["punc"]));
      if (value == "all")
	vm_procmode(con, VOICEMAN_PROCMODE_ALL); else
      if (value == "some")
	vm_procmode(con, VOICEMAN_PROCMODE_SOME); else
      if (value == "none")
	vm_procmode(con, VOICEMAN_PROCMODE_NONE); else
	{
	  std::cerr << ERROR_PREFIX << "punctuation mode can be only \'all\', \'some\' or \'none\'" << std::endl;
	  return 1;
	}
    }
  if (cmdLine.used("say"))
    {
      std::string value;
      for(size_t i = 0;i < cmdLine.files.size();i++)
	attachStringWithSpace(value, cmdLine.files[i]);
      vm_text(con, (char*)encodeUTF8(IO2WString(trim(value))).c_str());
      return 0;
    }
  if (cmdLine.used("stop"))
    return 0;

  std::cout << "VOICEMAN speech system. Version: " << PACKAGE_VERSION << "." << std::endl;
  std::cout << "Type \'quit\' or press Ctrl+D to leave this prompt." << std::endl;

  while(1)
    {
      std::string l;
      bool toQuit = 0;
      std::cout << "voiceman>";
      while(1)
	{
	  char c;
	  if (!std::cin.get(c))
	    {
	      toQuit = 1;
	      break;
	    }
	  if (c == '\r')
	    continue;
	  if (c == '\n')
	    break;
	  l += c;
	}
      if (toQuit || trim(toLower(l))  == "quit")
	break;
      process(l);
    }
  std::cout << std::endl;
  std::cout << "Bye!!!" << std::endl;
  return 0;
}
Ejemplo n.º 30
0
Archivo: qmp.c Proyecto: aliguori/qemu
void qmp_stop(Error **errp)
{
    vm_stop(RUN_STATE_PAUSED);
}