lineedit: fix completion with Unicode chars
authorDenys Vlasenko <dvlasenk@redhat.com>
Thu, 2 Sep 2010 10:03:11 +0000 (12:03 +0200)
committerDenys Vlasenko <dvlasenk@redhat.com>
Thu, 2 Sep 2010 10:03:11 +0000 (12:03 +0200)
function                                             old     new   delta
read_line_input                                     4966    5002     +36
bb_wcstombs                                          170     159     -11
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 36/-11)             Total: 25 bytes

Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
libbb/lineedit.c
libbb/unicode.c

index de7042491e2419b28d1cd287ff77ba7e2bce7e88..381203a22dff98ef1f9aab25e7c843cbbdb5b240 100644 (file)
@@ -1061,9 +1061,16 @@ static void input_tab(smallint *lastWasTab)
 
                /* Make a local copy of the string --
                 * up to the position of the cursor */
+#if !ENABLE_UNICODE_SUPPORT
                save_string(matchBuf, cursor + 1);
-#if ENABLE_UNICODE_SUPPORT
-               cursor_mb = strlen(matchBuf);
+#else
+               {
+                       CHAR_T wc = command_ps[cursor];
+                       command_ps[cursor] = 0;
+                       save_string(matchBuf, MAX_LINELEN);
+                       command_ps[cursor] = wc;
+                       cursor_mb = strlen(matchBuf);
+               }
 #endif
                tmp = matchBuf;
 
@@ -1167,7 +1174,10 @@ static void input_tab(smallint *lastWasTab)
                                sprintf(&command[cursor_mb - recalc_pos], "%s%s", tmp, matchBuf);
                                command_len = load_string(command, S.maxsize);
                                /* write out the matched command */
-                               redraw(cmdedit_y, command_len - len);
+                               /* paranoia: load_string can return 0 on conv error,
+                                * prevent passing len = (0 - 12) to redraw */
+                               len = command_len - len;
+                               redraw(cmdedit_y, len >= 0 ? len : 0);
                        }
                }
 #endif
index c4b5f86ee7034740c80e9a6bed19c10a70721955..70c6abe00106a2bc77a02eaf613df5949e9cfb60 100644 (file)
@@ -131,7 +131,7 @@ size_t FAST_FUNC wcstombs(char *dest, const wchar_t *src, size_t n)
                size_t len = wcrtomb_internal(tbuf, wc);
 
                if (len > n)
-                       len = n;
+                       break;
                memcpy(dest, tbuf, len);
                if (wc == L'\0')
                        return org_n - n;