? .inputrc Index: chartype.h =================================================================== RCS file: /cvsroot/src/lib/libedit/chartype.h,v retrieving revision 1.4 diff -u -r1.4 chartype.h --- chartype.h 3 Jan 2010 18:27:10 -0000 1.4 +++ chartype.h 1 Mar 2010 13:15:55 -0000 @@ -60,12 +60,12 @@ #warning Build environment does not support non-BMP characters #endif -#define ct_mbtowc mbtowc -#define ct_mbtowc_reset mbtowc(0,0,0) #define ct_wctomb wctomb #define ct_wctomb_reset wctomb(0,0) #define ct_wcstombs wcstombs #define ct_mbstowcs mbstowcs +#define ct_fgetwc fgetwc +#define ct_fputwc fputwc #define Char wchar_t #define Int wint_t @@ -109,12 +109,12 @@ #else /* NARROW */ -#define ct_mbtowc error -#define ct_mbtowc_reset #define ct_wctomb error #define ct_wctomb_reset #define ct_wcstombs(a, b, c) (strncpy(a, b, c), strlen(a)) #define ct_mbstowcs(a, b, c) (strncpy(a, b, c), strlen(a)) +#define ct_fgetwc fgetc +#define ct_fputwc fputc #define Char char #define Int int Index: el.c =================================================================== RCS file: /cvsroot/src/lib/libedit/el.c,v retrieving revision 1.58 diff -u -r1.58 el.c --- el.c 31 Dec 2009 15:58:26 -0000 1.58 +++ el.c 1 Mar 2010 13:15:56 -0000 @@ -59,9 +59,6 @@ public EditLine * el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr) { -#ifdef WIDECHAR - char *locale; -#endif EditLine *el = (EditLine *) el_malloc(sizeof(EditLine)); if (el == NULL) @@ -86,10 +83,8 @@ */ el->el_flags = 0; #ifdef WIDECHAR - if ((locale = setlocale(LC_CTYPE, NULL)) != NULL){ - if (strcasestr(locale, ".UTF-8") != NULL) - el->el_flags |= CHARSET_IS_UTF8; - } + mbrtowc(NULL, NULL, 0, &el->el_instate); + mbrtowc(NULL, NULL, 0, &el->el_outstate); #endif if (term_init(el) == -1) { Index: el.h =================================================================== RCS file: /cvsroot/src/lib/libedit/el.h,v retrieving revision 1.21 diff -u -r1.21 el.h --- el.h 31 Dec 2009 15:58:26 -0000 1.21 +++ el.h 1 Mar 2010 13:15:56 -0000 @@ -143,6 +143,8 @@ ct_buffer_t el_scratch; /* Scratch conversion buffer */ ct_buffer_t el_lgcyconv; /* Buffer for legacy wrappers */ LineInfo el_lgcylinfo; /* Legacy LineInfo buffer */ + mbstate_t el_instate; + mbstate_t el_outstate; #endif }; Index: read.c =================================================================== RCS file: /cvsroot/src/lib/libedit/read.c,v retrieving revision 1.54 diff -u -r1.54 read.c --- read.c 31 Dec 2009 15:58:26 -0000 1.54 +++ read.c 1 Mar 2010 13:15:56 -0000 @@ -294,18 +294,6 @@ return (OKCMD); } -#ifdef WIDECHAR -/* utf8_islead(): - * Test whether a byte is a leading byte of a UTF-8 sequence. - */ -private int -utf8_islead(unsigned char c) -{ - return (c < 0x80) || /* single byte char */ - (c >= 0xc2 && c <= 0xf4); /* start of multibyte sequence */ -} -#endif - /* read_char(): * Read a character from the tty. */ @@ -314,13 +302,11 @@ { ssize_t num_read; int tried = 0; - char cbuf[MB_LEN_MAX]; - int cbp = 0; - int bytes = 0; + char ch; again: el->el_signal->sig_no = 0; - while ((num_read = read(el->el_infd, cbuf + cbp, 1)) == -1) { + while ((num_read = read(el->el_infd, &ch, 1)) == -1) { if (el->el_signal->sig_no == SIGCONT) { sig_set(el); el_set(el, EL_REFRESH); @@ -333,27 +319,19 @@ return (-1); } } - + if (num_read > 0) { #ifdef WIDECHAR - if (el->el_flags & CHARSET_IS_UTF8) { - if (!utf8_islead((unsigned char)cbuf[0])) - goto again; /* discard the byte we read and try again */ - ++cbp; - if ((bytes = ct_mbtowc(cp, cbuf, cbp)) == -1) { - ct_mbtowc_reset; - if (cbp >= MB_LEN_MAX) { /* "shouldn't happen" */ - *cp = '\0'; - return (-1); + if ((el->el_flags & IGNORE_EXTCHARS) == 0) { + switch (mbrtowc(cp, &ch, 1, &el->el_instate)) { + case (size_t)-1: + mbrtowc(NULL, NULL, 0, &el->el_instate); + /*FALLTHROUGH*/ + case (size_t)-2: + goto again; } - goto again; - } - } else /* we don't support other multibyte charsets */ + } else #endif - *cp = (unsigned char)cbuf[0]; - - if ((el->el_flags & IGNORE_EXTCHARS) && bytes > 1) { - cbp = 0; /* skip this character */ - goto again; + *cp = (unsigned char)ch; } return (int)num_read; Index: vi.c =================================================================== RCS file: /cvsroot/src/lib/libedit/vi.c,v retrieving revision 1.31 diff -u -r1.31 vi.c --- vi.c 30 Dec 2009 22:37:40 -0000 1.31 +++ vi.c 1 Mar 2010 13:15:56 -0000 @@ -1001,13 +1001,12 @@ vi_histedit(EditLine *el, Int c) { int fd; + FILE *fp; pid_t pid; - ssize_t st; int status; char tempfile[] = "/tmp/histedit.XXXXXXXXXX"; - char *cp; - size_t len; - Char *line; + Char *cp; + Int ch; if (el->el_state.doingarg) { if (vi_to_history_line(el, 0) == CC_ERROR) @@ -1017,61 +1016,55 @@ fd = mkstemp(tempfile); if (fd < 0) return CC_ERROR; - len = (size_t)(el->el_line.lastchar - el->el_line.buffer); -#define TMP_BUFSIZ (EL_BUFSIZ * MB_LEN_MAX) - cp = el_malloc(TMP_BUFSIZ); - if (cp == NULL) - return CC_ERROR; - line = el_malloc(len * sizeof(*line)); - if (line == NULL) { - el_free((ptr_t)cp); + fp = fdopen(fd, "w+"); + if (fp == NULL) { + close(fd); return CC_ERROR; } - Strncpy(line, el->el_line.buffer, len); - line[len] = '\0'; - ct_wcstombs(cp, line, TMP_BUFSIZ - 1); - cp[TMP_BUFSIZ - 1] = '\0'; - len = strlen(cp); - write(fd, cp, len); - write(fd, "\n", 1); + for (cp = el->el_line.buffer; cp < el->el_line.lastchar; ++cp) { + ct_fputwc(*cp, fp); + if (ferror(fp)) + goto fatal; + } + ct_fputwc(STR('\n'), fp); + if (ferror(fp)) + goto fatal; + fflush(fp); pid = fork(); switch (pid) { case -1: - close(fd); - unlink(tempfile); - el_free(cp); - el_free(line); - return CC_ERROR; + goto fatal; case 0: - close(fd); + fclose(fp); execlp("vi", "vi", tempfile, (char *)NULL); exit(0); /*NOTREACHED*/ default: while (waitpid(pid, &status, 0) != pid) continue; - lseek(fd, (off_t)0, SEEK_SET); - st = read(fd, cp, TMP_BUFSIZ); - if (st > 0) { - len = (size_t)(el->el_line.lastchar - - el->el_line.buffer); - len = ct_mbstowcs(el->el_line.buffer, cp, len); - if (len > 0 && el->el_line.buffer[len -1] == '\n') - --len; + unlink(tempfile); + rewind(fp); + for (cp = el->el_line.buffer; cp < el->el_line.limit; ++cp) { + ch = ct_fgetwc(fp); + if (ferror(fp)) + goto fatal; + if (feof(fp)) + break; + *cp = ch; } - else - len = 0; - el->el_line.cursor = el->el_line.buffer; - el->el_line.lastchar = el->el_line.buffer + len; - el_free(cp); - el_free(line); + if (cp != el->el_line.buffer && *(cp - 1) == STR('\n')) + --cp; + el->el_line.cursor = el->el_line.buffer; + el->el_line.lastchar = cp; break; } - close(fd); - unlink(tempfile); + fclose(fp); /* return CC_REFRESH; */ return ed_newline(el, 0); +fatal: + fclose(fp); + return CC_ERROR; } /* vi_history_word():