static void update_toc(GooList* items, int level) { unsigned short ucs[256]; char label[256]; int i, j; if (! items) return; if (items->getLength() < 1) return; for (i = 0; i < items->getLength(); i++) { OutlineItem* outlineItem = (OutlineItem*)items->get(i); Unicode* title = outlineItem->getTitle(); int tlen = outlineItem->getTitleLength(); if (tlen > sizeof(ucs) - 1) tlen = sizeof(ucs) - 1; for (j = 0; j < tlen; j++) ucs[j] = (unsigned short)title[j]; ucs[j] = 0; ucs2utf(ucs, label, sizeof(label)); LinkAction* a = outlineItem->getAction(); if (a && (a->getKind() == actionGoTo)) { // page number is contained/referenced in a LinkGoTo LinkGoTo* g = static_cast< LinkGoTo* >(a); LinkDest* destination = g->getDest(); if (!destination && g->getNamedDest()) { GooString* s = g->getNamedDest(); if (named_size <= named_count + 1) { named_size += 64; named_dest = (char**) realloc(named_dest, named_size * sizeof(char*)); } named_dest[named_count] = strdup(s->getCString()); add_toc_item(level, label, -1, 100000 + named_count); named_count++; } else if (destination && destination->isOk() && destination->isPageRef()) { Ref page_ref = destination->getPageRef(); int num = doc->findPage(page_ref.num, page_ref.gen); add_toc_item(level, label, num, num); } else { add_toc_item(level, label, -1, -1); } } else { add_toc_item(level, label, -1, -1); } outlineItem->open(); GooList* children = outlineItem->getKids(); if (children) update_toc(children, level + 1); outlineItem->close(); } }
JNIEXPORT jstring JNICALL Java_org_gnu_readline_Readline_getHistoryLineImpl (JNIEnv *env, jclass theClass, jint i) { HIST_ENTRY *hist = NULL; if ((hist = history_get ((int) (i + 1))) != NULL) { ucs2utf(hist->line); return (*env)->NewStringUTF(env,buffer); } return NULL; }
JNIEXPORT void JNICALL Java_org_gnu_readline_Readline_getHistoryImpl (JNIEnv *env, jclass theClass, jobject jcoll) { jclass cls; jmethodID mid; jstring jline; #if defined JavaReadline && !defined MAC_OS HIST_ENTRY **hist; #endif #if defined JavaEditline || defined MAC_OS HIST_ENTRY *histSingle; int pos; #endif cls = (*env)->GetObjectClass(env,jcoll); mid = (*env)->GetMethodID(env,cls,"add","(Ljava/lang/Object;)Z"); #if defined JavaReadline && !defined MAC_OS hist = history_list(); if (hist != NULL) { while (*hist != NULL) { jline = (*env)->NewStringUTF(env,(*hist)->line); (*env)->CallBooleanMethod(env,jcoll,mid,jline); hist++; } } #endif #if defined JavaEditline || defined MAC_OS for (pos = 0; pos < history_length; pos++) { histSingle = history_get(pos + 1); if (histSingle) { ucs2utf(histSingle->line); jline = (*env)->NewStringUTF(env,buffer); (*env)->CallBooleanMethod(env,jcoll,mid,jline); } } #endif }
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; }
struct evbuffer * evbuffer_b2u(struct evbuffer *source) { unsigned char c[16]; int out = 0; if (evbuffer_get_length(source) == 0) return source; struct evbuffer *destination = evbuffer_new(); // Peek at first byte while (evbuffer_copyout(source, c, 1) > 0) { if (isascii(c[0])) { if (evbuffer_add(destination, c, 1) < 0) break; // Remove byte from source buffer evbuffer_drain(source, 1); out++; } else { // Big5 int todrain = 2; // Handle in-character colors int fg = -1, bg = -1, bright = -1; int n = evbuffer_copyout(source, c, sizeof(c)); if (n < 2) break; while (c[1] == '\033') { c[n - 1] = '\0'; // At least have \033[m if (n < 4 || c[2] != '[') break; unsigned char *p = c + 3; if (*p == 'm') { // ANSI reset fg = 7; bg = 0; bright = 0; } while (1) { int v = (int) strtol((char *)p, (char **)&p, 10); if (*p != 'm' && *p != ';') break; if (v == 0) bright = 0; else if (v == 1) bright = 1; else if (v >= 30 && v <= 37) fg = v; else if (v >= 40 && v <= 47) bg = v; if (*p == 'm') break; p++; } if (*p != 'm') { // Skip malicious or unsupported codes fg = bg = bright = -1; break; } else { evbuffer_drain(source, p - c + 1); todrain = 1; // We keep a byte on buffer, so fix offset n = evbuffer_copyout(source, c + 1, sizeof(c) - 1); if (n < 1) break; n++; } } #ifdef EXTENDED_INCHAR_ANSI // Output control codes before the Big5 character if (fg >= 0 || bg >= 0 || bright >= 0) { int dlen = evbuffer_add_ext_ansi_escape_code(destination, fg, bg, bright); if (dlen < 0) break; out += dlen; } #endif // n may be changed, check again if (n < 2) break; uint8_t utf8[4]; int len = ucs2utf(b2u_table[c[0] << 8 | c[1]], utf8); utf8[len] = 0; if (evbuffer_add(destination, utf8, len) < 0) break; #ifndef EXTENDED_INCHAR_ANSI // Output in-char control codes to make state consistent if (fg >= 0 || bg >= 0 || bright >= 0) { int dlen = evbuffer_add_ansi_escape_code(destination, fg, bg, bright); if (dlen < 0) break; out += dlen; } #endif // Remove DBCS character from source buffer evbuffer_drain(source, todrain); out += len; } } if (evbuffer_get_length(source) == 0 && out) { // Success evbuffer_free(source); return destination; } // Fail evbuffer_free(source); evbuffer_free(destination); return NULL; }
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. }