예제 #1
0
void drain_clients() {
  int rc, *fd, pos;
  char buf[1024];

  fd=NULL;
  while ( (fd=(int*)utarray_next(cfg.fds,fd))) {
    do {
      rc = read(*fd, buf, sizeof(buf));
      switch(rc) { 
        default: fprintf(stderr,"received %d bytes\n", rc);         break;
        case  0: fprintf(stderr,"fd %d closed\n", *fd);             break;
        case -1: if (errno == EWOULDBLOCK || errno == EAGAIN)       break;
                 fprintf(stderr, "recv: %s\n", strerror(errno));    break;
      }
    } while(rc > 0);

    if (rc==0) {
      fprintf(stderr,"client %d has closed\n", *fd);
      close(*fd);
      *fd = -1; /* mark for cleanup after forward iteration */
    }
  }

  /* cleanup any sockets that we closed, reverse iteration */
  fd=NULL;
  while ( (fd=(int*)utarray_prev(cfg.fds,fd))) {
    pos = utarray_eltidx(cfg.fds,fd);
    if (*fd == -1) utarray_erase(cfg.fds,pos,1);
  }

}
예제 #2
0
파일: cmdline.c 프로젝트: jollywho/nav
static bool valid_exec(Cmdline *cmdline, Cmdstr *cmd, Token *token)
{
  if (utarray_eltidx(cmdline->tokens, token) != 1)
    return false;

  Token *prev = (Token*)utarray_prev(cmdline->tokens, token);
  cmd->rev = prev->end == token->start;

  return cmd->rev;
}
예제 #3
0
FCITX_EXPORT_API
void FcitxInstanceRealEnd(FcitxInstance* instance) {

    FcitxProfileSave(instance->profile);
    FcitxInstanceSaveAllIM(instance);

    if (instance->uinormal && instance->uinormal->ui->Destroy)
        instance->uinormal->ui->Destroy(instance->uinormal->addonInstance);

    if (instance->uifallback && instance->uifallback->ui->Destroy)
        instance->uifallback->ui->Destroy(instance->uifallback->addonInstance);

    instance->uifallback = NULL;
    instance->ui = NULL;
    instance->uinormal = NULL;

    /* handle exit */
    FcitxAddon** pimclass;
    FcitxAddon** pfrontend;
    FcitxFrontend* frontend;
    FcitxInputContext* rec = NULL;

    for (pimclass = (FcitxAddon**)utarray_front(&instance->imeclasses);
         pimclass != NULL;
         pimclass = (FcitxAddon**)utarray_next(&instance->imeclasses, pimclass)
        ) {
        if ((*pimclass)->imclass->Destroy)
            (*pimclass)->imclass->Destroy((*pimclass)->addonInstance);
    }

    for (rec = instance->ic_list; rec != NULL; rec = rec->next) {
        pfrontend = (FcitxAddon**)utarray_eltptr(&instance->frontends,
                                                 (unsigned int)rec->frontendid);
        frontend = (*pfrontend)->frontend;
        frontend->CloseIM((*pfrontend)->addonInstance, rec);
    }

    for (rec = instance->ic_list; rec != NULL; rec = rec->next) {
        pfrontend = (FcitxAddon**)utarray_eltptr(&instance->frontends,
                                                 (unsigned int)rec->frontendid);
        frontend = (*pfrontend)->frontend;
        frontend->DestroyIC((*pfrontend)->addonInstance, rec);
    }

    for (pfrontend = (FcitxAddon**)utarray_front(&instance->frontends);
         pfrontend != NULL;
         pfrontend = (FcitxAddon**)utarray_next(&instance->frontends, pfrontend)
        ) {
        if (pfrontend == NULL)
            continue;
        FcitxFrontend* frontend = (*pfrontend)->frontend;
        frontend->Destroy((*pfrontend)->addonInstance);
    }

    FcitxAddon** pmodule;
    for (pmodule = (FcitxAddon**) utarray_back(&instance->modules);
         pmodule != NULL;
         pmodule = (FcitxAddon**) utarray_prev(&instance->modules, pmodule)) {
        if (pmodule == NULL)
            return;
        FcitxModule* module = (*pmodule)->module;
        if (module->Destroy)
            module->Destroy((*pmodule)->addonInstance);
    }

    if (instance->sem) {
        sem_post(instance->sem);
    }
}
예제 #4
0
void mtex2MML_perform_replacement(UT_array **environment_data_stack, UT_array *rowlines_stack, envType environment_type, UT_array *eqn_number_stack, UT_array *row_spacing_stack)
{
  char *a, *attr_rowlines, *attr_rowspacing;
  envdata_t env_data;

  /* we cut the last char because we can always skip the first row */
  if (utarray_len(rowlines_stack) != 0) {
    utarray_pop_back(rowlines_stack);
  }

  if (utarray_len(eqn_number_stack) > 1) {
    utarray_erase(eqn_number_stack, 0, 1);
  }

  unsigned int line_count = utarray_len(rowlines_stack);

  /* empty rowlines should be reset */
  if (line_count == 0) {
    a = "none";
    utarray_push_back(rowlines_stack, &a);
  }

  /* given the row_attribute values, construct an attribute list (separated by spaces) */
  UT_string *l;
  utstring_new(l);
  char **o=NULL;
  a = "rowlines=\"";
  utstring_printf(l, "%s", a);
  while ( (o=(char**)utarray_prev(rowlines_stack,o))) {
    utstring_printf(l, "%s ", *o);
  }

  attr_rowlines = utstring_body(l);
  if (strlen(attr_rowlines) > 0) {
    mtex2MML_remove_last_char(attr_rowlines); /* remove the final space */
  }

  /* given the row_spacing values, construct an attribute list (separated by spaces) */
  UT_string *s;
  utstring_new(s);
  char **p=NULL;
  while ( (p=(char**)utarray_prev(row_spacing_stack,p))) {
    if (environment_type == ENV_SMALLMATRIX && strcmp(*p, "0.5ex") == 0) {
      utstring_printf(s, "%s ", "0.2em");
    } else if (environment_type == ENV_GATHERED && strcmp(*p, "0.5ex") == 0) {
      utstring_printf(s, "%s ", "1.0ex");
    } else {
      utstring_printf(s, "%s ", *p);
    }
  }

  attr_rowspacing = utstring_body(s);
  if (strlen(attr_rowspacing) > 0) {
    mtex2MML_remove_last_char(attr_rowspacing); /* remove the final space */
  } else {
    if (environment_type == ENV_SMALLMATRIX) {
      attr_rowspacing = "0.2em";
    } else if (environment_type == ENV_GATHERED) {
      attr_rowspacing = "1.0ex";
    } else {
      attr_rowspacing = "0.5ex";
    }
  }

  /* store pertinent metadata */
  env_data.rowspacing = attr_rowspacing;
  env_data.rowlines = attr_rowlines;
  env_data.environment_type = environment_type;
  env_data.eqn_numbers = eqn_number_stack;
  env_data.line_count = line_count;

  utarray_push_back(*environment_data_stack, &env_data);
  utstring_free(l);
  utstring_free(s);
}
예제 #5
0
파일: kvsp-tpub.c 프로젝트: JHUAPL/kvspool
int main(int argc, char *argv[]) {
  int opt, rc, n, *fd;
  cfg.prog = argv[0];
  utarray_new(cfg.clients,&ut_int_icd);
  utarray_new(cfg.outbufs,&ut_ptr_icd);
  utarray_new(cfg.outidxs, &ut_int_icd);
  cfg.set = kv_set_new();
  struct epoll_event ev;
  UT_string **s;

  utstring_new(cfg.s);
  utarray_new(output_keys, &ut_str_icd);
  utarray_new(output_defaults, &ut_str_icd);
  utarray_new(output_types,&ut_int_icd);

  while ( (opt=getopt(argc,argv,"vb:p:m:d:rS:h")) != -1) {
    switch(opt) {
      case 'v': cfg.verbose++; break;
      case 'p': cfg.listener_port=atoi(optarg); break; 
      case 'm': cfg.mb_per_client=atoi(optarg); break; 
      case 'd': cfg.dir=strdup(optarg); break; 
      case 'r': cfg.mode=round_robin; break; 
      case 'b': cfg.config_file=strdup(optarg); break;
      case 'S': cfg.stats_file=strdup(optarg); break;
      case 'h': default: usage(); break;
    }
  }
  if (cfg.listener_port==0) usage();
  if (setup_client_listener()) goto done;
  if (cfg.config_file==NULL) goto done;
  if (parse_config(cfg.config_file) < 0) goto done;
  if ( !(cfg.sp = kv_spoolreader_new(cfg.dir))) goto done;

  /* block all signals. we take signals synchronously via signalfd */
  sigset_t all;
  sigfillset(&all);
  sigprocmask(SIG_SETMASK,&all,NULL);

  /* a few signals we'll accept via our signalfd */
  sigset_t sw;
  sigemptyset(&sw);
  for(n=0; n < sizeof(sigs)/sizeof(*sigs); n++) sigaddset(&sw, sigs[n]);

  /* create the signalfd for receiving signals */
  cfg.signal_fd = signalfd(-1, &sw, 0);
  if (cfg.signal_fd == -1) {
    fprintf(stderr,"signalfd: %s\n", strerror(errno));
    goto done;
  }

  /* set up the epoll instance */
  cfg.epoll_fd = epoll_create(1); 
  if (cfg.epoll_fd == -1) {
    fprintf(stderr,"epoll: %s\n", strerror(errno));
    goto done;
  }

  /* add descriptors of interest */
  if (new_epoll(EPOLLIN, cfg.listener_fd)) goto done; // new client connections
  if (new_epoll(EPOLLIN, cfg.signal_fd))   goto done; // signal socket

  alarm(1);
  while (epoll_wait(cfg.epoll_fd, &ev, 1, -1) > 0) {
    if (cfg.verbose > 1)  fprintf(stderr,"epoll reports fd %d\n", ev.data.fd);
    if      (ev.data.fd == cfg.signal_fd)   { if (handle_signal() < 0) goto done; }
    else if (ev.data.fd == cfg.listener_fd) { if (accept_client() < 0) goto done; }
    else    feed_client(ev.data.fd, ev.events);
  }

done:
  /* free the clients: close and deep free their buffers */
  fd=NULL; s=NULL;
  while ( (fd=(int*)utarray_prev(cfg.clients,fd))) {
    s=(UT_string**)utarray_prev(cfg.outbufs,s);
    close(*fd);
    utstring_free(*s);
  }
  utarray_free(cfg.clients);
  utarray_free(cfg.outbufs);
  utarray_free(cfg.outidxs);
  utarray_free(output_keys);
  utarray_free(output_defaults);
  utarray_free(output_types);
  utstring_free(cfg.s);
  if (cfg.listener_fd) close(cfg.listener_fd);
  if (cfg.signal_fd) close(cfg.signal_fd);
  if (cfg.sp) kv_spoolreader_free(cfg.sp);
  if (cfg.set) kv_set_free(cfg.set);
  return 0;
}