示例#1
0
文件: candidate.c 项目: DirtYiCE/uim
static int
set_page_candidates(uim_context context, candidate_info *cand)
{
  int i, nr_in_page, start;

  start = cand->page_index * cand->disp_limit;
  if (cand->disp_limit && ((cand->num - start) > cand->disp_limit))
    nr_in_page = cand->disp_limit;
  else
    nr_in_page = cand->num - start;

  for (i = start; i < (start + nr_in_page); i++) {
    uim_candidate u_cand;

    u_cand = uim_get_candidate(context, i, cand->disp_limit ?
					   i % cand->disp_limit :
					   i);
    free(cand->cand_array[i].str);
    free(cand->cand_array[i].label);
    cand->cand_array[i].str = uim_strdup(uim_candidate_get_cand_str(u_cand));
    cand->cand_array[i].label = uim_strdup(uim_candidate_get_heading_label(u_cand));
    uim_candidate_free(u_cand);
  }

  return 1;
}
示例#2
0
文件: context.c 项目: DirtYiCE/uim
uim_agent_context *
create_uim_agent_context(const char *encoding)
{

  uim_agent_context *ret;
  const char *im;

  debug_printf(DEBUG_NOTE, "create_uim_agent_context\n");

  ret = uim_malloc(sizeof(uim_agent_context));

  if (encoding) {
	ret->encoding = uim_strdup(encoding);
  } else {
	if (debug_level > 0)
	  ret->encoding = uim_strdup("EUC-JP");
	else
	  ret->encoding = uim_strdup("UTF-8");
  }

  ret->context = create_context(ret->encoding, ret);

  if ((im = uim_get_default_im_name(setlocale(LC_CTYPE, NULL))))
	ret->im = uim_strdup(im);
  else
	ret->im = NULL;

  ret->pe = create_preedit();
  ret->cand = create_candidate();
  ret->prop = create_prop();

  ret->comstr = (char *)NULL;

  return ret;
}
示例#3
0
文件: m17nlib.c 项目: NgoHuy/uim
static void
pushback_input_method(MInputMethod *im, char *lang, char *name)
{
  im_array = uim_realloc(im_array,
			 sizeof(struct im_) * (nr_input_methods + 1));
  im_array[nr_input_methods].im = im;
  im_array[nr_input_methods].name = uim_strdup(name);
  im_array[nr_input_methods].lang = uim_strdup(lang);

  nr_input_methods++;
}
示例#4
0
static void
close_client(struct client *cl)
{
    close(cl->fd);
    if (cl->rbuf) {
        free(cl->rbuf);
        cl->rbuf = uim_strdup("");
    }
    if (cl->wbuf) {
        free(cl->wbuf);
        cl->wbuf = uim_strdup("");
    }
    cl->fd = -1;
}
示例#5
0
文件: preedit.c 项目: NgoHuy/uim
void
add_preedit(preedit *pe, int attr, const char *str)
{
  preedit_buffer *pb;

  pe->valid = 1;

  pb = uim_malloc(sizeof(preedit_buffer));

  if (pe->head == NULL) {
	pe->head = pb;
	pe->tail = pb;
  } else {
	pe->tail->next = pb;
	pe->tail = pb;
  }

  if (strlen(str) > 0) {
	pb->str = uim_strdup(str);
	pe->length += strlen(str);
  } else {
	pb->str = NULL;
  }

  pb->attr = attr;
  pb->next = NULL;
}
示例#6
0
static uim_lisp
c_execvp(uim_lisp file_, uim_lisp argv_)
{
  char **argv;
  int i;
  int len = uim_scm_length(argv_);
  uim_lisp ret_;

  if (len < 1)
    return uim_scm_f();

  argv = uim_malloc(sizeof(char *) * (len + 1));

  for (i = 0; i < len; i++) {
    argv[i] = uim_strdup(REFER_C_STR(CAR(argv_)));
    argv_ = CDR(argv_);
  }
  argv[len] = NULL;

  ret_ = MAKE_INT(execvp(REFER_C_STR(file_), argv));

  for (i = 0; i < len; i++)
    free(argv[i]);
  free(argv);

  return ret_;
}
示例#7
0
static struct client *
get_unused_client(void)
{
    int i;

    for (i = 0; i < nr_client_slots; i++) {
        if (clients[i].fd == -1)
            return &clients[i];
    }

    nr_client_slots++;
    clients = uim_realloc(clients, sizeof(struct client) * nr_client_slots);
    clients[nr_client_slots - 1].rbuf = uim_strdup("");
    clients[nr_client_slots - 1].wbuf = uim_strdup("");

    return &clients[nr_client_slots - 1];
}
示例#8
0
文件: context.c 项目: DirtYiCE/uim
uim_agent_context *
switch_context_im(uim_agent_context *ua, const char *im)
{
  const char *encoding;

  debug_printf(DEBUG_NOTE, "switch_context_im\n");

  encoding = get_im_encoding(im);

  /* update IM name */
  free(ua->im);

  if (im)
	ua->im = uim_strdup(im);
  else
	ua->im = NULL;

  if (strcmp(ua->encoding, encoding) == 0) {
	/* encodings are same */

	debug_printf(DEBUG_NOTE, "same encoding %s %s\n", ua->im, im);

	update_context_im(ua);

	uim_prop_list_update(ua->context);

  } else {
	/* encodings are different */

	debug_printf(DEBUG_NOTE, 
				 "different encoding %s %s\n", ua->encoding, encoding);

	free(ua->encoding);
	ua->encoding = uim_strdup(encoding);

	update_context_encoding(ua);
	update_context_im(ua);
  }

  uim_prop_list_update(ua->context);

  return ua;

}
示例#9
0
文件: m17nlib.c 项目: NgoHuy/uim
static char *
convert_mtext2str(MText *mtext)
{
  mconv_rebind_buffer(converter, (unsigned char *)buffer_for_converter,
		      sizeof(buffer_for_converter));
  mconv_encode(converter, mtext);
  buffer_for_converter[converter->nbytes] = 0;

  return uim_strdup(buffer_for_converter);
}
示例#10
0
文件: context.c 项目: DirtYiCE/uim
/* handle configuration change */
void
update_context_configuration(uim_agent_context *ua)
{

  /* configuration of context has changed at uim side */
  debug_printf(DEBUG_NOTE, "update_context_configuration\n");
  
  /* update IM name */
  free(ua->im);
  ua->im = uim_strdup(uim_get_current_im_name(ua->context));

  debug_printf(DEBUG_NOTE, "ua->im %s\n", ua->im);

  free(ua->encoding);
  ua->encoding = uim_strdup(get_im_encoding(ua->im));

  debug_printf(DEBUG_NOTE, "ua->encoding %s\n", ua->encoding);

  /* switch IM again */
  update_context_encoding(ua);
}
示例#11
0
文件: prop.c 项目: DirtYiCE/uim
void
update_prop_list(property *prop, const char *encoding, const char *str)
{
  debug_printf(DEBUG_NOTE, "update_prop_list\n");

  prop->valid = 1;

  free(prop->list);

  prop->list = uim_strdup(str);
  
  prop->list_update = 1;
}
示例#12
0
文件: candidate.c 项目: DirtYiCE/uim
int
new_candidate(uim_context context, candidate_info *cand, int num, int limit)
{
  int i;
#if !UIM_EL_USE_NEW_PAGE_HANDLING
  uim_candidate u_cand;
#endif

  if (cand->valid) clear_candidate(cand);

  cand->valid = 1;

  cand->index = -1;
  cand->disp_limit = limit;
  cand->num = num;
#if UIM_EL_USE_NEW_PAGE_HANDLING
  cand->page_index = 0;
#endif

  cand->cand_array = uim_malloc(sizeof(candidate) * num);

#if !UIM_EL_USE_NEW_PAGE_HANDLING
  /* get candidates from context */
  for (i = 0; i < num; i ++) {
	u_cand = uim_get_candidate(context, i, limit ? i % limit : i);
	cand->cand_array[i].str = uim_strdup(uim_candidate_get_cand_str(u_cand));
	cand->cand_array[i].label = uim_strdup(uim_candidate_get_heading_label(u_cand));
	uim_candidate_free(u_cand);
  }
#else
  for (i = 0; i < num; i++) {
	cand->cand_array[i].str = NULL;
	cand->cand_array[i].label = NULL;
  }
  set_page_candidates(context, cand);
#endif

  return 1;
}
示例#13
0
static uim_lisp
c_execve(uim_lisp file_, uim_lisp argv_, uim_lisp envp_)
{
  char **argv;
  char **envp;
  int i;
  int argv_len = uim_scm_length(argv_);
  int envp_len;
  uim_lisp ret_;

  if (argv_len < 1)
    return uim_scm_f();

  argv = uim_malloc(sizeof(char *) * (argv_len + 1));

  for (i = 0; i < argv_len; i++) {
    argv[i] = uim_strdup(REFER_C_STR(CAR(argv_)));
    argv_ = CDR(argv_);
  }
  argv[argv_len] = NULL;

  if (FALSEP(envp_) || NULLP(envp_)) {
    envp_len = 0;
    envp = NULL;
  } else {
    envp_len = uim_scm_length(envp_);
    envp = uim_malloc(sizeof(char *) * (envp_len + 1));

    for (i = 0; i < envp_len; i++) {
      uim_lisp env_ = CAR(envp_);

      uim_asprintf(&envp[i], "%s=%s", REFER_C_STR(CAR(env_)), REFER_C_STR(CDR(env_)));
      envp_ = CDR(envp_);
    }
    envp[envp_len] = NULL;
  }

  ret_ = MAKE_INT(execve(REFER_C_STR(file_), argv, envp));

  for (i = 0; i < argv_len; i++)
    free(argv[i]);
  free(argv);

  for (i = 0; i < envp_len; i++)
    free(envp[i]);
  free(envp);

  return ret_;
}
示例#14
0
文件: look.c 项目: DirtYiCE/uim
static uim_lisp
uim_look_look(uim_lisp isdict_, uim_lisp iscase_, uim_lisp words_, uim_lisp dict_, uim_lisp str_)
{
  const char *dict = REFER_C_STR(dict_);
  const char *str = REFER_C_STR(str_);
  uim_look_ctx *ctx;
  char *dict_str;
  uim_lisp ret_ = uim_scm_f();
  int words = -1;

  ctx = uim_look_init();

  uim_look_set_option_dictionary_order(C_BOOL(isdict_), ctx);
  uim_look_set_option_ignore_case(C_BOOL(iscase_), ctx);

  if (!ctx)
    uim_fatal_error("uim_look_init() failed");

  if (!uim_look_open_dict(dict, ctx))
    return ret_;

  dict_str = uim_strdup(str);

  if (INTP(words_))
    words = C_INT(words_);

  ret_ = uim_scm_null();
  if (uim_look(dict_str, ctx) != 0) {
    struct uim_look_look_internal_args args;

    uim_look_set(ctx);

    args.ctx = ctx;
    args.dict_str = dict_str;
    args.words = words;
    ret_ = (uim_lisp)uim_scm_call_with_gc_ready_stack((uim_gc_gate_func_ptr)uim_look_look_internal,
						      (void *)&args);
  }

  uim_look_finish(ctx);
  free(dict_str);

  return uim_scm_callf("reverse", "o", ret_);
}
示例#15
0
文件: escseq.c 项目: NgoHuy/uim
void init_escseq(const struct attribute_tag *attr_uim)
{
  s_attr_uim = *attr_uim;

  s_enter_underline_num = escseq2n(enter_underline_mode);
  s_exit_underline_num = escseq2n(exit_underline_mode);
  s_enter_standout_num = escseq2n(enter_standout_mode);
  s_exit_standout_num = escseq2n(exit_standout_mode);
  s_bold_num = escseq2n(enter_bold_mode);
  s_blink_num = escseq2n(enter_blink_mode);
  s_orig_pair_num = escseq2n(orig_pair);
  escseq2n2(orig_pair, &s_orig_fore_num, &s_orig_back_num);
  s_enter_uim_mode = attr2escseq(&s_attr_uim);
  if (s_enter_uim_mode != NULL) {
    s_enter_uim_mode = uim_strdup(s_enter_uim_mode);
  }

  fixtty();
  check_escseq();
  s_init = TRUE;
}
示例#16
0
static void
write_message(struct client *cl)
{
    int ret, message_len, out_len;
    char *out;

    out = cl->wbuf;
    message_len = out_len = strlen(cl->wbuf);
    while (out_len > 0) {
        if ((ret = write(cl->fd, out, out_len)) < 0) {
            if (errno == EAGAIN) {
#if 0
                fprintf(stderr, "EAGAIN: fd = %d\n", cl->fd);
#endif
            } else {
                perror("uim-helper_server write(2) failed");
                if (errno == EPIPE) {
                    fprintf(stderr, "fd = %d\n", cl->fd);
                    FD_CLR(cl->fd, &s_fdset_read);
                    FD_CLR(cl->fd, &s_fdset_write);
                    if (cl->fd == s_max_fd)
                        s_max_fd--;
                    close_client(cl);
                }
            }
            break;
        } else {
            out += ret;
            out_len -= ret;
        }
    }
    if (out_len == 0) {
        free(cl->wbuf);
        cl->wbuf = uim_strdup("");
        FD_CLR(cl->fd, &s_fdset_write);
    } else {
        uim_helper_buffer_shift(cl->wbuf, message_len - out_len);
    }
}
示例#17
0
int uim_helper_init_client_fd(void (*disconnect_cb)(void))
{
  struct sockaddr_un server;
  char path[MAXPATHLEN];
  FILE *serv_r = NULL, *serv_w = NULL;
  int fd = -1;
  
  uim_fd = -1;

  if (!uim_helper_get_pathname(path, sizeof(path)))
    goto error;

  memset(&server, 0, sizeof(server));
  server.sun_family = PF_UNIX;
  strlcpy(server.sun_path, path, sizeof(server.sun_path));

#ifdef SOCK_CLOEXEC
  /* linux-2.6.27+ variant that prevents racing on concurrent fork & exec in other thread */
  fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
  if (fd == -1 && errno == EINVAL)
    /* fallback to plain SOCK_TYPE on older kernel */
#endif
  fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (fd < 0) {
    perror("fail to create socket");
    goto error;
  }
  fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
  
#ifdef LOCAL_CREDS /* for NetBSD */
  /* Set the socket to receive credentials on the next message */
  {
    int on = 1;
    setsockopt(fd, 0, LOCAL_CREDS, &on, sizeof(on));
  }
#endif


  if (connect(fd, (struct sockaddr *)&server,sizeof(server)) < 0) {
    pid_t serv_pid = 0;
    char buf[128];
    
    serv_pid = uim_ipc_open_command(serv_pid, &serv_r, &serv_w,
				    get_server_command());

    if (serv_pid == 0)
      goto error;
    
    while (fgets (buf, sizeof(buf), serv_r ) != NULL ) {
      if (strcmp( buf, "\n" ) == 0)
	break;
    }
    
    if (connect(fd, (struct sockaddr *)&server,sizeof(server)) < 0)
      goto error;
  }

  if (uim_helper_check_connection_fd(fd))
    goto error;

  if (!uim_read_buf)
    uim_read_buf = uim_strdup("");
  uim_disconnect_cb = disconnect_cb;
  uim_fd = fd;

  return fd;

error:
  if (fd != -1)
    close(fd);

  if (serv_r)
    fclose(serv_r);
 
  if (serv_w)
    fclose(serv_w);

  return -1;
}
示例#18
0
static char *
mana_ipc_send_command(pid_t *pid,
		      FILE **read_fp, FILE **write_fp,
		      const char *str)
{
  char *tmp = uim_strdup("");
  char buf[8192];

  struct sigaction act, oact;

  act.sa_handler = SIG_IGN;
  sigemptyset(&act.sa_mask);
  act.sa_flags = 0;

  sigaction(SIGPIPE, &act, &oact);

  fputs(str, *write_fp);

 again:
  if (fflush(*write_fp) != 0) {
    switch (errno) {
    case EINTR:
      goto again;
    case EPIPE:

      while (!feof(*read_fp)) {
        fgets(buf, sizeof(buf), *read_fp);
        if (buf != NULL) {
          if (strcmp(buf, "err") == 0)
            uim_notify_fatal(N_("uim-mana: Command 'mana' not found."));
          else
            uim_notify_fatal("uim-mana: %s", buf);
	}
      }

      *pid = 0;
      fclose(*read_fp);
      fclose(*write_fp);
      *read_fp = NULL;
      *write_fp = NULL;

      sigaction(SIGPIPE, &oact, NULL);
      free(tmp);

      return NULL;
    default:
      sigaction(SIGPIPE, &oact, NULL);
      free(tmp);
      return NULL;
    }
  }

  sigaction(SIGPIPE, &oact, NULL);

  if (feof(*read_fp)) {
    *pid = 0;
    fclose(*read_fp);
    fclose(*write_fp);
    *read_fp = NULL;
    *write_fp = NULL;
    free(tmp);
    return NULL;
  }

  while (fgets (buf, sizeof(buf), *read_fp) != NULL) {

    tmp = uim_realloc(tmp, strlen(tmp) + strlen(buf) + 1);
    strcat(tmp, buf);

    if (strchr( buf, '\n' )) {
      break;
    }
  }
 
  return tmp;

}
示例#19
0
static char *
uim_iconv_code_conv(void *obj, const char *instr)
{
  iconv_t cd = (iconv_t)obj;
  size_t ins;
  const char *in;
  size_t outbufsiz, outs;
  char   *outbuf = NULL, *out;
  size_t ret = 0;
  size_t nconv = 0;
  size_t idx = 0;
  char *str = NULL;

  if (UIM_CATCH_ERROR_BEGIN())
    return NULL;

  if (!instr)
    goto err;

  if (!obj) {
    UIM_CATCH_ERROR_END();
    return uim_strdup(instr);
  }

  ins = strlen(instr);
  in = instr;

  outbufsiz = (ins + sizeof("")) * MBCHAR_LEN_MAX;
  out = outbuf = uim_malloc(outbufsiz);

  while (ins > 0) {
    out = outbuf;
    outs = outbufsiz;

    ret = iconv(cd, (ICONV_CONST char **)&in, &ins, &out, &outs);
    nconv = outbufsiz - outs;
    if (ret == (size_t)-1) {
      switch (errno) {
      case EINVAL:
	goto err;
      case E2BIG:
	outbufsiz *= 2;
	out = uim_realloc(outbuf, outbufsiz);
	outbuf = out;
	break;
      default:
	goto err;
      }
    } else {
      /* XXX: irreversible characters */
    }
    if (nconv > 0) {
      if (str == NULL)
	str = uim_malloc(nconv + 1);
      else
	str = uim_realloc(str, idx + nconv + 1);
      memcpy(&str[idx], outbuf, nconv);
      idx += nconv;
    }
  }
  do {
    out = outbuf;
    outs = outbufsiz;

    ret = iconv(cd, NULL, NULL, &out, &outs);
    nconv = outbufsiz - outs;

    if (ret == (size_t)-1) {
      outbufsiz *= 2;
      out = uim_realloc(outbuf, outbufsiz);
      outbuf = out;
    } else {
      /* XXX: irreversible characters */
    }
    if (nconv > 0) {
      if (str == NULL)
	str = uim_malloc(nconv + 1);
      else
	str = uim_realloc(str, idx + nconv + 1);
      memcpy(&str[idx], outbuf, nconv);
      idx += nconv;
    }
  } while (ret == (size_t)-1);

  if (str == NULL)
    str = uim_strdup("");
  else
    str[idx] = '\0';
  free(outbuf);

  UIM_CATCH_ERROR_END();

  return str;

 err:

  free(str);
  free(outbuf);

  UIM_CATCH_ERROR_END();

  return uim_strdup("");
}
示例#20
0
文件: prop.c 项目: DirtYiCE/uim
int
show_prop(property *prop)
{
  char *buf;
  char *head, *tail;
  char *p[6] = {0};
  char *indication_id = NULL, *iconic_label =NULL, *label_string = NULL;
  int check_leaf = 0;
  
  /* output new prop_list for Emacs */

  if (prop->list == NULL) {
	debug_printf(DEBUG_ERROR, "no prop_list\n");
	a_printf(" ( e ) ");
	return 0;
  }

  a_printf(" ( l ");

  head = buf = uim_strdup(prop->list);

#define PART_BRANCH "branch"
#define PART_LEAF   "leaf"
#define ACTION_ID_IMSW "action_imsw_"

  while (head && *head) { 

	/* 
	 * head: beginning of each line
	 * tail: end of each line 
	 * p[n]: token
	 */
	tail = strchr(head, '\n');

	if (tail)
	  *tail = '\0';
	else
	  break;

	/* head always not equal NULL */
	if (strlen(head) >= strlen(PART_BRANCH)
		&& strncmp(head, PART_BRANCH, strlen(PART_BRANCH)) == 0) {
	  if ((p[0] = strchr(head, '\t')) 
		  && (p[1] = strchr(p[0] + 1, '\t'))
		  && (p[2] = strchr(p[1] + 1, '\t'))) {
		*p[0] = *p[1] = *p[2] = '\0';
		indication_id = p[0] + 1;
		iconic_label = p[1] + 1;
		label_string = p[2] + 1;

		check_leaf = 1; /* check next leaf */
		/*a_printf(" ( \"%s\" \"%s\" \"%s\" ) ", p[0] + 1, p[1] + 1, p[2] + 1);*/
	  }
	} else if (strlen(head) >= strlen(PART_LEAF) 
			   && strncmp(head, PART_LEAF, strlen(PART_LEAF)) == 0) {
	  if (check_leaf && indication_id && iconic_label && label_string) {
		check_leaf = 0;
		/* im_switcher detection */
		if ((p[0] = strchr(head, '\t')) 
			&& (p[1] = strchr(p[0] + 1, '\t'))
			&& (p[2] = strchr(p[1] + 1, '\t'))
			&& (p[3] = strchr(p[2] + 1, '\t'))
			&& (p[4] = strchr(p[3] + 1, '\t'))
			&& (p[5] = strchr(p[4] + 1, '\t')))
		  *p[0] = *p[1] = *p[2] = *p[3] = *p[4] = *p[5] = '\0';

		  if (strlen(p[4] + 1) >= strlen(ACTION_ID_IMSW)
			  && strncmp(p[4] + 1, ACTION_ID_IMSW, strlen(ACTION_ID_IMSW)) == 0)
			a_printf(" ( \"im-name\" \"%s\" \"%s\" \"%s\" ) ", 
					 indication_id, iconic_label, label_string);
		  else
			a_printf(" ( \"im-mode\" \"%s\" \"%s\" \"%s\" ) ", 
					 indication_id, iconic_label, label_string);
	  }
	}
	head = tail + 1;
  }

  free(buf);

  a_printf(" ) ");

  return 1;

#undef PART_BRANCH
#undef PART_LEAF
#undef ACTION_ID_IMSW
}