blob: 896bbc88c155ccd755f2cac48a11ce43b397e04f [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 */
Erik Andersen13456d12000-03-16 08:09:57 +000016/*
Denis Vlasenko78ff8192008-06-28 21:03:43 +000017 * Usage and known bugs:
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010018 * Terminal key codes are not extensive, more needs to be added.
19 * This version was created on Debian GNU/Linux 2.x.
Denis Vlasenko78ff8192008-06-28 21:03:43 +000020 * Delete, Backspace, Home, End, and the arrow keys were tested
21 * to work in an Xterm and console. Ctrl-A also works as Home.
22 * Ctrl-E also works as End.
23 *
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010024 * The following readline-like commands are not implemented:
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010025 * CTL-t -- Transpose two characters
26 *
Denis Vlasenko78ff8192008-06-28 21:03:43 +000027 * lineedit does not know that the terminal escape sequences do not
28 * take up space on the screen. The redisplay code assumes, unless
29 * told otherwise, that each character in the prompt is a printable
30 * character that takes up one character position on the screen.
31 * You need to tell lineedit that some sequences of characters
32 * in the prompt take up no screen space. Compatibly with readline,
33 * use the \[ escape to begin a sequence of non-printing characters,
34 * and the \] escape to signal the end of such a sequence. Example:
35 *
36 * PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +020037 *
38 * Unicode in PS1 is not fully supported: prompt length calulation is wrong,
39 * resulting in line wrap problems with long (multi-line) input.
Erik Andersen13456d12000-03-16 08:09:57 +000040 */
Ron Yorstonf23264b2015-05-29 11:31:40 +010041#include "busybox.h"
42#include "NUM_APPLETS.h"
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020043#include "unicode.h"
Alexey Fomenko232ebaa2011-05-20 04:26:29 +020044#ifndef _POSIX_VDISABLE
45# define _POSIX_VDISABLE '\0'
46#endif
47
Glenn L McGrath475820c2004-01-22 12:42:23 +000048
Glenn L McGrath3b251852004-01-03 12:07:32 +000049#ifdef TEST
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020050# define ENABLE_FEATURE_EDITING 0
51# define ENABLE_FEATURE_TAB_COMPLETION 0
52# define ENABLE_FEATURE_USERNAME_COMPLETION 0
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020053#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000054
Eric Andersen5165fbe2001-02-20 06:42:29 +000055
Denis Vlasenko82b39e82007-01-21 19:18:19 +000056/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko38f63192007-01-22 09:03:07 +000057#if ENABLE_FEATURE_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000058
Denis Vlasenko82b39e82007-01-21 19:18:19 +000059
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +020060#define ENABLE_USERNAME_OR_HOMEDIR \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000061 (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +020062#define IF_USERNAME_OR_HOMEDIR(...)
63#if ENABLE_USERNAME_OR_HOMEDIR
64# undef IF_USERNAME_OR_HOMEDIR
65# define IF_USERNAME_OR_HOMEDIR(...) __VA_ARGS__
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000066#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000067
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020068
69#undef CHAR_T
Denys Vlasenko19158a82010-03-26 14:06:56 +010070#if ENABLE_UNICODE_SUPPORT
Tomas Heinricha659b812010-04-29 13:43:39 +020071# define BB_NUL ((wchar_t)0)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020072# define CHAR_T wchar_t
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010073static bool BB_isspace(CHAR_T c) { return ((unsigned)c < 256 && isspace(c)); }
Denys Vlasenko31e2e7b2009-12-12 02:42:35 +010074# if ENABLE_FEATURE_EDITING_VI
Natanael Copa7e6f9312016-08-14 23:30:29 +020075static bool BB_isalnum_or_underscore(CHAR_T c) {
76 return ((unsigned)c < 256 && isalnum(c)) || c == '_';
77}
Denys Vlasenko31e2e7b2009-12-12 02:42:35 +010078# endif
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010079static bool BB_ispunct(CHAR_T c) { return ((unsigned)c < 256 && ispunct(c)); }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020080# undef isspace
81# undef isalnum
82# undef ispunct
83# undef isprint
84# define isspace isspace_must_not_be_used
85# define isalnum isalnum_must_not_be_used
86# define ispunct ispunct_must_not_be_used
87# define isprint isprint_must_not_be_used
88#else
89# define BB_NUL '\0'
90# define CHAR_T char
91# define BB_isspace(c) isspace(c)
Natanael Copa7e6f9312016-08-14 23:30:29 +020092# if ENABLE_FEATURE_EDITING_VI
Denys Vlasenko82d1c1f2017-12-31 17:30:02 +010093static bool BB_isalnum_or_underscore(CHAR_T c)
94{
Natanael Copa7e6f9312016-08-14 23:30:29 +020095 return ((unsigned)c < 256 && isalnum(c)) || c == '_';
96}
97# endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020098# define BB_ispunct(c) ispunct(c)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020099#endif
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200100#if ENABLE_UNICODE_PRESERVE_BROKEN
101# define unicode_mark_raw_byte(wc) ((wc) | 0x20000000)
102# define unicode_is_raw_byte(wc) ((wc) & 0x20000000)
103#else
104# define unicode_is_raw_byte(wc) 0
105#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200106
107
Marek Polacek7b181072010-10-28 21:34:56 +0200108#define ESC "\033"
109
110#define SEQ_CLEAR_TILL_END_OF_SCREEN ESC"[J"
111//#define SEQ_CLEAR_TILL_END_OF_LINE ESC"[K"
Tomas Heinricha659b812010-04-29 13:43:39 +0200112
113
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000114enum {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000115 MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN < 0x7ff0
Denis Vlasenko6b404432008-01-07 16:13:14 +0000116 ? CONFIG_FEATURE_EDITING_MAX_LEN
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000117 : 0x7ff0
118};
Robert Griebl350d26b2002-12-03 22:45:46 +0000119
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200120#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000121static const char null_str[] ALIGN1 = "";
122#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +0000123
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000124/* We try to minimize both static and stack usage. */
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000125struct lineedit_statics {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000126 line_input_t *state;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000127
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100128 unsigned cmdedit_termw; /* = 80; */ /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +0000129
Denys Vlasenko020f4062009-05-17 16:44:54 +0200130 unsigned cmdedit_x; /* real x (col) terminal position */
131 unsigned cmdedit_y; /* pseudoreal y (row) terminal position */
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200132 unsigned cmdedit_prmt_len; /* on-screen length of last/sole prompt line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000133
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000134 unsigned cursor;
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +0200135 int command_len; /* must be signed */
136 /* signed maxsize: we want x in "if (x > S.maxsize)"
Denys Vlasenko044b1802009-07-12 02:50:35 +0200137 * to _not_ be promoted to unsigned */
138 int maxsize;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200139 CHAR_T *command_ps;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000140
141 const char *cmdedit_prompt;
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200142 const char *prompt_last_line; /* last/sole prompt line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000143
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200144#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000145 char *user_buf;
146 char *home_pwd_buf; /* = (char*)null_str; */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000147#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000148
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000149#if ENABLE_FEATURE_TAB_COMPLETION
150 char **matches;
151 unsigned num_matches;
152#endif
153
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100154 unsigned SIGWINCH_saved;
155 volatile unsigned SIGWINCH_count;
156 volatile smallint ok_to_redraw;
157
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000158#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200159# define DELBUFSIZ 128
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000160 smallint newdelflag; /* whether delbuf should be reused yet */
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100161 CHAR_T *delptr;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200162 CHAR_T delbuf[DELBUFSIZ]; /* a place to store deleted characters */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000163#endif
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100164#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +0100165 smallint sent_ESC_br6n;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100166#endif
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100167
168 /* Largish struct, keeping it last results in smaller code */
169 struct sigaction SIGWINCH_handler;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000170};
171
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000172/* See lineedit_ptr_hack.c */
173extern struct lineedit_statics *const lineedit_ptr_to_statics;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000174
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000175#define S (*lineedit_ptr_to_statics)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000176#define state (S.state )
177#define cmdedit_termw (S.cmdedit_termw )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000178#define cmdedit_x (S.cmdedit_x )
179#define cmdedit_y (S.cmdedit_y )
180#define cmdedit_prmt_len (S.cmdedit_prmt_len)
181#define cursor (S.cursor )
182#define command_len (S.command_len )
183#define command_ps (S.command_ps )
184#define cmdedit_prompt (S.cmdedit_prompt )
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200185#define prompt_last_line (S.prompt_last_line)
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000186#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000187#define home_pwd_buf (S.home_pwd_buf )
188#define matches (S.matches )
189#define num_matches (S.num_matches )
190#define delptr (S.delptr )
191#define newdelflag (S.newdelflag )
192#define delbuf (S.delbuf )
193
194#define INIT_S() do { \
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000195 (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000196 barrier(); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000197 cmdedit_termw = 80; \
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200198 IF_USERNAME_OR_HOMEDIR(home_pwd_buf = (char*)null_str;) \
Shawn J. Goff46031da2013-02-27 18:30:05 +0100199 IF_FEATURE_EDITING_VI(delptr = delbuf;) \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000200} while (0)
Alexey Fomenko232ebaa2011-05-20 04:26:29 +0200201
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000202static void deinit_S(void)
203{
204#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000205 /* This one is allocated only if FANCY_PROMPT is on
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200206 * (otherwise it points to verbatim prompt (NOT malloced)) */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000207 free((char*)cmdedit_prompt);
208#endif
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200209#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000210 free(user_buf);
211 if (home_pwd_buf != null_str)
212 free(home_pwd_buf);
213#endif
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000214 free(lineedit_ptr_to_statics);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000215}
216#define DEINIT_S() deinit_S()
217
Denis Vlasenko2c844952008-04-25 18:44:35 +0000218
Denys Vlasenko19158a82010-03-26 14:06:56 +0100219#if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200220static size_t load_string(const char *src)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200221{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100222 if (unicode_status == UNICODE_ON) {
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200223 ssize_t len = mbstowcs(command_ps, src, S.maxsize - 1);
Denys Vlasenko353680a2011-03-27 01:18:07 +0100224 if (len < 0)
225 len = 0;
226 command_ps[len] = BB_NUL;
227 return len;
228 } else {
229 unsigned i = 0;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200230 while (src[i] && i < S.maxsize - 1) {
231 command_ps[i] = src[i];
Denys Vlasenko353680a2011-03-27 01:18:07 +0100232 i++;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200233 }
234 command_ps[i] = BB_NUL;
Denys Vlasenko353680a2011-03-27 01:18:07 +0100235 return i;
236 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200237}
Tomas Heinricha659b812010-04-29 13:43:39 +0200238static unsigned save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200239{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100240 if (unicode_status == UNICODE_ON) {
Denys Vlasenko94043e82010-05-11 14:49:13 +0200241# if !ENABLE_UNICODE_PRESERVE_BROKEN
Denys Vlasenko353680a2011-03-27 01:18:07 +0100242 ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
243 if (len < 0)
244 len = 0;
245 dst[len] = '\0';
246 return len;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200247# else
Denys Vlasenko353680a2011-03-27 01:18:07 +0100248 unsigned dstpos = 0;
249 unsigned srcpos = 0;
Tomas Heinricha659b812010-04-29 13:43:39 +0200250
Denys Vlasenko353680a2011-03-27 01:18:07 +0100251 maxsize--;
252 while (dstpos < maxsize) {
253 wchar_t wc;
254 int n = srcpos;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200255
Denys Vlasenko353680a2011-03-27 01:18:07 +0100256 /* Convert up to 1st invalid byte (or up to end) */
257 while ((wc = command_ps[srcpos]) != BB_NUL
258 && !unicode_is_raw_byte(wc)
259 ) {
260 srcpos++;
261 }
262 command_ps[srcpos] = BB_NUL;
263 n = wcstombs(dst + dstpos, command_ps + n, maxsize - dstpos);
264 if (n < 0) /* should not happen */
265 break;
266 dstpos += n;
267 if (wc == BB_NUL) /* usually is */
268 break;
269
270 /* We do have invalid byte here! */
271 command_ps[srcpos] = wc; /* restore it */
Tomas Heinricha659b812010-04-29 13:43:39 +0200272 srcpos++;
Denys Vlasenko353680a2011-03-27 01:18:07 +0100273 if (dstpos == maxsize)
274 break;
275 dst[dstpos++] = (char) wc;
Tomas Heinricha659b812010-04-29 13:43:39 +0200276 }
Denys Vlasenko353680a2011-03-27 01:18:07 +0100277 dst[dstpos] = '\0';
278 return dstpos;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200279# endif
Denys Vlasenko353680a2011-03-27 01:18:07 +0100280 } else {
281 unsigned i = 0;
282 while ((dst[i] = command_ps[i]) != 0)
283 i++;
284 return i;
285 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200286}
287/* I thought just fputwc(c, stdout) would work. But no... */
288static void BB_PUTCHAR(wchar_t c)
289{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100290 if (unicode_status == UNICODE_ON) {
291 char buf[MB_CUR_MAX + 1];
292 mbstate_t mbst = { 0 };
293 ssize_t len = wcrtomb(buf, c, &mbst);
294 if (len > 0) {
295 buf[len] = '\0';
296 fputs(buf, stdout);
297 }
298 } else {
299 /* In this case, c is always one byte */
300 putchar(c);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200301 }
302}
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200303# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
304static wchar_t adjust_width_and_validate_wc(unsigned *width_adj, wchar_t wc)
305# else
306static wchar_t adjust_width_and_validate_wc(wchar_t wc)
307# define adjust_width_and_validate_wc(width_adj, wc) \
308 ((*(width_adj))++, adjust_width_and_validate_wc(wc))
309# endif
310{
311 int w = 1;
312
313 if (unicode_status == UNICODE_ON) {
Denys Vlasenko26e2c1d2010-05-16 21:15:03 +0200314 if (wc > CONFIG_LAST_SUPPORTED_WCHAR) {
315 /* note: also true for unicode_is_raw_byte(wc) */
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200316 goto subst;
317 }
318 w = wcwidth(wc);
319 if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0)
320 || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
321 || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
322 ) {
323 subst:
324 w = 1;
325 wc = CONFIG_SUBST_WCHAR;
326 }
327 }
328
329# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
330 *width_adj += w;
331#endif
332 return wc;
333}
334#else /* !UNICODE */
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200335static size_t load_string(const char *src)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200336{
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200337 safe_strncpy(command_ps, src, S.maxsize);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200338 return strlen(command_ps);
339}
Denys Vlasenko9038d6f2009-07-15 20:02:19 +0200340# if ENABLE_FEATURE_TAB_COMPLETION
Tomas Heinricha659b812010-04-29 13:43:39 +0200341static void save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200342{
343 safe_strncpy(dst, command_ps, maxsize);
344}
Denys Vlasenko7dd0ce42009-07-15 18:27:47 +0200345# endif
346# define BB_PUTCHAR(c) bb_putchar(c)
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200347/* Should never be called: */
348int adjust_width_and_validate_wc(unsigned *width_adj, int wc);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200349#endif
350
351
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000352/* Put 'command_ps[cursor]', cursor++.
353 * Advance cursor on screen. If we reached right margin, scroll text up
354 * and remove terminal margin effect by printing 'next_char' */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000355#define HACK_FOR_WRONG_WIDTH 1
Denys Vlasenko94043e82010-05-11 14:49:13 +0200356static void put_cur_glyph_and_inc_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000357{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200358 CHAR_T c = command_ps[cursor];
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200359 unsigned width = 0;
360 int ofs_to_right;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000361
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200362 if (c == BB_NUL) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000363 /* erase character after end of input string */
364 c = ' ';
Denys Vlasenko94043e82010-05-11 14:49:13 +0200365 } else {
366 /* advance cursor only if we aren't at the end yet */
367 cursor++;
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200368 if (unicode_status == UNICODE_ON) {
369 IF_UNICODE_WIDE_WCHARS(width = cmdedit_x;)
370 c = adjust_width_and_validate_wc(&cmdedit_x, c);
371 IF_UNICODE_WIDE_WCHARS(width = cmdedit_x - width;)
372 } else {
373 cmdedit_x++;
374 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000375 }
Denys Vlasenko94043e82010-05-11 14:49:13 +0200376
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200377 ofs_to_right = cmdedit_x - cmdedit_termw;
378 if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right <= 0) {
379 /* c fits on this line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200380 BB_PUTCHAR(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000381 }
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200382
383 if (ofs_to_right >= 0) {
384 /* we go to the next line */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000385#if HACK_FOR_WRONG_WIDTH
386 /* This works better if our idea of term width is wrong
387 * and it is actually wider (often happens on serial lines).
388 * Printing CR,LF *forces* cursor to next line.
389 * OTOH if terminal width is correct AND terminal does NOT
390 * have automargin (IOW: it is moving cursor to next line
391 * by itself (which is wrong for VT-10x terminals)),
392 * this will break things: there will be one extra empty line */
393 puts("\r"); /* + implicit '\n' */
394#else
Denys Vlasenko94043e82010-05-11 14:49:13 +0200395 /* VT-10x terminals don't wrap cursor to next line when last char
396 * on the line is printed - cursor stays "over" this char.
397 * Need to print _next_ char too (first one to appear on next line)
398 * to make cursor move down to next line.
399 */
400 /* Works ok only if cmdedit_termw is correct. */
401 c = command_ps[cursor];
402 if (c == BB_NUL)
403 c = ' ';
404 BB_PUTCHAR(c);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000405 bb_putchar('\b');
Denis Vlasenko2c844952008-04-25 18:44:35 +0000406#endif
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200407 cmdedit_y++;
408 if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right == 0) {
409 width = 0;
410 } else { /* ofs_to_right > 0 */
411 /* wide char c didn't fit on prev line */
412 BB_PUTCHAR(c);
413 }
414 cmdedit_x = width;
Mark Whitley4e338752001-01-26 20:42:23 +0000415 }
Erik Andersen13456d12000-03-16 08:09:57 +0000416}
417
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000418/* Move to end of line (by printing all chars till the end) */
Denys Vlasenko94043e82010-05-11 14:49:13 +0200419static void put_till_end_and_adv_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000420{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000421 while (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200422 put_cur_glyph_and_inc_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +0000423}
424
425/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000426static void goto_new_line(void)
427{
Denys Vlasenko94043e82010-05-11 14:49:13 +0200428 put_till_end_and_adv_cursor();
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200429 if (cmdedit_x != 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000430 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000431}
432
Rob Landley88621d72006-08-29 19:41:06 +0000433static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000434{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000435 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000436}
437
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200438/* Full or last/sole prompt line, reset edit cursor, calculate terminal cursor.
439 * cmdedit_y is always calculated for the last/sole prompt line.
440 */
441static void put_prompt_custom(bool is_full)
Denys Vlasenko248c3242010-05-17 00:45:44 +0200442{
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200443 fputs((is_full ? cmdedit_prompt : prompt_last_line), stdout);
Denys Vlasenko248c3242010-05-17 00:45:44 +0200444 cursor = 0;
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100445 cmdedit_y = cmdedit_prmt_len / cmdedit_termw; /* new quasireal y */
446 cmdedit_x = cmdedit_prmt_len % cmdedit_termw;
Denys Vlasenko248c3242010-05-17 00:45:44 +0200447}
448
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200449#define put_prompt_last_line() put_prompt_custom(0)
450#define put_prompt() put_prompt_custom(1)
451
Eric Andersenaff114c2004-04-14 17:51:38 +0000452/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000453/* (optimized for slow terminals) */
454static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000455{
456 if (num > cursor)
457 num = cursor;
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200458 if (num == 0)
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000459 return;
460 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000461
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200462 if ((ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS)
463 && unicode_status == UNICODE_ON
464 ) {
465 /* correct NUM to be equal to _screen_ width */
466 int n = num;
467 num = 0;
468 while (--n >= 0)
469 adjust_width_and_validate_wc(&num, command_ps[cursor + n]);
470 if (num == 0)
471 return;
472 }
473
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000474 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000475 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000476 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000477 /* This is longer by 5 bytes on x86.
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000478 * Also gets miscompiled for ARM users
479 * (busybox.net/bugs/view.php?id=2274).
Denis Vlasenko6f043912008-02-18 22:28:03 +0000480 * printf(("\b\b\b\b" + 4) - num);
481 * return;
482 */
483 do {
484 bb_putchar('\b');
485 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000486 return;
487 }
Marek Polacek7b181072010-10-28 21:34:56 +0200488 printf(ESC"[%uD", num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000489 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000490 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000491
492 /* Need to go one or more lines up */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200493 if (ENABLE_UNICODE_WIDE_WCHARS) {
494 /* With wide chars, it is hard to "backtrack"
495 * and reliably figure out where to put cursor.
496 * Example (<> is a wide char; # is an ordinary char, _ cursor):
497 * |prompt: <><> |
498 * |<><><><><><> |
499 * |_ |
500 * and user presses left arrow. num = 1, cmdedit_x = 0,
501 * We need to go up one line, and then - how do we know that
502 * we need to go *10* positions to the right? Because
503 * |prompt: <>#<>|
504 * |<><><>#<><><>|
505 * |_ |
506 * in this situation we need to go *11* positions to the right.
507 *
508 * A simpler thing to do is to redraw everything from the start
509 * up to new cursor position (which is already known):
510 */
511 unsigned sv_cursor;
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200512 /* go to 1st column; go up to first line */
Marek Polacek7b181072010-10-28 21:34:56 +0200513 printf("\r" ESC"[%uA", cmdedit_y);
Denys Vlasenko248c3242010-05-17 00:45:44 +0200514 cmdedit_y = 0;
515 sv_cursor = cursor;
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200516 put_prompt_last_line(); /* sets cursor to 0 */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200517 while (cursor < sv_cursor)
518 put_cur_glyph_and_inc_cursor();
519 } else {
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200520 int lines_up;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200521 /* num = chars to go back from the beginning of current line: */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200522 num -= cmdedit_x;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200523 /* num=1...w: one line up, w+1...2w: two, etc: */
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100524 lines_up = 1 + (num - 1) / cmdedit_termw;
525 cmdedit_x = (cmdedit_termw * cmdedit_y - num) % cmdedit_termw;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200526 cmdedit_y -= lines_up;
527 /* go to 1st column; go up */
Marek Polacek7b181072010-10-28 21:34:56 +0200528 printf("\r" ESC"[%uA", lines_up);
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200529 /* go to correct column.
Denys Vlasenkobbf1aa12010-05-17 12:33:13 +0200530 * xterm, konsole, Linux VT interpret 0 as 1 below! wow.
531 * need to *make sure* we skip it if cmdedit_x == 0 */
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200532 if (cmdedit_x)
Marek Polacek7b181072010-10-28 21:34:56 +0200533 printf(ESC"[%uC", cmdedit_x);
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000534 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000535}
536
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200537/* See redraw and draw_full below */
538static void draw_custom(int y, int back_cursor, bool is_full)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000539{
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200540 if (y > 0) /* up y lines */
Marek Polacek7b181072010-10-28 21:34:56 +0200541 printf(ESC"[%uA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000542 bb_putchar('\r');
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200543 put_prompt_custom(is_full);
Denys Vlasenko94043e82010-05-11 14:49:13 +0200544 put_till_end_and_adv_cursor();
545 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000546 input_backward(back_cursor);
547}
548
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200549/* Move y lines up, draw last/sole prompt line, editor line[s], and clear tail.
550 * goal: redraw the prompt+input+cursor in-place, overwriting the previous */
551#define redraw(y, back_cursor) draw_custom((y), (back_cursor), 0)
552
553/* Like above, but without moving up, and while using all the prompt lines.
554 * goal: draw a full prompt+input+cursor unrelated to a previous position.
555 * note: cmdedit_y always ends up relating to the last/sole prompt line */
556#define draw_full(back_cursor) draw_custom(0, (back_cursor), 1)
557
Paul Fox3f11b1b2005-08-04 19:04:46 +0000558/* Delete the char in front of the cursor, optionally saving it
559 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000560#if !ENABLE_FEATURE_EDITING_VI
561static void input_delete(void)
562#define input_delete(save) input_delete()
563#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000564static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000565#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000566{
Mark Whitley4e338752001-01-26 20:42:23 +0000567 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000568
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000569 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000570 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000571
Denis Vlasenko38f63192007-01-22 09:03:07 +0000572#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000573 if (save) {
574 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000575 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000576 newdelflag = 0;
577 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000578 if ((delptr - delbuf) < DELBUFSIZ)
579 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000580 }
581#endif
582
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200583 memmove(command_ps + j, command_ps + j + 1,
Denys Vlasenko9531f7d2009-07-16 14:33:16 +0200584 /* (command_len + 1 [because of NUL]) - (j + 1)
585 * simplified into (command_len - j) */
586 (command_len - j) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000587 command_len--;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200588 put_till_end_and_adv_cursor();
589 /* Last char is still visible, erase it (and more) */
590 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersenc470f442003-07-28 09:56:35 +0000591 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000592}
593
Denis Vlasenko38f63192007-01-22 09:03:07 +0000594#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000595static void put(void)
596{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000597 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000598 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000599
Paul Fox3f11b1b2005-08-04 19:04:46 +0000600 if (j == 0)
601 return;
602 ocursor = cursor;
603 /* open hole and then fill it */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200604 memmove(command_ps + cursor + j, command_ps + cursor,
605 (command_len - cursor + 1) * sizeof(command_ps[0]));
606 memcpy(command_ps + cursor, delbuf, j * sizeof(command_ps[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000607 command_len += j;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200608 put_till_end_and_adv_cursor();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000609 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000610}
611#endif
612
Mark Whitley4e338752001-01-26 20:42:23 +0000613/* Delete the char in back of the cursor */
614static void input_backspace(void)
615{
616 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000617 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000618 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000619 }
620}
621
Eric Andersenaff114c2004-04-14 17:51:38 +0000622/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000623static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000624{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000625 if (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200626 put_cur_glyph_and_inc_cursor();
Erik Andersenf0657d32000-04-12 17:49:52 +0000627}
628
Denis Vlasenko38f63192007-01-22 09:03:07 +0000629#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000630
Mike Shalf3763032010-11-22 03:49:18 +0100631//FIXME:
632//needs to be more clever: currently it thinks that "foo\ b<TAB>
633//matches the file named "foo bar", which is untrue.
634//Also, perhaps "foo b<TAB> needs to complete to "foo bar" <cursor>,
635//not "foo bar <cursor>...
636
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000637static void free_tab_completion_data(void)
638{
639 if (matches) {
640 while (num_matches)
641 free(matches[--num_matches]);
642 free(matches);
643 matches = NULL;
644 }
645}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000646
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000647static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000648{
Denys Vlasenkoc3797d42017-11-07 18:09:29 +0100649 unsigned char *p = (unsigned char*)matched;
650 while (*p) {
651 /* ESC attack fix: drop any string with control chars */
652 if (*p < ' '
653 || (!ENABLE_UNICODE_SUPPORT && *p >= 0x7f)
654 || (ENABLE_UNICODE_SUPPORT && *p == 0x7f)
655 ) {
656 free(matched);
657 return;
658 }
659 p++;
660 }
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000661 matches = xrealloc_vector(matches, 4, num_matches);
662 matches[num_matches] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000663 num_matches++;
664}
665
Mike Shalf3763032010-11-22 03:49:18 +0100666# if ENABLE_FEATURE_USERNAME_COMPLETION
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200667/* Replace "~user/..." with "/homedir/...".
668 * The parameter is malloced, free it or return it
669 * unchanged if no user is matched.
670 */
671static char *username_path_completion(char *ud)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000672{
673 struct passwd *entry;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200674 char *tilde_name = ud;
675 char *home = NULL;
676
677 ud++; /* skip ~ */
678 if (*ud == '/') { /* "~/..." */
679 home = home_pwd_buf;
680 } else {
681 /* "~user/..." */
682 ud = strchr(ud, '/');
683 *ud = '\0'; /* "~user" */
684 entry = getpwnam(tilde_name + 1);
685 *ud = '/'; /* restore "~user/..." */
686 if (entry)
687 home = entry->pw_dir;
688 }
689 if (home) {
690 ud = concat_path_file(home, ud);
691 free(tilde_name);
692 tilde_name = ud;
693 }
694 return tilde_name;
695}
696
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200697/* ~use<tab> - find all users with this prefix.
698 * Return the length of the prefix used for matching.
699 */
700static NOINLINE unsigned complete_username(const char *ud)
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200701{
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100702 struct passwd *pw;
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200703 unsigned userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000704
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200705 ud++; /* skip ~ */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000706 userlen = strlen(ud);
707
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200708 setpwent();
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100709 while ((pw = getpwent()) != NULL) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200710 /* Null usernames should result in all users as possible completions. */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100711 if (/* !ud[0] || */ is_prefixed_with(pw->pw_name, ud)) {
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100712 add_match(xasprintf("~%s/", pw->pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000713 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000714 }
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100715 endpwent(); /* don't keep password file open */
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200716
717 return 1 + userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000718}
Mike Shalf3763032010-11-22 03:49:18 +0100719# endif /* FEATURE_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000720
721enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000722 FIND_EXE_ONLY = 0,
723 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000724 FIND_FILE_ONLY = 2,
725};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000726
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200727static int path_parse(char ***p)
Mark Whitley4e338752001-01-26 20:42:23 +0000728{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000729 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000730 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000731 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000732 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000733
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000734 if (state->flags & WITH_PATH_LOOKUP)
735 pth = state->path_lookup;
736 else
737 pth = getenv("PATH");
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200738
739 /* PATH="" or PATH=":"? */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000740 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
741 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000742
Denis Vlasenko253ce002007-01-22 08:34:44 +0000743 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000744 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000745 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000746 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000747 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000748 break;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200749 tmp++;
750 if (*tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000751 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000752 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000753 }
754
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200755 *p = res = xmalloc(npth * sizeof(res[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000756 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000757 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000758 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000759 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000760 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000761 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000762 *tmp++ = '\0'; /* ':' -> '\0' */
763 if (*tmp == '\0')
764 break; /* :<empty> */
765 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000766 }
Mark Whitley4e338752001-01-26 20:42:23 +0000767 return npth;
768}
769
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200770/* Complete command, directory or file name.
771 * Return the length of the prefix used for matching.
772 */
773static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000774{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000775 char *path1[1];
776 char **paths = path1;
777 int npaths;
778 int i;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200779 unsigned pf_len;
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200780 const char *pfind;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200781 char *dirbuf = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +0000782
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000783 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000784 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000785
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200786 pfind = strrchr(command, '/');
787 if (!pfind) {
788 if (type == FIND_EXE_ONLY)
789 npaths = path_parse(&paths);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000790 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000791 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000792 /* point to 'l' in "..../last_component" */
793 pfind++;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200794 /* dirbuf = ".../.../.../" */
795 dirbuf = xstrndup(command, pfind - command);
Mike Shalf3763032010-11-22 03:49:18 +0100796# if ENABLE_FEATURE_USERNAME_COMPLETION
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200797 if (dirbuf[0] == '~') /* ~/... or ~user/... */
798 dirbuf = username_path_completion(dirbuf);
Mike Shalf3763032010-11-22 03:49:18 +0100799# endif
Denys Vlasenko7063e862010-09-02 12:44:39 +0200800 path1[0] = dirbuf;
Mark Whitley4e338752001-01-26 20:42:23 +0000801 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200802 pf_len = strlen(pfind);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000803
Denys Vlasenkof128bdb2017-07-29 00:59:24 +0200804# if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1
Denys Vlasenko13360522016-10-24 01:25:05 +0200805 if (type == FIND_EXE_ONLY && !dirbuf) {
Ron Yorstonf23264b2015-05-29 11:31:40 +0100806 const char *p = applet_names;
807
Ron Yorston2b919582016-04-08 11:57:20 +0100808 while (*p) {
Ron Yorstonf23264b2015-05-29 11:31:40 +0100809 if (strncmp(pfind, p, pf_len) == 0)
810 add_match(xstrdup(p));
Ron Yorston2b919582016-04-08 11:57:20 +0100811 while (*p++ != '\0')
812 continue;
Ron Yorstonf23264b2015-05-29 11:31:40 +0100813 }
814 }
Denys Vlasenkof128bdb2017-07-29 00:59:24 +0200815# endif
Ron Yorstonf23264b2015-05-29 11:31:40 +0100816
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000817 for (i = 0; i < npaths; i++) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200818 DIR *dir;
819 struct dirent *next;
820 struct stat st;
821 char *found;
822
Mark Whitley4e338752001-01-26 20:42:23 +0000823 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000824 if (!dir)
825 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000826
827 while ((next = readdir(dir)) != NULL) {
Denys Vlasenko39263632010-09-03 14:11:08 +0200828 unsigned len;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200829 const char *name_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000830
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200831 /* .../<tab>: bash 3.2.0 shows dotfiles, but not . and .. */
832 if (!pfind[0] && DOT_OR_DOTDOT(name_found))
Mark Whitley4e338752001-01-26 20:42:23 +0000833 continue;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200834 /* match? */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100835 if (!is_prefixed_with(name_found, pfind))
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200836 continue; /* no */
837
838 found = concat_path_file(paths[i], name_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000839 /* NB: stat() first so that we see is it a directory;
840 * but if that fails, use lstat() so that
841 * we still match dangling links */
842 if (stat(found, &st) && lstat(found, &st))
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200843 goto cont; /* hmm, remove in progress? */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000844
Denys Vlasenko39263632010-09-03 14:11:08 +0200845 /* Save only name */
846 len = strlen(name_found);
847 found = xrealloc(found, len + 2); /* +2: for slash and NUL */
848 strcpy(found, name_found);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000849
Mark Whitley4e338752001-01-26 20:42:23 +0000850 if (S_ISDIR(st.st_mode)) {
Denys Vlasenko39263632010-09-03 14:11:08 +0200851 /* name is a directory, add slash */
852 found[len] = '/';
853 found[len + 1] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +0000854 } else {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200855 /* skip files if looking for dirs only (example: cd) */
Eric Andersenc470f442003-07-28 09:56:35 +0000856 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000857 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000858 }
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200859 /* add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000860 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000861 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000862 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000863 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000864 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000865 closedir(dir);
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200866 } /* for every path */
867
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000868 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000869 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000870 free(paths);
871 }
Denys Vlasenko39263632010-09-03 14:11:08 +0200872 free(dirbuf);
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200873
874 return pf_len;
Erik Andersen6273f652000-03-17 01:12:41 +0000875}
Erik Andersenf0657d32000-04-12 17:49:52 +0000876
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200877/* build_match_prefix:
Denys Vlasenko76939e72010-09-03 14:09:24 +0200878 * On entry, match_buf contains everything up to cursor at the moment <tab>
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200879 * was pressed. This function looks at it, figures out what part of it
880 * constitutes the command/file/directory prefix to use for completion,
Denys Vlasenko76939e72010-09-03 14:09:24 +0200881 * and rewrites match_buf to contain only that part.
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200882 */
Denys Vlasenko76939e72010-09-03 14:09:24 +0200883#define dbg_bmp 0
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200884/* Helpers: */
885/* QUOT is used on elements of int_buf[], which are bytes,
886 * not Unicode chars. Therefore it works correctly even in Unicode mode.
887 */
888#define QUOT (UCHAR_MAX+1)
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200889static void remove_chunk(int16_t *int_buf, int beg, int end)
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200890{
891 /* beg must be <= end */
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200892 if (beg == end)
893 return;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200894
895 while ((int_buf[beg] = int_buf[end]) != 0)
896 beg++, end++;
897
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200898 if (dbg_bmp) {
899 int i;
900 for (i = 0; int_buf[i]; i++)
901 bb_putchar((unsigned char)int_buf[i]);
902 bb_putchar('\n');
903 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200904}
Denys Vlasenko76939e72010-09-03 14:09:24 +0200905/* Caller ensures that match_buf points to a malloced buffer
906 * big enough to hold strlen(match_buf)*2 + 2
907 */
908static NOINLINE int build_match_prefix(char *match_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000909{
910 int i, j;
911 int command_mode;
Denys Vlasenko76939e72010-09-03 14:09:24 +0200912 int16_t *int_buf = (int16_t*)match_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000913
Denys Vlasenko76939e72010-09-03 14:09:24 +0200914 if (dbg_bmp) printf("\n%s\n", match_buf);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200915
Denys Vlasenko76939e72010-09-03 14:09:24 +0200916 /* Copy in reverse order, since they overlap */
917 i = strlen(match_buf);
918 do {
919 int_buf[i] = (unsigned char)match_buf[i];
920 i--;
921 } while (i >= 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000922
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200923 /* Mark every \c as "quoted c" */
Denys Vlasenko76939e72010-09-03 14:09:24 +0200924 for (i = 0; int_buf[i]; i++) {
925 if (int_buf[i] == '\\') {
926 remove_chunk(int_buf, i, i + 1);
927 int_buf[i] |= QUOT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000928 }
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200929 }
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200930 /* Quote-mark "chars" and 'chars', drop delimiters */
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200931 {
932 int in_quote = 0;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200933 i = 0;
934 while (int_buf[i]) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200935 int cur = int_buf[i];
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200936 if (!cur)
937 break;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200938 if (cur == '\'' || cur == '"') {
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200939 if (!in_quote || (cur == in_quote)) {
940 in_quote ^= cur;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200941 remove_chunk(int_buf, i, i + 1);
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200942 continue;
943 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000944 }
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200945 if (in_quote)
946 int_buf[i] = cur | QUOT;
947 i++;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200948 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000949 }
950
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200951 /* Remove everything up to command delimiters:
952 * ';' ';;' '&' '|' '&&' '||',
953 * but careful with '>&' '<&' '>|'
954 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000955 for (i = 0; int_buf[i]; i++) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200956 int cur = int_buf[i];
957 if (cur == ';' || cur == '&' || cur == '|') {
958 int prev = i ? int_buf[i - 1] : 0;
959 if (cur == '&' && (prev == '>' || prev == '<')) {
960 continue;
961 } else if (cur == '|' && prev == '>') {
962 continue;
963 }
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200964 remove_chunk(int_buf, 0, i + 1 + (cur == int_buf[i + 1]));
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200965 i = -1; /* back to square 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000966 }
967 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200968 /* Remove all `cmd` */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200969 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000970 if (int_buf[i] == '`') {
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200971 for (j = i + 1; int_buf[j]; j++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000972 if (int_buf[j] == '`') {
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200973 /* `cmd` should count as a word:
974 * `cmd` c<tab> should search for files c*,
975 * not commands c*. Therefore we don't drop
976 * `cmd` entirely, we replace it with single `.
977 */
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200978 remove_chunk(int_buf, i, j);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200979 goto next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000980 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200981 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200982 /* No closing ` - command mode, remove all up to ` */
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200983 remove_chunk(int_buf, 0, i + 1);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200984 break;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200985 next: ;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000986 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200987 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000988
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200989 /* Remove "cmd (" and "cmd {"
990 * Example: "if { c<tab>"
991 * In this example, c should be matched as command pfx.
992 */
993 for (i = 0; int_buf[i]; i++) {
994 if (int_buf[i] == '(' || int_buf[i] == '{') {
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200995 remove_chunk(int_buf, 0, i + 1);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +0200996 i = -1; /* back to square 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000997 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200998 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000999
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001000 /* Remove leading unquoted spaces */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001001 for (i = 0; int_buf[i]; i++)
1002 if (int_buf[i] != ' ')
1003 break;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001004 remove_chunk(int_buf, 0, i);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001005
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001006 /* Determine completion mode */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001007 command_mode = FIND_EXE_ONLY;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001008 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001009 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001010 if (int_buf[i] == ' '
1011 && command_mode == FIND_EXE_ONLY
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001012 && (char)int_buf[0] == 'c'
1013 && (char)int_buf[1] == 'd'
1014 && i == 2 /* -> int_buf[2] == ' ' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001015 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001016 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001017 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001018 command_mode = FIND_FILE_ONLY;
1019 break;
1020 }
1021 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001022 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001023 if (dbg_bmp) printf("command_mode(0:exe/1:dir/2:file):%d\n", command_mode);
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001024
1025 /* Remove everything except last word */
Denys Vlasenkob068bd72010-09-02 12:01:11 +02001026 for (i = 0; int_buf[i]; i++) /* quasi-strlen(int_buf) */
1027 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001028 for (--i; i >= 0; i--) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001029 int cur = int_buf[i];
1030 if (cur == ' ' || cur == '<' || cur == '>' || cur == '|' || cur == '&') {
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001031 remove_chunk(int_buf, 0, i + 1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001032 break;
1033 }
1034 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001035
Denys Vlasenko76939e72010-09-03 14:09:24 +02001036 /* Convert back to string of _chars_ */
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001037 i = 0;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001038 while ((match_buf[i] = int_buf[i]) != '\0')
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001039 i++;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001040
Denys Vlasenko76939e72010-09-03 14:09:24 +02001041 if (dbg_bmp) printf("final match_buf:'%s'\n", match_buf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001042
1043 return command_mode;
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02001044}
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001045
Glenn L McGrath4d001292003-01-06 01:11:50 +00001046/*
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001047 * Display by column (original idea from ls applet,
1048 * very optimized by me [Vladimir] :)
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001049 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001050static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +00001051{
1052 int ncols, row;
1053 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001054 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001055 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001056 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001057
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001058 /* find the longest file name - use that as the column width */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001059 for (row = 0; row < nrows; row++) {
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001060 l = unicode_strwidth(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001061 if (column_width < l)
1062 column_width = l;
1063 }
1064 column_width += 2; /* min space for columns */
1065 ncols = cmdedit_termw / column_width;
1066
1067 if (ncols > 1) {
1068 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001069 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +00001070 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001071 } else {
1072 ncols = 1;
1073 }
1074 for (row = 0; row < nrows; row++) {
1075 int n = row;
1076 int nc;
1077
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001078 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001079 printf("%s%-*s", matches[n],
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001080 (int)(column_width - unicode_strwidth(matches[n])), ""
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001081 );
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001082 }
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001083 if (ENABLE_UNICODE_SUPPORT)
1084 puts(printable_string(NULL, matches[n]));
1085 else
1086 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001087 }
1088}
1089
Mike Shalf3763032010-11-22 03:49:18 +01001090static const char *is_special_char(char c)
1091{
1092 return strchr(" `\"#$%^&*()=+{}[]:;'|\\<>", c);
1093}
1094
1095static char *quote_special_chars(char *found)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001096{
1097 int l = 0;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001098 char *s = xzalloc((strlen(found) + 1) * 2);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001099
1100 while (*found) {
Mike Shalf3763032010-11-22 03:49:18 +01001101 if (is_special_char(*found))
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001102 s[l++] = '\\';
1103 s[l++] = *found++;
1104 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001105 /* s[l] = '\0'; - already is */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001106 return s;
1107}
1108
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001109/* Do TAB completion */
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001110static NOINLINE void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001111{
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001112 char *chosen_match;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001113 char *match_buf;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001114 size_t len_found;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001115 /* Length of string used for matching */
1116 unsigned match_pfx_len = match_pfx_len;
1117 int find_type;
Mike Shalf3763032010-11-22 03:49:18 +01001118# if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001119 /* cursor pos in command converted to multibyte form */
1120 int cursor_mb;
Mike Shalf3763032010-11-22 03:49:18 +01001121# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001122 if (!(state->flags & TAB_COMPLETION))
1123 return;
1124
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001125 if (*lastWasTab) {
1126 /* The last char was a TAB too.
1127 * Print a list of all the available choices.
1128 */
Denys Vlasenkoa46e16e2010-09-03 13:05:51 +02001129 if (num_matches > 0) {
1130 /* cursor will be changed by goto_new_line() */
Denys Vlasenko044b1802009-07-12 02:50:35 +02001131 int sav_cursor = cursor;
Mark Whitley4e338752001-01-26 20:42:23 +00001132 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001133 showfiles();
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001134 draw_full(command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001135 }
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001136 return;
Erik Andersenf0657d32000-04-12 17:49:52 +00001137 }
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001138
1139 *lastWasTab = 1;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001140 chosen_match = NULL;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001141
Denys Vlasenko76939e72010-09-03 14:09:24 +02001142 /* Make a local copy of the string up to the position of the cursor.
1143 * build_match_prefix will expand it into int16_t's, need to allocate
1144 * twice as much as the string_len+1.
1145 * (we then also (ab)use this extra space later - see (**))
1146 */
1147 match_buf = xmalloc(MAX_LINELEN * sizeof(int16_t));
Mike Shalf3763032010-11-22 03:49:18 +01001148# if !ENABLE_UNICODE_SUPPORT
Denys Vlasenko76939e72010-09-03 14:09:24 +02001149 save_string(match_buf, cursor + 1); /* +1 for NUL */
Mike Shalf3763032010-11-22 03:49:18 +01001150# else
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001151 {
1152 CHAR_T wc = command_ps[cursor];
1153 command_ps[cursor] = BB_NUL;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001154 save_string(match_buf, MAX_LINELEN);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001155 command_ps[cursor] = wc;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001156 cursor_mb = strlen(match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001157 }
Mike Shalf3763032010-11-22 03:49:18 +01001158# endif
Denys Vlasenko76939e72010-09-03 14:09:24 +02001159 find_type = build_match_prefix(match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001160
1161 /* Free up any memory already allocated */
1162 free_tab_completion_data();
1163
Mike Shalf3763032010-11-22 03:49:18 +01001164# if ENABLE_FEATURE_USERNAME_COMPLETION
1165 /* If the word starts with ~ and there is no slash in the word,
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001166 * then try completing this word as a username. */
1167 if (state->flags & USERNAME_COMPLETION)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001168 if (match_buf[0] == '~' && strchr(match_buf, '/') == NULL)
1169 match_pfx_len = complete_username(match_buf);
Mike Shalf3763032010-11-22 03:49:18 +01001170# endif
1171 /* If complete_username() did not match,
1172 * try to match a command in $PATH, or a directory, or a file */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001173 if (!matches)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001174 match_pfx_len = complete_cmd_dir_file(match_buf, find_type);
Mike Shalf3763032010-11-22 03:49:18 +01001175
1176 /* Account for backslashes which will be inserted
1177 * by quote_special_chars() later */
1178 {
1179 const char *e = match_buf + strlen(match_buf);
1180 const char *s = e - match_pfx_len;
1181 while (s < e)
1182 if (is_special_char(*s++))
1183 match_pfx_len++;
1184 }
1185
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001186 /* Remove duplicates */
1187 if (matches) {
Mike Shalf3763032010-11-22 03:49:18 +01001188 unsigned i, n = 0;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001189 qsort_string_vector(matches, num_matches);
1190 for (i = 0; i < num_matches - 1; ++i) {
1191 //if (matches[i] && matches[i+1]) { /* paranoia */
1192 if (strcmp(matches[i], matches[i+1]) == 0) {
1193 free(matches[i]);
1194 //matches[i] = NULL; /* paranoia */
1195 } else {
1196 matches[n++] = matches[i];
1197 }
1198 //}
1199 }
1200 matches[n++] = matches[i];
1201 num_matches = n;
1202 }
Mike Shalf3763032010-11-22 03:49:18 +01001203
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001204 /* Did we find exactly one match? */
1205 if (num_matches != 1) { /* no */
1206 char *cp;
1207 beep();
1208 if (!matches)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001209 goto ret; /* no matches at all */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001210 /* Find common prefix */
1211 chosen_match = xstrdup(matches[0]);
1212 for (cp = chosen_match; *cp; cp++) {
1213 unsigned n;
1214 for (n = 1; n < num_matches; n++) {
1215 if (matches[n][cp - chosen_match] != *cp) {
1216 goto stop;
1217 }
1218 }
1219 }
1220 stop:
1221 if (cp == chosen_match) { /* have unique prefix? */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001222 goto ret; /* no */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001223 }
1224 *cp = '\0';
Mike Shalf3763032010-11-22 03:49:18 +01001225 cp = quote_special_chars(chosen_match);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001226 free(chosen_match);
1227 chosen_match = cp;
1228 len_found = strlen(chosen_match);
1229 } else { /* exactly one match */
1230 /* Next <tab> is not a double-tab */
1231 *lastWasTab = 0;
1232
Mike Shalf3763032010-11-22 03:49:18 +01001233 chosen_match = quote_special_chars(matches[0]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001234 len_found = strlen(chosen_match);
1235 if (chosen_match[len_found-1] != '/') {
1236 chosen_match[len_found] = ' ';
1237 chosen_match[++len_found] = '\0';
1238 }
1239 }
1240
Mike Shalf3763032010-11-22 03:49:18 +01001241# if !ENABLE_UNICODE_SUPPORT
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001242 /* Have space to place the match? */
1243 /* The result consists of three parts with these lengths: */
1244 /* cursor + (len_found - match_pfx_len) + (command_len - cursor) */
1245 /* it simplifies into: */
1246 if ((int)(len_found - match_pfx_len + command_len) < S.maxsize) {
1247 int pos;
1248 /* save tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001249 strcpy(match_buf, &command_ps[cursor]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001250 /* add match and tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001251 sprintf(&command_ps[cursor], "%s%s", chosen_match + match_pfx_len, match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001252 command_len = strlen(command_ps);
1253 /* new pos */
1254 pos = cursor + len_found - match_pfx_len;
1255 /* write out the matched command */
1256 redraw(cmdedit_y, command_len - pos);
1257 }
Mike Shalf3763032010-11-22 03:49:18 +01001258# else
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001259 {
Denys Vlasenko76939e72010-09-03 14:09:24 +02001260 /* Use 2nd half of match_buf as scratch space - see (**) */
1261 char *command = match_buf + MAX_LINELEN;
1262 int len = save_string(command, MAX_LINELEN);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001263 /* Have space to place the match? */
1264 /* cursor_mb + (len_found - match_pfx_len) + (len - cursor_mb) */
1265 if ((int)(len_found - match_pfx_len + len) < MAX_LINELEN) {
1266 int pos;
1267 /* save tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001268 strcpy(match_buf, &command[cursor_mb]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001269 /* where do we want to have cursor after all? */
1270 strcpy(&command[cursor_mb], chosen_match + match_pfx_len);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02001271 len = load_string(command);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001272 /* add match and tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001273 sprintf(&command[cursor_mb], "%s%s", chosen_match + match_pfx_len, match_buf);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02001274 command_len = load_string(command);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001275 /* write out the matched command */
1276 /* paranoia: load_string can return 0 on conv error,
1277 * prevent passing pos = (0 - 12) to redraw */
1278 pos = command_len - len;
1279 redraw(cmdedit_y, pos >= 0 ? pos : 0);
1280 }
1281 }
Mike Shalf3763032010-11-22 03:49:18 +01001282# endif
Denys Vlasenko76939e72010-09-03 14:09:24 +02001283 ret:
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001284 free(chosen_match);
Denys Vlasenko76939e72010-09-03 14:09:24 +02001285 free(match_buf);
Erik Andersenf0657d32000-04-12 17:49:52 +00001286}
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001287
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001288#endif /* FEATURE_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001289
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001290
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001291line_input_t* FAST_FUNC new_line_input_t(int flags)
1292{
1293 line_input_t *n = xzalloc(sizeof(*n));
1294 n->flags = flags;
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02001295 n->timeout = -1;
Denys Vlasenko79df4812014-01-10 14:38:26 +01001296#if MAX_HISTORY > 0
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001297 n->max_history = MAX_HISTORY;
Denys Vlasenko79df4812014-01-10 14:38:26 +01001298#endif
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001299 return n;
1300}
1301
1302
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001303#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001304
Flemming Madsend96ffda2013-04-07 18:47:24 +02001305unsigned FAST_FUNC size_from_HISTFILESIZE(const char *hp)
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001306{
1307 int size = MAX_HISTORY;
1308 if (hp) {
1309 size = atoi(hp);
1310 if (size <= 0)
1311 return 1;
1312 if (size > MAX_HISTORY)
1313 return MAX_HISTORY;
1314 }
1315 return size;
1316}
1317
Denis Vlasenko682ad302008-09-27 01:28:56 +00001318static void save_command_ps_at_cur_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001319{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001320 if (command_ps[0] != BB_NUL) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001321 int cur = state->cur_history;
1322 free(state->history[cur]);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001323
Denys Vlasenko19158a82010-03-26 14:06:56 +01001324# if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001325 {
1326 char tbuf[MAX_LINELEN];
1327 save_string(tbuf, sizeof(tbuf));
1328 state->history[cur] = xstrdup(tbuf);
1329 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001330# else
Denis Vlasenko682ad302008-09-27 01:28:56 +00001331 state->history[cur] = xstrdup(command_ps);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001332# endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001333 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001334}
1335
1336/* state->flags is already checked to be nonzero */
1337static int get_previous_history(void)
1338{
1339 if ((state->flags & DO_HISTORY) && state->cur_history) {
1340 save_command_ps_at_cur_history();
1341 state->cur_history--;
1342 return 1;
1343 }
1344 beep();
1345 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001346}
1347
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001348static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001349{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001350 if (state->flags & DO_HISTORY) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001351 if (state->cur_history < state->cnt_history) {
1352 save_command_ps_at_cur_history(); /* save the current history line */
1353 return ++state->cur_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001354 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001355 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001356 beep();
1357 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001358}
Robert Griebl350d26b2002-12-03 22:45:46 +00001359
Flemming Madsend96ffda2013-04-07 18:47:24 +02001360/* Lists command history. Used by shell 'history' builtins */
1361void FAST_FUNC show_history(const line_input_t *st)
1362{
1363 int i;
1364
1365 if (!st)
1366 return;
1367 for (i = 0; i < st->cnt_history; i++)
1368 printf("%4d %s\n", i, st->history[i]);
1369}
1370
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001371# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001372/* We try to ensure that concurrent additions to the history
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001373 * do not overwrite each other.
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001374 * Otherwise shell users get unhappy.
1375 *
1376 * History file is trimmed lazily, when it grows several times longer
1377 * than configured MAX_HISTORY lines.
1378 */
1379
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001380static void free_line_input_t(line_input_t *n)
1381{
1382 int i = n->cnt_history;
1383 while (i > 0)
1384 free(n->history[--i]);
1385 free(n);
1386}
1387
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001388/* state->flags is already checked to be nonzero */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001389static void load_history(line_input_t *st_parm)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001390{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001391 char *temp_h[MAX_HISTORY];
1392 char *line;
Robert Griebl350d26b2002-12-03 22:45:46 +00001393 FILE *fp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001394 unsigned idx, i, line_len;
Robert Griebl350d26b2002-12-03 22:45:46 +00001395
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001396 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +00001397
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001398 fp = fopen_for_read(st_parm->hist_file);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001399 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001400 /* clean up old history */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001401 for (idx = st_parm->cnt_history; idx > 0;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001402 idx--;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001403 free(st_parm->history[idx]);
1404 st_parm->history[idx] = NULL;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001405 }
1406
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001407 /* fill temp_h[], retaining only last MAX_HISTORY lines */
1408 memset(temp_h, 0, sizeof(temp_h));
Denys Vlasenkobede2152011-09-04 16:12:33 +02001409 idx = 0;
Dennis Groenendeee3562012-04-24 22:40:58 +02001410 st_parm->cnt_history_in_file = 0;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001411 while ((line = xmalloc_fgetline(fp)) != NULL) {
1412 if (line[0] == '\0') {
1413 free(line);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001414 continue;
1415 }
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001416 free(temp_h[idx]);
1417 temp_h[idx] = line;
Dennis Groenendeee3562012-04-24 22:40:58 +02001418 st_parm->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001419 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001420 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001421 idx = 0;
Robert Griebl350d26b2002-12-03 22:45:46 +00001422 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001423 fclose(fp);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001424
1425 /* find first non-NULL temp_h[], if any */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001426 if (st_parm->cnt_history_in_file) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001427 while (temp_h[idx] == NULL) {
1428 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001429 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001430 idx = 0;
1431 }
1432 }
1433
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001434 /* copy temp_h[] to st_parm->history[] */
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001435 for (i = 0; i < st_parm->max_history;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001436 line = temp_h[idx];
1437 if (!line)
1438 break;
1439 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001440 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001441 idx = 0;
1442 line_len = strlen(line);
1443 if (line_len >= MAX_LINELEN)
1444 line[MAX_LINELEN-1] = '\0';
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001445 st_parm->history[i++] = line;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001446 }
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001447 st_parm->cnt_history = i;
Denys Vlasenkobede2152011-09-04 16:12:33 +02001448 if (ENABLE_FEATURE_EDITING_SAVE_ON_EXIT)
1449 st_parm->cnt_history_in_file = i;
Robert Griebl350d26b2002-12-03 22:45:46 +00001450 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001451}
1452
Denys Vlasenkobede2152011-09-04 16:12:33 +02001453# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1454void save_history(line_input_t *st)
1455{
1456 FILE *fp;
1457
Denys Vlasenkobede2152011-09-04 16:12:33 +02001458 if (!st->hist_file)
1459 return;
1460 if (st->cnt_history <= st->cnt_history_in_file)
1461 return;
1462
1463 fp = fopen(st->hist_file, "a");
1464 if (fp) {
1465 int i, fd;
1466 char *new_name;
1467 line_input_t *st_temp;
1468
1469 for (i = st->cnt_history_in_file; i < st->cnt_history; i++)
1470 fprintf(fp, "%s\n", st->history[i]);
1471 fclose(fp);
1472
1473 /* we may have concurrently written entries from others.
1474 * load them */
1475 st_temp = new_line_input_t(st->flags);
1476 st_temp->hist_file = st->hist_file;
1477 st_temp->max_history = st->max_history;
1478 load_history(st_temp);
1479
1480 /* write out temp file and replace hist_file atomically */
1481 new_name = xasprintf("%s.%u.new", st->hist_file, (int) getpid());
1482 fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1483 if (fd >= 0) {
1484 fp = xfdopen_for_write(fd);
1485 for (i = 0; i < st_temp->cnt_history; i++)
1486 fprintf(fp, "%s\n", st_temp->history[i]);
1487 fclose(fp);
1488 if (rename(new_name, st->hist_file) == 0)
1489 st->cnt_history_in_file = st_temp->cnt_history;
1490 }
1491 free(new_name);
1492 free_line_input_t(st_temp);
1493 }
1494}
1495# else
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001496static void save_history(char *str)
Robert Griebl350d26b2002-12-03 22:45:46 +00001497{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001498 int fd;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001499 int len, len2;
Eric Andersenc470f442003-07-28 09:56:35 +00001500
Denys Vlasenkobede2152011-09-04 16:12:33 +02001501 if (!state->hist_file)
1502 return;
1503
Wolfram Sang2e9aeae2010-11-15 02:58:28 +01001504 fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0600);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001505 if (fd < 0)
1506 return;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001507 xlseek(fd, 0, SEEK_END); /* paranoia */
1508 len = strlen(str);
1509 str[len] = '\n'; /* we (try to) do atomic write */
1510 len2 = full_write(fd, str, len + 1);
1511 str[len] = '\0';
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001512 close(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001513 if (len2 != len + 1)
1514 return; /* "wtf?" */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001515
1516 /* did we write so much that history file needs trimming? */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001517 state->cnt_history_in_file++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001518 if (state->cnt_history_in_file > state->max_history * 4) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001519 char *new_name;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001520 line_input_t *st_temp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001521
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001522 /* we may have concurrently written entries from others.
1523 * load them */
1524 st_temp = new_line_input_t(state->flags);
1525 st_temp->hist_file = state->hist_file;
Denys Vlasenkoe3d8d072011-03-31 14:39:38 +02001526 st_temp->max_history = state->max_history;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001527 load_history(st_temp);
1528
1529 /* write out temp file and replace hist_file atomically */
1530 new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
Denys Vlasenko4840ae82011-09-04 15:28:03 +02001531 fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Wolfram Sang2e9aeae2010-11-15 02:58:28 +01001532 if (fd >= 0) {
1533 FILE *fp;
1534 int i;
1535
1536 fp = xfdopen_for_write(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001537 for (i = 0; i < st_temp->cnt_history; i++)
1538 fprintf(fp, "%s\n", st_temp->history[i]);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001539 fclose(fp);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001540 if (rename(new_name, state->hist_file) == 0)
1541 state->cnt_history_in_file = st_temp->cnt_history;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001542 }
1543 free(new_name);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001544 free_line_input_t(st_temp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001545 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001546}
Denys Vlasenkobede2152011-09-04 16:12:33 +02001547# endif
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001548# else
1549# define load_history(a) ((void)0)
1550# define save_history(a) ((void)0)
1551# endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001552
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001553static void remember_in_history(char *str)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001554{
1555 int i;
1556
1557 if (!(state->flags & DO_HISTORY))
1558 return;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001559 if (str[0] == '\0')
1560 return;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001561 i = state->cnt_history;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001562 /* Don't save dupes */
1563 if (i && strcmp(state->history[i-1], str) == 0)
1564 return;
1565
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001566 free(state->history[state->max_history]); /* redundant, paranoia */
1567 state->history[state->max_history] = NULL; /* redundant, paranoia */
Denis Vlasenko682ad302008-09-27 01:28:56 +00001568
1569 /* If history[] is full, remove the oldest command */
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001570 /* we need to keep history[state->max_history] empty, hence >=, not > */
1571 if (i >= state->max_history) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001572 free(state->history[0]);
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001573 for (i = 0; i < state->max_history-1; i++)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001574 state->history[i] = state->history[i+1];
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001575 /* i == state->max_history-1 */
Denys Vlasenkoc0cae522011-11-04 01:09:09 +01001576# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1577 if (state->cnt_history_in_file)
Denys Vlasenkobede2152011-09-04 16:12:33 +02001578 state->cnt_history_in_file--;
Denys Vlasenkoc0cae522011-11-04 01:09:09 +01001579# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001580 }
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001581 /* i <= state->max_history-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001582 state->history[i++] = xstrdup(str);
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001583 /* i <= state->max_history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001584 state->cur_history = i;
1585 state->cnt_history = i;
Denys Vlasenkobede2152011-09-04 16:12:33 +02001586# if ENABLE_FEATURE_EDITING_SAVEHISTORY && !ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1587 save_history(str);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001588# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001589}
1590
1591#else /* MAX_HISTORY == 0 */
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001592# define remember_in_history(a) ((void)0)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001593#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001594
1595
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001596#if ENABLE_FEATURE_EDITING_VI
Erik Andersen6273f652000-03-17 01:12:41 +00001597/*
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001598 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001599 */
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001600static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001601vi_Word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001602{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001603 CHAR_T *command = command_ps;
1604
1605 while (cursor < command_len && !BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001606 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001607 if (eat) while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001608 input_forward();
1609}
1610
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001611static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001612vi_word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001613{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001614 CHAR_T *command = command_ps;
1615
Natanael Copa7e6f9312016-08-14 23:30:29 +02001616 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001617 while (cursor < command_len
Natanael Copa7e6f9312016-08-14 23:30:29 +02001618 && (BB_isalnum_or_underscore(command[cursor+1]))
Denys Vlasenko13028922009-07-12 00:51:15 +02001619 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001620 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001621 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001622 } else if (BB_ispunct(command[cursor])) {
1623 while (cursor < command_len && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001624 input_forward();
1625 }
1626
Denis Vlasenko253ce002007-01-22 08:34:44 +00001627 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001628 input_forward();
1629
Denys Vlasenko13028922009-07-12 00:51:15 +02001630 if (eat) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001631 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001632 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001633 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001634}
1635
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001636static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001637vi_End_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001638{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001639 CHAR_T *command = command_ps;
1640
Paul Fox3f11b1b2005-08-04 19:04:46 +00001641 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001642 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001643 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001644 while (cursor < command_len-1 && !BB_isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001645 input_forward();
1646}
1647
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001648static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001649vi_end_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001650{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001651 CHAR_T *command = command_ps;
1652
Denis Vlasenko253ce002007-01-22 08:34:44 +00001653 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001654 return;
1655 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001656 while (cursor < command_len-1 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001657 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001658 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001659 return;
Natanael Copa7e6f9312016-08-14 23:30:29 +02001660 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001661 while (cursor < command_len-1
Natanael Copa7e6f9312016-08-14 23:30:29 +02001662 && (BB_isalnum_or_underscore(command[cursor+1]))
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001663 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001664 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001665 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001666 } else if (BB_ispunct(command[cursor])) {
1667 while (cursor < command_len-1 && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001668 input_forward();
1669 }
1670}
1671
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001672static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001673vi_Back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001674{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001675 CHAR_T *command = command_ps;
1676
1677 while (cursor > 0 && BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001678 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001679 while (cursor > 0 && !BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001680 input_backward(1);
1681}
1682
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001683static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001684vi_back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001685{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001686 CHAR_T *command = command_ps;
1687
Paul Fox3f11b1b2005-08-04 19:04:46 +00001688 if (cursor <= 0)
1689 return;
1690 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001691 while (cursor > 0 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001692 input_backward(1);
1693 if (cursor <= 0)
1694 return;
Natanael Copa7e6f9312016-08-14 23:30:29 +02001695 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001696 while (cursor > 0
Natanael Copa7e6f9312016-08-14 23:30:29 +02001697 && (BB_isalnum_or_underscore(command[cursor-1]))
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001698 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001699 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001700 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001701 } else if (BB_ispunct(command[cursor])) {
1702 while (cursor > 0 && BB_ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001703 input_backward(1);
1704 }
1705}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001706#endif
1707
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001708/* Modelled after bash 4.0 behavior of Ctrl-<arrow> */
1709static void ctrl_left(void)
1710{
1711 CHAR_T *command = command_ps;
1712
1713 while (1) {
1714 CHAR_T c;
1715
1716 input_backward(1);
1717 if (cursor == 0)
1718 break;
1719 c = command[cursor];
1720 if (c != ' ' && !BB_ispunct(c)) {
1721 /* we reached a "word" delimited by spaces/punct.
1722 * go to its beginning */
1723 while (1) {
1724 c = command[cursor - 1];
1725 if (c == ' ' || BB_ispunct(c))
1726 break;
1727 input_backward(1);
1728 if (cursor == 0)
1729 break;
1730 }
1731 break;
1732 }
1733 }
1734}
1735static void ctrl_right(void)
1736{
1737 CHAR_T *command = command_ps;
1738
1739 while (1) {
1740 CHAR_T c;
1741
1742 c = command[cursor];
1743 if (c == BB_NUL)
1744 break;
1745 if (c != ' ' && !BB_ispunct(c)) {
1746 /* we reached a "word" delimited by spaces/punct.
1747 * go to its end + 1 */
1748 while (1) {
1749 input_forward();
1750 c = command[cursor];
1751 if (c == BB_NUL || c == ' ' || BB_ispunct(c))
1752 break;
1753 }
1754 break;
1755 }
1756 input_forward();
1757 }
1758}
1759
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001760
1761/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001762 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001763 */
1764
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001765#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
1766static void ask_terminal(void)
1767{
1768 /* Ask terminal where is the cursor now.
1769 * lineedit_read_key handles response and corrects
1770 * our idea of current cursor position.
1771 * Testcase: run "echo -n long_line_long_line_long_line",
1772 * then type in a long, wrapping command and try to
1773 * delete it using backspace key.
1774 * Note: we print it _after_ prompt, because
1775 * prompt may contain CR. Example: PS1='\[\r\n\]\w '
1776 */
1777 /* Problem: if there is buffered input on stdin,
1778 * the response will be delivered later,
1779 * possibly to an unsuspecting application.
1780 * Testcase: "sleep 1; busybox ash" + press and hold [Enter].
1781 * Result:
1782 * ~/srcdevel/bbox/fix/busybox.t4 #
1783 * ~/srcdevel/bbox/fix/busybox.t4 #
1784 * ^[[59;34~/srcdevel/bbox/fix/busybox.t4 # <-- garbage
1785 * ~/srcdevel/bbox/fix/busybox.t4 #
1786 *
1787 * Checking for input with poll only makes the race narrower,
1788 * I still can trigger it. Strace:
1789 *
1790 * write(1, "~/srcdevel/bbox/fix/busybox.t4 # ", 33) = 33
1791 * poll([{fd=0, events=POLLIN}], 1, 0) = 0 (Timeout) <-- no input exists
1792 * write(1, "\33[6n", 4) = 4 <-- send the ESC sequence, quick!
Alexey Fomenko232ebaa2011-05-20 04:26:29 +02001793 * poll([{fd=0, events=POLLIN}], 1, -1) = 1 ([{fd=0, revents=POLLIN}])
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001794 * read(0, "\n", 1) = 1 <-- oh crap, user's input got in first
1795 */
Denys Vlasenko451add42010-07-25 00:06:41 +02001796 struct pollfd pfd;
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001797
Denys Vlasenko451add42010-07-25 00:06:41 +02001798 pfd.fd = STDIN_FILENO;
1799 pfd.events = POLLIN;
1800 if (safe_poll(&pfd, 1, 0) == 0) {
1801 S.sent_ESC_br6n = 1;
Marek Polacek7b181072010-10-28 21:34:56 +02001802 fputs(ESC"[6n", stdout);
Denys Vlasenko451add42010-07-25 00:06:41 +02001803 fflush_all(); /* make terminal see it ASAP! */
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001804 }
1805}
1806#else
1807#define ask_terminal() ((void)0)
1808#endif
1809
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001810/* Note about multi-line PS1 (e.g. "\n\w \u@\h\n> ") and prompt redrawing:
1811 *
1812 * If the prompt has any newlines, after we print it once we use only its last
1813 * line to redraw in-place, which makes it simpler to calculate how many lines
1814 * we should move the cursor up to align the redraw (cmdedit_y). The earlier
1815 * prompt lines just stay on screen and we redraw below them.
1816 *
1817 * Use cases for all prompt lines beyond the initial draw:
1818 * - After clear-screen (^L) or after displaying tab-completion choices, we
1819 * print the full prompt, as it isn't redrawn in-place.
1820 * - During terminal resize we could try to redraw all lines, but we don't,
1821 * because it requires delicate alignment, it's good enough with only the
1822 * last line, and doing it wrong is arguably worse than not doing it at all.
1823 *
1824 * Terminology wise, if it doesn't mention "full", then it means the last/sole
1825 * prompt line. We use the prompt (last/sole line) while redrawing in-place,
1826 * and the full where we need a fresh one unrelated to an earlier position.
1827 *
1828 * If PS1 is not multiline, the last/sole line and the full are the same string.
1829 */
1830
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001831/* Called just once at read_line_input() init time */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001832#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001833static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001834{
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001835 const char *p;
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001836 cmdedit_prompt = prompt_last_line = prmt_ptr;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001837 p = strrchr(prmt_ptr, '\n');
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001838 if (p)
1839 prompt_last_line = p + 1;
1840 cmdedit_prmt_len = unicode_strwidth(prompt_last_line);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001841 put_prompt();
1842}
1843#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001844static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001845{
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001846 int prmt_size = 0;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001847 char *prmt_mem_ptr = xzalloc(1);
Denys Vlasenko1d145692013-03-29 13:09:05 +01001848# if ENABLE_USERNAME_OR_HOMEDIR
1849 char *cwd_buf = NULL;
1850# endif
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001851 char flg_not_length = '[';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001852 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001853
Denys Vlasenko7a180432013-08-19 16:44:05 +02001854 /*cmdedit_prmt_len = 0; - already is */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001855
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001856 cbuf[1] = '\0'; /* never changes */
1857
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001858 while (*prmt_ptr) {
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001859 char timebuf[sizeof("HH:MM:SS")];
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001860 char *free_me = NULL;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001861 char *pbuf;
1862 char c;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001863
1864 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001865 c = *prmt_ptr++;
1866 if (c == '\\') {
Denys Vlasenko1d145692013-03-29 13:09:05 +01001867 const char *cp;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001868 int l;
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001869/*
1870 * Supported via bb_process_escape_sequence:
1871 * \a ASCII bell character (07)
1872 * \e ASCII escape character (033)
1873 * \n newline
1874 * \r carriage return
1875 * \\ backslash
1876 * \nnn char with octal code nnn
1877 * Supported:
1878 * \$ if the effective UID is 0, a #, otherwise a $
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001879 * \w current working directory, with $HOME abbreviated with a tilde
1880 * Note: we do not support $PROMPT_DIRTRIM=n feature
Denys Vlasenko1d145692013-03-29 13:09:05 +01001881 * \W basename of the current working directory, with $HOME abbreviated with a tilde
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001882 * \h hostname up to the first '.'
1883 * \H hostname
1884 * \u username
1885 * \[ begin a sequence of non-printing characters
1886 * \] end a sequence of non-printing characters
Denys Vlasenko1d145692013-03-29 13:09:05 +01001887 * \T current time in 12-hour HH:MM:SS format
1888 * \@ current time in 12-hour am/pm format
1889 * \A current time in 24-hour HH:MM format
1890 * \t current time in 24-hour HH:MM:SS format
1891 * (all of the above work as \A)
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001892 * Not supported:
Denys Vlasenko1d145692013-03-29 13:09:05 +01001893 * \! history number of this command
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001894 * \# command number of this command
1895 * \j number of jobs currently managed by the shell
1896 * \l basename of the shell's terminal device name
1897 * \s name of the shell, the basename of $0 (the portion following the final slash)
1898 * \V release of bash, version + patch level (e.g., 2.00.0)
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001899 * \d date in "Weekday Month Date" format (e.g., "Tue May 26")
1900 * \D{format}
1901 * format is passed to strftime(3).
1902 * An empty format results in a locale-specific time representation.
1903 * The braces are required.
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001904 * Mishandled by bb_process_escape_sequence:
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001905 * \v version of bash (e.g., 2.00)
1906 */
Denys Vlasenko1d145692013-03-29 13:09:05 +01001907 cp = prmt_ptr;
1908 c = *cp;
1909 if (c != 't') /* don't treat \t as tab */
1910 c = bb_process_escape_sequence(&prmt_ptr);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001911 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001912 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001913 break;
1914 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001915
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001916 switch (c) {
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02001917# if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001918 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001919 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001920 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001921# endif
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001922 case 'H':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001923 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001924 pbuf = free_me = safe_gethostname();
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001925 if (c == 'h')
1926 strchrnul(pbuf, '.')[0] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001927 break;
1928 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001929 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001930 break;
Denys Vlasenko1d145692013-03-29 13:09:05 +01001931 case 'T': /* 12-hour HH:MM:SS format */
1932 case '@': /* 12-hour am/pm format */
1933 case 'A': /* 24-hour HH:MM format */
1934 case 't': /* 24-hour HH:MM:SS format */
1935 /* We show all of them as 24-hour HH:MM */
1936 strftime_HHMMSS(timebuf, sizeof(timebuf), NULL)[-3] = '\0';
1937 pbuf = timebuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001938 break;
Denys Vlasenko1d145692013-03-29 13:09:05 +01001939# if ENABLE_USERNAME_OR_HOMEDIR
1940 case 'w': /* current dir */
1941 case 'W': /* basename of cur dir */
1942 if (!cwd_buf) {
1943 cwd_buf = xrealloc_getcwd_or_warn(NULL);
1944 if (!cwd_buf)
1945 cwd_buf = (char *)bb_msg_unknown;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001946 else if (home_pwd_buf[0]) {
1947 char *after_home_user;
1948
Denys Vlasenko1d145692013-03-29 13:09:05 +01001949 /* /home/user[/something] -> ~[/something] */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001950 after_home_user = is_prefixed_with(cwd_buf, home_pwd_buf);
1951 if (after_home_user
1952 && (*after_home_user == '/' || *after_home_user == '\0')
Denys Vlasenko1d145692013-03-29 13:09:05 +01001953 ) {
1954 cwd_buf[0] = '~';
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001955 overlapping_strcpy(cwd_buf + 1, after_home_user);
Denys Vlasenko1d145692013-03-29 13:09:05 +01001956 }
1957 }
1958 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001959 pbuf = cwd_buf;
Denys Vlasenko1d145692013-03-29 13:09:05 +01001960 if (c == 'w')
1961 break;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001962 cp = strrchr(pbuf, '/');
Denys Vlasenko8172d052013-03-29 13:21:53 +01001963 if (cp)
Denys Vlasenko1d145692013-03-29 13:09:05 +01001964 pbuf = (char*)cp + 1;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001965 break;
Denys Vlasenko1d145692013-03-29 13:09:05 +01001966# endif
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001967// bb_process_escape_sequence does this now:
1968// case 'e': case 'E': /* \e \E = \033 */
1969// c = '\033';
1970// break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001971 case 'x': case 'X': {
1972 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001973 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001974 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001975 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001976 buf2[l] = '\0';
1977 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001978 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001979 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001980 break;
1981 }
1982 prmt_ptr++;
1983 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001984 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001985 if (c == 0)
1986 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001987 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001988 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001989 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001990 case '[': case ']':
1991 if (c == flg_not_length) {
Denys Vlasenko7a180432013-08-19 16:44:05 +02001992 /* Toggle '['/']' hex 5b/5d */
1993 flg_not_length ^= 6;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001994 continue;
1995 }
1996 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001997 } /* switch */
1998 } /* if */
1999 } /* if */
2000 cbuf[0] = c;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002001 {
2002 int n = strlen(pbuf);
2003 prmt_size += n;
2004 if (c == '\n')
2005 cmdedit_prmt_len = 0;
2006 else if (flg_not_length != ']') {
2007#if 0 /*ENABLE_UNICODE_SUPPORT*/
2008/* Won't work, pbuf is one BYTE string here instead of an one Unicode char string. */
2009/* FIXME */
Denys Vlasenko1b9ac212013-08-20 16:13:05 +02002010 cmdedit_prmt_len += unicode_strwidth(pbuf);
Denys Vlasenko7a180432013-08-19 16:44:05 +02002011#else
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002012 cmdedit_prmt_len += n;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002013#endif
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002014 }
Denys Vlasenko7a180432013-08-19 16:44:05 +02002015 }
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002016 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_size+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002017 free(free_me);
2018 } /* while */
2019
Denys Vlasenko1d145692013-03-29 13:09:05 +01002020# if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002021 if (cwd_buf != (char *)bb_msg_unknown)
2022 free(cwd_buf);
Denys Vlasenko1d145692013-03-29 13:09:05 +01002023# endif
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002024 /* see comment (above this function) about multiline prompt redrawing */
2025 cmdedit_prompt = prompt_last_line = prmt_mem_ptr;
2026 prmt_ptr = strrchr(cmdedit_prompt, '\n');
2027 if (prmt_ptr)
2028 prompt_last_line = prmt_ptr + 1;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002029 put_prompt();
2030}
Paul Fox3f11b1b2005-08-04 19:04:46 +00002031#endif
2032
Denys Vlasenko038a9772016-11-28 01:10:16 +01002033static void cmdedit_setwidth(void)
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002034{
Denys Vlasenko038a9772016-11-28 01:10:16 +01002035 int new_y;
2036
2037 cmdedit_termw = get_terminal_width(STDIN_FILENO);
2038 /* new y for current cursor */
2039 new_y = (cursor + cmdedit_prmt_len) / cmdedit_termw;
2040 /* redraw */
2041 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002042}
2043
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002044static void win_changed(int nsig UNUSED_PARAM)
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002045{
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002046 if (S.ok_to_redraw) {
2047 /* We are in read_key(), safe to redraw immediately */
2048 int sv_errno = errno;
Denys Vlasenko038a9772016-11-28 01:10:16 +01002049 cmdedit_setwidth();
2050 fflush_all();
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002051 errno = sv_errno;
2052 } else {
2053 /* Signal main loop that redraw is necessary */
2054 S.SIGWINCH_count++;
2055 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002056}
2057
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002058static int lineedit_read_key(char *read_key_buffer, int timeout)
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002059{
Denys Vlasenko020f4062009-05-17 16:44:54 +02002060 int64_t ic;
Denys Vlasenko19158a82010-03-26 14:06:56 +01002061#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002062 char unicode_buf[MB_CUR_MAX + 1];
2063 int unicode_idx = 0;
2064#endif
Denys Vlasenko020f4062009-05-17 16:44:54 +02002065
Denys Vlasenko038a9772016-11-28 01:10:16 +01002066 fflush_all();
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002067 while (1) {
2068 /* Wait for input. TIMEOUT = -1 makes read_key wait even
2069 * on nonblocking stdin, TIMEOUT = 50 makes sure we won't
2070 * insist on full MB_CUR_MAX buffer to declare input like
2071 * "\xff\n",pause,"ls\n" invalid and thus won't lose "ls".
2072 *
2073 * Note: read_key sets errno to 0 on success.
2074 */
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002075 S.ok_to_redraw = 1;
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002076 ic = read_key(STDIN_FILENO, read_key_buffer, timeout);
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002077 S.ok_to_redraw = 0;
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002078 if (errno) {
Denys Vlasenko19158a82010-03-26 14:06:56 +01002079#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002080 if (errno == EAGAIN && unicode_idx != 0)
2081 goto pushback;
Denys Vlasenko1f6d2302009-10-29 03:45:26 +01002082#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002083 break;
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +02002084 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002085
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002086#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
2087 if ((int32_t)ic == KEYCODE_CURSOR_POS
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002088 && S.sent_ESC_br6n
Denys Vlasenko020f4062009-05-17 16:44:54 +02002089 ) {
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002090 S.sent_ESC_br6n = 0;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002091 if (cursor == 0) { /* otherwise it may be bogus */
2092 int col = ((ic >> 32) & 0x7fff) - 1;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002093 /*
2094 * Is col > cmdedit_prmt_len?
2095 * If yes (terminal says cursor is farther to the right
2096 * of where we think it should be),
2097 * the prompt wasn't printed starting at col 1,
2098 * there was additional text before it.
2099 */
2100 if ((int)(col - cmdedit_prmt_len) > 0) {
2101 /* Fix our understanding of current x position */
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002102 cmdedit_x += (col - cmdedit_prmt_len);
2103 while (cmdedit_x >= cmdedit_termw) {
2104 cmdedit_x -= cmdedit_termw;
2105 cmdedit_y++;
2106 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02002107 }
2108 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002109 continue;
Denys Vlasenko020f4062009-05-17 16:44:54 +02002110 }
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002111#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002112
Denys Vlasenko19158a82010-03-26 14:06:56 +01002113#if ENABLE_UNICODE_SUPPORT
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002114 if (unicode_status == UNICODE_ON) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002115 wchar_t wc;
2116
2117 if ((int32_t)ic < 0) /* KEYCODE_xxx */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002118 break;
2119 // TODO: imagine sequence like: 0xff,<left-arrow>: we are currently losing 0xff...
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002120
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002121 unicode_buf[unicode_idx++] = ic;
2122 unicode_buf[unicode_idx] = '\0';
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002123 if (mbstowcs(&wc, unicode_buf, 1) != 1) {
2124 /* Not (yet?) a valid unicode char */
2125 if (unicode_idx < MB_CUR_MAX) {
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002126 timeout = 50;
2127 continue;
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002128 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002129 pushback:
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002130 /* Invalid sequence. Save all "bad bytes" except first */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002131 read_key_ungets(read_key_buffer, unicode_buf + 1, unicode_idx - 1);
Tomas Heinricha659b812010-04-29 13:43:39 +02002132# if !ENABLE_UNICODE_PRESERVE_BROKEN
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002133 ic = CONFIG_SUBST_WCHAR;
Tomas Heinricha659b812010-04-29 13:43:39 +02002134# else
Tomas Heinrichb8909c52010-05-16 20:46:53 +02002135 ic = unicode_mark_raw_byte(unicode_buf[0]);
Tomas Heinricha659b812010-04-29 13:43:39 +02002136# endif
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002137 } else {
2138 /* Valid unicode char, return its code */
2139 ic = wc;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002140 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002141 }
2142#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002143 break;
2144 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002145
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002146 return ic;
2147}
2148
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002149#if ENABLE_UNICODE_BIDI_SUPPORT
2150static int isrtl_str(void)
2151{
2152 int idx = cursor;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002153
2154 while (idx < command_len && unicode_bidi_is_neutral_wchar(command_ps[idx]))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002155 idx++;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002156 return unicode_bidi_isrtl(command_ps[idx]);
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002157}
2158#else
2159# define isrtl_str() 0
2160#endif
2161
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002162/* leave out the "vi-mode"-only case labels if vi editing isn't
2163 * configured. */
Denys Vlasenko13028922009-07-12 00:51:15 +02002164#define vi_case(caselabel) IF_FEATURE_EDITING_VI(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00002165
2166/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002167#undef CTRL
2168#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00002169
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002170enum {
2171 VI_CMDMODE_BIT = 0x40000000,
2172 /* 0x80000000 bit flags KEYCODE_xxx */
2173};
2174
2175#if ENABLE_FEATURE_REVERSE_SEARCH
2176/* Mimic readline Ctrl-R reverse history search.
2177 * When invoked, it shows the following prompt:
2178 * (reverse-i-search)'': user_input [cursor pos unchanged by Ctrl-R]
2179 * and typing results in search being performed:
2180 * (reverse-i-search)'tmp': cd /tmp [cursor under t in /tmp]
2181 * Search is performed by looking at progressively older lines in history.
2182 * Ctrl-R again searches for the next match in history.
2183 * Backspace deletes last matched char.
2184 * Control keys exit search and return to normal editing (at current history line).
2185 */
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002186static int32_t reverse_i_search(int timeout)
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002187{
2188 char match_buf[128]; /* for user input */
2189 char read_key_buffer[KEYCODE_BUFFER_SIZE];
2190 const char *matched_history_line;
2191 const char *saved_prompt;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002192 unsigned saved_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002193 int32_t ic;
2194
2195 matched_history_line = NULL;
2196 read_key_buffer[0] = 0;
2197 match_buf[0] = '\0';
2198
2199 /* Save and replace the prompt */
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002200 saved_prompt = prompt_last_line;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002201 saved_prmt_len = cmdedit_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002202 goto set_prompt;
2203
2204 while (1) {
2205 int h;
2206 unsigned match_buf_len = strlen(match_buf);
2207
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002208//FIXME: correct timeout? (i.e. count it down?)
2209 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002210
2211 switch (ic) {
2212 case CTRL('R'): /* searching for the next match */
2213 break;
2214
2215 case '\b':
2216 case '\x7f':
2217 /* Backspace */
2218 if (unicode_status == UNICODE_ON) {
2219 while (match_buf_len != 0) {
2220 uint8_t c = match_buf[--match_buf_len];
2221 if ((c & 0xc0) != 0x80) /* start of UTF-8 char? */
2222 break; /* yes */
2223 }
2224 } else {
2225 if (match_buf_len != 0)
2226 match_buf_len--;
2227 }
2228 match_buf[match_buf_len] = '\0';
2229 break;
2230
2231 default:
2232 if (ic < ' '
2233 || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2234 || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
2235 ) {
2236 goto ret;
2237 }
2238
2239 /* Append this char */
2240#if ENABLE_UNICODE_SUPPORT
2241 if (unicode_status == UNICODE_ON) {
2242 mbstate_t mbstate = { 0 };
2243 char buf[MB_CUR_MAX + 1];
2244 int len = wcrtomb(buf, ic, &mbstate);
2245 if (len > 0) {
2246 buf[len] = '\0';
2247 if (match_buf_len + len < sizeof(match_buf))
2248 strcpy(match_buf + match_buf_len, buf);
2249 }
2250 } else
2251#endif
2252 if (match_buf_len < sizeof(match_buf) - 1) {
2253 match_buf[match_buf_len] = ic;
2254 match_buf[match_buf_len + 1] = '\0';
2255 }
2256 break;
2257 } /* switch (ic) */
2258
2259 /* Search in history for match_buf */
2260 h = state->cur_history;
2261 if (ic == CTRL('R'))
2262 h--;
2263 while (h >= 0) {
2264 if (state->history[h]) {
2265 char *match = strstr(state->history[h], match_buf);
2266 if (match) {
2267 state->cur_history = h;
2268 matched_history_line = state->history[h];
2269 command_len = load_string(matched_history_line);
2270 cursor = match - matched_history_line;
2271//FIXME: cursor position for Unicode case
2272
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002273 free((char*)prompt_last_line);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002274 set_prompt:
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002275 prompt_last_line = xasprintf("(reverse-i-search)'%s': ", match_buf);
2276 cmdedit_prmt_len = unicode_strwidth(prompt_last_line);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002277 goto do_redraw;
2278 }
2279 }
2280 h--;
2281 }
2282
2283 /* Not found */
2284 match_buf[match_buf_len] = '\0';
2285 beep();
2286 continue;
2287
2288 do_redraw:
2289 redraw(cmdedit_y, command_len - cursor);
2290 } /* while (1) */
2291
2292 ret:
2293 if (matched_history_line)
2294 command_len = load_string(matched_history_line);
2295
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002296 free((char*)prompt_last_line);
2297 prompt_last_line = saved_prompt;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002298 cmdedit_prmt_len = saved_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002299 redraw(cmdedit_y, command_len - cursor);
2300
2301 return ic;
2302}
2303#endif
2304
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02002305/* maxsize must be >= 2.
2306 * Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00002307 * -1 on read errors or EOF, or on bare Ctrl-D,
2308 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denys Vlasenko4b89d512016-11-25 03:41:03 +01002309 * (in both cases the cursor remains on the input line, '\n' is not printed)
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00002310 * >0 length of input string, including terminating '\n'
2311 */
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002312int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize)
Erik Andersen13456d12000-03-16 08:09:57 +00002313{
Denys Vlasenkoaaaaaa52017-09-15 17:14:01 +02002314 int len, n;
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002315 int timeout;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002316#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002317 smallint lastWasTab = 0;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002318#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002319 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00002320#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002321 smallint vi_cmdmode = 0;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002322#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002323 struct termios initial_settings;
2324 struct termios new_settings;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002325 char read_key_buffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002326
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002327 INIT_S();
2328
Denys Vlasenkoaaaaaa52017-09-15 17:14:01 +02002329 n = get_termios_and_make_raw(STDIN_FILENO, &new_settings, &initial_settings, 0
2330 | TERMIOS_CLEAR_ISIG /* turn off INTR (ctrl-C), QUIT, SUSP */
2331 );
2332 if (n != 0 || (initial_settings.c_lflag & (ECHO|ICANON)) == ICANON) {
Denys Vlasenkod598a8d2014-12-10 17:22:13 +01002333 /* Happens when e.g. stty -echo was run before.
2334 * But if ICANON is not set, we don't come here.
2335 * (example: interactive python ^Z-backgrounded,
2336 * tty is still in "raw mode").
2337 */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002338 parse_and_put_prompt(prompt);
Denys Vlasenko038a9772016-11-28 01:10:16 +01002339 fflush_all();
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00002340 if (fgets(command, maxsize, stdin) == NULL)
2341 len = -1; /* EOF or error */
2342 else
2343 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002344 DEINIT_S();
2345 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00002346 }
2347
Denys Vlasenko28055022010-01-04 20:49:58 +01002348 init_unicode();
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02002349
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002350// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00002351 if (maxsize > MAX_LINELEN)
2352 maxsize = MAX_LINELEN;
Denys Vlasenko044b1802009-07-12 02:50:35 +02002353 S.maxsize = maxsize;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002354
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002355 timeout = -1;
2356 /* Make state->flags == 0 if st is NULL.
2357 * With zeroed flags, no other fields are ever referenced.
2358 */
2359 state = (line_input_t*) &const_int_0;
2360 if (st) {
2361 state = st;
2362 timeout = st->timeout;
2363 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002364#if MAX_HISTORY > 0
2365# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denys Vlasenkoe45af7a2011-09-04 16:15:24 +02002366 if (state->hist_file)
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00002367 if (state->cnt_history == 0)
2368 load_history(state);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002369# endif
Denis Vlasenko3c385cd2008-11-02 00:41:05 +00002370 if (state->flags & DO_HISTORY)
2371 state->cur_history = state->cnt_history;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002372#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002373
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002374 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00002375 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002376 command_len = 0;
Denys Vlasenko19158a82010-03-26 14:06:56 +01002377#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002378 command_ps = xzalloc(maxsize * sizeof(command_ps[0]));
2379#else
Mark Whitley4e338752001-01-26 20:42:23 +00002380 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00002381 command[0] = '\0';
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002382#endif
2383#define command command_must_not_be_used
Mark Whitley4e338752001-01-26 20:42:23 +00002384
Denis Vlasenko202ac502008-11-05 13:20:58 +00002385 tcsetattr_stdin_TCSANOW(&new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00002386
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002387#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002388 {
2389 struct passwd *entry;
2390
2391 entry = getpwuid(geteuid());
2392 if (entry) {
2393 user_buf = xstrdup(entry->pw_name);
2394 home_pwd_buf = xstrdup(entry->pw_dir);
2395 }
2396 }
2397#endif
Denis Vlasenko682ad302008-09-27 01:28:56 +00002398
2399#if 0
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02002400 for (i = 0; i <= state->max_history; i++)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002401 bb_error_msg("history[%d]:'%s'", i, state->history[i]);
Denis Vlasenko682ad302008-09-27 01:28:56 +00002402 bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
2403#endif
2404
Denys Vlasenko0a677222017-11-08 13:38:12 +01002405 /* Get width (before printing prompt) */
2406 cmdedit_termw = get_terminal_width(STDIN_FILENO);
Denys Vlasenko13ad9062009-11-11 03:19:30 +01002407 /* Print out the command prompt, optionally ask where cursor is */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002408 parse_and_put_prompt(prompt);
Denys Vlasenko13ad9062009-11-11 03:19:30 +01002409 ask_terminal();
Eric Andersenb3dc3b82001-01-04 11:08:45 +00002410
Alexey Fomenko232ebaa2011-05-20 04:26:29 +02002411 /* Install window resize handler (NB: after *all* init is complete) */
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002412 S.SIGWINCH_handler.sa_handler = win_changed;
2413 S.SIGWINCH_handler.sa_flags = SA_RESTART;
2414 sigaction(SIGWINCH, &S.SIGWINCH_handler, &S.SIGWINCH_handler);
2415
Denys Vlasenkoc396fe62009-05-17 19:28:14 +02002416 read_key_buffer[0] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002417 while (1) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002418 /*
2419 * The emacs and vi modes share much of the code in the big
2420 * command loop. Commands entered when in vi's command mode
2421 * (aka "escape mode") get an extra bit added to distinguish
2422 * them - this keeps them from being self-inserted. This
2423 * clutters the big switch a bit, but keeps all the code
2424 * in one place.
2425 */
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002426 int32_t ic, ic_raw;
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002427 unsigned count;
2428
2429 count = S.SIGWINCH_count;
2430 if (S.SIGWINCH_saved != count) {
2431 S.SIGWINCH_saved = count;
Denys Vlasenko038a9772016-11-28 01:10:16 +01002432 cmdedit_setwidth();
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002433 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002434
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002435 ic = ic_raw = lineedit_read_key(read_key_buffer, timeout);
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002436
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002437#if ENABLE_FEATURE_REVERSE_SEARCH
2438 again:
2439#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00002440#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00002441 newdelflag = 1;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002442 if (vi_cmdmode) {
2443 /* btw, since KEYCODE_xxx are all < 0, this doesn't
2444 * change ic if it contains one of them: */
2445 ic |= VI_CMDMODE_BIT;
2446 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00002447#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002448
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002449 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00002450 case '\n':
2451 case '\r':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002452 vi_case('\n'|VI_CMDMODE_BIT:)
2453 vi_case('\r'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002454 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002455 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00002456 break_out = 1;
2457 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002458 case CTRL('A'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002459 vi_case('0'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002460 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002461 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00002462 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002463 case CTRL('B'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002464 vi_case('h'|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002465 vi_case('\b'|VI_CMDMODE_BIT:) /* ^H */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002466 vi_case('\x7f'|VI_CMDMODE_BIT:) /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002467 input_backward(1); /* Move back one character */
Erik Andersenf0657d32000-04-12 17:49:52 +00002468 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002469 case CTRL('E'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002470 vi_case('$'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002471 /* Control-e -- End of line */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002472 put_till_end_and_adv_cursor();
Erik Andersenc7c634b2000-03-19 05:28:55 +00002473 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002474 case CTRL('F'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002475 vi_case('l'|VI_CMDMODE_BIT:)
2476 vi_case(' '|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002477 input_forward(); /* Move forward one character */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002478 break;
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002479 case '\b': /* ^H */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002480 case '\x7f': /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002481 if (!isrtl_str())
2482 input_backspace();
2483 else
2484 input_delete(0);
2485 break;
2486 case KEYCODE_DELETE:
2487 if (!isrtl_str())
2488 input_delete(0);
2489 else
2490 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00002491 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002492#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00002493 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002494 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002495 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002496#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002497 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00002498 /* Control-k -- clear to end of line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002499 command_ps[cursor] = BB_NUL;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002500 command_len = cursor;
Denys Vlasenko94043e82010-05-11 14:49:13 +02002501 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen65a07302002-04-13 13:26:49 +00002502 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002503 case CTRL('L'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002504 vi_case(CTRL('L')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002505 /* Control-l -- clear screen */
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002506 /* cursor to top,left; clear to the end of screen */
2507 printf(ESC"[H" ESC"[J");
2508 draw_full(command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00002509 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002510#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002511 case CTRL('N'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002512 vi_case(CTRL('N')|VI_CMDMODE_BIT:)
2513 vi_case('j'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002514 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002515 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002516 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00002517 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002518 case CTRL('P'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002519 vi_case(CTRL('P')|VI_CMDMODE_BIT:)
2520 vi_case('k'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002521 /* Control-p -- Get previous command from history */
Denis Vlasenko682ad302008-09-27 01:28:56 +00002522 if (get_previous_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002523 goto rewrite_line;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002524 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002525#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002526 case CTRL('U'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002527 vi_case(CTRL('U')|VI_CMDMODE_BIT:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002528 /* Control-U -- Clear line before cursor */
2529 if (cursor) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002530 command_len -= cursor;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002531 memmove(command_ps, command_ps + cursor,
2532 (command_len + 1) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002533 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002534 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002535 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002536 case CTRL('W'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002537 vi_case(CTRL('W')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002538 /* Control-W -- Remove the last word */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002539 while (cursor > 0 && BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002540 input_backspace();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002541 while (cursor > 0 && !BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002542 input_backspace();
2543 break;
Rostislav Skudnov2e4ef382016-11-24 15:04:00 +01002544 case KEYCODE_ALT_D: {
2545 /* Delete word forward */
2546 int nc, sc = cursor;
2547 ctrl_right();
2548 nc = cursor - sc;
2549 input_backward(nc);
2550 while (--nc >= 0)
2551 input_delete(1);
2552 break;
2553 }
2554 case KEYCODE_ALT_BACKSPACE: {
2555 /* Delete word backward */
2556 int sc = cursor;
2557 ctrl_left();
2558 while (sc-- > cursor)
2559 input_delete(1);
2560 break;
2561 }
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002562#if ENABLE_FEATURE_REVERSE_SEARCH
2563 case CTRL('R'):
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002564 ic = ic_raw = reverse_i_search(timeout);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002565 goto again;
2566#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002567
Denis Vlasenko38f63192007-01-22 09:03:07 +00002568#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002569 case 'i'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002570 vi_cmdmode = 0;
2571 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002572 case 'I'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002573 input_backward(cursor);
2574 vi_cmdmode = 0;
2575 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002576 case 'a'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002577 input_forward();
2578 vi_cmdmode = 0;
2579 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002580 case 'A'|VI_CMDMODE_BIT:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002581 put_till_end_and_adv_cursor();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002582 vi_cmdmode = 0;
2583 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002584 case 'x'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002585 input_delete(1);
2586 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002587 case 'X'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002588 if (cursor > 0) {
2589 input_backward(1);
2590 input_delete(1);
2591 }
2592 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002593 case 'W'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002594 vi_Word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002595 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002596 case 'w'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002597 vi_word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002598 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002599 case 'E'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002600 vi_End_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002601 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002602 case 'e'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002603 vi_end_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002604 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002605 case 'B'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002606 vi_Back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002607 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002608 case 'b'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002609 vi_back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002610 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002611 case 'C'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002612 vi_cmdmode = 0;
2613 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002614 case 'D'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002615 goto clear_to_eol;
2616
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002617 case 'c'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002618 vi_cmdmode = 0;
2619 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002620 case 'd'|VI_CMDMODE_BIT: {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002621 int nc, sc;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002622
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002623 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002624 if (errno) /* error */
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002625 goto return_error_indicator;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002626 if (ic == ic_raw) { /* "cc", "dd" */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002627 input_backward(cursor);
2628 goto clear_to_eol;
2629 break;
2630 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002631
2632 sc = cursor;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002633 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002634 case 'w':
2635 case 'W':
2636 case 'e':
2637 case 'E':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002638 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002639 case 'w': /* "dw", "cw" */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002640 vi_word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002641 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002642 case 'W': /* 'dW', 'cW' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002643 vi_Word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002644 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002645 case 'e': /* 'de', 'ce' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002646 vi_end_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002647 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002648 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002649 case 'E': /* 'dE', 'cE' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002650 vi_End_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002651 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002652 break;
2653 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002654 nc = cursor;
2655 input_backward(cursor - sc);
2656 while (nc-- > cursor)
2657 input_delete(1);
2658 break;
2659 case 'b': /* "db", "cb" */
2660 case 'B': /* implemented as B */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002661 if (ic == 'b')
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002662 vi_back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002663 else
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002664 vi_Back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002665 while (sc-- > cursor)
2666 input_delete(1);
2667 break;
2668 case ' ': /* "d ", "c " */
2669 input_delete(1);
2670 break;
2671 case '$': /* "d$", "c$" */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002672 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002673 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002674 input_delete(1);
2675 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002676 }
2677 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002678 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002679 case 'p'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002680 input_forward();
2681 /* fallthrough */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002682 case 'P'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002683 put();
2684 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002685 case 'r'|VI_CMDMODE_BIT:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002686//FIXME: unicode case?
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002687 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002688 if (errno) /* error */
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002689 goto return_error_indicator;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002690 if (ic < ' ' || ic > 255) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00002691 beep();
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002692 } else {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002693 command_ps[cursor] = ic;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002694 bb_putchar(ic);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002695 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00002696 }
2697 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002698 case '\x1b': /* ESC */
2699 if (state->flags & VI_MODE) {
2700 /* insert mode --> command mode */
2701 vi_cmdmode = 1;
2702 input_backward(1);
2703 }
2704 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002705#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002706
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002707#if MAX_HISTORY > 0
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002708 case KEYCODE_UP:
2709 if (get_previous_history())
2710 goto rewrite_line;
2711 beep();
2712 break;
2713 case KEYCODE_DOWN:
2714 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002715 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002716 rewrite_line:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002717 /* Rewrite the line with the selected history item */
2718 /* change command */
Denys Vlasenko90a99042009-09-06 02:36:23 +02002719 command_len = load_string(state->history[state->cur_history] ?
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002720 state->history[state->cur_history] : "");
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002721 /* redraw and go to eol (bol, in vi) */
2722 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
2723 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002724#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002725 case KEYCODE_RIGHT:
2726 input_forward();
2727 break;
2728 case KEYCODE_LEFT:
2729 input_backward(1);
2730 break;
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002731 case KEYCODE_CTRL_LEFT:
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002732 case KEYCODE_ALT_LEFT: /* bash doesn't do it */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002733 ctrl_left();
2734 break;
2735 case KEYCODE_CTRL_RIGHT:
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002736 case KEYCODE_ALT_RIGHT: /* bash doesn't do it */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002737 ctrl_right();
2738 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002739 case KEYCODE_HOME:
2740 input_backward(cursor);
2741 break;
2742 case KEYCODE_END:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002743 put_till_end_and_adv_cursor();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002744 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002745
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002746 default:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002747 if (initial_settings.c_cc[VINTR] != 0
2748 && ic_raw == initial_settings.c_cc[VINTR]
2749 ) {
2750 /* Ctrl-C (usually) - stop gathering input */
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002751 command_len = 0;
2752 break_out = -1; /* "do not append '\n'" */
2753 break;
2754 }
2755 if (initial_settings.c_cc[VEOF] != 0
2756 && ic_raw == initial_settings.c_cc[VEOF]
2757 ) {
2758 /* Ctrl-D (usually) - delete one character,
2759 * or exit if len=0 and no chars to delete */
2760 if (command_len == 0) {
2761 errno = 0;
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002762
2763 case -1: /* error (e.g. EIO when tty is destroyed) */
2764 IF_FEATURE_EDITING_VI(return_error_indicator:)
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002765 break_out = command_len = -1;
2766 break;
2767 }
2768 input_delete(0);
2769 break;
2770 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002771// /* Control-V -- force insert of next char */
2772// if (c == CTRL('V')) {
2773// if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002774// goto return_error_indicator;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002775// if (c == 0) {
2776// beep();
2777// break;
2778// }
2779// }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002780 if (ic < ' '
Denys Vlasenko19158a82010-03-26 14:06:56 +01002781 || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2782 || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002783 ) {
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002784 /* If VI_CMDMODE_BIT is set, ic is >= 256
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002785 * and vi mode ignores unexpected chars.
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002786 * Otherwise, we are here if ic is a
2787 * control char or an unhandled ESC sequence,
2788 * which is also ignored.
2789 */
2790 break;
2791 }
2792 if ((int)command_len >= (maxsize - 2)) {
2793 /* Not enough space for the char and EOL */
2794 break;
Paul Fox84bbac52008-01-11 16:50:08 +00002795 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002796
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002797 command_len++;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002798 if (cursor == (command_len - 1)) {
2799 /* We are at the end, append */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002800 command_ps[cursor] = ic;
2801 command_ps[cursor + 1] = BB_NUL;
Denys Vlasenko94043e82010-05-11 14:49:13 +02002802 put_cur_glyph_and_inc_cursor();
Tomas Heinrichaa167552010-03-26 13:13:24 +01002803 if (unicode_bidi_isrtl(ic))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002804 input_backward(1);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002805 } else {
2806 /* In the middle, insert */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002807 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002808
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002809 memmove(command_ps + sc + 1, command_ps + sc,
2810 (command_len - sc) * sizeof(command_ps[0]));
2811 command_ps[sc] = ic;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002812 /* is right-to-left char, or neutral one (e.g. comma) was just added to rtl text? */
2813 if (!isrtl_str())
2814 sc++; /* no */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002815 put_till_end_and_adv_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +00002816 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002817 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002818 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00002819 break;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002820 } /* switch (ic) */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002821
2822 if (break_out)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002823 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002824
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002825#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002826 if (ic_raw != '\t')
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002827 lastWasTab = 0;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002828#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002829 } /* while (1) */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002830
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002831#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002832 if (S.sent_ESC_br6n) {
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002833 /* "sleep 1; busybox ash" + hold [Enter] to trigger.
2834 * We sent "ESC [ 6 n", but got '\n' first, and
2835 * KEYCODE_CURSOR_POS response is now buffered from terminal.
2836 * It's bad already and not much can be done with it
2837 * (it _will_ be visible for the next process to read stdin),
2838 * but without this delay it even shows up on the screen
2839 * as garbage because we restore echo settings with tcsetattr
2840 * before it comes in. UGLY!
2841 */
2842 usleep(20*1000);
2843 }
2844#endif
2845
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002846/* End of bug-catching "command_must_not_be_used" trick */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002847#undef command
2848
Denys Vlasenko19158a82010-03-26 14:06:56 +01002849#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +02002850 command[0] = '\0';
2851 if (command_len > 0)
2852 command_len = save_string(command, maxsize - 1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002853 free(command_ps);
2854#endif
2855
Flemming Madsend96ffda2013-04-07 18:47:24 +02002856 if (command_len > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002857 remember_in_history(command);
Flemming Madsend96ffda2013-04-07 18:47:24 +02002858 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002859
Eric Andersen27bb7902003-12-23 20:24:51 +00002860 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002861 command[command_len++] = '\n';
2862 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00002863 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002864
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002865#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002866 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002867#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002868
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002869 /* restore initial_settings */
Denis Vlasenko202ac502008-11-05 13:20:58 +00002870 tcsetattr_stdin_TCSANOW(&initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002871 /* restore SIGWINCH handler */
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002872 sigaction_set(SIGWINCH, &S.SIGWINCH_handler);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002873 fflush_all();
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002874
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002875 len = command_len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002876 DEINIT_S();
2877
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002878 return len; /* can't return command_len, DEINIT_S() destroys it */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002879}
2880
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002881#else /* !FEATURE_EDITING */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002882
2883#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00002884int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002885{
2886 fputs(prompt, stdout);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002887 fflush_all();
Denys Vlasenkob2320372012-09-27 16:03:49 +02002888 if (!fgets(command, maxsize, stdin))
2889 return -1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002890 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00002891}
2892
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002893#endif /* !FEATURE_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00002894
2895
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002896/*
2897 * Testing
2898 */
2899
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002900#ifdef TEST
2901
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002902#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002903
2904const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002905
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002906int main(int argc, char **argv)
2907{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00002908 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002909 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00002910#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002911 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
2912 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
Denys Vlasenko8187e012017-09-13 22:48:30 +02002913 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002914#else
2915 "% ";
2916#endif
2917
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002918 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00002919 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002920 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002921 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00002922 break;
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002923 buff[l-1] = '\0';
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002924 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00002925 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002926 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002927 return 0;
2928}
2929
Eric Andersenc470f442003-07-28 09:56:35 +00002930#endif /* TEST */