Пример #1
0
Файл: curl.c Проект: NgoHuy/uim
static size_t
uim_curl_write_func(void *ptr, size_t size, size_t nmemb, void *data)
{
  struct curl_memory_struct *mem = (struct curl_memory_struct *)data;
  size_t realsize = size * nmemb;

  /*
   * We know that it isn't possible to overflow during multiplication if
   * neither operand uses any of the most significant half of the bits in
   * a size_t.
   */
  if((unsigned long long)((nmemb | size) &
	((unsigned long long)SIZE_MAX << (sizeof(size_t) << 2))) &&
     (realsize / size != nmemb))
    return 0;

  if(SIZE_MAX - mem->size - 1 < realsize)
    realsize = SIZE_MAX - mem->size - 1;

  if(mem->str != NULL)
    mem->str = uim_realloc(mem->str, mem->size + realsize + 1);
  else
    mem->str = uim_malloc(realsize + 1);

  if(mem->str != NULL) {
    memcpy(&(mem->str[mem->size]), ptr, realsize);
    mem->size += realsize;
    mem->str[mem->size] = '\0';
  }

  return realsize;
}
Пример #2
0
/**
 * @return 1 if success, 0 if error.
 */
static int
read_command()
{
  ssize_t len;
  char rbuf[DEFAULT_MESSAGE_SIZE];

  debug_printf(DEBUG_NOTE, "read command\n");

  do {
	len = read(STDIN_FILENO, rbuf, sizeof(rbuf) - 1);
	if (len == -1) {
	  debug_printf(DEBUG_NOTE, "stdin is corrupt: %s\n", strerror (errno));
	  return 0;
	}
	if (len == 0) {
	  debug_printf(DEBUG_NOTE, "unexpected EOF\n");
	  return 0;
	}

	rbuf[len] = '\0';

	if (strlen(cmdbuf) + len + 1 > cmdbuf_len) {
	  cmdbuf_len += DEFAULT_MESSAGE_SIZE;
	  cmdbuf = uim_realloc(cmdbuf, cmdbuf_len);
	  debug_printf(DEBUG_NOTE, "cmdbuf has extended\n");
	}

	strcat(cmdbuf, rbuf);

  } while (!command_exists_in_cmdbuf());

  return 1;
}
Пример #3
0
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 uim_lisp
im_pushback_mode_list(uim_lisp uc_, uim_lisp str_)
{
    uim_context uc;
    const char *str;

    uc = retrieve_uim_context(uc_);
    str = REFER_C_STR(str_);

    uc->modes = uim_realloc(uc->modes, sizeof(char *) * (uc->nr_modes + 1));
    uc->modes[uc->nr_modes] = uc->conv_if->convert(uc->outbound_conv, str);
    uc->nr_modes++;

    return uim_scm_f();
}
Пример #5
0
char *
uim_helper_buffer_append(char *buf, const char *fragment, size_t fragment_size)
{
  size_t buf_size, extended_size;

  if (buf) {
	  buf_size = strlen(buf);
	  extended_size = buf_size + fragment_size + 1;
	  buf = uim_realloc(buf, extended_size);
	  memcpy(&buf[buf_size], fragment, fragment_size);
	  buf[extended_size - 1] = '\0';
  }

  return buf;
}
Пример #6
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];
}
Пример #7
0
static int
unused_ic_id(void)
{
  int i;

  for (i = 0; i < nr_input_contexts; i++) {
    if (!ic_array[i].mic)
      return i;
  }

  ic_array = uim_realloc(ic_array,
			 sizeof(struct ic_) * (nr_input_contexts + 1));
  ic_array[nr_input_contexts].mic = NULL;
  nr_input_contexts++;

  return nr_input_contexts - 1;
}
Пример #8
0
static int
uim_eb_strappend(char **dest, const char *append, size_t append_len)
{
  if (*dest) {
    char *str;
    size_t dest_len = strlen(*dest);
    size_t len = dest_len + append_len;

    str = uim_realloc(*dest, len + 1);
    memcpy(&str[dest_len], append, append_len);
    str[len] = '\0';
    *dest = str;
  } else {
    char *str;

    str = uim_malloc(append_len + 1);
    memcpy(str, append, append_len);
    str[append_len] = '\0';
    *dest = str;
  }
  return 1;
}
Пример #9
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;

}
Пример #10
0
/*
 * strから属性を変更するエスケープシーケンスを探す
 * len > 0
 */
static void set_attr(const char *str, int len)
{
  /* エスケープシーケンスの開始 */
  const char *start;
  const char *free_str = NULL;
  /* TRUEのときfree_strをfreeする必要がある */
  int must_free = FALSE;
  const char *end = str + len;
  /* 前回の途中のバッファがあるか */
  if (s_escseq_buf != NULL) {
    const char *tmp_str = str;
    /* s_escseq_buf には'\0'は含まれない */
    int escseq_buf_len = strlen(s_escseq_buf);
    must_free = TRUE;
    free_str = str = uim_malloc(escseq_buf_len + len + 1);
    memcpy((char *)str, s_escseq_buf, escseq_buf_len);
    memcpy((char *)str + escseq_buf_len, tmp_str, len);
    ((char *)str)[escseq_buf_len + len] = '\0';
    free(s_escseq_buf);
    s_escseq_buf = NULL;
    end = str + escseq_buf_len + len;
  }

  while ((start = str = memchr(str, ESCAPE_CODE, end - str)) != NULL) {
    str++;
    if (str == end) {
      int escseq_buf_len = end - start;
      s_escseq_buf = uim_malloc(escseq_buf_len + 1);
      memcpy(s_escseq_buf, start, escseq_buf_len);
      s_escseq_buf[escseq_buf_len] = '\0';
    }

    else if (str[0] == '[') {
      int nr_params = 1;
      /* ^[[1;2;3の1, 2, 3を入れる */
      int *params = uim_malloc(sizeof(int));
      params[0] = 0;
      str++;

      while (isdigit((unsigned char)str[0]) || str[0] == ';') {
        if (isdigit((unsigned char)str[0])) {
          int n = 0;
          while (isdigit((unsigned char)str[0])) {
            n = n * 10 + str[0] - '0';
            str++;
          }
          params[nr_params - 1] = n;
        }
        if (str[0] == ';') {
          nr_params++;
          params = uim_realloc(params, sizeof(int) * nr_params);
          params[nr_params - 1] = 0;
          str++;
        }
      }

      if (str == end) {
        int escseq_buf_len = end - start;
        s_escseq_buf = uim_malloc(escseq_buf_len + 1);
        memcpy(s_escseq_buf, start, escseq_buf_len);
        s_escseq_buf[escseq_buf_len] = '\0';
      }

      else if (str[0] == 'm') {
        int i;
        str++;
        for (i = 0; i < nr_params; i++) {
          if (params[i] == 0) {
            /* 属性消去 */
            s_attr = s_attr_none;
          } else if (s_enter_underline_num != NULL && params[i] == atoi(s_enter_underline_num)) {
            s_attr.underline = TRUE;
          } else if (s_exit_underline_num != NULL && params[i] == atoi(s_exit_underline_num)) {
            s_attr.underline = FALSE;
          } else if (s_enter_standout_num != NULL && params[i] == atoi(s_enter_standout_num)) {
            s_attr.standout = TRUE;
          } else if (s_exit_standout_num != NULL && params[i] == atoi(s_exit_standout_num)) {
            s_attr.standout = FALSE;
          } else if (s_bold_num != NULL && params[i] == atoi(s_bold_num)) {
            s_attr.bold = TRUE;
          } else if (s_blink_num != NULL && params[i] == atoi(s_blink_num)) {
            s_attr.blink = TRUE;
          } else if (s_orig_pair_num != NULL && params[i] == atoi(s_orig_pair_num)) {
            s_attr.foreground = s_attr.background = FALSE;
          } else if (s_orig_fore_num != NULL && params[i] == atoi(s_orig_fore_num)) {
            s_attr.foreground = FALSE;
          } else if (s_orig_back_num != NULL && params[i] == atoi(s_orig_back_num)) {
            s_attr.background = FALSE;
          } else if (params[i] == 22) {
            s_attr.bold = FALSE;
          } else if (params[i] == 25) {
            s_attr.blink = FALSE;
          } else if ((30 <= params[i] && params[i] <= 37) || (90 <= params[i] && params[i] <= 97)) {
            s_attr.foreground = params[i];
          } else if ((40 <= params[i] && params[i] <= 47) || (100 <= params[i] && params[i] <= 107)) {
            s_attr.background = params[i];
          }
        }
      }
      free(params);
    }
  }
  if (must_free) {
    free((char *)free_str);
  }
  s_attr_pty = s_attr;
}
Пример #11
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("");
}