blob: ff5d2840897a33e46338aa6842b3e389e98ffafb [file] [log] [blame]
Erik Andersenc7c634b2000-03-19 05:28:55 +00001/* vi: set sw=4 ts=4: */
Erik Andersen13456d12000-03-16 08:09:57 +00002/*
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02003 * Command line editing.
Erik Andersen13456d12000-03-16 08:09:57 +00004 *
Eric Andersen81fe1232003-07-29 06:38:40 +00005 * Copyright (c) 1986-2003 may safely be consumed by a BSD or GPL license.
Eric Andersen7467c8d2001-07-12 20:26:32 +00006 * Written by: Vladimir Oleynik <dzo@simtreas.ru>
Eric Andersen5f2c79d2001-02-16 18:36:04 +00007 *
8 * Used ideas:
9 * Adam Rogoyski <rogoyski@cs.utexas.edu>
10 * Dave Cinege <dcinege@psychosis.com>
11 * Jakub Jelinek (c) 1995
Eric Andersen81fe1232003-07-29 06:38:40 +000012 * Erik Andersen <andersen@codepoet.org> (Majorly adjusted for busybox)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000013 *
Erik Andersen13456d12000-03-16 08:09:57 +000014 * This code is 'as is' with no warranty.
Erik Andersen13456d12000-03-16 08:09:57 +000015 */
16
17/*
Denis Vlasenko78ff8192008-06-28 21:03:43 +000018 * Usage and known bugs:
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010019 * Terminal key codes are not extensive, more needs to be added.
20 * This version was created on Debian GNU/Linux 2.x.
Denis Vlasenko78ff8192008-06-28 21:03:43 +000021 * Delete, Backspace, Home, End, and the arrow keys were tested
22 * to work in an Xterm and console. Ctrl-A also works as Home.
23 * Ctrl-E also works as End.
24 *
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010025 * The following readline-like commands are not implemented:
26 * ESC-b -- Move back one word
27 * ESC-f -- Move forward one word
28 * ESC-d -- Delete forward one word
29 * CTL-t -- Transpose two characters
30 *
Denis Vlasenko78ff8192008-06-28 21:03:43 +000031 * lineedit does not know that the terminal escape sequences do not
32 * take up space on the screen. The redisplay code assumes, unless
33 * told otherwise, that each character in the prompt is a printable
34 * character that takes up one character position on the screen.
35 * You need to tell lineedit that some sequences of characters
36 * in the prompt take up no screen space. Compatibly with readline,
37 * use the \[ escape to begin a sequence of non-printing characters,
38 * and the \] escape to signal the end of such a sequence. Example:
39 *
40 * PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '
Erik Andersen13456d12000-03-16 08:09:57 +000041 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000042#include "libbb.h"
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020043#include "unicode.h"
Glenn L McGrath475820c2004-01-22 12:42:23 +000044
Glenn L McGrath3b251852004-01-03 12:07:32 +000045#ifdef TEST
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020046# define ENABLE_FEATURE_EDITING 0
47# define ENABLE_FEATURE_TAB_COMPLETION 0
48# define ENABLE_FEATURE_USERNAME_COMPLETION 0
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020049#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000050
Eric Andersen5165fbe2001-02-20 06:42:29 +000051
Denis Vlasenko82b39e82007-01-21 19:18:19 +000052/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko38f63192007-01-22 09:03:07 +000053#if ENABLE_FEATURE_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000054
Denis Vlasenko82b39e82007-01-21 19:18:19 +000055
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +020056#define ENABLE_USERNAME_OR_HOMEDIR \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000057 (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +020058#define IF_USERNAME_OR_HOMEDIR(...)
59#if ENABLE_USERNAME_OR_HOMEDIR
60# undef IF_USERNAME_OR_HOMEDIR
61# define IF_USERNAME_OR_HOMEDIR(...) __VA_ARGS__
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000062#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000063
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020064
65#undef CHAR_T
Denys Vlasenko19158a82010-03-26 14:06:56 +010066#if ENABLE_UNICODE_SUPPORT
Tomas Heinricha659b812010-04-29 13:43:39 +020067# define BB_NUL ((wchar_t)0)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020068# define CHAR_T wchar_t
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010069static bool BB_isspace(CHAR_T c) { return ((unsigned)c < 256 && isspace(c)); }
Denys Vlasenko31e2e7b2009-12-12 02:42:35 +010070# if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010071static bool BB_isalnum(CHAR_T c) { return ((unsigned)c < 256 && isalnum(c)); }
Denys Vlasenko31e2e7b2009-12-12 02:42:35 +010072# endif
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010073static bool BB_ispunct(CHAR_T c) { return ((unsigned)c < 256 && ispunct(c)); }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020074# undef isspace
75# undef isalnum
76# undef ispunct
77# undef isprint
78# define isspace isspace_must_not_be_used
79# define isalnum isalnum_must_not_be_used
80# define ispunct ispunct_must_not_be_used
81# define isprint isprint_must_not_be_used
82#else
83# define BB_NUL '\0'
84# define CHAR_T char
85# define BB_isspace(c) isspace(c)
86# define BB_isalnum(c) isalnum(c)
87# define BB_ispunct(c) ispunct(c)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020088#endif
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +020089#if ENABLE_UNICODE_PRESERVE_BROKEN
90# define unicode_mark_raw_byte(wc) ((wc) | 0x20000000)
91# define unicode_is_raw_byte(wc) ((wc) & 0x20000000)
92#else
93# define unicode_is_raw_byte(wc) 0
94#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020095
96
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +020097#define SEQ_CLEAR_TILL_END_OF_SCREEN "\033[J"
98//#define SEQ_CLEAR_TILL_END_OF_LINE "\033[K"
Tomas Heinricha659b812010-04-29 13:43:39 +020099
100
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000101enum {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000102 MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN < 0x7ff0
Denis Vlasenko6b404432008-01-07 16:13:14 +0000103 ? CONFIG_FEATURE_EDITING_MAX_LEN
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000104 : 0x7ff0
105};
Robert Griebl350d26b2002-12-03 22:45:46 +0000106
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200107#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000108static const char null_str[] ALIGN1 = "";
109#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +0000110
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000111/* We try to minimize both static and stack usage. */
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000112struct lineedit_statics {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000113 line_input_t *state;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000114
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000115 volatile unsigned cmdedit_termw; /* = 80; */ /* actual terminal width */
116 sighandler_t previous_SIGWINCH_handler;
Mark Whitley4e338752001-01-26 20:42:23 +0000117
Denys Vlasenko020f4062009-05-17 16:44:54 +0200118 unsigned cmdedit_x; /* real x (col) terminal position */
119 unsigned cmdedit_y; /* pseudoreal y (row) terminal position */
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000120 unsigned cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000121
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000122 unsigned cursor;
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +0200123 int command_len; /* must be signed */
124 /* signed maxsize: we want x in "if (x > S.maxsize)"
Denys Vlasenko044b1802009-07-12 02:50:35 +0200125 * to _not_ be promoted to unsigned */
126 int maxsize;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200127 CHAR_T *command_ps;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000128
129 const char *cmdedit_prompt;
Denis Vlasenko38f63192007-01-22 09:03:07 +0000130#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000131 int num_ok_lines; /* = 1; */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000132#endif
133
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200134#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000135 char *user_buf;
136 char *home_pwd_buf; /* = (char*)null_str; */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000137#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000138
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000139#if ENABLE_FEATURE_TAB_COMPLETION
140 char **matches;
141 unsigned num_matches;
142#endif
143
144#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200145# define DELBUFSIZ 128
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200146 CHAR_T *delptr;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000147 smallint newdelflag; /* whether delbuf should be reused yet */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200148 CHAR_T delbuf[DELBUFSIZ]; /* a place to store deleted characters */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000149#endif
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100150#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +0100151 smallint sent_ESC_br6n;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100152#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000153
154 /* Formerly these were big buffers on stack: */
155#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000156 char input_tab__matchBuf[MAX_LINELEN];
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000157#endif
158};
159
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000160/* See lineedit_ptr_hack.c */
161extern struct lineedit_statics *const lineedit_ptr_to_statics;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000162
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000163#define S (*lineedit_ptr_to_statics)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000164#define state (S.state )
165#define cmdedit_termw (S.cmdedit_termw )
166#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
167#define cmdedit_x (S.cmdedit_x )
168#define cmdedit_y (S.cmdedit_y )
169#define cmdedit_prmt_len (S.cmdedit_prmt_len)
170#define cursor (S.cursor )
171#define command_len (S.command_len )
172#define command_ps (S.command_ps )
173#define cmdedit_prompt (S.cmdedit_prompt )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000174#define num_ok_lines (S.num_ok_lines )
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000175#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000176#define home_pwd_buf (S.home_pwd_buf )
177#define matches (S.matches )
178#define num_matches (S.num_matches )
179#define delptr (S.delptr )
180#define newdelflag (S.newdelflag )
181#define delbuf (S.delbuf )
182
183#define INIT_S() do { \
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000184 (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000185 barrier(); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000186 cmdedit_termw = 80; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000187 IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200188 IF_USERNAME_OR_HOMEDIR(home_pwd_buf = (char*)null_str;) \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000189} while (0)
190static void deinit_S(void)
191{
192#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000193 /* This one is allocated only if FANCY_PROMPT is on
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200194 * (otherwise it points to verbatim prompt (NOT malloced)) */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000195 free((char*)cmdedit_prompt);
196#endif
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200197#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000198 free(user_buf);
199 if (home_pwd_buf != null_str)
200 free(home_pwd_buf);
201#endif
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000202 free(lineedit_ptr_to_statics);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000203}
204#define DEINIT_S() deinit_S()
205
Denis Vlasenko2c844952008-04-25 18:44:35 +0000206
Denys Vlasenko19158a82010-03-26 14:06:56 +0100207#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200208static size_t load_string(const char *src, int maxsize)
209{
210 ssize_t len = mbstowcs(command_ps, src, maxsize - 1);
211 if (len < 0)
212 len = 0;
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200213 command_ps[len] = BB_NUL;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200214 return len;
215}
Tomas Heinricha659b812010-04-29 13:43:39 +0200216static unsigned save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200217{
Denys Vlasenko94043e82010-05-11 14:49:13 +0200218# if !ENABLE_UNICODE_PRESERVE_BROKEN
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200219 ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
220 if (len < 0)
221 len = 0;
222 dst[len] = '\0';
223 return len;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200224# else
Tomas Heinricha659b812010-04-29 13:43:39 +0200225 unsigned dstpos = 0;
226 unsigned srcpos = 0;
227
228 maxsize--;
229 while (dstpos < maxsize) {
230 wchar_t wc;
231 int n = srcpos;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200232
233 /* Convert up to 1st invalid byte (or up to end) */
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200234 while ((wc = command_ps[srcpos]) != BB_NUL
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200235 && !unicode_is_raw_byte(wc)
Tomas Heinricha659b812010-04-29 13:43:39 +0200236 ) {
237 srcpos++;
238 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200239 command_ps[srcpos] = BB_NUL;
Tomas Heinricha659b812010-04-29 13:43:39 +0200240 n = wcstombs(dst + dstpos, command_ps + n, maxsize - dstpos);
241 if (n < 0) /* should not happen */
242 break;
243 dstpos += n;
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200244 if (wc == BB_NUL) /* usually is */
Tomas Heinricha659b812010-04-29 13:43:39 +0200245 break;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200246
Tomas Heinricha659b812010-04-29 13:43:39 +0200247 /* We do have invalid byte here! */
248 command_ps[srcpos] = wc; /* restore it */
249 srcpos++;
250 if (dstpos == maxsize)
251 break;
252 dst[dstpos++] = (char) wc;
253 }
254 dst[dstpos] = '\0';
255 return dstpos;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200256# endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200257}
258/* I thought just fputwc(c, stdout) would work. But no... */
259static void BB_PUTCHAR(wchar_t c)
260{
261 char buf[MB_CUR_MAX + 1];
262 mbstate_t mbst = { 0 };
Tomas Heinricha659b812010-04-29 13:43:39 +0200263 ssize_t len;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200264
Tomas Heinricha659b812010-04-29 13:43:39 +0200265 len = wcrtomb(buf, c, &mbst);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200266 if (len > 0) {
267 buf[len] = '\0';
268 fputs(buf, stdout);
269 }
270}
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200271# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
272static wchar_t adjust_width_and_validate_wc(unsigned *width_adj, wchar_t wc)
273# else
274static wchar_t adjust_width_and_validate_wc(wchar_t wc)
275# define adjust_width_and_validate_wc(width_adj, wc) \
276 ((*(width_adj))++, adjust_width_and_validate_wc(wc))
277# endif
278{
279 int w = 1;
280
281 if (unicode_status == UNICODE_ON) {
Denys Vlasenko26e2c1d2010-05-16 21:15:03 +0200282 if (wc > CONFIG_LAST_SUPPORTED_WCHAR) {
283 /* note: also true for unicode_is_raw_byte(wc) */
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200284 goto subst;
285 }
286 w = wcwidth(wc);
287 if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0)
288 || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
289 || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
290 ) {
291 subst:
292 w = 1;
293 wc = CONFIG_SUBST_WCHAR;
294 }
295 }
296
297# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
298 *width_adj += w;
299#endif
300 return wc;
301}
302#else /* !UNICODE */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200303static size_t load_string(const char *src, int maxsize)
304{
305 safe_strncpy(command_ps, src, maxsize);
306 return strlen(command_ps);
307}
Denys Vlasenko9038d6f2009-07-15 20:02:19 +0200308# if ENABLE_FEATURE_TAB_COMPLETION
Tomas Heinricha659b812010-04-29 13:43:39 +0200309static void save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200310{
311 safe_strncpy(dst, command_ps, maxsize);
312}
Denys Vlasenko7dd0ce42009-07-15 18:27:47 +0200313# endif
314# define BB_PUTCHAR(c) bb_putchar(c)
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200315/* Should never be called: */
316int adjust_width_and_validate_wc(unsigned *width_adj, int wc);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200317#endif
318
319
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000320/* Put 'command_ps[cursor]', cursor++.
321 * Advance cursor on screen. If we reached right margin, scroll text up
322 * and remove terminal margin effect by printing 'next_char' */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000323#define HACK_FOR_WRONG_WIDTH 1
Denys Vlasenko94043e82010-05-11 14:49:13 +0200324static void put_cur_glyph_and_inc_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000325{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200326 CHAR_T c = command_ps[cursor];
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200327 unsigned width = 0;
328 int ofs_to_right;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000329
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200330 if (c == BB_NUL) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000331 /* erase character after end of input string */
332 c = ' ';
Denys Vlasenko94043e82010-05-11 14:49:13 +0200333 } else {
334 /* advance cursor only if we aren't at the end yet */
335 cursor++;
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200336 if (unicode_status == UNICODE_ON) {
337 IF_UNICODE_WIDE_WCHARS(width = cmdedit_x;)
338 c = adjust_width_and_validate_wc(&cmdedit_x, c);
339 IF_UNICODE_WIDE_WCHARS(width = cmdedit_x - width;)
340 } else {
341 cmdedit_x++;
342 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000343 }
Denys Vlasenko94043e82010-05-11 14:49:13 +0200344
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200345 ofs_to_right = cmdedit_x - cmdedit_termw;
346 if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right <= 0) {
347 /* c fits on this line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200348 BB_PUTCHAR(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000349 }
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200350
351 if (ofs_to_right >= 0) {
352 /* we go to the next line */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000353#if HACK_FOR_WRONG_WIDTH
354 /* This works better if our idea of term width is wrong
355 * and it is actually wider (often happens on serial lines).
356 * Printing CR,LF *forces* cursor to next line.
357 * OTOH if terminal width is correct AND terminal does NOT
358 * have automargin (IOW: it is moving cursor to next line
359 * by itself (which is wrong for VT-10x terminals)),
360 * this will break things: there will be one extra empty line */
361 puts("\r"); /* + implicit '\n' */
362#else
Denys Vlasenko94043e82010-05-11 14:49:13 +0200363 /* VT-10x terminals don't wrap cursor to next line when last char
364 * on the line is printed - cursor stays "over" this char.
365 * Need to print _next_ char too (first one to appear on next line)
366 * to make cursor move down to next line.
367 */
368 /* Works ok only if cmdedit_termw is correct. */
369 c = command_ps[cursor];
370 if (c == BB_NUL)
371 c = ' ';
372 BB_PUTCHAR(c);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000373 bb_putchar('\b');
Denis Vlasenko2c844952008-04-25 18:44:35 +0000374#endif
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200375 cmdedit_y++;
376 if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right == 0) {
377 width = 0;
378 } else { /* ofs_to_right > 0 */
379 /* wide char c didn't fit on prev line */
380 BB_PUTCHAR(c);
381 }
382 cmdedit_x = width;
Mark Whitley4e338752001-01-26 20:42:23 +0000383 }
Erik Andersen13456d12000-03-16 08:09:57 +0000384}
385
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000386/* Move to end of line (by printing all chars till the end) */
Denys Vlasenko94043e82010-05-11 14:49:13 +0200387static void put_till_end_and_adv_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000388{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000389 while (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200390 put_cur_glyph_and_inc_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +0000391}
392
393/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000394static void goto_new_line(void)
395{
Denys Vlasenko94043e82010-05-11 14:49:13 +0200396 put_till_end_and_adv_cursor();
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200397 if (cmdedit_x != 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000398 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000399}
400
Rob Landley88621d72006-08-29 19:41:06 +0000401static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000402{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000403 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000404}
405
Denys Vlasenko248c3242010-05-17 00:45:44 +0200406static void put_prompt(void)
407{
408 unsigned w;
409
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200410 fputs(cmdedit_prompt, stdout);
Denys Vlasenko248c3242010-05-17 00:45:44 +0200411 fflush_all();
412 cursor = 0;
413 w = cmdedit_termw; /* read volatile var once */
414 cmdedit_y = cmdedit_prmt_len / w; /* new quasireal y */
415 cmdedit_x = cmdedit_prmt_len % w;
416}
417
Eric Andersenaff114c2004-04-14 17:51:38 +0000418/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000419/* (optimized for slow terminals) */
420static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000421{
422 if (num > cursor)
423 num = cursor;
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200424 if (num == 0)
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000425 return;
426 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000427
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200428 if ((ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS)
429 && unicode_status == UNICODE_ON
430 ) {
431 /* correct NUM to be equal to _screen_ width */
432 int n = num;
433 num = 0;
434 while (--n >= 0)
435 adjust_width_and_validate_wc(&num, command_ps[cursor + n]);
436 if (num == 0)
437 return;
438 }
439
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000440 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000441 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000442 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000443 /* This is longer by 5 bytes on x86.
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000444 * Also gets miscompiled for ARM users
445 * (busybox.net/bugs/view.php?id=2274).
Denis Vlasenko6f043912008-02-18 22:28:03 +0000446 * printf(("\b\b\b\b" + 4) - num);
447 * return;
448 */
449 do {
450 bb_putchar('\b');
451 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000452 return;
453 }
454 printf("\033[%uD", num);
455 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000456 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000457
458 /* Need to go one or more lines up */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200459 if (ENABLE_UNICODE_WIDE_WCHARS) {
460 /* With wide chars, it is hard to "backtrack"
461 * and reliably figure out where to put cursor.
462 * Example (<> is a wide char; # is an ordinary char, _ cursor):
463 * |prompt: <><> |
464 * |<><><><><><> |
465 * |_ |
466 * and user presses left arrow. num = 1, cmdedit_x = 0,
467 * We need to go up one line, and then - how do we know that
468 * we need to go *10* positions to the right? Because
469 * |prompt: <>#<>|
470 * |<><><>#<><><>|
471 * |_ |
472 * in this situation we need to go *11* positions to the right.
473 *
474 * A simpler thing to do is to redraw everything from the start
475 * up to new cursor position (which is already known):
476 */
477 unsigned sv_cursor;
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200478 /* go to 1st column; go up to first line */
479 printf("\r" "\033[%uA", cmdedit_y);
Denys Vlasenko248c3242010-05-17 00:45:44 +0200480 cmdedit_y = 0;
481 sv_cursor = cursor;
482 put_prompt(); /* sets cursor to 0 */
483 while (cursor < sv_cursor)
484 put_cur_glyph_and_inc_cursor();
485 } else {
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200486 int lines_up;
487 unsigned width;
488 /* num = chars to go back from the beginning of current line: */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200489 num -= cmdedit_x;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200490 width = cmdedit_termw; /* read volatile var once */
491 /* num=1...w: one line up, w+1...2w: two, etc: */
492 lines_up = 1 + (num - 1) / width;
493 cmdedit_x = (width * cmdedit_y - num) % width;
494 cmdedit_y -= lines_up;
495 /* go to 1st column; go up */
496 printf("\r" "\033[%uA", lines_up);
497 /* go to correct column.
Denys Vlasenkobbf1aa12010-05-17 12:33:13 +0200498 * xterm, konsole, Linux VT interpret 0 as 1 below! wow.
499 * need to *make sure* we skip it if cmdedit_x == 0 */
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200500 if (cmdedit_x)
501 printf("\033[%uC", cmdedit_x);
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000502 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000503}
504
Eric Andersenaff114c2004-04-14 17:51:38 +0000505/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000506static void redraw(int y, int back_cursor)
507{
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200508 if (y > 0) /* up y lines */
Denys Vlasenko044b1802009-07-12 02:50:35 +0200509 printf("\033[%uA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000510 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000511 put_prompt();
Denys Vlasenko94043e82010-05-11 14:49:13 +0200512 put_till_end_and_adv_cursor();
513 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000514 input_backward(back_cursor);
515}
516
Paul Fox3f11b1b2005-08-04 19:04:46 +0000517/* Delete the char in front of the cursor, optionally saving it
518 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000519#if !ENABLE_FEATURE_EDITING_VI
520static void input_delete(void)
521#define input_delete(save) input_delete()
522#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000523static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000524#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000525{
Mark Whitley4e338752001-01-26 20:42:23 +0000526 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000527
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000528 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000529 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000530
Denis Vlasenko38f63192007-01-22 09:03:07 +0000531#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000532 if (save) {
533 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000534 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000535 newdelflag = 0;
536 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000537 if ((delptr - delbuf) < DELBUFSIZ)
538 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000539 }
540#endif
541
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200542 memmove(command_ps + j, command_ps + j + 1,
Denys Vlasenko9531f7d2009-07-16 14:33:16 +0200543 /* (command_len + 1 [because of NUL]) - (j + 1)
544 * simplified into (command_len - j) */
545 (command_len - j) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000546 command_len--;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200547 put_till_end_and_adv_cursor();
548 /* Last char is still visible, erase it (and more) */
549 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersenc470f442003-07-28 09:56:35 +0000550 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000551}
552
Denis Vlasenko38f63192007-01-22 09:03:07 +0000553#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000554static void put(void)
555{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000556 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000557 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000558
Paul Fox3f11b1b2005-08-04 19:04:46 +0000559 if (j == 0)
560 return;
561 ocursor = cursor;
562 /* open hole and then fill it */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200563 memmove(command_ps + cursor + j, command_ps + cursor,
564 (command_len - cursor + 1) * sizeof(command_ps[0]));
565 memcpy(command_ps + cursor, delbuf, j * sizeof(command_ps[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000566 command_len += j;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200567 put_till_end_and_adv_cursor();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000568 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000569}
570#endif
571
Mark Whitley4e338752001-01-26 20:42:23 +0000572/* Delete the char in back of the cursor */
573static void input_backspace(void)
574{
575 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000576 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000577 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000578 }
579}
580
Eric Andersenaff114c2004-04-14 17:51:38 +0000581/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000582static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000583{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000584 if (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200585 put_cur_glyph_and_inc_cursor();
Erik Andersenf0657d32000-04-12 17:49:52 +0000586}
587
Denis Vlasenko38f63192007-01-22 09:03:07 +0000588#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000589
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000590static void free_tab_completion_data(void)
591{
592 if (matches) {
593 while (num_matches)
594 free(matches[--num_matches]);
595 free(matches);
596 matches = NULL;
597 }
598}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000599
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000600static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000601{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000602 matches = xrealloc_vector(matches, 4, num_matches);
603 matches[num_matches] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000604 num_matches++;
605}
606
Denis Vlasenko38f63192007-01-22 09:03:07 +0000607#if ENABLE_FEATURE_USERNAME_COMPLETION
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200608/* Replace "~user/..." with "/homedir/...".
609 * The parameter is malloced, free it or return it
610 * unchanged if no user is matched.
611 */
612static char *username_path_completion(char *ud)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000613{
614 struct passwd *entry;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200615 char *tilde_name = ud;
616 char *home = NULL;
617
618 ud++; /* skip ~ */
619 if (*ud == '/') { /* "~/..." */
620 home = home_pwd_buf;
621 } else {
622 /* "~user/..." */
623 ud = strchr(ud, '/');
624 *ud = '\0'; /* "~user" */
625 entry = getpwnam(tilde_name + 1);
626 *ud = '/'; /* restore "~user/..." */
627 if (entry)
628 home = entry->pw_dir;
629 }
630 if (home) {
631 ud = concat_path_file(home, ud);
632 free(tilde_name);
633 tilde_name = ud;
634 }
635 return tilde_name;
636}
637
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200638/* ~use<tab> - find all users with this prefix.
639 * Return the length of the prefix used for matching.
640 */
641static NOINLINE unsigned complete_username(const char *ud)
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200642{
643 /* Using _r function to avoid pulling in static buffers */
644 char line_buff[256];
645 struct passwd pwd;
646 struct passwd *result;
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200647 unsigned userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000648
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200649 ud++; /* skip ~ */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000650 userlen = strlen(ud);
651
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200652 setpwent();
653 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
654 /* Null usernames should result in all users as possible completions. */
655 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
656 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000657 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000658 }
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200659 endpwent();
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200660
661 return 1 + userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000662}
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200663#endif /* FEATURE_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000664
665enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000666 FIND_EXE_ONLY = 0,
667 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000668 FIND_FILE_ONLY = 2,
669};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000670
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200671static int path_parse(char ***p)
Mark Whitley4e338752001-01-26 20:42:23 +0000672{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000673 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000674 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000675 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000676 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000677
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000678 if (state->flags & WITH_PATH_LOOKUP)
679 pth = state->path_lookup;
680 else
681 pth = getenv("PATH");
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200682
683 /* PATH="" or PATH=":"? */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000684 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
685 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000686
Denis Vlasenko253ce002007-01-22 08:34:44 +0000687 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000688 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000689 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000690 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000691 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000692 break;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200693 tmp++;
694 if (*tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000695 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000696 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000697 }
698
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200699 *p = res = xmalloc(npth * sizeof(res[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000700 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000701 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000702 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000703 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000704 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000705 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000706 *tmp++ = '\0'; /* ':' -> '\0' */
707 if (*tmp == '\0')
708 break; /* :<empty> */
709 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000710 }
Mark Whitley4e338752001-01-26 20:42:23 +0000711 return npth;
712}
713
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200714/* Complete command, directory or file name.
715 * Return the length of the prefix used for matching.
716 */
717static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000718{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000719 char *path1[1];
720 char **paths = path1;
721 int npaths;
722 int i;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200723 unsigned pf_len;
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200724 const char *pfind;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200725 char *dirbuf = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +0000726
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000727 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000728 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000729
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200730 pfind = strrchr(command, '/');
731 if (!pfind) {
732 if (type == FIND_EXE_ONLY)
733 npaths = path_parse(&paths);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000734 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000735 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000736 /* point to 'l' in "..../last_component" */
737 pfind++;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200738 /* dirbuf = ".../.../.../" */
739 dirbuf = xstrndup(command, pfind - command);
740#if ENABLE_FEATURE_USERNAME_COMPLETION
741 if (dirbuf[0] == '~') /* ~/... or ~user/... */
742 dirbuf = username_path_completion(dirbuf);
743#endif
Denys Vlasenko7063e862010-09-02 12:44:39 +0200744 path1[0] = dirbuf;
Mark Whitley4e338752001-01-26 20:42:23 +0000745 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200746 pf_len = strlen(pfind);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000747
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000748 for (i = 0; i < npaths; i++) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200749 DIR *dir;
750 struct dirent *next;
751 struct stat st;
752 char *found;
753
Mark Whitley4e338752001-01-26 20:42:23 +0000754 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000755 if (!dir)
756 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000757
758 while ((next = readdir(dir)) != NULL) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200759 const char *name_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000760
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200761 /* .../<tab>: bash 3.2.0 shows dotfiles, but not . and .. */
762 if (!pfind[0] && DOT_OR_DOTDOT(name_found))
Mark Whitley4e338752001-01-26 20:42:23 +0000763 continue;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200764 /* match? */
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200765 if (strncmp(name_found, pfind, pf_len) != 0)
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200766 continue; /* no */
767
768 found = concat_path_file(paths[i], name_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000769 /* NB: stat() first so that we see is it a directory;
770 * but if that fails, use lstat() so that
771 * we still match dangling links */
772 if (stat(found, &st) && lstat(found, &st))
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200773 goto cont; /* hmm, remove in progress? */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000774
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200775 /* save only name if we scan PATH */
776 if (paths[i] != dirbuf)
777 strcpy(found, name_found);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000778
Mark Whitley4e338752001-01-26 20:42:23 +0000779 if (S_ISDIR(st.st_mode)) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200780 unsigned len1 = strlen(found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000781 /* name is a directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000782 if (found[len1-1] != '/') {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200783 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000784 found[len1] = '/';
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200785 found[len1 + 1] = '\0';
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000786 }
Mark Whitley4e338752001-01-26 20:42:23 +0000787 } else {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200788 /* skip files if looking for dirs only (example: cd) */
Eric Andersenc470f442003-07-28 09:56:35 +0000789 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000790 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000791 }
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200792 /* add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000793 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000794 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000795 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000796 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000797 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000798 closedir(dir);
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200799 } /* for every path */
800
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000801 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000802 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000803 free(paths);
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200804 } else if (dirbuf) {
805 pf_len += strlen(dirbuf);
806 free(dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000807 }
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200808
809 return pf_len;
Erik Andersen6273f652000-03-17 01:12:41 +0000810}
Erik Andersenf0657d32000-04-12 17:49:52 +0000811
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200812/* build_match_prefix:
813 * On entry, matchBuf contains everything up to cursor at the moment <tab>
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200814 * was pressed. This function looks at it, figures out what part of it
815 * constitutes the command/file/directory prefix to use for completion,
816 * and rewrites matchBuf to contain only that part.
817 */
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200818/* Helpers: */
819/* QUOT is used on elements of int_buf[], which are bytes,
820 * not Unicode chars. Therefore it works correctly even in Unicode mode.
821 */
822#define QUOT (UCHAR_MAX+1)
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200823#define dbg_bmp 0
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200824static void remove_chunk(int16_t *int_buf, int beg, int end)
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200825{
826 /* beg must be <= end */
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200827 if (beg == end)
828 return;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200829
830 while ((int_buf[beg] = int_buf[end]) != 0)
831 beg++, end++;
832
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200833 if (dbg_bmp) {
834 int i;
835 for (i = 0; int_buf[i]; i++)
836 bb_putchar((unsigned char)int_buf[i]);
837 bb_putchar('\n');
838 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200839}
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200840static NOINLINE int build_match_prefix(char *matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000841{
842 int i, j;
843 int command_mode;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200844 int16_t *int_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000845
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200846 if (dbg_bmp) printf("\n%s\n", matchBuf);
847
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200848 int_buf = xmalloc(sizeof(int_buf[0]) * (strlen(matchBuf) + 1));
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200849 i = 0;
850 while ((int_buf[i] = (unsigned char)matchBuf[i]) != '\0')
851 i++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000852
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200853 /* Mark every \c as "quoted c" */
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200854 for (i = j = 0; matchBuf[i]; i++, j++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000855 if (matchBuf[i] == '\\') {
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200856 remove_chunk(int_buf, j, j + 1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000857 int_buf[j] |= QUOT;
858 i++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000859 }
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200860 }
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200861 /* Quote-mark "chars" and 'chars', drop delimiters */
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200862 {
863 int in_quote = 0;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200864 i = 0;
865 while (int_buf[i]) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200866 int cur = int_buf[i];
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200867 if (!cur)
868 break;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200869 if (cur == '\'' || cur == '"') {
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200870 if (!in_quote || (cur == in_quote)) {
871 in_quote ^= cur;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200872 remove_chunk(int_buf, i, i + 1);
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200873 continue;
874 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000875 }
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200876 if (in_quote)
877 int_buf[i] = cur | QUOT;
878 i++;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200879 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000880 }
881
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200882 /* Remove everything up to command delimiters:
883 * ';' ';;' '&' '|' '&&' '||',
884 * but careful with '>&' '<&' '>|'
885 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000886 for (i = 0; int_buf[i]; i++) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200887 int cur = int_buf[i];
888 if (cur == ';' || cur == '&' || cur == '|') {
889 int prev = i ? int_buf[i - 1] : 0;
890 if (cur == '&' && (prev == '>' || prev == '<')) {
891 continue;
892 } else if (cur == '|' && prev == '>') {
893 continue;
894 }
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200895 remove_chunk(int_buf, 0, i + 1 + (cur == int_buf[i + 1]));
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200896 i = -1; /* back to square 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000897 }
898 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200899 /* Remove all `cmd` */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200900 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000901 if (int_buf[i] == '`') {
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200902 for (j = i + 1; int_buf[j]; j++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000903 if (int_buf[j] == '`') {
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200904 /* `cmd` should count as a word:
905 * `cmd` c<tab> should search for files c*,
906 * not commands c*. Therefore we don't drop
907 * `cmd` entirely, we replace it with single `.
908 */
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200909 remove_chunk(int_buf, i, j);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200910 goto next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000911 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200912 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200913 /* No closing ` - command mode, remove all up to ` */
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200914 remove_chunk(int_buf, 0, i + 1);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200915 break;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200916 next: ;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000917 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200918 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000919
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200920 /* Remove "cmd (" and "cmd {"
921 * Example: "if { c<tab>"
922 * In this example, c should be matched as command pfx.
923 */
924 for (i = 0; int_buf[i]; i++) {
925 if (int_buf[i] == '(' || int_buf[i] == '{') {
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200926 remove_chunk(int_buf, 0, i + 1);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +0200927 i = -1; /* back to square 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000928 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200929 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000930
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200931 /* Remove leading unquoted spaces */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000932 for (i = 0; int_buf[i]; i++)
933 if (int_buf[i] != ' ')
934 break;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200935 remove_chunk(int_buf, 0, i);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000936
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200937 /* Determine completion mode */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000938 command_mode = FIND_EXE_ONLY;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200939 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000940 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200941 if (int_buf[i] == ' '
942 && command_mode == FIND_EXE_ONLY
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200943 && (char)int_buf[0] == 'c'
944 && (char)int_buf[1] == 'd'
945 && i == 2 /* -> int_buf[2] == ' ' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000946 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000947 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000948 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000949 command_mode = FIND_FILE_ONLY;
950 break;
951 }
952 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200953 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200954 if (dbg_bmp) printf("command_mode(0:exe/1:dir/2:file):%d\n", command_mode);
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200955
956 /* Remove everything except last word */
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200957 for (i = 0; int_buf[i]; i++) /* quasi-strlen(int_buf) */
958 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000959 for (--i; i >= 0; i--) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200960 int cur = int_buf[i];
961 if (cur == ' ' || cur == '<' || cur == '>' || cur == '|' || cur == '&') {
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200962 remove_chunk(int_buf, 0, i + 1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000963 break;
964 }
965 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000966
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200967 /* Store only match prefix */
968 i = 0;
969 while ((matchBuf[i] = int_buf[i]) != '\0')
970 i++;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200971 free(int_buf);
972
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200973 if (dbg_bmp) printf("final matchBuf:'%s'\n", matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000974
975 return command_mode;
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +0200976}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000977
Glenn L McGrath4d001292003-01-06 01:11:50 +0000978/*
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200979 * Display by column (original idea from ls applet,
980 * very optimized by me [Vladimir] :)
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000981 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000982static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000983{
984 int ncols, row;
985 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000986 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000987 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000988 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000989
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200990 /* find the longest file name - use that as the column width */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000991 for (row = 0; row < nrows; row++) {
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +0200992 l = unicode_strwidth(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000993 if (column_width < l)
994 column_width = l;
995 }
996 column_width += 2; /* min space for columns */
997 ncols = cmdedit_termw / column_width;
998
999 if (ncols > 1) {
1000 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001001 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +00001002 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001003 } else {
1004 ncols = 1;
1005 }
1006 for (row = 0; row < nrows; row++) {
1007 int n = row;
1008 int nc;
1009
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001010 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001011 printf("%s%-*s", matches[n],
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001012 (int)(column_width - unicode_strwidth(matches[n])), ""
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001013 );
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001014 }
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001015 if (ENABLE_UNICODE_SUPPORT)
1016 puts(printable_string(NULL, matches[n]));
1017 else
1018 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001019 }
1020}
1021
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001022static char *add_quote_for_spec_chars(char *found)
1023{
1024 int l = 0;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001025 char *s = xzalloc((strlen(found) + 1) * 2);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001026
1027 while (*found) {
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02001028 if (strchr(" `\"#$%^&*()=+{}[]:;'|\\<>", *found))
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001029 s[l++] = '\\';
1030 s[l++] = *found++;
1031 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001032 /* s[l] = '\0'; - already is */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001033 return s;
1034}
1035
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001036/* Do TAB completion */
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001037static NOINLINE void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001038{
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001039 char *chosen_match;
1040 size_t len_found;
1041/* char matchBuf[MAX_LINELEN]; */
1042#define matchBuf (S.input_tab__matchBuf)
1043 /* Length of string used for matching */
1044 unsigned match_pfx_len = match_pfx_len;
1045 int find_type;
1046#if ENABLE_UNICODE_SUPPORT
1047 /* cursor pos in command converted to multibyte form */
1048 int cursor_mb;
1049#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001050 if (!(state->flags & TAB_COMPLETION))
1051 return;
1052
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001053 if (*lastWasTab) {
1054 /* The last char was a TAB too.
1055 * Print a list of all the available choices.
1056 */
Denys Vlasenkoa46e16e2010-09-03 13:05:51 +02001057 if (num_matches > 0) {
1058 /* cursor will be changed by goto_new_line() */
Denys Vlasenko044b1802009-07-12 02:50:35 +02001059 int sav_cursor = cursor;
Mark Whitley4e338752001-01-26 20:42:23 +00001060 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001061 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001062 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001063 }
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001064 return;
Erik Andersenf0657d32000-04-12 17:49:52 +00001065 }
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001066
1067 *lastWasTab = 1;
1068
1069 /* Make a local copy of the string --
1070 * up to the position of the cursor */
1071#if !ENABLE_UNICODE_SUPPORT
1072 save_string(matchBuf, cursor + 1);
1073#else
1074 {
1075 CHAR_T wc = command_ps[cursor];
1076 command_ps[cursor] = BB_NUL;
1077 save_string(matchBuf, MAX_LINELEN);
1078 command_ps[cursor] = wc;
1079 cursor_mb = strlen(matchBuf);
1080 }
1081#endif
1082 find_type = build_match_prefix(matchBuf);
1083
1084 /* Free up any memory already allocated */
1085 free_tab_completion_data();
1086
1087#if ENABLE_FEATURE_USERNAME_COMPLETION
1088 /* If the word starts with `~' and there is no slash in the word,
1089 * then try completing this word as a username. */
1090 if (state->flags & USERNAME_COMPLETION)
1091 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == NULL)
1092 match_pfx_len = complete_username(matchBuf);
1093#endif
1094 /* Try to match a command in $PATH, or a directory, or a file */
1095 if (!matches)
1096 match_pfx_len = complete_cmd_dir_file(matchBuf, find_type);
1097 /* Remove duplicates */
1098 if (matches) {
1099 unsigned i;
1100 unsigned n = 0;
1101 qsort_string_vector(matches, num_matches);
1102 for (i = 0; i < num_matches - 1; ++i) {
1103 //if (matches[i] && matches[i+1]) { /* paranoia */
1104 if (strcmp(matches[i], matches[i+1]) == 0) {
1105 free(matches[i]);
1106 //matches[i] = NULL; /* paranoia */
1107 } else {
1108 matches[n++] = matches[i];
1109 }
1110 //}
1111 }
1112 matches[n++] = matches[i];
1113 num_matches = n;
1114 }
1115 /* Did we find exactly one match? */
1116 if (num_matches != 1) { /* no */
1117 char *cp;
1118 beep();
1119 if (!matches)
1120 return; /* no matches at all */
1121 /* Find common prefix */
1122 chosen_match = xstrdup(matches[0]);
1123 for (cp = chosen_match; *cp; cp++) {
1124 unsigned n;
1125 for (n = 1; n < num_matches; n++) {
1126 if (matches[n][cp - chosen_match] != *cp) {
1127 goto stop;
1128 }
1129 }
1130 }
1131 stop:
1132 if (cp == chosen_match) { /* have unique prefix? */
1133 free(chosen_match); /* no */
1134 return;
1135 }
1136 *cp = '\0';
1137 cp = add_quote_for_spec_chars(chosen_match);
1138 free(chosen_match);
1139 chosen_match = cp;
1140 len_found = strlen(chosen_match);
1141 } else { /* exactly one match */
1142 /* Next <tab> is not a double-tab */
1143 *lastWasTab = 0;
1144
1145 chosen_match = add_quote_for_spec_chars(matches[0]);
1146 len_found = strlen(chosen_match);
1147 if (chosen_match[len_found-1] != '/') {
1148 chosen_match[len_found] = ' ';
1149 chosen_match[++len_found] = '\0';
1150 }
1151 }
1152
1153#if !ENABLE_UNICODE_SUPPORT
1154 /* Have space to place the match? */
1155 /* The result consists of three parts with these lengths: */
1156 /* cursor + (len_found - match_pfx_len) + (command_len - cursor) */
1157 /* it simplifies into: */
1158 if ((int)(len_found - match_pfx_len + command_len) < S.maxsize) {
1159 int pos;
1160 /* save tail */
1161 strcpy(matchBuf, &command_ps[cursor]);
1162 /* add match and tail */
1163 sprintf(&command_ps[cursor], "%s%s", chosen_match + match_pfx_len, matchBuf);
1164 command_len = strlen(command_ps);
1165 /* new pos */
1166 pos = cursor + len_found - match_pfx_len;
1167 /* write out the matched command */
1168 redraw(cmdedit_y, command_len - pos);
1169 }
1170#else
1171 {
1172 char command[MAX_LINELEN];
1173 int len = save_string(command, sizeof(command));
1174 /* Have space to place the match? */
1175 /* cursor_mb + (len_found - match_pfx_len) + (len - cursor_mb) */
1176 if ((int)(len_found - match_pfx_len + len) < MAX_LINELEN) {
1177 int pos;
1178 /* save tail */
1179 strcpy(matchBuf, &command[cursor_mb]);
1180 /* where do we want to have cursor after all? */
1181 strcpy(&command[cursor_mb], chosen_match + match_pfx_len);
1182 len = load_string(command, S.maxsize);
1183 /* add match and tail */
1184 sprintf(&command[cursor_mb], "%s%s", chosen_match + match_pfx_len, matchBuf);
1185 command_len = load_string(command, S.maxsize);
1186 /* write out the matched command */
1187 /* paranoia: load_string can return 0 on conv error,
1188 * prevent passing pos = (0 - 12) to redraw */
1189 pos = command_len - len;
1190 redraw(cmdedit_y, pos >= 0 ? pos : 0);
1191 }
1192 }
1193#endif
1194 free(chosen_match);
1195#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +00001196}
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001197
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001198#endif /* FEATURE_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001199
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001200
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001201line_input_t* FAST_FUNC new_line_input_t(int flags)
1202{
1203 line_input_t *n = xzalloc(sizeof(*n));
1204 n->flags = flags;
1205 return n;
1206}
1207
1208
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001209#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001210
Denis Vlasenko682ad302008-09-27 01:28:56 +00001211static void save_command_ps_at_cur_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001212{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001213 if (command_ps[0] != BB_NUL) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001214 int cur = state->cur_history;
1215 free(state->history[cur]);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001216
Denys Vlasenko19158a82010-03-26 14:06:56 +01001217# if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001218 {
1219 char tbuf[MAX_LINELEN];
1220 save_string(tbuf, sizeof(tbuf));
1221 state->history[cur] = xstrdup(tbuf);
1222 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001223# else
Denis Vlasenko682ad302008-09-27 01:28:56 +00001224 state->history[cur] = xstrdup(command_ps);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001225# endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001226 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001227}
1228
1229/* state->flags is already checked to be nonzero */
1230static int get_previous_history(void)
1231{
1232 if ((state->flags & DO_HISTORY) && state->cur_history) {
1233 save_command_ps_at_cur_history();
1234 state->cur_history--;
1235 return 1;
1236 }
1237 beep();
1238 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001239}
1240
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001241static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001242{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001243 if (state->flags & DO_HISTORY) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001244 if (state->cur_history < state->cnt_history) {
1245 save_command_ps_at_cur_history(); /* save the current history line */
1246 return ++state->cur_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001247 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001248 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001249 beep();
1250 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001251}
Robert Griebl350d26b2002-12-03 22:45:46 +00001252
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001253# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001254/* We try to ensure that concurrent additions to the history
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001255 * do not overwrite each other.
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001256 * Otherwise shell users get unhappy.
1257 *
1258 * History file is trimmed lazily, when it grows several times longer
1259 * than configured MAX_HISTORY lines.
1260 */
1261
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001262static void free_line_input_t(line_input_t *n)
1263{
1264 int i = n->cnt_history;
1265 while (i > 0)
1266 free(n->history[--i]);
1267 free(n);
1268}
1269
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001270/* state->flags is already checked to be nonzero */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001271static void load_history(line_input_t *st_parm)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001272{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001273 char *temp_h[MAX_HISTORY];
1274 char *line;
Robert Griebl350d26b2002-12-03 22:45:46 +00001275 FILE *fp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001276 unsigned idx, i, line_len;
Robert Griebl350d26b2002-12-03 22:45:46 +00001277
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001278 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +00001279
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001280 fp = fopen_for_read(st_parm->hist_file);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001281 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001282 /* clean up old history */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001283 for (idx = st_parm->cnt_history; idx > 0;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001284 idx--;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001285 free(st_parm->history[idx]);
1286 st_parm->history[idx] = NULL;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001287 }
1288
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001289 /* fill temp_h[], retaining only last MAX_HISTORY lines */
1290 memset(temp_h, 0, sizeof(temp_h));
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001291 st_parm->cnt_history_in_file = idx = 0;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001292 while ((line = xmalloc_fgetline(fp)) != NULL) {
1293 if (line[0] == '\0') {
1294 free(line);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001295 continue;
1296 }
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001297 free(temp_h[idx]);
1298 temp_h[idx] = line;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001299 st_parm->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001300 idx++;
1301 if (idx == MAX_HISTORY)
1302 idx = 0;
Robert Griebl350d26b2002-12-03 22:45:46 +00001303 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001304 fclose(fp);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001305
1306 /* find first non-NULL temp_h[], if any */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001307 if (st_parm->cnt_history_in_file) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001308 while (temp_h[idx] == NULL) {
1309 idx++;
1310 if (idx == MAX_HISTORY)
1311 idx = 0;
1312 }
1313 }
1314
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001315 /* copy temp_h[] to st_parm->history[] */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001316 for (i = 0; i < MAX_HISTORY;) {
1317 line = temp_h[idx];
1318 if (!line)
1319 break;
1320 idx++;
1321 if (idx == MAX_HISTORY)
1322 idx = 0;
1323 line_len = strlen(line);
1324 if (line_len >= MAX_LINELEN)
1325 line[MAX_LINELEN-1] = '\0';
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001326 st_parm->history[i++] = line;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001327 }
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001328 st_parm->cnt_history = i;
Robert Griebl350d26b2002-12-03 22:45:46 +00001329 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001330}
1331
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001332/* state->flags is already checked to be nonzero */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001333static void save_history(char *str)
Robert Griebl350d26b2002-12-03 22:45:46 +00001334{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001335 int fd;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001336 int len, len2;
Eric Andersenc470f442003-07-28 09:56:35 +00001337
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001338 fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0666);
1339 if (fd < 0)
1340 return;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001341 xlseek(fd, 0, SEEK_END); /* paranoia */
1342 len = strlen(str);
1343 str[len] = '\n'; /* we (try to) do atomic write */
1344 len2 = full_write(fd, str, len + 1);
1345 str[len] = '\0';
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001346 close(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001347 if (len2 != len + 1)
1348 return; /* "wtf?" */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001349
1350 /* did we write so much that history file needs trimming? */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001351 state->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001352 if (state->cnt_history_in_file > MAX_HISTORY * 4) {
1353 FILE *fp;
1354 char *new_name;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001355 line_input_t *st_temp;
1356 int i;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001357
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001358 /* we may have concurrently written entries from others.
1359 * load them */
1360 st_temp = new_line_input_t(state->flags);
1361 st_temp->hist_file = state->hist_file;
1362 load_history(st_temp);
1363
1364 /* write out temp file and replace hist_file atomically */
1365 new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001366 fp = fopen_for_write(new_name);
1367 if (fp) {
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001368 for (i = 0; i < st_temp->cnt_history; i++)
1369 fprintf(fp, "%s\n", st_temp->history[i]);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001370 fclose(fp);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001371 if (rename(new_name, state->hist_file) == 0)
1372 state->cnt_history_in_file = st_temp->cnt_history;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001373 }
1374 free(new_name);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001375 free_line_input_t(st_temp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001376 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001377}
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001378# else
1379# define load_history(a) ((void)0)
1380# define save_history(a) ((void)0)
1381# endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001382
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001383static void remember_in_history(char *str)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001384{
1385 int i;
1386
1387 if (!(state->flags & DO_HISTORY))
1388 return;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001389 if (str[0] == '\0')
1390 return;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001391 i = state->cnt_history;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001392 /* Don't save dupes */
1393 if (i && strcmp(state->history[i-1], str) == 0)
1394 return;
1395
1396 free(state->history[MAX_HISTORY]); /* redundant, paranoia */
1397 state->history[MAX_HISTORY] = NULL; /* redundant, paranoia */
1398
1399 /* If history[] is full, remove the oldest command */
1400 /* we need to keep history[MAX_HISTORY] empty, hence >=, not > */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001401 if (i >= MAX_HISTORY) {
1402 free(state->history[0]);
1403 for (i = 0; i < MAX_HISTORY-1; i++)
1404 state->history[i] = state->history[i+1];
Denis Vlasenko682ad302008-09-27 01:28:56 +00001405 /* i == MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001406 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001407 /* i <= MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001408 state->history[i++] = xstrdup(str);
Denis Vlasenko682ad302008-09-27 01:28:56 +00001409 /* i <= MAX_HISTORY */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001410 state->cur_history = i;
1411 state->cnt_history = i;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001412# if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001413 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001414 save_history(str);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001415# endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001416 IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001417}
1418
1419#else /* MAX_HISTORY == 0 */
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001420# define remember_in_history(a) ((void)0)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001421#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001422
1423
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001424#if ENABLE_FEATURE_EDITING_VI
Erik Andersen6273f652000-03-17 01:12:41 +00001425/*
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001426 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001427 */
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001428static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001429vi_Word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001430{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001431 CHAR_T *command = command_ps;
1432
1433 while (cursor < command_len && !BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001434 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001435 if (eat) while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001436 input_forward();
1437}
1438
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001439static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001440vi_word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001441{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001442 CHAR_T *command = command_ps;
1443
1444 if (BB_isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001445 while (cursor < command_len
Denys Vlasenko13028922009-07-12 00:51:15 +02001446 && (BB_isalnum(command[cursor+1]) || command[cursor+1] == '_')
1447 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001448 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001449 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001450 } else if (BB_ispunct(command[cursor])) {
1451 while (cursor < command_len && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001452 input_forward();
1453 }
1454
Denis Vlasenko253ce002007-01-22 08:34:44 +00001455 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001456 input_forward();
1457
Denys Vlasenko13028922009-07-12 00:51:15 +02001458 if (eat) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001459 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001460 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001461 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001462}
1463
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001464static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001465vi_End_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001466{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001467 CHAR_T *command = command_ps;
1468
Paul Fox3f11b1b2005-08-04 19:04:46 +00001469 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001470 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001471 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001472 while (cursor < command_len-1 && !BB_isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001473 input_forward();
1474}
1475
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001476static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001477vi_end_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001478{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001479 CHAR_T *command = command_ps;
1480
Denis Vlasenko253ce002007-01-22 08:34:44 +00001481 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001482 return;
1483 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001484 while (cursor < command_len-1 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001485 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001486 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001487 return;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001488 if (BB_isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001489 while (cursor < command_len-1
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001490 && (BB_isalnum(command[cursor+1]) || command[cursor+1] == '_')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001491 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001492 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001493 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001494 } else if (BB_ispunct(command[cursor])) {
1495 while (cursor < command_len-1 && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001496 input_forward();
1497 }
1498}
1499
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001500static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001501vi_Back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001502{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001503 CHAR_T *command = command_ps;
1504
1505 while (cursor > 0 && BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001506 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001507 while (cursor > 0 && !BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001508 input_backward(1);
1509}
1510
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001511static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001512vi_back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001513{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001514 CHAR_T *command = command_ps;
1515
Paul Fox3f11b1b2005-08-04 19:04:46 +00001516 if (cursor <= 0)
1517 return;
1518 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001519 while (cursor > 0 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001520 input_backward(1);
1521 if (cursor <= 0)
1522 return;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001523 if (BB_isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001524 while (cursor > 0
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001525 && (BB_isalnum(command[cursor-1]) || command[cursor-1] == '_')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001526 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001527 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001528 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001529 } else if (BB_ispunct(command[cursor])) {
1530 while (cursor > 0 && BB_ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001531 input_backward(1);
1532 }
1533}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001534#endif
1535
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001536/* Modelled after bash 4.0 behavior of Ctrl-<arrow> */
1537static void ctrl_left(void)
1538{
1539 CHAR_T *command = command_ps;
1540
1541 while (1) {
1542 CHAR_T c;
1543
1544 input_backward(1);
1545 if (cursor == 0)
1546 break;
1547 c = command[cursor];
1548 if (c != ' ' && !BB_ispunct(c)) {
1549 /* we reached a "word" delimited by spaces/punct.
1550 * go to its beginning */
1551 while (1) {
1552 c = command[cursor - 1];
1553 if (c == ' ' || BB_ispunct(c))
1554 break;
1555 input_backward(1);
1556 if (cursor == 0)
1557 break;
1558 }
1559 break;
1560 }
1561 }
1562}
1563static void ctrl_right(void)
1564{
1565 CHAR_T *command = command_ps;
1566
1567 while (1) {
1568 CHAR_T c;
1569
1570 c = command[cursor];
1571 if (c == BB_NUL)
1572 break;
1573 if (c != ' ' && !BB_ispunct(c)) {
1574 /* we reached a "word" delimited by spaces/punct.
1575 * go to its end + 1 */
1576 while (1) {
1577 input_forward();
1578 c = command[cursor];
1579 if (c == BB_NUL || c == ' ' || BB_ispunct(c))
1580 break;
1581 }
1582 break;
1583 }
1584 input_forward();
1585 }
1586}
1587
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001588
1589/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001590 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001591 */
1592
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001593#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
1594static void ask_terminal(void)
1595{
1596 /* Ask terminal where is the cursor now.
1597 * lineedit_read_key handles response and corrects
1598 * our idea of current cursor position.
1599 * Testcase: run "echo -n long_line_long_line_long_line",
1600 * then type in a long, wrapping command and try to
1601 * delete it using backspace key.
1602 * Note: we print it _after_ prompt, because
1603 * prompt may contain CR. Example: PS1='\[\r\n\]\w '
1604 */
1605 /* Problem: if there is buffered input on stdin,
1606 * the response will be delivered later,
1607 * possibly to an unsuspecting application.
1608 * Testcase: "sleep 1; busybox ash" + press and hold [Enter].
1609 * Result:
1610 * ~/srcdevel/bbox/fix/busybox.t4 #
1611 * ~/srcdevel/bbox/fix/busybox.t4 #
1612 * ^[[59;34~/srcdevel/bbox/fix/busybox.t4 # <-- garbage
1613 * ~/srcdevel/bbox/fix/busybox.t4 #
1614 *
1615 * Checking for input with poll only makes the race narrower,
1616 * I still can trigger it. Strace:
1617 *
1618 * write(1, "~/srcdevel/bbox/fix/busybox.t4 # ", 33) = 33
1619 * poll([{fd=0, events=POLLIN}], 1, 0) = 0 (Timeout) <-- no input exists
1620 * write(1, "\33[6n", 4) = 4 <-- send the ESC sequence, quick!
1621 * poll([{fd=0, events=POLLIN}], 1, 4294967295) = 1 ([{fd=0, revents=POLLIN}])
1622 * read(0, "\n", 1) = 1 <-- oh crap, user's input got in first
1623 */
Denys Vlasenko451add42010-07-25 00:06:41 +02001624 struct pollfd pfd;
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001625
Denys Vlasenko451add42010-07-25 00:06:41 +02001626 pfd.fd = STDIN_FILENO;
1627 pfd.events = POLLIN;
1628 if (safe_poll(&pfd, 1, 0) == 0) {
1629 S.sent_ESC_br6n = 1;
1630 fputs("\033" "[6n", stdout);
1631 fflush_all(); /* make terminal see it ASAP! */
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001632 }
1633}
1634#else
1635#define ask_terminal() ((void)0)
1636#endif
1637
Denis Vlasenko38f63192007-01-22 09:03:07 +00001638#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001639static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001640{
1641 cmdedit_prompt = prmt_ptr;
1642 cmdedit_prmt_len = strlen(prmt_ptr);
1643 put_prompt();
1644}
1645#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001646static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001647{
1648 int prmt_len = 0;
1649 size_t cur_prmt_len = 0;
1650 char flg_not_length = '[';
1651 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001652 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1653 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001654 char c;
1655 char *pbuf;
1656
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001657 cmdedit_prmt_len = 0;
1658
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001659 if (!cwd_buf) {
1660 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001661 }
1662
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001663 cbuf[1] = '\0'; /* never changes */
1664
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001665 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001666 char *free_me = NULL;
1667
1668 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001669 c = *prmt_ptr++;
1670 if (c == '\\') {
1671 const char *cp = prmt_ptr;
1672 int l;
1673
1674 c = bb_process_escape_sequence(&prmt_ptr);
1675 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001676 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001677 break;
1678 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001679
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001680 switch (c) {
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02001681# if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001682 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001683 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001684 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001685# endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001686 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001687 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001688 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001689 break;
1690 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001691 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001692 break;
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02001693# if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001694 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001695 /* /home/user[/something] -> ~[/something] */
1696 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001697 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001698 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001699 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1700 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1701 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001702 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001703 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001704 }
1705 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001706# endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001707 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001708 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001709 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001710 if (cp != NULL && cp != pbuf)
1711 pbuf += (cp-pbuf) + 1;
1712 break;
1713 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001714 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001715 break;
1716 case 'e': case 'E': /* \e \E = \033 */
1717 c = '\033';
1718 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001719 case 'x': case 'X': {
1720 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001721 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001722 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001723 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001724 buf2[l] = '\0';
1725 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001726 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001727 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001728 break;
1729 }
1730 prmt_ptr++;
1731 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001732 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001733 if (c == 0)
1734 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001735 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001736 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001737 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001738 case '[': case ']':
1739 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001740 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001741 continue;
1742 }
1743 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001744 } /* switch */
1745 } /* if */
1746 } /* if */
1747 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001748 cur_prmt_len = strlen(pbuf);
1749 prmt_len += cur_prmt_len;
1750 if (flg_not_length != ']')
1751 cmdedit_prmt_len += cur_prmt_len;
1752 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001753 free(free_me);
1754 } /* while */
1755
1756 if (cwd_buf != (char *)bb_msg_unknown)
1757 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001758 cmdedit_prompt = prmt_mem_ptr;
1759 put_prompt();
1760}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001761#endif
1762
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001763static void cmdedit_setwidth(unsigned w, int redraw_flg)
1764{
1765 cmdedit_termw = w;
1766 if (redraw_flg) {
1767 /* new y for current cursor */
1768 int new_y = (cursor + cmdedit_prmt_len) / w;
1769 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001770 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001771 fflush_all();
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001772 }
1773}
1774
1775static void win_changed(int nsig)
1776{
Denys Vlasenko55241fa2010-07-18 22:53:06 +02001777 int sv_errno = errno;
Denis Vlasenko55995022008-05-18 22:28:26 +00001778 unsigned width;
Denys Vlasenko451add42010-07-25 00:06:41 +02001779 get_terminal_width_height(0, &width, NULL);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001780 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1781 if (nsig == SIGWINCH)
1782 signal(SIGWINCH, win_changed); /* rearm ourself */
Denys Vlasenko55241fa2010-07-18 22:53:06 +02001783 errno = sv_errno;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001784}
1785
Denys Vlasenko020f4062009-05-17 16:44:54 +02001786static int lineedit_read_key(char *read_key_buffer)
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001787{
Denys Vlasenko020f4062009-05-17 16:44:54 +02001788 int64_t ic;
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001789 int timeout = -1;
Denys Vlasenko19158a82010-03-26 14:06:56 +01001790#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001791 char unicode_buf[MB_CUR_MAX + 1];
1792 int unicode_idx = 0;
1793#endif
Denys Vlasenko020f4062009-05-17 16:44:54 +02001794
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001795 while (1) {
1796 /* Wait for input. TIMEOUT = -1 makes read_key wait even
1797 * on nonblocking stdin, TIMEOUT = 50 makes sure we won't
1798 * insist on full MB_CUR_MAX buffer to declare input like
1799 * "\xff\n",pause,"ls\n" invalid and thus won't lose "ls".
1800 *
1801 * Note: read_key sets errno to 0 on success.
1802 */
1803 ic = read_key(STDIN_FILENO, read_key_buffer, timeout);
1804 if (errno) {
Denys Vlasenko19158a82010-03-26 14:06:56 +01001805#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001806 if (errno == EAGAIN && unicode_idx != 0)
1807 goto pushback;
Denys Vlasenko1f6d2302009-10-29 03:45:26 +01001808#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001809 break;
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +02001810 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02001811
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01001812#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
1813 if ((int32_t)ic == KEYCODE_CURSOR_POS
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01001814 && S.sent_ESC_br6n
Denys Vlasenko020f4062009-05-17 16:44:54 +02001815 ) {
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01001816 S.sent_ESC_br6n = 0;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01001817 if (cursor == 0) { /* otherwise it may be bogus */
1818 int col = ((ic >> 32) & 0x7fff) - 1;
1819 if (col > cmdedit_prmt_len) {
1820 cmdedit_x += (col - cmdedit_prmt_len);
1821 while (cmdedit_x >= cmdedit_termw) {
1822 cmdedit_x -= cmdedit_termw;
1823 cmdedit_y++;
1824 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02001825 }
1826 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001827 continue;
Denys Vlasenko020f4062009-05-17 16:44:54 +02001828 }
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01001829#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001830
Denys Vlasenko19158a82010-03-26 14:06:56 +01001831#if ENABLE_UNICODE_SUPPORT
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001832 if (unicode_status == UNICODE_ON) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001833 wchar_t wc;
1834
1835 if ((int32_t)ic < 0) /* KEYCODE_xxx */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001836 break;
1837 // TODO: imagine sequence like: 0xff,<left-arrow>: we are currently losing 0xff...
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001838
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001839 unicode_buf[unicode_idx++] = ic;
1840 unicode_buf[unicode_idx] = '\0';
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001841 if (mbstowcs(&wc, unicode_buf, 1) != 1) {
1842 /* Not (yet?) a valid unicode char */
1843 if (unicode_idx < MB_CUR_MAX) {
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001844 timeout = 50;
1845 continue;
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001846 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001847 pushback:
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001848 /* Invalid sequence. Save all "bad bytes" except first */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001849 read_key_ungets(read_key_buffer, unicode_buf + 1, unicode_idx - 1);
Tomas Heinricha659b812010-04-29 13:43:39 +02001850# if !ENABLE_UNICODE_PRESERVE_BROKEN
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001851 ic = CONFIG_SUBST_WCHAR;
Tomas Heinricha659b812010-04-29 13:43:39 +02001852# else
Tomas Heinrichb8909c52010-05-16 20:46:53 +02001853 ic = unicode_mark_raw_byte(unicode_buf[0]);
Tomas Heinricha659b812010-04-29 13:43:39 +02001854# endif
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001855 } else {
1856 /* Valid unicode char, return its code */
1857 ic = wc;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001858 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001859 }
1860#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001861 break;
1862 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001863
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001864 return ic;
1865}
1866
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01001867#if ENABLE_UNICODE_BIDI_SUPPORT
1868static int isrtl_str(void)
1869{
1870 int idx = cursor;
Tomas Heinrichaa167552010-03-26 13:13:24 +01001871
1872 while (idx < command_len && unicode_bidi_is_neutral_wchar(command_ps[idx]))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01001873 idx++;
Tomas Heinrichaa167552010-03-26 13:13:24 +01001874 return unicode_bidi_isrtl(command_ps[idx]);
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01001875}
1876#else
1877# define isrtl_str() 0
1878#endif
1879
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001880/* leave out the "vi-mode"-only case labels if vi editing isn't
1881 * configured. */
Denys Vlasenko13028922009-07-12 00:51:15 +02001882#define vi_case(caselabel) IF_FEATURE_EDITING_VI(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001883
1884/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001885#undef CTRL
1886#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001887
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02001888/* maxsize must be >= 2.
1889 * Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001890 * -1 on read errors or EOF, or on bare Ctrl-D,
1891 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001892 * >0 length of input string, including terminating '\n'
1893 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001894int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001895{
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00001896 int len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001897#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001898 smallint lastWasTab = 0;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001899#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001900 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001901#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001902 smallint vi_cmdmode = 0;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001903#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001904 struct termios initial_settings;
1905 struct termios new_settings;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001906 char read_key_buffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001907
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001908 INIT_S();
1909
1910 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1911 || !(initial_settings.c_lflag & ECHO)
1912 ) {
1913 /* Happens when e.g. stty -echo was run before */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001914 parse_and_put_prompt(prompt);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001915 /* fflush_all(); - done by parse_and_put_prompt */
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001916 if (fgets(command, maxsize, stdin) == NULL)
1917 len = -1; /* EOF or error */
1918 else
1919 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001920 DEINIT_S();
1921 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001922 }
1923
Denys Vlasenko28055022010-01-04 20:49:58 +01001924 init_unicode();
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02001925
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001926// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001927 if (maxsize > MAX_LINELEN)
1928 maxsize = MAX_LINELEN;
Denys Vlasenko044b1802009-07-12 02:50:35 +02001929 S.maxsize = maxsize;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001930
1931 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001932 state = st ? st : (line_input_t*) &const_int_0;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001933#if MAX_HISTORY > 0
1934# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001935 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001936 if (state->cnt_history == 0)
1937 load_history(state);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001938# endif
Denis Vlasenko3c385cd2008-11-02 00:41:05 +00001939 if (state->flags & DO_HISTORY)
1940 state->cur_history = state->cnt_history;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001941#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001942
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001943 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001944 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001945 command_len = 0;
Denys Vlasenko19158a82010-03-26 14:06:56 +01001946#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001947 command_ps = xzalloc(maxsize * sizeof(command_ps[0]));
1948#else
Mark Whitley4e338752001-01-26 20:42:23 +00001949 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001950 command[0] = '\0';
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001951#endif
1952#define command command_must_not_be_used
Mark Whitley4e338752001-01-26 20:42:23 +00001953
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001954 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001955 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1956 /* Turn off echoing and CTRL-C, so we can trap it */
1957 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001958 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001959 new_settings.c_cc[VMIN] = 1;
1960 new_settings.c_cc[VTIME] = 0;
1961 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001962#ifndef _POSIX_VDISABLE
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001963# define _POSIX_VDISABLE '\0'
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001964#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001965 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko202ac502008-11-05 13:20:58 +00001966 tcsetattr_stdin_TCSANOW(&new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001967
Eric Andersen6faae7d2001-02-16 20:09:17 +00001968 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001969 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1970 win_changed(0); /* do initial resizing */
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02001971#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001972 {
1973 struct passwd *entry;
1974
1975 entry = getpwuid(geteuid());
1976 if (entry) {
1977 user_buf = xstrdup(entry->pw_name);
1978 home_pwd_buf = xstrdup(entry->pw_dir);
1979 }
1980 }
1981#endif
Denis Vlasenko682ad302008-09-27 01:28:56 +00001982
1983#if 0
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001984 for (i = 0; i <= MAX_HISTORY; i++)
1985 bb_error_msg("history[%d]:'%s'", i, state->history[i]);
Denis Vlasenko682ad302008-09-27 01:28:56 +00001986 bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
1987#endif
1988
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001989 /* Print out the command prompt, optionally ask where cursor is */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001990 parse_and_put_prompt(prompt);
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001991 ask_terminal();
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001992
Denys Vlasenkoc396fe62009-05-17 19:28:14 +02001993 read_key_buffer[0] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001994 while (1) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001995 /*
1996 * The emacs and vi modes share much of the code in the big
1997 * command loop. Commands entered when in vi's command mode
1998 * (aka "escape mode") get an extra bit added to distinguish
1999 * them - this keeps them from being self-inserted. This
2000 * clutters the big switch a bit, but keeps all the code
2001 * in one place.
2002 */
2003 enum {
2004 VI_CMDMODE_BIT = 0x40000000,
2005 /* 0x80000000 bit flags KEYCODE_xxx */
2006 };
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002007 int32_t ic, ic_raw;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002008
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002009 fflush_all();
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002010 ic = ic_raw = lineedit_read_key(read_key_buffer);
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002011
Denis Vlasenko38f63192007-01-22 09:03:07 +00002012#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00002013 newdelflag = 1;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002014 if (vi_cmdmode) {
2015 /* btw, since KEYCODE_xxx are all < 0, this doesn't
2016 * change ic if it contains one of them: */
2017 ic |= VI_CMDMODE_BIT;
2018 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00002019#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002020
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002021 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00002022 case '\n':
2023 case '\r':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002024 vi_case('\n'|VI_CMDMODE_BIT:)
2025 vi_case('\r'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002026 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002027 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00002028 break_out = 1;
2029 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002030 case CTRL('A'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002031 vi_case('0'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002032 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002033 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00002034 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002035 case CTRL('B'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002036 vi_case('h'|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002037 vi_case('\b'|VI_CMDMODE_BIT:) /* ^H */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002038 vi_case('\x7f'|VI_CMDMODE_BIT:) /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002039 input_backward(1); /* Move back one character */
Erik Andersenf0657d32000-04-12 17:49:52 +00002040 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002041 case CTRL('E'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002042 vi_case('$'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002043 /* Control-e -- End of line */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002044 put_till_end_and_adv_cursor();
Erik Andersenc7c634b2000-03-19 05:28:55 +00002045 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002046 case CTRL('F'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002047 vi_case('l'|VI_CMDMODE_BIT:)
2048 vi_case(' '|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002049 input_forward(); /* Move forward one character */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002050 break;
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002051 case '\b': /* ^H */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002052 case '\x7f': /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002053 if (!isrtl_str())
2054 input_backspace();
2055 else
2056 input_delete(0);
2057 break;
2058 case KEYCODE_DELETE:
2059 if (!isrtl_str())
2060 input_delete(0);
2061 else
2062 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00002063 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002064#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00002065 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002066 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002067 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002068#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002069 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00002070 /* Control-k -- clear to end of line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002071 command_ps[cursor] = BB_NUL;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002072 command_len = cursor;
Denys Vlasenko94043e82010-05-11 14:49:13 +02002073 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen65a07302002-04-13 13:26:49 +00002074 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002075 case CTRL('L'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002076 vi_case(CTRL('L')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002077 /* Control-l -- clear screen */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002078 printf("\033[H"); /* cursor to top,left */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002079 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00002080 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002081#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002082 case CTRL('N'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002083 vi_case(CTRL('N')|VI_CMDMODE_BIT:)
2084 vi_case('j'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002085 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002086 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002087 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00002088 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002089 case CTRL('P'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002090 vi_case(CTRL('P')|VI_CMDMODE_BIT:)
2091 vi_case('k'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002092 /* Control-p -- Get previous command from history */
Denis Vlasenko682ad302008-09-27 01:28:56 +00002093 if (get_previous_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002094 goto rewrite_line;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002095 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002096#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002097 case CTRL('U'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002098 vi_case(CTRL('U')|VI_CMDMODE_BIT:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002099 /* Control-U -- Clear line before cursor */
2100 if (cursor) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002101 command_len -= cursor;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002102 memmove(command_ps, command_ps + cursor,
2103 (command_len + 1) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002104 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002105 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002106 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002107 case CTRL('W'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002108 vi_case(CTRL('W')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002109 /* Control-W -- Remove the last word */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002110 while (cursor > 0 && BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002111 input_backspace();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002112 while (cursor > 0 && !BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002113 input_backspace();
2114 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002115
Denis Vlasenko38f63192007-01-22 09:03:07 +00002116#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002117 case 'i'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002118 vi_cmdmode = 0;
2119 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002120 case 'I'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002121 input_backward(cursor);
2122 vi_cmdmode = 0;
2123 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002124 case 'a'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002125 input_forward();
2126 vi_cmdmode = 0;
2127 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002128 case 'A'|VI_CMDMODE_BIT:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002129 put_till_end_and_adv_cursor();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002130 vi_cmdmode = 0;
2131 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002132 case 'x'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002133 input_delete(1);
2134 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002135 case 'X'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002136 if (cursor > 0) {
2137 input_backward(1);
2138 input_delete(1);
2139 }
2140 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002141 case 'W'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002142 vi_Word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002143 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002144 case 'w'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002145 vi_word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002146 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002147 case 'E'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002148 vi_End_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002149 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002150 case 'e'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002151 vi_end_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002152 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002153 case 'B'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002154 vi_Back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002155 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002156 case 'b'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002157 vi_back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002158 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002159 case 'C'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002160 vi_cmdmode = 0;
2161 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002162 case 'D'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002163 goto clear_to_eol;
2164
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002165 case 'c'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002166 vi_cmdmode = 0;
2167 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002168 case 'd'|VI_CMDMODE_BIT: {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002169 int nc, sc;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002170
Denys Vlasenko020f4062009-05-17 16:44:54 +02002171 ic = lineedit_read_key(read_key_buffer);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002172 if (errno) /* error */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002173 goto prepare_to_die;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002174 if (ic == ic_raw) { /* "cc", "dd" */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002175 input_backward(cursor);
2176 goto clear_to_eol;
2177 break;
2178 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002179
2180 sc = cursor;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002181 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002182 case 'w':
2183 case 'W':
2184 case 'e':
2185 case 'E':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002186 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002187 case 'w': /* "dw", "cw" */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002188 vi_word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002189 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002190 case 'W': /* 'dW', 'cW' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002191 vi_Word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002192 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002193 case 'e': /* 'de', 'ce' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002194 vi_end_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002195 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002196 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002197 case 'E': /* 'dE', 'cE' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002198 vi_End_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002199 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002200 break;
2201 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002202 nc = cursor;
2203 input_backward(cursor - sc);
2204 while (nc-- > cursor)
2205 input_delete(1);
2206 break;
2207 case 'b': /* "db", "cb" */
2208 case 'B': /* implemented as B */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002209 if (ic == 'b')
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002210 vi_back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002211 else
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002212 vi_Back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002213 while (sc-- > cursor)
2214 input_delete(1);
2215 break;
2216 case ' ': /* "d ", "c " */
2217 input_delete(1);
2218 break;
2219 case '$': /* "d$", "c$" */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002220 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002221 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002222 input_delete(1);
2223 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002224 }
2225 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002226 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002227 case 'p'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002228 input_forward();
2229 /* fallthrough */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002230 case 'P'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002231 put();
2232 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002233 case 'r'|VI_CMDMODE_BIT:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002234//FIXME: unicode case?
Denys Vlasenko020f4062009-05-17 16:44:54 +02002235 ic = lineedit_read_key(read_key_buffer);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002236 if (errno) /* error */
Paul Fox3f11b1b2005-08-04 19:04:46 +00002237 goto prepare_to_die;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002238 if (ic < ' ' || ic > 255) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00002239 beep();
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002240 } else {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002241 command_ps[cursor] = ic;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002242 bb_putchar(ic);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002243 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00002244 }
2245 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002246 case '\x1b': /* ESC */
2247 if (state->flags & VI_MODE) {
2248 /* insert mode --> command mode */
2249 vi_cmdmode = 1;
2250 input_backward(1);
2251 }
2252 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002253#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002254
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002255#if MAX_HISTORY > 0
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002256 case KEYCODE_UP:
2257 if (get_previous_history())
2258 goto rewrite_line;
2259 beep();
2260 break;
2261 case KEYCODE_DOWN:
2262 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002263 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002264 rewrite_line:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002265 /* Rewrite the line with the selected history item */
2266 /* change command */
Denys Vlasenko90a99042009-09-06 02:36:23 +02002267 command_len = load_string(state->history[state->cur_history] ?
2268 state->history[state->cur_history] : "", maxsize);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002269 /* redraw and go to eol (bol, in vi) */
2270 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
2271 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002272#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002273 case KEYCODE_RIGHT:
2274 input_forward();
2275 break;
2276 case KEYCODE_LEFT:
2277 input_backward(1);
2278 break;
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002279 case KEYCODE_CTRL_LEFT:
2280 ctrl_left();
2281 break;
2282 case KEYCODE_CTRL_RIGHT:
2283 ctrl_right();
2284 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002285 case KEYCODE_HOME:
2286 input_backward(cursor);
2287 break;
2288 case KEYCODE_END:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002289 put_till_end_and_adv_cursor();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002290 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002291
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002292 default:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002293 if (initial_settings.c_cc[VINTR] != 0
2294 && ic_raw == initial_settings.c_cc[VINTR]
2295 ) {
2296 /* Ctrl-C (usually) - stop gathering input */
2297 goto_new_line();
2298 command_len = 0;
2299 break_out = -1; /* "do not append '\n'" */
2300 break;
2301 }
2302 if (initial_settings.c_cc[VEOF] != 0
2303 && ic_raw == initial_settings.c_cc[VEOF]
2304 ) {
2305 /* Ctrl-D (usually) - delete one character,
2306 * or exit if len=0 and no chars to delete */
2307 if (command_len == 0) {
2308 errno = 0;
2309#if ENABLE_FEATURE_EDITING_VI
2310 prepare_to_die:
2311#endif
2312 break_out = command_len = -1;
2313 break;
2314 }
2315 input_delete(0);
2316 break;
2317 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002318// /* Control-V -- force insert of next char */
2319// if (c == CTRL('V')) {
2320// if (safe_read(STDIN_FILENO, &c, 1) < 1)
2321// goto prepare_to_die;
2322// if (c == 0) {
2323// beep();
2324// break;
2325// }
2326// }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002327 if (ic < ' '
Denys Vlasenko19158a82010-03-26 14:06:56 +01002328 || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2329 || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002330 ) {
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002331 /* If VI_CMDMODE_BIT is set, ic is >= 256
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002332 * and vi mode ignores unexpected chars.
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002333 * Otherwise, we are here if ic is a
2334 * control char or an unhandled ESC sequence,
2335 * which is also ignored.
2336 */
2337 break;
2338 }
2339 if ((int)command_len >= (maxsize - 2)) {
2340 /* Not enough space for the char and EOL */
2341 break;
Paul Fox84bbac52008-01-11 16:50:08 +00002342 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002343
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002344 command_len++;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002345 if (cursor == (command_len - 1)) {
2346 /* We are at the end, append */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002347 command_ps[cursor] = ic;
2348 command_ps[cursor + 1] = BB_NUL;
Denys Vlasenko94043e82010-05-11 14:49:13 +02002349 put_cur_glyph_and_inc_cursor();
Tomas Heinrichaa167552010-03-26 13:13:24 +01002350 if (unicode_bidi_isrtl(ic))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002351 input_backward(1);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002352 } else {
2353 /* In the middle, insert */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002354 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002355
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002356 memmove(command_ps + sc + 1, command_ps + sc,
2357 (command_len - sc) * sizeof(command_ps[0]));
2358 command_ps[sc] = ic;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002359 /* is right-to-left char, or neutral one (e.g. comma) was just added to rtl text? */
2360 if (!isrtl_str())
2361 sc++; /* no */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002362 put_till_end_and_adv_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +00002363 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002364 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002365 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00002366 break;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002367 } /* switch (ic) */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002368
2369 if (break_out)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002370 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002371
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002372#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002373 if (ic_raw != '\t')
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002374 lastWasTab = 0;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002375#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002376 } /* while (1) */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002377
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002378#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002379 if (S.sent_ESC_br6n) {
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002380 /* "sleep 1; busybox ash" + hold [Enter] to trigger.
2381 * We sent "ESC [ 6 n", but got '\n' first, and
2382 * KEYCODE_CURSOR_POS response is now buffered from terminal.
2383 * It's bad already and not much can be done with it
2384 * (it _will_ be visible for the next process to read stdin),
2385 * but without this delay it even shows up on the screen
2386 * as garbage because we restore echo settings with tcsetattr
2387 * before it comes in. UGLY!
2388 */
2389 usleep(20*1000);
2390 }
2391#endif
2392
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002393/* End of bug-catching "command_must_not_be_used" trick */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002394#undef command
2395
Denys Vlasenko19158a82010-03-26 14:06:56 +01002396#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +02002397 command[0] = '\0';
2398 if (command_len > 0)
2399 command_len = save_string(command, maxsize - 1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002400 free(command_ps);
2401#endif
2402
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002403 if (command_len > 0)
2404 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002405
Eric Andersen27bb7902003-12-23 20:24:51 +00002406 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002407 command[command_len++] = '\n';
2408 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00002409 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002410
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002411#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002412 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002413#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002414
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002415 /* restore initial_settings */
Denis Vlasenko202ac502008-11-05 13:20:58 +00002416 tcsetattr_stdin_TCSANOW(&initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002417 /* restore SIGWINCH handler */
2418 signal(SIGWINCH, previous_SIGWINCH_handler);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002419 fflush_all();
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002420
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002421 len = command_len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002422 DEINIT_S();
2423
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002424 return len; /* can't return command_len, DEINIT_S() destroys it */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002425}
2426
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002427#else /* !FEATURE_EDITING */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002428
2429#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00002430int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002431{
2432 fputs(prompt, stdout);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002433 fflush_all();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002434 fgets(command, maxsize, stdin);
2435 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00002436}
2437
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002438#endif /* !FEATURE_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00002439
2440
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002441/*
2442 * Testing
2443 */
2444
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002445#ifdef TEST
2446
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002447#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002448
2449const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002450
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002451int main(int argc, char **argv)
2452{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00002453 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002454 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00002455#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002456 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
2457 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
2458 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002459#else
2460 "% ";
2461#endif
2462
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002463 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00002464 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002465 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002466 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00002467 break;
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002468 buff[l-1] = '\0';
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002469 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00002470 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002471 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002472 return 0;
2473}
2474
Eric Andersenc470f442003-07-28 09:56:35 +00002475#endif /* TEST */