JNIEXPORT jboolean JNICALL Java_org_gnu_readline_Readline_parseAndBindImpl
                                (JNIEnv *env, jclass theClass, jstring jline) {
  const char *line;
  jboolean is_copy;

  /* retrieve line argument and convert to ucs -------------------------- */

  line = (*env)->GetStringUTFChars(env,jline,&is_copy);
  if (!utf2ucs(line)) {
    jclass newExcCls;
    if (is_copy == JNI_TRUE)
      (*env)->ReleaseStringUTFChars(env,jline,line);
    newExcCls = (*env)->FindClass(env,"java/io/UnsupportedEncodingException");
    if (newExcCls != NULL)
      (*env)->ThrowNew(env,newExcCls,"");
    return (jboolean) JNI_FALSE;
  }
  if (is_copy == JNI_TRUE)
    (*env)->ReleaseStringUTFChars(env,jline,line);

  /* pass to readline function ---------------------------------------------- */

  if (rl_parse_and_bind(buffer))
    return (jboolean) JNI_FALSE;
  else
    return (jboolean) JNI_TRUE;
}
JNIEXPORT void JNICALL Java_org_gnu_readline_Readline_readInitFileImpl
                            (JNIEnv *env, jclass theClass, jstring jfilename) {
  const char *filename;
  jboolean is_copy;

  /* retrieve filename argument and convert to ucs -------------------------- */

  filename = (*env)->GetStringUTFChars(env,jfilename,&is_copy);
  if (!utf2ucs(filename)) {
    jclass newExcCls;
    if (is_copy == JNI_TRUE)
      (*env)->ReleaseStringUTFChars(env,jfilename,filename);
    newExcCls = (*env)->FindClass(env,"java/io/UnsupportedEncodingException");
    if (newExcCls != NULL)
      (*env)->ThrowNew(env,newExcCls,"");
    return;
  }
  if (is_copy == JNI_TRUE)
    (*env)->ReleaseStringUTFChars(env,jfilename,filename);

  /* pass to readline function ---------------------------------------------- */

  if (rl_read_init_file(buffer)) {
    jclass newExcCls;
    newExcCls = (*env)->FindClass(env,"java/io/IOException");
    if (newExcCls != NULL)
      (*env)->ThrowNew(env,newExcCls,strerror(errno));
    return;
  }
}
JNIEXPORT void JNICALL Java_org_gnu_readline_Readline_addToHistoryImpl
                               (JNIEnv *env, jclass theClass, jstring jline) {

  const char *line;
  jboolean is_copy;

  line = (*env)->GetStringUTFChars(env,jline,&is_copy);
  if (!utf2ucs(line)) {
    jclass newExcCls;
    if (is_copy == JNI_TRUE)
      (*env)->ReleaseStringUTFChars(env, jline, line);
    newExcCls = (*env)->FindClass(env,"java/io/UnsupportedEncodingException");
    if (newExcCls != NULL)
      (*env)->ThrowNew(env,newExcCls,"");
    return;
  }

  if (is_copy == JNI_TRUE)
    (*env)->ReleaseStringUTFChars(env, jline, line);

  add_history(buffer);
  return;
}
JNIEXPORT jstring JNICALL Java_org_gnu_readline_Readline_readlineImpl
                               (JNIEnv *env, jclass theClass, jstring jprompt) {

  const char *prompt;
  char *input;
  jboolean is_copy;

  /* retrieve prompt argument and convert to ucs ---------------------------- */

  prompt = (*env)->GetStringUTFChars(env,jprompt,&is_copy);
  if (!utf2ucs(prompt)) {
    jclass newExcCls;
    if (is_copy == JNI_TRUE)
      (*env)->ReleaseStringUTFChars(env, jprompt, prompt);
    newExcCls = (*env)->FindClass(env,"java/io/UnsupportedEncodingException");
    if (newExcCls != NULL)
      (*env)->ThrowNew(env,newExcCls,"");
    return NULL;
  }
  if (is_copy == JNI_TRUE)
    (*env)->ReleaseStringUTFChars(env, jprompt, prompt);

  /* use gnu-readline, convert string to utf8 ------------------------------- */

  input = readline(buffer);
  if (input == NULL) {
    jclass     newExcCls;
    newExcCls = (*env)->FindClass(env,"java/io/EOFException");
    if (newExcCls != NULL)
      (*env)->ThrowNew(env,newExcCls,"");
    return NULL;
  } else if (*input) {
    ucs2utf(input);
    return (*env)->NewStringUTF(env,buffer);
  } else
    return NULL;
}
Beispiel #5
0
static int
pg_utf_dsplen(const unsigned char *s)
{
	return ucs_wcwidth(utf2ucs(s));
}
Beispiel #6
0
int
convert_write_utf8(VBUF *v, char c) {
    static uint8_t utf8[4];
    static union {
        char c[2];
        uint16_t u;
    } trail = { .u = 0, };

    // trail must be little endian.
    if (trail.c[1]) {
        int len, i;
        uint16_t ucs;

        trail.c[0] = c;
        ucs = b2u_table[trail.u];

        len = ucs2utf(ucs, utf8);
        utf8[len] = 0;

        // assert(len > 0 && len < 4);
        for (i = 0; i < len; i++)
            vbuf_add(v, utf8[i]);

        trail.c[1] = 0;
        return 1;
    }

    if (isascii(c)) {
        vbuf_add(v, c);
        return 1;
    }

    trail.c[1] = c;
    return 0;
}

int convert_read_utf8(VBUF *v, const void *buf, size_t len) {
    static uint8_t trail[6];
    static int ctrail = 0;
    uint16_t ucs;
    uint8_t c;
    int written = 0;

    while (len-- > 0) {
        c = *(uint8_t*)buf ++;
        if (ctrail) {
            trail[ctrail++] = c;
            // TODO this may create invalid chars.
            if (utf2ucs(trail, &ucs) > ctrail)
                continue;
            ucs = u2b_table[ucs];
            vbuf_add(v, ucs >> 8);
            vbuf_add(v, ucs & 0xFF);
            written += 2;
            ctrail = 0;
            continue;
        }

        if (isascii(c)) {
            vbuf_add(v, c);
            written++;
        } else {
            trail[0] = c;
            ctrail = 1;
        }
    }
    return written;
}

void set_converting_type(ConvertMode mode)
{
    switch(mode) {
        case CONV_NORMAL:
            convert_read = vbuf_putblk;
            convert_write = vbuf_add;
            break;

        case CONV_UTF8:
            convert_read = convert_read_utf8;
            convert_write = convert_write_utf8;
            break;
    }
    convert_mode = mode;
}

void init_convert()
{
    // nothing now.
}