blob: 678c4d29c5cd0f5f13baa9b5d9dd23513cd3573b [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
Ron Yorston23286902018-02-25 20:09:54 +0100154#if ENABLE_FEATURE_EDITING_WINCH
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100155 unsigned SIGWINCH_saved;
156 volatile unsigned SIGWINCH_count;
157 volatile smallint ok_to_redraw;
Ron Yorston23286902018-02-25 20:09:54 +0100158#endif
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100159
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000160#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200161# define DELBUFSIZ 128
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000162 smallint newdelflag; /* whether delbuf should be reused yet */
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100163 CHAR_T *delptr;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200164 CHAR_T delbuf[DELBUFSIZ]; /* a place to store deleted characters */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000165#endif
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100166#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +0100167 smallint sent_ESC_br6n;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100168#endif
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100169
Ron Yorston23286902018-02-25 20:09:54 +0100170#if ENABLE_FEATURE_EDITING_WINCH
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100171 /* Largish struct, keeping it last results in smaller code */
172 struct sigaction SIGWINCH_handler;
Ron Yorston23286902018-02-25 20:09:54 +0100173#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000174};
175
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000176/* See lineedit_ptr_hack.c */
177extern struct lineedit_statics *const lineedit_ptr_to_statics;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000178
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000179#define S (*lineedit_ptr_to_statics)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000180#define state (S.state )
181#define cmdedit_termw (S.cmdedit_termw )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000182#define cmdedit_x (S.cmdedit_x )
183#define cmdedit_y (S.cmdedit_y )
184#define cmdedit_prmt_len (S.cmdedit_prmt_len)
185#define cursor (S.cursor )
186#define command_len (S.command_len )
187#define command_ps (S.command_ps )
188#define cmdedit_prompt (S.cmdedit_prompt )
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200189#define prompt_last_line (S.prompt_last_line)
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000190#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000191#define home_pwd_buf (S.home_pwd_buf )
192#define matches (S.matches )
193#define num_matches (S.num_matches )
194#define delptr (S.delptr )
195#define newdelflag (S.newdelflag )
196#define delbuf (S.delbuf )
197
198#define INIT_S() do { \
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000199 (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000200 barrier(); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000201 cmdedit_termw = 80; \
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200202 IF_USERNAME_OR_HOMEDIR(home_pwd_buf = (char*)null_str;) \
Shawn J. Goff46031da2013-02-27 18:30:05 +0100203 IF_FEATURE_EDITING_VI(delptr = delbuf;) \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000204} while (0)
Alexey Fomenko232ebaa2011-05-20 04:26:29 +0200205
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000206static void deinit_S(void)
207{
208#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000209 /* This one is allocated only if FANCY_PROMPT is on
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200210 * (otherwise it points to verbatim prompt (NOT malloced)) */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000211 free((char*)cmdedit_prompt);
212#endif
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200213#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000214 free(user_buf);
215 if (home_pwd_buf != null_str)
216 free(home_pwd_buf);
217#endif
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000218 free(lineedit_ptr_to_statics);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000219}
220#define DEINIT_S() deinit_S()
221
Denis Vlasenko2c844952008-04-25 18:44:35 +0000222
Denys Vlasenko19158a82010-03-26 14:06:56 +0100223#if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200224static size_t load_string(const char *src)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200225{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100226 if (unicode_status == UNICODE_ON) {
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200227 ssize_t len = mbstowcs(command_ps, src, S.maxsize - 1);
Denys Vlasenko353680a2011-03-27 01:18:07 +0100228 if (len < 0)
229 len = 0;
230 command_ps[len] = BB_NUL;
231 return len;
232 } else {
233 unsigned i = 0;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200234 while (src[i] && i < S.maxsize - 1) {
235 command_ps[i] = src[i];
Denys Vlasenko353680a2011-03-27 01:18:07 +0100236 i++;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200237 }
238 command_ps[i] = BB_NUL;
Denys Vlasenko353680a2011-03-27 01:18:07 +0100239 return i;
240 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200241}
Tomas Heinricha659b812010-04-29 13:43:39 +0200242static unsigned save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200243{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100244 if (unicode_status == UNICODE_ON) {
Denys Vlasenko94043e82010-05-11 14:49:13 +0200245# if !ENABLE_UNICODE_PRESERVE_BROKEN
Denys Vlasenko353680a2011-03-27 01:18:07 +0100246 ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
247 if (len < 0)
248 len = 0;
249 dst[len] = '\0';
250 return len;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200251# else
Denys Vlasenko353680a2011-03-27 01:18:07 +0100252 unsigned dstpos = 0;
253 unsigned srcpos = 0;
Tomas Heinricha659b812010-04-29 13:43:39 +0200254
Denys Vlasenko353680a2011-03-27 01:18:07 +0100255 maxsize--;
256 while (dstpos < maxsize) {
257 wchar_t wc;
258 int n = srcpos;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200259
Denys Vlasenko353680a2011-03-27 01:18:07 +0100260 /* Convert up to 1st invalid byte (or up to end) */
261 while ((wc = command_ps[srcpos]) != BB_NUL
262 && !unicode_is_raw_byte(wc)
263 ) {
264 srcpos++;
265 }
266 command_ps[srcpos] = BB_NUL;
267 n = wcstombs(dst + dstpos, command_ps + n, maxsize - dstpos);
268 if (n < 0) /* should not happen */
269 break;
270 dstpos += n;
271 if (wc == BB_NUL) /* usually is */
272 break;
273
274 /* We do have invalid byte here! */
275 command_ps[srcpos] = wc; /* restore it */
Tomas Heinricha659b812010-04-29 13:43:39 +0200276 srcpos++;
Denys Vlasenko353680a2011-03-27 01:18:07 +0100277 if (dstpos == maxsize)
278 break;
279 dst[dstpos++] = (char) wc;
Tomas Heinricha659b812010-04-29 13:43:39 +0200280 }
Denys Vlasenko353680a2011-03-27 01:18:07 +0100281 dst[dstpos] = '\0';
282 return dstpos;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200283# endif
Denys Vlasenko353680a2011-03-27 01:18:07 +0100284 } else {
285 unsigned i = 0;
286 while ((dst[i] = command_ps[i]) != 0)
287 i++;
288 return i;
289 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200290}
291/* I thought just fputwc(c, stdout) would work. But no... */
292static void BB_PUTCHAR(wchar_t c)
293{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100294 if (unicode_status == UNICODE_ON) {
295 char buf[MB_CUR_MAX + 1];
296 mbstate_t mbst = { 0 };
297 ssize_t len = wcrtomb(buf, c, &mbst);
298 if (len > 0) {
299 buf[len] = '\0';
300 fputs(buf, stdout);
301 }
302 } else {
303 /* In this case, c is always one byte */
304 putchar(c);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200305 }
306}
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200307# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
308static wchar_t adjust_width_and_validate_wc(unsigned *width_adj, wchar_t wc)
309# else
310static wchar_t adjust_width_and_validate_wc(wchar_t wc)
311# define adjust_width_and_validate_wc(width_adj, wc) \
312 ((*(width_adj))++, adjust_width_and_validate_wc(wc))
313# endif
314{
315 int w = 1;
316
317 if (unicode_status == UNICODE_ON) {
Denys Vlasenko26e2c1d2010-05-16 21:15:03 +0200318 if (wc > CONFIG_LAST_SUPPORTED_WCHAR) {
319 /* note: also true for unicode_is_raw_byte(wc) */
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200320 goto subst;
321 }
322 w = wcwidth(wc);
323 if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0)
324 || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
325 || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
326 ) {
327 subst:
328 w = 1;
329 wc = CONFIG_SUBST_WCHAR;
330 }
331 }
332
333# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
334 *width_adj += w;
335#endif
336 return wc;
337}
338#else /* !UNICODE */
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200339static size_t load_string(const char *src)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200340{
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200341 safe_strncpy(command_ps, src, S.maxsize);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200342 return strlen(command_ps);
343}
Denys Vlasenko9038d6f2009-07-15 20:02:19 +0200344# if ENABLE_FEATURE_TAB_COMPLETION
Tomas Heinricha659b812010-04-29 13:43:39 +0200345static void save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200346{
347 safe_strncpy(dst, command_ps, maxsize);
348}
Denys Vlasenko7dd0ce42009-07-15 18:27:47 +0200349# endif
350# define BB_PUTCHAR(c) bb_putchar(c)
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200351/* Should never be called: */
352int adjust_width_and_validate_wc(unsigned *width_adj, int wc);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200353#endif
354
355
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000356/* Put 'command_ps[cursor]', cursor++.
357 * Advance cursor on screen. If we reached right margin, scroll text up
358 * and remove terminal margin effect by printing 'next_char' */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000359#define HACK_FOR_WRONG_WIDTH 1
Denys Vlasenko94043e82010-05-11 14:49:13 +0200360static void put_cur_glyph_and_inc_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000361{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200362 CHAR_T c = command_ps[cursor];
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200363 unsigned width = 0;
364 int ofs_to_right;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000365
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200366 if (c == BB_NUL) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000367 /* erase character after end of input string */
368 c = ' ';
Denys Vlasenko94043e82010-05-11 14:49:13 +0200369 } else {
370 /* advance cursor only if we aren't at the end yet */
371 cursor++;
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200372 if (unicode_status == UNICODE_ON) {
373 IF_UNICODE_WIDE_WCHARS(width = cmdedit_x;)
374 c = adjust_width_and_validate_wc(&cmdedit_x, c);
375 IF_UNICODE_WIDE_WCHARS(width = cmdedit_x - width;)
376 } else {
377 cmdedit_x++;
378 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000379 }
Denys Vlasenko94043e82010-05-11 14:49:13 +0200380
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200381 ofs_to_right = cmdedit_x - cmdedit_termw;
382 if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right <= 0) {
383 /* c fits on this line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200384 BB_PUTCHAR(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000385 }
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200386
387 if (ofs_to_right >= 0) {
388 /* we go to the next line */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000389#if HACK_FOR_WRONG_WIDTH
390 /* This works better if our idea of term width is wrong
391 * and it is actually wider (often happens on serial lines).
392 * Printing CR,LF *forces* cursor to next line.
393 * OTOH if terminal width is correct AND terminal does NOT
394 * have automargin (IOW: it is moving cursor to next line
395 * by itself (which is wrong for VT-10x terminals)),
396 * this will break things: there will be one extra empty line */
397 puts("\r"); /* + implicit '\n' */
398#else
Denys Vlasenko94043e82010-05-11 14:49:13 +0200399 /* VT-10x terminals don't wrap cursor to next line when last char
400 * on the line is printed - cursor stays "over" this char.
401 * Need to print _next_ char too (first one to appear on next line)
402 * to make cursor move down to next line.
403 */
404 /* Works ok only if cmdedit_termw is correct. */
405 c = command_ps[cursor];
406 if (c == BB_NUL)
407 c = ' ';
408 BB_PUTCHAR(c);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000409 bb_putchar('\b');
Denis Vlasenko2c844952008-04-25 18:44:35 +0000410#endif
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200411 cmdedit_y++;
412 if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right == 0) {
413 width = 0;
414 } else { /* ofs_to_right > 0 */
415 /* wide char c didn't fit on prev line */
416 BB_PUTCHAR(c);
417 }
418 cmdedit_x = width;
Mark Whitley4e338752001-01-26 20:42:23 +0000419 }
Erik Andersen13456d12000-03-16 08:09:57 +0000420}
421
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000422/* Move to end of line (by printing all chars till the end) */
Denys Vlasenko94043e82010-05-11 14:49:13 +0200423static void put_till_end_and_adv_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000424{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000425 while (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200426 put_cur_glyph_and_inc_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +0000427}
428
429/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430static void goto_new_line(void)
431{
Denys Vlasenko94043e82010-05-11 14:49:13 +0200432 put_till_end_and_adv_cursor();
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200433 if (cmdedit_x != 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000434 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000435}
436
Rob Landley88621d72006-08-29 19:41:06 +0000437static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000438{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000439 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000440}
441
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200442/* Full or last/sole prompt line, reset edit cursor, calculate terminal cursor.
443 * cmdedit_y is always calculated for the last/sole prompt line.
444 */
445static void put_prompt_custom(bool is_full)
Denys Vlasenko248c3242010-05-17 00:45:44 +0200446{
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200447 fputs((is_full ? cmdedit_prompt : prompt_last_line), stdout);
Denys Vlasenko248c3242010-05-17 00:45:44 +0200448 cursor = 0;
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100449 cmdedit_y = cmdedit_prmt_len / cmdedit_termw; /* new quasireal y */
450 cmdedit_x = cmdedit_prmt_len % cmdedit_termw;
Denys Vlasenko248c3242010-05-17 00:45:44 +0200451}
452
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200453#define put_prompt_last_line() put_prompt_custom(0)
454#define put_prompt() put_prompt_custom(1)
455
Eric Andersenaff114c2004-04-14 17:51:38 +0000456/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000457/* (optimized for slow terminals) */
458static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000459{
460 if (num > cursor)
461 num = cursor;
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200462 if (num == 0)
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000463 return;
464 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000465
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200466 if ((ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS)
467 && unicode_status == UNICODE_ON
468 ) {
469 /* correct NUM to be equal to _screen_ width */
470 int n = num;
471 num = 0;
472 while (--n >= 0)
473 adjust_width_and_validate_wc(&num, command_ps[cursor + n]);
474 if (num == 0)
475 return;
476 }
477
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000478 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000479 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000480 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000481 /* This is longer by 5 bytes on x86.
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000482 * Also gets miscompiled for ARM users
483 * (busybox.net/bugs/view.php?id=2274).
Denis Vlasenko6f043912008-02-18 22:28:03 +0000484 * printf(("\b\b\b\b" + 4) - num);
485 * return;
486 */
487 do {
488 bb_putchar('\b');
489 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000490 return;
491 }
Marek Polacek7b181072010-10-28 21:34:56 +0200492 printf(ESC"[%uD", num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000493 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000494 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000495
496 /* Need to go one or more lines up */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200497 if (ENABLE_UNICODE_WIDE_WCHARS) {
498 /* With wide chars, it is hard to "backtrack"
499 * and reliably figure out where to put cursor.
500 * Example (<> is a wide char; # is an ordinary char, _ cursor):
501 * |prompt: <><> |
502 * |<><><><><><> |
503 * |_ |
504 * and user presses left arrow. num = 1, cmdedit_x = 0,
505 * We need to go up one line, and then - how do we know that
506 * we need to go *10* positions to the right? Because
507 * |prompt: <>#<>|
508 * |<><><>#<><><>|
509 * |_ |
510 * in this situation we need to go *11* positions to the right.
511 *
512 * A simpler thing to do is to redraw everything from the start
513 * up to new cursor position (which is already known):
514 */
515 unsigned sv_cursor;
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200516 /* go to 1st column; go up to first line */
Marek Polacek7b181072010-10-28 21:34:56 +0200517 printf("\r" ESC"[%uA", cmdedit_y);
Denys Vlasenko248c3242010-05-17 00:45:44 +0200518 cmdedit_y = 0;
519 sv_cursor = cursor;
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200520 put_prompt_last_line(); /* sets cursor to 0 */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200521 while (cursor < sv_cursor)
522 put_cur_glyph_and_inc_cursor();
523 } else {
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200524 int lines_up;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200525 /* num = chars to go back from the beginning of current line: */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200526 num -= cmdedit_x;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200527 /* num=1...w: one line up, w+1...2w: two, etc: */
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100528 lines_up = 1 + (num - 1) / cmdedit_termw;
529 cmdedit_x = (cmdedit_termw * cmdedit_y - num) % cmdedit_termw;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200530 cmdedit_y -= lines_up;
531 /* go to 1st column; go up */
Marek Polacek7b181072010-10-28 21:34:56 +0200532 printf("\r" ESC"[%uA", lines_up);
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200533 /* go to correct column.
Denys Vlasenkobbf1aa12010-05-17 12:33:13 +0200534 * xterm, konsole, Linux VT interpret 0 as 1 below! wow.
535 * need to *make sure* we skip it if cmdedit_x == 0 */
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200536 if (cmdedit_x)
Marek Polacek7b181072010-10-28 21:34:56 +0200537 printf(ESC"[%uC", cmdedit_x);
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000538 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000539}
540
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200541/* See redraw and draw_full below */
542static void draw_custom(int y, int back_cursor, bool is_full)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000543{
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200544 if (y > 0) /* up y lines */
Marek Polacek7b181072010-10-28 21:34:56 +0200545 printf(ESC"[%uA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000546 bb_putchar('\r');
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200547 put_prompt_custom(is_full);
Denys Vlasenko94043e82010-05-11 14:49:13 +0200548 put_till_end_and_adv_cursor();
549 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000550 input_backward(back_cursor);
551}
552
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200553/* Move y lines up, draw last/sole prompt line, editor line[s], and clear tail.
554 * goal: redraw the prompt+input+cursor in-place, overwriting the previous */
555#define redraw(y, back_cursor) draw_custom((y), (back_cursor), 0)
556
557/* Like above, but without moving up, and while using all the prompt lines.
558 * goal: draw a full prompt+input+cursor unrelated to a previous position.
559 * note: cmdedit_y always ends up relating to the last/sole prompt line */
560#define draw_full(back_cursor) draw_custom(0, (back_cursor), 1)
561
Paul Fox3f11b1b2005-08-04 19:04:46 +0000562/* Delete the char in front of the cursor, optionally saving it
563 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000564#if !ENABLE_FEATURE_EDITING_VI
565static void input_delete(void)
566#define input_delete(save) input_delete()
567#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000568static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000569#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000570{
Mark Whitley4e338752001-01-26 20:42:23 +0000571 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000572
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000573 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000574 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000575
Denis Vlasenko38f63192007-01-22 09:03:07 +0000576#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000577 if (save) {
578 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000579 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000580 newdelflag = 0;
581 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000582 if ((delptr - delbuf) < DELBUFSIZ)
583 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000584 }
585#endif
586
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200587 memmove(command_ps + j, command_ps + j + 1,
Denys Vlasenko9531f7d2009-07-16 14:33:16 +0200588 /* (command_len + 1 [because of NUL]) - (j + 1)
589 * simplified into (command_len - j) */
590 (command_len - j) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000591 command_len--;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200592 put_till_end_and_adv_cursor();
593 /* Last char is still visible, erase it (and more) */
594 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersenc470f442003-07-28 09:56:35 +0000595 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000596}
597
Denis Vlasenko38f63192007-01-22 09:03:07 +0000598#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000599static void put(void)
600{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000601 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000602 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000603
Paul Fox3f11b1b2005-08-04 19:04:46 +0000604 if (j == 0)
605 return;
606 ocursor = cursor;
607 /* open hole and then fill it */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200608 memmove(command_ps + cursor + j, command_ps + cursor,
609 (command_len - cursor + 1) * sizeof(command_ps[0]));
610 memcpy(command_ps + cursor, delbuf, j * sizeof(command_ps[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000611 command_len += j;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200612 put_till_end_and_adv_cursor();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000613 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000614}
615#endif
616
Mark Whitley4e338752001-01-26 20:42:23 +0000617/* Delete the char in back of the cursor */
618static void input_backspace(void)
619{
620 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000621 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000622 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000623 }
624}
625
Eric Andersenaff114c2004-04-14 17:51:38 +0000626/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000627static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000628{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000629 if (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200630 put_cur_glyph_and_inc_cursor();
Erik Andersenf0657d32000-04-12 17:49:52 +0000631}
632
Denis Vlasenko38f63192007-01-22 09:03:07 +0000633#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000634
Mike Shalf3763032010-11-22 03:49:18 +0100635//FIXME:
636//needs to be more clever: currently it thinks that "foo\ b<TAB>
637//matches the file named "foo bar", which is untrue.
638//Also, perhaps "foo b<TAB> needs to complete to "foo bar" <cursor>,
639//not "foo bar <cursor>...
640
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000641static void free_tab_completion_data(void)
642{
643 if (matches) {
644 while (num_matches)
645 free(matches[--num_matches]);
646 free(matches);
647 matches = NULL;
648 }
649}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000650
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000651static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000652{
Denys Vlasenkoc3797d42017-11-07 18:09:29 +0100653 unsigned char *p = (unsigned char*)matched;
654 while (*p) {
655 /* ESC attack fix: drop any string with control chars */
656 if (*p < ' '
657 || (!ENABLE_UNICODE_SUPPORT && *p >= 0x7f)
658 || (ENABLE_UNICODE_SUPPORT && *p == 0x7f)
659 ) {
660 free(matched);
661 return;
662 }
663 p++;
664 }
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000665 matches = xrealloc_vector(matches, 4, num_matches);
666 matches[num_matches] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000667 num_matches++;
668}
669
Mike Shalf3763032010-11-22 03:49:18 +0100670# if ENABLE_FEATURE_USERNAME_COMPLETION
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200671/* Replace "~user/..." with "/homedir/...".
672 * The parameter is malloced, free it or return it
673 * unchanged if no user is matched.
674 */
675static char *username_path_completion(char *ud)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000676{
677 struct passwd *entry;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200678 char *tilde_name = ud;
679 char *home = NULL;
680
681 ud++; /* skip ~ */
682 if (*ud == '/') { /* "~/..." */
683 home = home_pwd_buf;
684 } else {
685 /* "~user/..." */
686 ud = strchr(ud, '/');
687 *ud = '\0'; /* "~user" */
688 entry = getpwnam(tilde_name + 1);
689 *ud = '/'; /* restore "~user/..." */
690 if (entry)
691 home = entry->pw_dir;
692 }
693 if (home) {
694 ud = concat_path_file(home, ud);
695 free(tilde_name);
696 tilde_name = ud;
697 }
698 return tilde_name;
699}
700
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200701/* ~use<tab> - find all users with this prefix.
702 * Return the length of the prefix used for matching.
703 */
704static NOINLINE unsigned complete_username(const char *ud)
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200705{
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100706 struct passwd *pw;
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200707 unsigned userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000708
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200709 ud++; /* skip ~ */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000710 userlen = strlen(ud);
711
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200712 setpwent();
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100713 while ((pw = getpwent()) != NULL) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200714 /* Null usernames should result in all users as possible completions. */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100715 if (/* !ud[0] || */ is_prefixed_with(pw->pw_name, ud)) {
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100716 add_match(xasprintf("~%s/", pw->pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000717 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000718 }
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100719 endpwent(); /* don't keep password file open */
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200720
721 return 1 + userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000722}
Mike Shalf3763032010-11-22 03:49:18 +0100723# endif /* FEATURE_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000724
725enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000726 FIND_EXE_ONLY = 0,
727 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000728 FIND_FILE_ONLY = 2,
729};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000730
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200731static int path_parse(char ***p)
Mark Whitley4e338752001-01-26 20:42:23 +0000732{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000733 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000734 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000735 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000736 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000737
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000738 if (state->flags & WITH_PATH_LOOKUP)
739 pth = state->path_lookup;
740 else
741 pth = getenv("PATH");
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200742
743 /* PATH="" or PATH=":"? */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000744 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
745 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000746
Denis Vlasenko253ce002007-01-22 08:34:44 +0000747 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000748 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000749 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000750 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000751 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000752 break;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200753 tmp++;
754 if (*tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000755 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000756 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000757 }
758
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200759 *p = res = xmalloc(npth * sizeof(res[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000760 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000761 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000762 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000763 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000764 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000765 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000766 *tmp++ = '\0'; /* ':' -> '\0' */
767 if (*tmp == '\0')
768 break; /* :<empty> */
769 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000770 }
Mark Whitley4e338752001-01-26 20:42:23 +0000771 return npth;
772}
773
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200774/* Complete command, directory or file name.
775 * Return the length of the prefix used for matching.
776 */
777static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000778{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000779 char *path1[1];
780 char **paths = path1;
781 int npaths;
782 int i;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200783 unsigned pf_len;
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200784 const char *pfind;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200785 char *dirbuf = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +0000786
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000787 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000788 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000789
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200790 pfind = strrchr(command, '/');
791 if (!pfind) {
792 if (type == FIND_EXE_ONLY)
793 npaths = path_parse(&paths);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000794 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000795 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000796 /* point to 'l' in "..../last_component" */
797 pfind++;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200798 /* dirbuf = ".../.../.../" */
799 dirbuf = xstrndup(command, pfind - command);
Mike Shalf3763032010-11-22 03:49:18 +0100800# if ENABLE_FEATURE_USERNAME_COMPLETION
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200801 if (dirbuf[0] == '~') /* ~/... or ~user/... */
802 dirbuf = username_path_completion(dirbuf);
Mike Shalf3763032010-11-22 03:49:18 +0100803# endif
Denys Vlasenko7063e862010-09-02 12:44:39 +0200804 path1[0] = dirbuf;
Mark Whitley4e338752001-01-26 20:42:23 +0000805 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200806 pf_len = strlen(pfind);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000807
Denys Vlasenkof128bdb2017-07-29 00:59:24 +0200808# if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1
Denys Vlasenko13360522016-10-24 01:25:05 +0200809 if (type == FIND_EXE_ONLY && !dirbuf) {
Ron Yorstonf23264b2015-05-29 11:31:40 +0100810 const char *p = applet_names;
811
Ron Yorston2b919582016-04-08 11:57:20 +0100812 while (*p) {
Ron Yorstonf23264b2015-05-29 11:31:40 +0100813 if (strncmp(pfind, p, pf_len) == 0)
814 add_match(xstrdup(p));
Ron Yorston2b919582016-04-08 11:57:20 +0100815 while (*p++ != '\0')
816 continue;
Ron Yorstonf23264b2015-05-29 11:31:40 +0100817 }
818 }
Denys Vlasenkof128bdb2017-07-29 00:59:24 +0200819# endif
Ron Yorstonf23264b2015-05-29 11:31:40 +0100820
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000821 for (i = 0; i < npaths; i++) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200822 DIR *dir;
823 struct dirent *next;
824 struct stat st;
825 char *found;
826
Mark Whitley4e338752001-01-26 20:42:23 +0000827 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000828 if (!dir)
829 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000830
831 while ((next = readdir(dir)) != NULL) {
Denys Vlasenko39263632010-09-03 14:11:08 +0200832 unsigned len;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200833 const char *name_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000834
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200835 /* .../<tab>: bash 3.2.0 shows dotfiles, but not . and .. */
836 if (!pfind[0] && DOT_OR_DOTDOT(name_found))
Mark Whitley4e338752001-01-26 20:42:23 +0000837 continue;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200838 /* match? */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100839 if (!is_prefixed_with(name_found, pfind))
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200840 continue; /* no */
841
842 found = concat_path_file(paths[i], name_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000843 /* NB: stat() first so that we see is it a directory;
844 * but if that fails, use lstat() so that
845 * we still match dangling links */
846 if (stat(found, &st) && lstat(found, &st))
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200847 goto cont; /* hmm, remove in progress? */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000848
Denys Vlasenko39263632010-09-03 14:11:08 +0200849 /* Save only name */
850 len = strlen(name_found);
851 found = xrealloc(found, len + 2); /* +2: for slash and NUL */
852 strcpy(found, name_found);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000853
Mark Whitley4e338752001-01-26 20:42:23 +0000854 if (S_ISDIR(st.st_mode)) {
Denys Vlasenko39263632010-09-03 14:11:08 +0200855 /* name is a directory, add slash */
856 found[len] = '/';
857 found[len + 1] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +0000858 } else {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200859 /* skip files if looking for dirs only (example: cd) */
Eric Andersenc470f442003-07-28 09:56:35 +0000860 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000861 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000862 }
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200863 /* add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000864 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000865 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000866 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000867 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000868 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000869 closedir(dir);
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200870 } /* for every path */
871
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000872 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000873 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000874 free(paths);
875 }
Denys Vlasenko39263632010-09-03 14:11:08 +0200876 free(dirbuf);
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200877
878 return pf_len;
Erik Andersen6273f652000-03-17 01:12:41 +0000879}
Erik Andersenf0657d32000-04-12 17:49:52 +0000880
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200881/* build_match_prefix:
Denys Vlasenko76939e72010-09-03 14:09:24 +0200882 * On entry, match_buf contains everything up to cursor at the moment <tab>
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200883 * was pressed. This function looks at it, figures out what part of it
884 * constitutes the command/file/directory prefix to use for completion,
Denys Vlasenko76939e72010-09-03 14:09:24 +0200885 * and rewrites match_buf to contain only that part.
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200886 */
Denys Vlasenko76939e72010-09-03 14:09:24 +0200887#define dbg_bmp 0
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200888/* Helpers: */
889/* QUOT is used on elements of int_buf[], which are bytes,
890 * not Unicode chars. Therefore it works correctly even in Unicode mode.
891 */
892#define QUOT (UCHAR_MAX+1)
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200893static void remove_chunk(int16_t *int_buf, int beg, int end)
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200894{
895 /* beg must be <= end */
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200896 if (beg == end)
897 return;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200898
899 while ((int_buf[beg] = int_buf[end]) != 0)
900 beg++, end++;
901
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200902 if (dbg_bmp) {
903 int i;
904 for (i = 0; int_buf[i]; i++)
905 bb_putchar((unsigned char)int_buf[i]);
906 bb_putchar('\n');
907 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200908}
Denys Vlasenko76939e72010-09-03 14:09:24 +0200909/* Caller ensures that match_buf points to a malloced buffer
910 * big enough to hold strlen(match_buf)*2 + 2
911 */
912static NOINLINE int build_match_prefix(char *match_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000913{
914 int i, j;
915 int command_mode;
Denys Vlasenko76939e72010-09-03 14:09:24 +0200916 int16_t *int_buf = (int16_t*)match_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000917
Denys Vlasenko76939e72010-09-03 14:09:24 +0200918 if (dbg_bmp) printf("\n%s\n", match_buf);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200919
Denys Vlasenko76939e72010-09-03 14:09:24 +0200920 /* Copy in reverse order, since they overlap */
921 i = strlen(match_buf);
922 do {
923 int_buf[i] = (unsigned char)match_buf[i];
924 i--;
925 } while (i >= 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000926
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200927 /* Mark every \c as "quoted c" */
Denys Vlasenko76939e72010-09-03 14:09:24 +0200928 for (i = 0; int_buf[i]; i++) {
929 if (int_buf[i] == '\\') {
930 remove_chunk(int_buf, i, i + 1);
931 int_buf[i] |= QUOT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000932 }
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200933 }
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200934 /* Quote-mark "chars" and 'chars', drop delimiters */
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200935 {
936 int in_quote = 0;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200937 i = 0;
938 while (int_buf[i]) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200939 int cur = int_buf[i];
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200940 if (!cur)
941 break;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200942 if (cur == '\'' || cur == '"') {
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200943 if (!in_quote || (cur == in_quote)) {
944 in_quote ^= cur;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200945 remove_chunk(int_buf, i, i + 1);
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200946 continue;
947 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000948 }
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200949 if (in_quote)
950 int_buf[i] = cur | QUOT;
951 i++;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200952 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000953 }
954
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200955 /* Remove everything up to command delimiters:
956 * ';' ';;' '&' '|' '&&' '||',
957 * but careful with '>&' '<&' '>|'
958 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000959 for (i = 0; int_buf[i]; i++) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200960 int cur = int_buf[i];
961 if (cur == ';' || cur == '&' || cur == '|') {
962 int prev = i ? int_buf[i - 1] : 0;
963 if (cur == '&' && (prev == '>' || prev == '<')) {
964 continue;
965 } else if (cur == '|' && prev == '>') {
966 continue;
967 }
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200968 remove_chunk(int_buf, 0, i + 1 + (cur == int_buf[i + 1]));
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200969 i = -1; /* back to square 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000970 }
971 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200972 /* Remove all `cmd` */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200973 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000974 if (int_buf[i] == '`') {
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200975 for (j = i + 1; int_buf[j]; j++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000976 if (int_buf[j] == '`') {
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200977 /* `cmd` should count as a word:
978 * `cmd` c<tab> should search for files c*,
979 * not commands c*. Therefore we don't drop
980 * `cmd` entirely, we replace it with single `.
981 */
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200982 remove_chunk(int_buf, i, j);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200983 goto next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000984 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200985 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200986 /* No closing ` - command mode, remove all up to ` */
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200987 remove_chunk(int_buf, 0, i + 1);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200988 break;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200989 next: ;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000990 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200991 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000992
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200993 /* Remove "cmd (" and "cmd {"
994 * Example: "if { c<tab>"
995 * In this example, c should be matched as command pfx.
996 */
997 for (i = 0; int_buf[i]; i++) {
998 if (int_buf[i] == '(' || int_buf[i] == '{') {
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200999 remove_chunk(int_buf, 0, i + 1);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001000 i = -1; /* back to square 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001001 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001002 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001003
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001004 /* Remove leading unquoted spaces */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001005 for (i = 0; int_buf[i]; i++)
1006 if (int_buf[i] != ' ')
1007 break;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001008 remove_chunk(int_buf, 0, i);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001009
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001010 /* Determine completion mode */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001011 command_mode = FIND_EXE_ONLY;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001012 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001013 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001014 if (int_buf[i] == ' '
1015 && command_mode == FIND_EXE_ONLY
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001016 && (char)int_buf[0] == 'c'
1017 && (char)int_buf[1] == 'd'
1018 && i == 2 /* -> int_buf[2] == ' ' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001019 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001020 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001021 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001022 command_mode = FIND_FILE_ONLY;
1023 break;
1024 }
1025 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001026 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001027 if (dbg_bmp) printf("command_mode(0:exe/1:dir/2:file):%d\n", command_mode);
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001028
1029 /* Remove everything except last word */
Denys Vlasenkob068bd72010-09-02 12:01:11 +02001030 for (i = 0; int_buf[i]; i++) /* quasi-strlen(int_buf) */
1031 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001032 for (--i; i >= 0; i--) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001033 int cur = int_buf[i];
1034 if (cur == ' ' || cur == '<' || cur == '>' || cur == '|' || cur == '&') {
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001035 remove_chunk(int_buf, 0, i + 1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001036 break;
1037 }
1038 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001039
Denys Vlasenko76939e72010-09-03 14:09:24 +02001040 /* Convert back to string of _chars_ */
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001041 i = 0;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001042 while ((match_buf[i] = int_buf[i]) != '\0')
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001043 i++;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001044
Denys Vlasenko76939e72010-09-03 14:09:24 +02001045 if (dbg_bmp) printf("final match_buf:'%s'\n", match_buf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001046
1047 return command_mode;
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02001048}
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001049
Glenn L McGrath4d001292003-01-06 01:11:50 +00001050/*
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001051 * Display by column (original idea from ls applet,
1052 * very optimized by me [Vladimir] :)
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001053 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001054static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +00001055{
1056 int ncols, row;
1057 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001058 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001059 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001060 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001061
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001062 /* find the longest file name - use that as the column width */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001063 for (row = 0; row < nrows; row++) {
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001064 l = unicode_strwidth(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001065 if (column_width < l)
1066 column_width = l;
1067 }
1068 column_width += 2; /* min space for columns */
1069 ncols = cmdedit_termw / column_width;
1070
1071 if (ncols > 1) {
1072 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001073 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +00001074 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001075 } else {
1076 ncols = 1;
1077 }
1078 for (row = 0; row < nrows; row++) {
1079 int n = row;
1080 int nc;
1081
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001082 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001083 printf("%s%-*s", matches[n],
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001084 (int)(column_width - unicode_strwidth(matches[n])), ""
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001085 );
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001086 }
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001087 if (ENABLE_UNICODE_SUPPORT)
1088 puts(printable_string(NULL, matches[n]));
1089 else
1090 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001091 }
1092}
1093
Mike Shalf3763032010-11-22 03:49:18 +01001094static const char *is_special_char(char c)
1095{
1096 return strchr(" `\"#$%^&*()=+{}[]:;'|\\<>", c);
1097}
1098
1099static char *quote_special_chars(char *found)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001100{
1101 int l = 0;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001102 char *s = xzalloc((strlen(found) + 1) * 2);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001103
1104 while (*found) {
Mike Shalf3763032010-11-22 03:49:18 +01001105 if (is_special_char(*found))
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001106 s[l++] = '\\';
1107 s[l++] = *found++;
1108 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001109 /* s[l] = '\0'; - already is */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001110 return s;
1111}
1112
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001113/* Do TAB completion */
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001114static NOINLINE void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001115{
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001116 char *chosen_match;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001117 char *match_buf;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001118 size_t len_found;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001119 /* Length of string used for matching */
1120 unsigned match_pfx_len = match_pfx_len;
1121 int find_type;
Mike Shalf3763032010-11-22 03:49:18 +01001122# if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001123 /* cursor pos in command converted to multibyte form */
1124 int cursor_mb;
Mike Shalf3763032010-11-22 03:49:18 +01001125# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001126 if (!(state->flags & TAB_COMPLETION))
1127 return;
1128
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001129 if (*lastWasTab) {
1130 /* The last char was a TAB too.
1131 * Print a list of all the available choices.
1132 */
Denys Vlasenkoa46e16e2010-09-03 13:05:51 +02001133 if (num_matches > 0) {
1134 /* cursor will be changed by goto_new_line() */
Denys Vlasenko044b1802009-07-12 02:50:35 +02001135 int sav_cursor = cursor;
Mark Whitley4e338752001-01-26 20:42:23 +00001136 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001137 showfiles();
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001138 draw_full(command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001139 }
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001140 return;
Erik Andersenf0657d32000-04-12 17:49:52 +00001141 }
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001142
1143 *lastWasTab = 1;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001144 chosen_match = NULL;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001145
Denys Vlasenko76939e72010-09-03 14:09:24 +02001146 /* Make a local copy of the string up to the position of the cursor.
1147 * build_match_prefix will expand it into int16_t's, need to allocate
1148 * twice as much as the string_len+1.
1149 * (we then also (ab)use this extra space later - see (**))
1150 */
1151 match_buf = xmalloc(MAX_LINELEN * sizeof(int16_t));
Mike Shalf3763032010-11-22 03:49:18 +01001152# if !ENABLE_UNICODE_SUPPORT
Denys Vlasenko76939e72010-09-03 14:09:24 +02001153 save_string(match_buf, cursor + 1); /* +1 for NUL */
Mike Shalf3763032010-11-22 03:49:18 +01001154# else
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001155 {
1156 CHAR_T wc = command_ps[cursor];
1157 command_ps[cursor] = BB_NUL;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001158 save_string(match_buf, MAX_LINELEN);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001159 command_ps[cursor] = wc;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001160 cursor_mb = strlen(match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001161 }
Mike Shalf3763032010-11-22 03:49:18 +01001162# endif
Denys Vlasenko76939e72010-09-03 14:09:24 +02001163 find_type = build_match_prefix(match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001164
1165 /* Free up any memory already allocated */
1166 free_tab_completion_data();
1167
Mike Shalf3763032010-11-22 03:49:18 +01001168# if ENABLE_FEATURE_USERNAME_COMPLETION
1169 /* If the word starts with ~ and there is no slash in the word,
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001170 * then try completing this word as a username. */
1171 if (state->flags & USERNAME_COMPLETION)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001172 if (match_buf[0] == '~' && strchr(match_buf, '/') == NULL)
1173 match_pfx_len = complete_username(match_buf);
Mike Shalf3763032010-11-22 03:49:18 +01001174# endif
1175 /* If complete_username() did not match,
1176 * try to match a command in $PATH, or a directory, or a file */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001177 if (!matches)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001178 match_pfx_len = complete_cmd_dir_file(match_buf, find_type);
Mike Shalf3763032010-11-22 03:49:18 +01001179
1180 /* Account for backslashes which will be inserted
1181 * by quote_special_chars() later */
1182 {
1183 const char *e = match_buf + strlen(match_buf);
1184 const char *s = e - match_pfx_len;
1185 while (s < e)
1186 if (is_special_char(*s++))
1187 match_pfx_len++;
1188 }
1189
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001190 /* Remove duplicates */
1191 if (matches) {
Mike Shalf3763032010-11-22 03:49:18 +01001192 unsigned i, n = 0;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001193 qsort_string_vector(matches, num_matches);
1194 for (i = 0; i < num_matches - 1; ++i) {
1195 //if (matches[i] && matches[i+1]) { /* paranoia */
1196 if (strcmp(matches[i], matches[i+1]) == 0) {
1197 free(matches[i]);
1198 //matches[i] = NULL; /* paranoia */
1199 } else {
1200 matches[n++] = matches[i];
1201 }
1202 //}
1203 }
1204 matches[n++] = matches[i];
1205 num_matches = n;
1206 }
Mike Shalf3763032010-11-22 03:49:18 +01001207
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001208 /* Did we find exactly one match? */
1209 if (num_matches != 1) { /* no */
1210 char *cp;
1211 beep();
1212 if (!matches)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001213 goto ret; /* no matches at all */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001214 /* Find common prefix */
1215 chosen_match = xstrdup(matches[0]);
1216 for (cp = chosen_match; *cp; cp++) {
1217 unsigned n;
1218 for (n = 1; n < num_matches; n++) {
1219 if (matches[n][cp - chosen_match] != *cp) {
1220 goto stop;
1221 }
1222 }
1223 }
1224 stop:
1225 if (cp == chosen_match) { /* have unique prefix? */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001226 goto ret; /* no */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001227 }
1228 *cp = '\0';
Mike Shalf3763032010-11-22 03:49:18 +01001229 cp = quote_special_chars(chosen_match);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001230 free(chosen_match);
1231 chosen_match = cp;
1232 len_found = strlen(chosen_match);
1233 } else { /* exactly one match */
1234 /* Next <tab> is not a double-tab */
1235 *lastWasTab = 0;
1236
Mike Shalf3763032010-11-22 03:49:18 +01001237 chosen_match = quote_special_chars(matches[0]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001238 len_found = strlen(chosen_match);
1239 if (chosen_match[len_found-1] != '/') {
1240 chosen_match[len_found] = ' ';
1241 chosen_match[++len_found] = '\0';
1242 }
1243 }
1244
Mike Shalf3763032010-11-22 03:49:18 +01001245# if !ENABLE_UNICODE_SUPPORT
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001246 /* Have space to place the match? */
1247 /* The result consists of three parts with these lengths: */
1248 /* cursor + (len_found - match_pfx_len) + (command_len - cursor) */
1249 /* it simplifies into: */
1250 if ((int)(len_found - match_pfx_len + command_len) < S.maxsize) {
1251 int pos;
1252 /* save tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001253 strcpy(match_buf, &command_ps[cursor]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001254 /* add match and tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001255 sprintf(&command_ps[cursor], "%s%s", chosen_match + match_pfx_len, match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001256 command_len = strlen(command_ps);
1257 /* new pos */
1258 pos = cursor + len_found - match_pfx_len;
1259 /* write out the matched command */
1260 redraw(cmdedit_y, command_len - pos);
1261 }
Mike Shalf3763032010-11-22 03:49:18 +01001262# else
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001263 {
Denys Vlasenko76939e72010-09-03 14:09:24 +02001264 /* Use 2nd half of match_buf as scratch space - see (**) */
1265 char *command = match_buf + MAX_LINELEN;
1266 int len = save_string(command, MAX_LINELEN);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001267 /* Have space to place the match? */
1268 /* cursor_mb + (len_found - match_pfx_len) + (len - cursor_mb) */
1269 if ((int)(len_found - match_pfx_len + len) < MAX_LINELEN) {
1270 int pos;
1271 /* save tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001272 strcpy(match_buf, &command[cursor_mb]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001273 /* where do we want to have cursor after all? */
1274 strcpy(&command[cursor_mb], chosen_match + match_pfx_len);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02001275 len = load_string(command);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001276 /* add match and tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001277 sprintf(&command[cursor_mb], "%s%s", chosen_match + match_pfx_len, match_buf);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02001278 command_len = load_string(command);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001279 /* write out the matched command */
1280 /* paranoia: load_string can return 0 on conv error,
1281 * prevent passing pos = (0 - 12) to redraw */
1282 pos = command_len - len;
1283 redraw(cmdedit_y, pos >= 0 ? pos : 0);
1284 }
1285 }
Mike Shalf3763032010-11-22 03:49:18 +01001286# endif
Denys Vlasenko76939e72010-09-03 14:09:24 +02001287 ret:
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001288 free(chosen_match);
Denys Vlasenko76939e72010-09-03 14:09:24 +02001289 free(match_buf);
Erik Andersenf0657d32000-04-12 17:49:52 +00001290}
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001291
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001292#endif /* FEATURE_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001293
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001294
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001295line_input_t* FAST_FUNC new_line_input_t(int flags)
1296{
1297 line_input_t *n = xzalloc(sizeof(*n));
1298 n->flags = flags;
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02001299 n->timeout = -1;
Denys Vlasenko79df4812014-01-10 14:38:26 +01001300#if MAX_HISTORY > 0
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001301 n->max_history = MAX_HISTORY;
Denys Vlasenko79df4812014-01-10 14:38:26 +01001302#endif
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001303 return n;
1304}
1305
1306
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001307#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001308
Flemming Madsend96ffda2013-04-07 18:47:24 +02001309unsigned FAST_FUNC size_from_HISTFILESIZE(const char *hp)
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001310{
1311 int size = MAX_HISTORY;
1312 if (hp) {
1313 size = atoi(hp);
1314 if (size <= 0)
1315 return 1;
1316 if (size > MAX_HISTORY)
1317 return MAX_HISTORY;
1318 }
1319 return size;
1320}
1321
Denis Vlasenko682ad302008-09-27 01:28:56 +00001322static void save_command_ps_at_cur_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001323{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001324 if (command_ps[0] != BB_NUL) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001325 int cur = state->cur_history;
1326 free(state->history[cur]);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001327
Denys Vlasenko19158a82010-03-26 14:06:56 +01001328# if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001329 {
1330 char tbuf[MAX_LINELEN];
1331 save_string(tbuf, sizeof(tbuf));
1332 state->history[cur] = xstrdup(tbuf);
1333 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001334# else
Denis Vlasenko682ad302008-09-27 01:28:56 +00001335 state->history[cur] = xstrdup(command_ps);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001336# endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001337 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001338}
1339
1340/* state->flags is already checked to be nonzero */
1341static int get_previous_history(void)
1342{
1343 if ((state->flags & DO_HISTORY) && state->cur_history) {
1344 save_command_ps_at_cur_history();
1345 state->cur_history--;
1346 return 1;
1347 }
1348 beep();
1349 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001350}
1351
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001352static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001353{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001354 if (state->flags & DO_HISTORY) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001355 if (state->cur_history < state->cnt_history) {
1356 save_command_ps_at_cur_history(); /* save the current history line */
1357 return ++state->cur_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001358 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001359 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001360 beep();
1361 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001362}
Robert Griebl350d26b2002-12-03 22:45:46 +00001363
Flemming Madsend96ffda2013-04-07 18:47:24 +02001364/* Lists command history. Used by shell 'history' builtins */
1365void FAST_FUNC show_history(const line_input_t *st)
1366{
1367 int i;
1368
1369 if (!st)
1370 return;
1371 for (i = 0; i < st->cnt_history; i++)
1372 printf("%4d %s\n", i, st->history[i]);
1373}
1374
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001375# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001376/* We try to ensure that concurrent additions to the history
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001377 * do not overwrite each other.
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001378 * Otherwise shell users get unhappy.
1379 *
1380 * History file is trimmed lazily, when it grows several times longer
1381 * than configured MAX_HISTORY lines.
1382 */
1383
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001384static void free_line_input_t(line_input_t *n)
1385{
1386 int i = n->cnt_history;
1387 while (i > 0)
1388 free(n->history[--i]);
1389 free(n);
1390}
1391
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001392/* state->flags is already checked to be nonzero */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001393static void load_history(line_input_t *st_parm)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001394{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001395 char *temp_h[MAX_HISTORY];
1396 char *line;
Robert Griebl350d26b2002-12-03 22:45:46 +00001397 FILE *fp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001398 unsigned idx, i, line_len;
Robert Griebl350d26b2002-12-03 22:45:46 +00001399
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001400 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +00001401
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001402 fp = fopen_for_read(st_parm->hist_file);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001403 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001404 /* clean up old history */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001405 for (idx = st_parm->cnt_history; idx > 0;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001406 idx--;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001407 free(st_parm->history[idx]);
1408 st_parm->history[idx] = NULL;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001409 }
1410
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001411 /* fill temp_h[], retaining only last MAX_HISTORY lines */
1412 memset(temp_h, 0, sizeof(temp_h));
Denys Vlasenkobede2152011-09-04 16:12:33 +02001413 idx = 0;
Dennis Groenendeee3562012-04-24 22:40:58 +02001414 st_parm->cnt_history_in_file = 0;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001415 while ((line = xmalloc_fgetline(fp)) != NULL) {
1416 if (line[0] == '\0') {
1417 free(line);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001418 continue;
1419 }
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001420 free(temp_h[idx]);
1421 temp_h[idx] = line;
Dennis Groenendeee3562012-04-24 22:40:58 +02001422 st_parm->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001423 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001424 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001425 idx = 0;
Robert Griebl350d26b2002-12-03 22:45:46 +00001426 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001427 fclose(fp);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001428
1429 /* find first non-NULL temp_h[], if any */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001430 if (st_parm->cnt_history_in_file) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001431 while (temp_h[idx] == NULL) {
1432 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001433 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001434 idx = 0;
1435 }
1436 }
1437
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001438 /* copy temp_h[] to st_parm->history[] */
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001439 for (i = 0; i < st_parm->max_history;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001440 line = temp_h[idx];
1441 if (!line)
1442 break;
1443 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001444 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001445 idx = 0;
1446 line_len = strlen(line);
1447 if (line_len >= MAX_LINELEN)
1448 line[MAX_LINELEN-1] = '\0';
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001449 st_parm->history[i++] = line;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001450 }
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001451 st_parm->cnt_history = i;
Denys Vlasenkobede2152011-09-04 16:12:33 +02001452 if (ENABLE_FEATURE_EDITING_SAVE_ON_EXIT)
1453 st_parm->cnt_history_in_file = i;
Robert Griebl350d26b2002-12-03 22:45:46 +00001454 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001455}
1456
Denys Vlasenkobede2152011-09-04 16:12:33 +02001457# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1458void save_history(line_input_t *st)
1459{
1460 FILE *fp;
1461
Denys Vlasenkobede2152011-09-04 16:12:33 +02001462 if (!st->hist_file)
1463 return;
1464 if (st->cnt_history <= st->cnt_history_in_file)
1465 return;
1466
1467 fp = fopen(st->hist_file, "a");
1468 if (fp) {
1469 int i, fd;
1470 char *new_name;
1471 line_input_t *st_temp;
1472
1473 for (i = st->cnt_history_in_file; i < st->cnt_history; i++)
1474 fprintf(fp, "%s\n", st->history[i]);
1475 fclose(fp);
1476
1477 /* we may have concurrently written entries from others.
1478 * load them */
1479 st_temp = new_line_input_t(st->flags);
1480 st_temp->hist_file = st->hist_file;
1481 st_temp->max_history = st->max_history;
1482 load_history(st_temp);
1483
1484 /* write out temp file and replace hist_file atomically */
1485 new_name = xasprintf("%s.%u.new", st->hist_file, (int) getpid());
1486 fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1487 if (fd >= 0) {
1488 fp = xfdopen_for_write(fd);
1489 for (i = 0; i < st_temp->cnt_history; i++)
1490 fprintf(fp, "%s\n", st_temp->history[i]);
1491 fclose(fp);
1492 if (rename(new_name, st->hist_file) == 0)
1493 st->cnt_history_in_file = st_temp->cnt_history;
1494 }
1495 free(new_name);
1496 free_line_input_t(st_temp);
1497 }
1498}
1499# else
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001500static void save_history(char *str)
Robert Griebl350d26b2002-12-03 22:45:46 +00001501{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001502 int fd;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001503 int len, len2;
Eric Andersenc470f442003-07-28 09:56:35 +00001504
Denys Vlasenkobede2152011-09-04 16:12:33 +02001505 if (!state->hist_file)
1506 return;
1507
Wolfram Sang2e9aeae2010-11-15 02:58:28 +01001508 fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0600);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001509 if (fd < 0)
1510 return;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001511 xlseek(fd, 0, SEEK_END); /* paranoia */
1512 len = strlen(str);
1513 str[len] = '\n'; /* we (try to) do atomic write */
1514 len2 = full_write(fd, str, len + 1);
1515 str[len] = '\0';
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001516 close(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001517 if (len2 != len + 1)
1518 return; /* "wtf?" */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001519
1520 /* did we write so much that history file needs trimming? */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001521 state->cnt_history_in_file++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001522 if (state->cnt_history_in_file > state->max_history * 4) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001523 char *new_name;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001524 line_input_t *st_temp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001525
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001526 /* we may have concurrently written entries from others.
1527 * load them */
1528 st_temp = new_line_input_t(state->flags);
1529 st_temp->hist_file = state->hist_file;
Denys Vlasenkoe3d8d072011-03-31 14:39:38 +02001530 st_temp->max_history = state->max_history;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001531 load_history(st_temp);
1532
1533 /* write out temp file and replace hist_file atomically */
1534 new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
Denys Vlasenko4840ae82011-09-04 15:28:03 +02001535 fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Wolfram Sang2e9aeae2010-11-15 02:58:28 +01001536 if (fd >= 0) {
1537 FILE *fp;
1538 int i;
1539
1540 fp = xfdopen_for_write(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001541 for (i = 0; i < st_temp->cnt_history; i++)
1542 fprintf(fp, "%s\n", st_temp->history[i]);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001543 fclose(fp);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001544 if (rename(new_name, state->hist_file) == 0)
1545 state->cnt_history_in_file = st_temp->cnt_history;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001546 }
1547 free(new_name);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001548 free_line_input_t(st_temp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001549 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001550}
Denys Vlasenkobede2152011-09-04 16:12:33 +02001551# endif
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001552# else
1553# define load_history(a) ((void)0)
1554# define save_history(a) ((void)0)
1555# endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001556
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001557static void remember_in_history(char *str)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001558{
1559 int i;
1560
1561 if (!(state->flags & DO_HISTORY))
1562 return;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001563 if (str[0] == '\0')
1564 return;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001565 i = state->cnt_history;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001566 /* Don't save dupes */
1567 if (i && strcmp(state->history[i-1], str) == 0)
1568 return;
1569
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001570 free(state->history[state->max_history]); /* redundant, paranoia */
1571 state->history[state->max_history] = NULL; /* redundant, paranoia */
Denis Vlasenko682ad302008-09-27 01:28:56 +00001572
1573 /* If history[] is full, remove the oldest command */
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001574 /* we need to keep history[state->max_history] empty, hence >=, not > */
1575 if (i >= state->max_history) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001576 free(state->history[0]);
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001577 for (i = 0; i < state->max_history-1; i++)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001578 state->history[i] = state->history[i+1];
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001579 /* i == state->max_history-1 */
Denys Vlasenkoc0cae522011-11-04 01:09:09 +01001580# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1581 if (state->cnt_history_in_file)
Denys Vlasenkobede2152011-09-04 16:12:33 +02001582 state->cnt_history_in_file--;
Denys Vlasenkoc0cae522011-11-04 01:09:09 +01001583# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001584 }
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001585 /* i <= state->max_history-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001586 state->history[i++] = xstrdup(str);
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001587 /* i <= state->max_history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001588 state->cur_history = i;
1589 state->cnt_history = i;
Denys Vlasenkobede2152011-09-04 16:12:33 +02001590# if ENABLE_FEATURE_EDITING_SAVEHISTORY && !ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1591 save_history(str);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001592# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001593}
1594
1595#else /* MAX_HISTORY == 0 */
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001596# define remember_in_history(a) ((void)0)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001597#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001598
1599
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001600#if ENABLE_FEATURE_EDITING_VI
Erik Andersen6273f652000-03-17 01:12:41 +00001601/*
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001602 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001603 */
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001604static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001605vi_Word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001606{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001607 CHAR_T *command = command_ps;
1608
1609 while (cursor < command_len && !BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001610 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001611 if (eat) while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001612 input_forward();
1613}
1614
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001615static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001616vi_word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001617{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001618 CHAR_T *command = command_ps;
1619
Natanael Copa7e6f9312016-08-14 23:30:29 +02001620 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001621 while (cursor < command_len
Natanael Copa7e6f9312016-08-14 23:30:29 +02001622 && (BB_isalnum_or_underscore(command[cursor+1]))
Denys Vlasenko13028922009-07-12 00:51:15 +02001623 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001624 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001625 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001626 } else if (BB_ispunct(command[cursor])) {
1627 while (cursor < command_len && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001628 input_forward();
1629 }
1630
Denis Vlasenko253ce002007-01-22 08:34:44 +00001631 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001632 input_forward();
1633
Denys Vlasenko13028922009-07-12 00:51:15 +02001634 if (eat) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001635 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001636 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001637 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001638}
1639
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001640static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001641vi_End_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001642{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001643 CHAR_T *command = command_ps;
1644
Paul Fox3f11b1b2005-08-04 19:04:46 +00001645 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001646 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001647 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001648 while (cursor < command_len-1 && !BB_isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001649 input_forward();
1650}
1651
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001652static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001653vi_end_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001654{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001655 CHAR_T *command = command_ps;
1656
Denis Vlasenko253ce002007-01-22 08:34:44 +00001657 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001658 return;
1659 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001660 while (cursor < command_len-1 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001661 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001662 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001663 return;
Natanael Copa7e6f9312016-08-14 23:30:29 +02001664 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001665 while (cursor < command_len-1
Natanael Copa7e6f9312016-08-14 23:30:29 +02001666 && (BB_isalnum_or_underscore(command[cursor+1]))
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001667 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001668 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001669 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001670 } else if (BB_ispunct(command[cursor])) {
1671 while (cursor < command_len-1 && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001672 input_forward();
1673 }
1674}
1675
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001676static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001677vi_Back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001678{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001679 CHAR_T *command = command_ps;
1680
1681 while (cursor > 0 && BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001682 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001683 while (cursor > 0 && !BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001684 input_backward(1);
1685}
1686
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001687static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001688vi_back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001689{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001690 CHAR_T *command = command_ps;
1691
Paul Fox3f11b1b2005-08-04 19:04:46 +00001692 if (cursor <= 0)
1693 return;
1694 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001695 while (cursor > 0 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001696 input_backward(1);
1697 if (cursor <= 0)
1698 return;
Natanael Copa7e6f9312016-08-14 23:30:29 +02001699 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001700 while (cursor > 0
Natanael Copa7e6f9312016-08-14 23:30:29 +02001701 && (BB_isalnum_or_underscore(command[cursor-1]))
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001702 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001703 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001704 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001705 } else if (BB_ispunct(command[cursor])) {
1706 while (cursor > 0 && BB_ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001707 input_backward(1);
1708 }
1709}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001710#endif
1711
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001712/* Modelled after bash 4.0 behavior of Ctrl-<arrow> */
1713static void ctrl_left(void)
1714{
1715 CHAR_T *command = command_ps;
1716
1717 while (1) {
1718 CHAR_T c;
1719
1720 input_backward(1);
1721 if (cursor == 0)
1722 break;
1723 c = command[cursor];
1724 if (c != ' ' && !BB_ispunct(c)) {
1725 /* we reached a "word" delimited by spaces/punct.
1726 * go to its beginning */
1727 while (1) {
1728 c = command[cursor - 1];
1729 if (c == ' ' || BB_ispunct(c))
1730 break;
1731 input_backward(1);
1732 if (cursor == 0)
1733 break;
1734 }
1735 break;
1736 }
1737 }
1738}
1739static void ctrl_right(void)
1740{
1741 CHAR_T *command = command_ps;
1742
1743 while (1) {
1744 CHAR_T c;
1745
1746 c = command[cursor];
1747 if (c == BB_NUL)
1748 break;
1749 if (c != ' ' && !BB_ispunct(c)) {
1750 /* we reached a "word" delimited by spaces/punct.
1751 * go to its end + 1 */
1752 while (1) {
1753 input_forward();
1754 c = command[cursor];
1755 if (c == BB_NUL || c == ' ' || BB_ispunct(c))
1756 break;
1757 }
1758 break;
1759 }
1760 input_forward();
1761 }
1762}
1763
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001764
1765/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001766 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001767 */
1768
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001769#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
1770static void ask_terminal(void)
1771{
1772 /* Ask terminal where is the cursor now.
1773 * lineedit_read_key handles response and corrects
1774 * our idea of current cursor position.
1775 * Testcase: run "echo -n long_line_long_line_long_line",
1776 * then type in a long, wrapping command and try to
1777 * delete it using backspace key.
1778 * Note: we print it _after_ prompt, because
1779 * prompt may contain CR. Example: PS1='\[\r\n\]\w '
1780 */
1781 /* Problem: if there is buffered input on stdin,
1782 * the response will be delivered later,
1783 * possibly to an unsuspecting application.
1784 * Testcase: "sleep 1; busybox ash" + press and hold [Enter].
1785 * Result:
1786 * ~/srcdevel/bbox/fix/busybox.t4 #
1787 * ~/srcdevel/bbox/fix/busybox.t4 #
1788 * ^[[59;34~/srcdevel/bbox/fix/busybox.t4 # <-- garbage
1789 * ~/srcdevel/bbox/fix/busybox.t4 #
1790 *
1791 * Checking for input with poll only makes the race narrower,
1792 * I still can trigger it. Strace:
1793 *
1794 * write(1, "~/srcdevel/bbox/fix/busybox.t4 # ", 33) = 33
1795 * poll([{fd=0, events=POLLIN}], 1, 0) = 0 (Timeout) <-- no input exists
1796 * write(1, "\33[6n", 4) = 4 <-- send the ESC sequence, quick!
Alexey Fomenko232ebaa2011-05-20 04:26:29 +02001797 * poll([{fd=0, events=POLLIN}], 1, -1) = 1 ([{fd=0, revents=POLLIN}])
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001798 * read(0, "\n", 1) = 1 <-- oh crap, user's input got in first
1799 */
Denys Vlasenko451add42010-07-25 00:06:41 +02001800 struct pollfd pfd;
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001801
Denys Vlasenko451add42010-07-25 00:06:41 +02001802 pfd.fd = STDIN_FILENO;
1803 pfd.events = POLLIN;
1804 if (safe_poll(&pfd, 1, 0) == 0) {
1805 S.sent_ESC_br6n = 1;
Marek Polacek7b181072010-10-28 21:34:56 +02001806 fputs(ESC"[6n", stdout);
Denys Vlasenko451add42010-07-25 00:06:41 +02001807 fflush_all(); /* make terminal see it ASAP! */
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001808 }
1809}
1810#else
1811#define ask_terminal() ((void)0)
1812#endif
1813
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001814/* Note about multi-line PS1 (e.g. "\n\w \u@\h\n> ") and prompt redrawing:
1815 *
1816 * If the prompt has any newlines, after we print it once we use only its last
1817 * line to redraw in-place, which makes it simpler to calculate how many lines
1818 * we should move the cursor up to align the redraw (cmdedit_y). The earlier
1819 * prompt lines just stay on screen and we redraw below them.
1820 *
1821 * Use cases for all prompt lines beyond the initial draw:
1822 * - After clear-screen (^L) or after displaying tab-completion choices, we
1823 * print the full prompt, as it isn't redrawn in-place.
1824 * - During terminal resize we could try to redraw all lines, but we don't,
1825 * because it requires delicate alignment, it's good enough with only the
1826 * last line, and doing it wrong is arguably worse than not doing it at all.
1827 *
1828 * Terminology wise, if it doesn't mention "full", then it means the last/sole
1829 * prompt line. We use the prompt (last/sole line) while redrawing in-place,
1830 * and the full where we need a fresh one unrelated to an earlier position.
1831 *
1832 * If PS1 is not multiline, the last/sole line and the full are the same string.
1833 */
1834
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001835/* Called just once at read_line_input() init time */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001836#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001837static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001838{
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001839 const char *p;
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001840 cmdedit_prompt = prompt_last_line = prmt_ptr;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001841 p = strrchr(prmt_ptr, '\n');
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001842 if (p)
1843 prompt_last_line = p + 1;
1844 cmdedit_prmt_len = unicode_strwidth(prompt_last_line);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001845 put_prompt();
1846}
1847#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001848static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001849{
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001850 int prmt_size = 0;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001851 char *prmt_mem_ptr = xzalloc(1);
Denys Vlasenko1d145692013-03-29 13:09:05 +01001852# if ENABLE_USERNAME_OR_HOMEDIR
1853 char *cwd_buf = NULL;
1854# endif
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001855 char flg_not_length = '[';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001856 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001857
Denys Vlasenko7a180432013-08-19 16:44:05 +02001858 /*cmdedit_prmt_len = 0; - already is */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001859
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001860 cbuf[1] = '\0'; /* never changes */
1861
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001862 while (*prmt_ptr) {
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001863 char timebuf[sizeof("HH:MM:SS")];
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001864 char *free_me = NULL;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001865 char *pbuf;
1866 char c;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001867
1868 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001869 c = *prmt_ptr++;
1870 if (c == '\\') {
Denys Vlasenko1d145692013-03-29 13:09:05 +01001871 const char *cp;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001872 int l;
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001873/*
1874 * Supported via bb_process_escape_sequence:
1875 * \a ASCII bell character (07)
1876 * \e ASCII escape character (033)
1877 * \n newline
1878 * \r carriage return
1879 * \\ backslash
1880 * \nnn char with octal code nnn
1881 * Supported:
1882 * \$ if the effective UID is 0, a #, otherwise a $
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001883 * \w current working directory, with $HOME abbreviated with a tilde
1884 * Note: we do not support $PROMPT_DIRTRIM=n feature
Denys Vlasenko1d145692013-03-29 13:09:05 +01001885 * \W basename of the current working directory, with $HOME abbreviated with a tilde
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001886 * \h hostname up to the first '.'
1887 * \H hostname
1888 * \u username
1889 * \[ begin a sequence of non-printing characters
1890 * \] end a sequence of non-printing characters
Denys Vlasenko1d145692013-03-29 13:09:05 +01001891 * \T current time in 12-hour HH:MM:SS format
1892 * \@ current time in 12-hour am/pm format
1893 * \A current time in 24-hour HH:MM format
1894 * \t current time in 24-hour HH:MM:SS format
1895 * (all of the above work as \A)
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001896 * Not supported:
Denys Vlasenko1d145692013-03-29 13:09:05 +01001897 * \! history number of this command
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001898 * \# command number of this command
1899 * \j number of jobs currently managed by the shell
1900 * \l basename of the shell's terminal device name
1901 * \s name of the shell, the basename of $0 (the portion following the final slash)
1902 * \V release of bash, version + patch level (e.g., 2.00.0)
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001903 * \d date in "Weekday Month Date" format (e.g., "Tue May 26")
1904 * \D{format}
1905 * format is passed to strftime(3).
1906 * An empty format results in a locale-specific time representation.
1907 * The braces are required.
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001908 * Mishandled by bb_process_escape_sequence:
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001909 * \v version of bash (e.g., 2.00)
1910 */
Denys Vlasenko1d145692013-03-29 13:09:05 +01001911 cp = prmt_ptr;
1912 c = *cp;
1913 if (c != 't') /* don't treat \t as tab */
1914 c = bb_process_escape_sequence(&prmt_ptr);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001915 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001916 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001917 break;
1918 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001919
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001920 switch (c) {
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02001921# if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001922 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001923 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001924 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001925# endif
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001926 case 'H':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001927 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001928 pbuf = free_me = safe_gethostname();
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001929 if (c == 'h')
1930 strchrnul(pbuf, '.')[0] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001931 break;
1932 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001933 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001934 break;
Denys Vlasenko1d145692013-03-29 13:09:05 +01001935 case 'T': /* 12-hour HH:MM:SS format */
1936 case '@': /* 12-hour am/pm format */
1937 case 'A': /* 24-hour HH:MM format */
1938 case 't': /* 24-hour HH:MM:SS format */
1939 /* We show all of them as 24-hour HH:MM */
1940 strftime_HHMMSS(timebuf, sizeof(timebuf), NULL)[-3] = '\0';
1941 pbuf = timebuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001942 break;
Denys Vlasenko1d145692013-03-29 13:09:05 +01001943# if ENABLE_USERNAME_OR_HOMEDIR
1944 case 'w': /* current dir */
1945 case 'W': /* basename of cur dir */
1946 if (!cwd_buf) {
1947 cwd_buf = xrealloc_getcwd_or_warn(NULL);
1948 if (!cwd_buf)
1949 cwd_buf = (char *)bb_msg_unknown;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001950 else if (home_pwd_buf[0]) {
1951 char *after_home_user;
1952
Denys Vlasenko1d145692013-03-29 13:09:05 +01001953 /* /home/user[/something] -> ~[/something] */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001954 after_home_user = is_prefixed_with(cwd_buf, home_pwd_buf);
1955 if (after_home_user
1956 && (*after_home_user == '/' || *after_home_user == '\0')
Denys Vlasenko1d145692013-03-29 13:09:05 +01001957 ) {
1958 cwd_buf[0] = '~';
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001959 overlapping_strcpy(cwd_buf + 1, after_home_user);
Denys Vlasenko1d145692013-03-29 13:09:05 +01001960 }
1961 }
1962 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001963 pbuf = cwd_buf;
Denys Vlasenko1d145692013-03-29 13:09:05 +01001964 if (c == 'w')
1965 break;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001966 cp = strrchr(pbuf, '/');
Denys Vlasenko8172d052013-03-29 13:21:53 +01001967 if (cp)
Denys Vlasenko1d145692013-03-29 13:09:05 +01001968 pbuf = (char*)cp + 1;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001969 break;
Denys Vlasenko1d145692013-03-29 13:09:05 +01001970# endif
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001971// bb_process_escape_sequence does this now:
1972// case 'e': case 'E': /* \e \E = \033 */
1973// c = '\033';
1974// break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001975 case 'x': case 'X': {
1976 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001977 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001978 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001979 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001980 buf2[l] = '\0';
1981 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001982 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001983 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001984 break;
1985 }
1986 prmt_ptr++;
1987 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001988 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001989 if (c == 0)
1990 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001991 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001992 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001993 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001994 case '[': case ']':
1995 if (c == flg_not_length) {
Denys Vlasenko7a180432013-08-19 16:44:05 +02001996 /* Toggle '['/']' hex 5b/5d */
1997 flg_not_length ^= 6;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001998 continue;
1999 }
2000 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002001 } /* switch */
2002 } /* if */
2003 } /* if */
2004 cbuf[0] = c;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002005 {
2006 int n = strlen(pbuf);
2007 prmt_size += n;
2008 if (c == '\n')
2009 cmdedit_prmt_len = 0;
2010 else if (flg_not_length != ']') {
2011#if 0 /*ENABLE_UNICODE_SUPPORT*/
2012/* Won't work, pbuf is one BYTE string here instead of an one Unicode char string. */
2013/* FIXME */
Denys Vlasenko1b9ac212013-08-20 16:13:05 +02002014 cmdedit_prmt_len += unicode_strwidth(pbuf);
Denys Vlasenko7a180432013-08-19 16:44:05 +02002015#else
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002016 cmdedit_prmt_len += n;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002017#endif
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002018 }
Denys Vlasenko7a180432013-08-19 16:44:05 +02002019 }
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002020 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_size+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002021 free(free_me);
2022 } /* while */
2023
Denys Vlasenko1d145692013-03-29 13:09:05 +01002024# if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002025 if (cwd_buf != (char *)bb_msg_unknown)
2026 free(cwd_buf);
Denys Vlasenko1d145692013-03-29 13:09:05 +01002027# endif
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002028 /* see comment (above this function) about multiline prompt redrawing */
2029 cmdedit_prompt = prompt_last_line = prmt_mem_ptr;
2030 prmt_ptr = strrchr(cmdedit_prompt, '\n');
2031 if (prmt_ptr)
2032 prompt_last_line = prmt_ptr + 1;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002033 put_prompt();
2034}
Paul Fox3f11b1b2005-08-04 19:04:46 +00002035#endif
2036
Ron Yorston23286902018-02-25 20:09:54 +01002037#if ENABLE_FEATURE_EDITING_WINCH
Denys Vlasenko038a9772016-11-28 01:10:16 +01002038static void cmdedit_setwidth(void)
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002039{
Denys Vlasenko038a9772016-11-28 01:10:16 +01002040 int new_y;
2041
2042 cmdedit_termw = get_terminal_width(STDIN_FILENO);
2043 /* new y for current cursor */
2044 new_y = (cursor + cmdedit_prmt_len) / cmdedit_termw;
2045 /* redraw */
2046 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002047}
2048
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002049static void win_changed(int nsig UNUSED_PARAM)
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002050{
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002051 if (S.ok_to_redraw) {
2052 /* We are in read_key(), safe to redraw immediately */
2053 int sv_errno = errno;
Denys Vlasenko038a9772016-11-28 01:10:16 +01002054 cmdedit_setwidth();
2055 fflush_all();
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002056 errno = sv_errno;
2057 } else {
2058 /* Signal main loop that redraw is necessary */
2059 S.SIGWINCH_count++;
2060 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002061}
Ron Yorston23286902018-02-25 20:09:54 +01002062#endif
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002063
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002064static int lineedit_read_key(char *read_key_buffer, int timeout)
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002065{
Denys Vlasenko020f4062009-05-17 16:44:54 +02002066 int64_t ic;
Denys Vlasenko19158a82010-03-26 14:06:56 +01002067#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002068 char unicode_buf[MB_CUR_MAX + 1];
2069 int unicode_idx = 0;
2070#endif
Denys Vlasenko020f4062009-05-17 16:44:54 +02002071
Denys Vlasenko038a9772016-11-28 01:10:16 +01002072 fflush_all();
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002073 while (1) {
2074 /* Wait for input. TIMEOUT = -1 makes read_key wait even
2075 * on nonblocking stdin, TIMEOUT = 50 makes sure we won't
2076 * insist on full MB_CUR_MAX buffer to declare input like
2077 * "\xff\n",pause,"ls\n" invalid and thus won't lose "ls".
2078 *
2079 * Note: read_key sets errno to 0 on success.
2080 */
Ron Yorston23286902018-02-25 20:09:54 +01002081 IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 1;)
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002082 ic = read_key(STDIN_FILENO, read_key_buffer, timeout);
Ron Yorston23286902018-02-25 20:09:54 +01002083 IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 0;)
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002084 if (errno) {
Denys Vlasenko19158a82010-03-26 14:06:56 +01002085#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002086 if (errno == EAGAIN && unicode_idx != 0)
2087 goto pushback;
Denys Vlasenko1f6d2302009-10-29 03:45:26 +01002088#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002089 break;
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +02002090 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002091
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002092#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
2093 if ((int32_t)ic == KEYCODE_CURSOR_POS
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002094 && S.sent_ESC_br6n
Denys Vlasenko020f4062009-05-17 16:44:54 +02002095 ) {
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002096 S.sent_ESC_br6n = 0;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002097 if (cursor == 0) { /* otherwise it may be bogus */
2098 int col = ((ic >> 32) & 0x7fff) - 1;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002099 /*
2100 * Is col > cmdedit_prmt_len?
2101 * If yes (terminal says cursor is farther to the right
2102 * of where we think it should be),
2103 * the prompt wasn't printed starting at col 1,
2104 * there was additional text before it.
2105 */
2106 if ((int)(col - cmdedit_prmt_len) > 0) {
2107 /* Fix our understanding of current x position */
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002108 cmdedit_x += (col - cmdedit_prmt_len);
2109 while (cmdedit_x >= cmdedit_termw) {
2110 cmdedit_x -= cmdedit_termw;
2111 cmdedit_y++;
2112 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02002113 }
2114 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002115 continue;
Denys Vlasenko020f4062009-05-17 16:44:54 +02002116 }
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002117#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002118
Denys Vlasenko19158a82010-03-26 14:06:56 +01002119#if ENABLE_UNICODE_SUPPORT
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002120 if (unicode_status == UNICODE_ON) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002121 wchar_t wc;
2122
2123 if ((int32_t)ic < 0) /* KEYCODE_xxx */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002124 break;
2125 // TODO: imagine sequence like: 0xff,<left-arrow>: we are currently losing 0xff...
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002126
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002127 unicode_buf[unicode_idx++] = ic;
2128 unicode_buf[unicode_idx] = '\0';
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002129 if (mbstowcs(&wc, unicode_buf, 1) != 1) {
2130 /* Not (yet?) a valid unicode char */
2131 if (unicode_idx < MB_CUR_MAX) {
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002132 timeout = 50;
2133 continue;
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002134 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002135 pushback:
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002136 /* Invalid sequence. Save all "bad bytes" except first */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002137 read_key_ungets(read_key_buffer, unicode_buf + 1, unicode_idx - 1);
Tomas Heinricha659b812010-04-29 13:43:39 +02002138# if !ENABLE_UNICODE_PRESERVE_BROKEN
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002139 ic = CONFIG_SUBST_WCHAR;
Tomas Heinricha659b812010-04-29 13:43:39 +02002140# else
Tomas Heinrichb8909c52010-05-16 20:46:53 +02002141 ic = unicode_mark_raw_byte(unicode_buf[0]);
Tomas Heinricha659b812010-04-29 13:43:39 +02002142# endif
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002143 } else {
2144 /* Valid unicode char, return its code */
2145 ic = wc;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002146 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002147 }
2148#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002149 break;
2150 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002151
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002152 return ic;
2153}
2154
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002155#if ENABLE_UNICODE_BIDI_SUPPORT
2156static int isrtl_str(void)
2157{
2158 int idx = cursor;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002159
2160 while (idx < command_len && unicode_bidi_is_neutral_wchar(command_ps[idx]))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002161 idx++;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002162 return unicode_bidi_isrtl(command_ps[idx]);
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002163}
2164#else
2165# define isrtl_str() 0
2166#endif
2167
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002168/* leave out the "vi-mode"-only case labels if vi editing isn't
2169 * configured. */
Denys Vlasenko13028922009-07-12 00:51:15 +02002170#define vi_case(caselabel) IF_FEATURE_EDITING_VI(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00002171
2172/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002173#undef CTRL
2174#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00002175
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002176enum {
2177 VI_CMDMODE_BIT = 0x40000000,
2178 /* 0x80000000 bit flags KEYCODE_xxx */
2179};
2180
2181#if ENABLE_FEATURE_REVERSE_SEARCH
2182/* Mimic readline Ctrl-R reverse history search.
2183 * When invoked, it shows the following prompt:
2184 * (reverse-i-search)'': user_input [cursor pos unchanged by Ctrl-R]
2185 * and typing results in search being performed:
2186 * (reverse-i-search)'tmp': cd /tmp [cursor under t in /tmp]
2187 * Search is performed by looking at progressively older lines in history.
2188 * Ctrl-R again searches for the next match in history.
2189 * Backspace deletes last matched char.
2190 * Control keys exit search and return to normal editing (at current history line).
2191 */
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002192static int32_t reverse_i_search(int timeout)
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002193{
2194 char match_buf[128]; /* for user input */
2195 char read_key_buffer[KEYCODE_BUFFER_SIZE];
2196 const char *matched_history_line;
2197 const char *saved_prompt;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002198 unsigned saved_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002199 int32_t ic;
2200
2201 matched_history_line = NULL;
2202 read_key_buffer[0] = 0;
2203 match_buf[0] = '\0';
2204
2205 /* Save and replace the prompt */
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002206 saved_prompt = prompt_last_line;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002207 saved_prmt_len = cmdedit_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002208 goto set_prompt;
2209
2210 while (1) {
2211 int h;
2212 unsigned match_buf_len = strlen(match_buf);
2213
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002214//FIXME: correct timeout? (i.e. count it down?)
2215 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002216
2217 switch (ic) {
2218 case CTRL('R'): /* searching for the next match */
2219 break;
2220
2221 case '\b':
2222 case '\x7f':
2223 /* Backspace */
2224 if (unicode_status == UNICODE_ON) {
2225 while (match_buf_len != 0) {
2226 uint8_t c = match_buf[--match_buf_len];
2227 if ((c & 0xc0) != 0x80) /* start of UTF-8 char? */
2228 break; /* yes */
2229 }
2230 } else {
2231 if (match_buf_len != 0)
2232 match_buf_len--;
2233 }
2234 match_buf[match_buf_len] = '\0';
2235 break;
2236
2237 default:
2238 if (ic < ' '
2239 || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2240 || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
2241 ) {
2242 goto ret;
2243 }
2244
2245 /* Append this char */
2246#if ENABLE_UNICODE_SUPPORT
2247 if (unicode_status == UNICODE_ON) {
2248 mbstate_t mbstate = { 0 };
2249 char buf[MB_CUR_MAX + 1];
2250 int len = wcrtomb(buf, ic, &mbstate);
2251 if (len > 0) {
2252 buf[len] = '\0';
2253 if (match_buf_len + len < sizeof(match_buf))
2254 strcpy(match_buf + match_buf_len, buf);
2255 }
2256 } else
2257#endif
2258 if (match_buf_len < sizeof(match_buf) - 1) {
2259 match_buf[match_buf_len] = ic;
2260 match_buf[match_buf_len + 1] = '\0';
2261 }
2262 break;
2263 } /* switch (ic) */
2264
2265 /* Search in history for match_buf */
2266 h = state->cur_history;
2267 if (ic == CTRL('R'))
2268 h--;
2269 while (h >= 0) {
2270 if (state->history[h]) {
2271 char *match = strstr(state->history[h], match_buf);
2272 if (match) {
2273 state->cur_history = h;
2274 matched_history_line = state->history[h];
2275 command_len = load_string(matched_history_line);
2276 cursor = match - matched_history_line;
2277//FIXME: cursor position for Unicode case
2278
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002279 free((char*)prompt_last_line);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002280 set_prompt:
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002281 prompt_last_line = xasprintf("(reverse-i-search)'%s': ", match_buf);
2282 cmdedit_prmt_len = unicode_strwidth(prompt_last_line);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002283 goto do_redraw;
2284 }
2285 }
2286 h--;
2287 }
2288
2289 /* Not found */
2290 match_buf[match_buf_len] = '\0';
2291 beep();
2292 continue;
2293
2294 do_redraw:
2295 redraw(cmdedit_y, command_len - cursor);
2296 } /* while (1) */
2297
2298 ret:
2299 if (matched_history_line)
2300 command_len = load_string(matched_history_line);
2301
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002302 free((char*)prompt_last_line);
2303 prompt_last_line = saved_prompt;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002304 cmdedit_prmt_len = saved_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002305 redraw(cmdedit_y, command_len - cursor);
2306
2307 return ic;
2308}
2309#endif
2310
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02002311/* maxsize must be >= 2.
2312 * Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00002313 * -1 on read errors or EOF, or on bare Ctrl-D,
2314 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denys Vlasenko4b89d512016-11-25 03:41:03 +01002315 * (in both cases the cursor remains on the input line, '\n' is not printed)
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00002316 * >0 length of input string, including terminating '\n'
2317 */
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002318int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize)
Erik Andersen13456d12000-03-16 08:09:57 +00002319{
Denys Vlasenkoaaaaaa52017-09-15 17:14:01 +02002320 int len, n;
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002321 int timeout;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002322#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002323 smallint lastWasTab = 0;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002324#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002325 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00002326#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002327 smallint vi_cmdmode = 0;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002328#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002329 struct termios initial_settings;
2330 struct termios new_settings;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002331 char read_key_buffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002332
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002333 INIT_S();
2334
Denys Vlasenkoaaaaaa52017-09-15 17:14:01 +02002335 n = get_termios_and_make_raw(STDIN_FILENO, &new_settings, &initial_settings, 0
2336 | TERMIOS_CLEAR_ISIG /* turn off INTR (ctrl-C), QUIT, SUSP */
2337 );
2338 if (n != 0 || (initial_settings.c_lflag & (ECHO|ICANON)) == ICANON) {
Denys Vlasenkod598a8d2014-12-10 17:22:13 +01002339 /* Happens when e.g. stty -echo was run before.
2340 * But if ICANON is not set, we don't come here.
2341 * (example: interactive python ^Z-backgrounded,
2342 * tty is still in "raw mode").
2343 */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002344 parse_and_put_prompt(prompt);
Denys Vlasenko038a9772016-11-28 01:10:16 +01002345 fflush_all();
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00002346 if (fgets(command, maxsize, stdin) == NULL)
2347 len = -1; /* EOF or error */
2348 else
2349 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002350 DEINIT_S();
2351 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00002352 }
2353
Denys Vlasenko28055022010-01-04 20:49:58 +01002354 init_unicode();
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02002355
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002356// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00002357 if (maxsize > MAX_LINELEN)
2358 maxsize = MAX_LINELEN;
Denys Vlasenko044b1802009-07-12 02:50:35 +02002359 S.maxsize = maxsize;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002360
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002361 timeout = -1;
2362 /* Make state->flags == 0 if st is NULL.
2363 * With zeroed flags, no other fields are ever referenced.
2364 */
2365 state = (line_input_t*) &const_int_0;
2366 if (st) {
2367 state = st;
2368 timeout = st->timeout;
2369 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002370#if MAX_HISTORY > 0
2371# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denys Vlasenkoe45af7a2011-09-04 16:15:24 +02002372 if (state->hist_file)
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00002373 if (state->cnt_history == 0)
2374 load_history(state);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002375# endif
Denis Vlasenko3c385cd2008-11-02 00:41:05 +00002376 if (state->flags & DO_HISTORY)
2377 state->cur_history = state->cnt_history;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002378#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002379
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002380 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00002381 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002382 command_len = 0;
Denys Vlasenko19158a82010-03-26 14:06:56 +01002383#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002384 command_ps = xzalloc(maxsize * sizeof(command_ps[0]));
2385#else
Mark Whitley4e338752001-01-26 20:42:23 +00002386 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00002387 command[0] = '\0';
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002388#endif
2389#define command command_must_not_be_used
Mark Whitley4e338752001-01-26 20:42:23 +00002390
Denis Vlasenko202ac502008-11-05 13:20:58 +00002391 tcsetattr_stdin_TCSANOW(&new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00002392
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002393#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002394 {
2395 struct passwd *entry;
2396
2397 entry = getpwuid(geteuid());
2398 if (entry) {
2399 user_buf = xstrdup(entry->pw_name);
2400 home_pwd_buf = xstrdup(entry->pw_dir);
2401 }
2402 }
2403#endif
Denis Vlasenko682ad302008-09-27 01:28:56 +00002404
2405#if 0
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02002406 for (i = 0; i <= state->max_history; i++)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002407 bb_error_msg("history[%d]:'%s'", i, state->history[i]);
Denis Vlasenko682ad302008-09-27 01:28:56 +00002408 bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
2409#endif
2410
Denys Vlasenko0a677222017-11-08 13:38:12 +01002411 /* Get width (before printing prompt) */
2412 cmdedit_termw = get_terminal_width(STDIN_FILENO);
Denys Vlasenko13ad9062009-11-11 03:19:30 +01002413 /* Print out the command prompt, optionally ask where cursor is */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002414 parse_and_put_prompt(prompt);
Denys Vlasenko13ad9062009-11-11 03:19:30 +01002415 ask_terminal();
Eric Andersenb3dc3b82001-01-04 11:08:45 +00002416
Ron Yorston23286902018-02-25 20:09:54 +01002417#if ENABLE_FEATURE_EDITING_WINCH
Alexey Fomenko232ebaa2011-05-20 04:26:29 +02002418 /* Install window resize handler (NB: after *all* init is complete) */
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002419 S.SIGWINCH_handler.sa_handler = win_changed;
2420 S.SIGWINCH_handler.sa_flags = SA_RESTART;
2421 sigaction(SIGWINCH, &S.SIGWINCH_handler, &S.SIGWINCH_handler);
Ron Yorston23286902018-02-25 20:09:54 +01002422#endif
Denys Vlasenkoc396fe62009-05-17 19:28:14 +02002423 read_key_buffer[0] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002424 while (1) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002425 /*
2426 * The emacs and vi modes share much of the code in the big
2427 * command loop. Commands entered when in vi's command mode
2428 * (aka "escape mode") get an extra bit added to distinguish
2429 * them - this keeps them from being self-inserted. This
2430 * clutters the big switch a bit, but keeps all the code
2431 * in one place.
2432 */
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002433 int32_t ic, ic_raw;
Ron Yorston23286902018-02-25 20:09:54 +01002434#if ENABLE_FEATURE_EDITING_WINCH
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002435 unsigned count;
2436
2437 count = S.SIGWINCH_count;
2438 if (S.SIGWINCH_saved != count) {
2439 S.SIGWINCH_saved = count;
Denys Vlasenko038a9772016-11-28 01:10:16 +01002440 cmdedit_setwidth();
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002441 }
Ron Yorston23286902018-02-25 20:09:54 +01002442#endif
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002443 ic = ic_raw = lineedit_read_key(read_key_buffer, timeout);
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002444
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002445#if ENABLE_FEATURE_REVERSE_SEARCH
2446 again:
2447#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00002448#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00002449 newdelflag = 1;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002450 if (vi_cmdmode) {
2451 /* btw, since KEYCODE_xxx are all < 0, this doesn't
2452 * change ic if it contains one of them: */
2453 ic |= VI_CMDMODE_BIT;
2454 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00002455#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002456
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002457 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00002458 case '\n':
2459 case '\r':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002460 vi_case('\n'|VI_CMDMODE_BIT:)
2461 vi_case('\r'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002462 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002463 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00002464 break_out = 1;
2465 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002466 case CTRL('A'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002467 vi_case('0'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002468 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002469 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00002470 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002471 case CTRL('B'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002472 vi_case('h'|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002473 vi_case('\b'|VI_CMDMODE_BIT:) /* ^H */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002474 vi_case('\x7f'|VI_CMDMODE_BIT:) /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002475 input_backward(1); /* Move back one character */
Erik Andersenf0657d32000-04-12 17:49:52 +00002476 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002477 case CTRL('E'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002478 vi_case('$'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002479 /* Control-e -- End of line */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002480 put_till_end_and_adv_cursor();
Erik Andersenc7c634b2000-03-19 05:28:55 +00002481 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002482 case CTRL('F'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002483 vi_case('l'|VI_CMDMODE_BIT:)
2484 vi_case(' '|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002485 input_forward(); /* Move forward one character */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002486 break;
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002487 case '\b': /* ^H */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002488 case '\x7f': /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002489 if (!isrtl_str())
2490 input_backspace();
2491 else
2492 input_delete(0);
2493 break;
2494 case KEYCODE_DELETE:
2495 if (!isrtl_str())
2496 input_delete(0);
2497 else
2498 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00002499 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002500#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00002501 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002502 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002503 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002504#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002505 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00002506 /* Control-k -- clear to end of line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002507 command_ps[cursor] = BB_NUL;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002508 command_len = cursor;
Denys Vlasenko94043e82010-05-11 14:49:13 +02002509 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen65a07302002-04-13 13:26:49 +00002510 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002511 case CTRL('L'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002512 vi_case(CTRL('L')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002513 /* Control-l -- clear screen */
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002514 /* cursor to top,left; clear to the end of screen */
2515 printf(ESC"[H" ESC"[J");
2516 draw_full(command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00002517 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002518#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002519 case CTRL('N'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002520 vi_case(CTRL('N')|VI_CMDMODE_BIT:)
2521 vi_case('j'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002522 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002523 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002524 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00002525 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002526 case CTRL('P'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002527 vi_case(CTRL('P')|VI_CMDMODE_BIT:)
2528 vi_case('k'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002529 /* Control-p -- Get previous command from history */
Denis Vlasenko682ad302008-09-27 01:28:56 +00002530 if (get_previous_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002531 goto rewrite_line;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002532 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002533#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002534 case CTRL('U'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002535 vi_case(CTRL('U')|VI_CMDMODE_BIT:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002536 /* Control-U -- Clear line before cursor */
2537 if (cursor) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002538 command_len -= cursor;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002539 memmove(command_ps, command_ps + cursor,
2540 (command_len + 1) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002541 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002542 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002543 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002544 case CTRL('W'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002545 vi_case(CTRL('W')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002546 /* Control-W -- Remove the last word */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002547 while (cursor > 0 && BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002548 input_backspace();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002549 while (cursor > 0 && !BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002550 input_backspace();
2551 break;
Rostislav Skudnov2e4ef382016-11-24 15:04:00 +01002552 case KEYCODE_ALT_D: {
2553 /* Delete word forward */
2554 int nc, sc = cursor;
2555 ctrl_right();
2556 nc = cursor - sc;
2557 input_backward(nc);
2558 while (--nc >= 0)
2559 input_delete(1);
2560 break;
2561 }
2562 case KEYCODE_ALT_BACKSPACE: {
2563 /* Delete word backward */
2564 int sc = cursor;
2565 ctrl_left();
2566 while (sc-- > cursor)
2567 input_delete(1);
2568 break;
2569 }
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002570#if ENABLE_FEATURE_REVERSE_SEARCH
2571 case CTRL('R'):
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002572 ic = ic_raw = reverse_i_search(timeout);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002573 goto again;
2574#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002575
Denis Vlasenko38f63192007-01-22 09:03:07 +00002576#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002577 case 'i'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002578 vi_cmdmode = 0;
2579 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002580 case 'I'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002581 input_backward(cursor);
2582 vi_cmdmode = 0;
2583 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002584 case 'a'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002585 input_forward();
2586 vi_cmdmode = 0;
2587 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002588 case 'A'|VI_CMDMODE_BIT:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002589 put_till_end_and_adv_cursor();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002590 vi_cmdmode = 0;
2591 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002592 case 'x'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002593 input_delete(1);
2594 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002595 case 'X'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002596 if (cursor > 0) {
2597 input_backward(1);
2598 input_delete(1);
2599 }
2600 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002601 case 'W'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002602 vi_Word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002603 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002604 case 'w'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002605 vi_word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002606 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002607 case 'E'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002608 vi_End_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002609 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002610 case 'e'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002611 vi_end_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002612 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002613 case 'B'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002614 vi_Back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002615 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002616 case 'b'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002617 vi_back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002618 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002619 case 'C'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002620 vi_cmdmode = 0;
2621 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002622 case 'D'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002623 goto clear_to_eol;
2624
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002625 case 'c'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002626 vi_cmdmode = 0;
2627 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002628 case 'd'|VI_CMDMODE_BIT: {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002629 int nc, sc;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002630
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002631 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002632 if (errno) /* error */
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002633 goto return_error_indicator;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002634 if (ic == ic_raw) { /* "cc", "dd" */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002635 input_backward(cursor);
2636 goto clear_to_eol;
2637 break;
2638 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002639
2640 sc = cursor;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002641 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002642 case 'w':
2643 case 'W':
2644 case 'e':
2645 case 'E':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002646 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002647 case 'w': /* "dw", "cw" */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002648 vi_word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002649 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002650 case 'W': /* 'dW', 'cW' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002651 vi_Word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002652 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002653 case 'e': /* 'de', 'ce' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002654 vi_end_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002655 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002656 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002657 case 'E': /* 'dE', 'cE' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002658 vi_End_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002659 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002660 break;
2661 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002662 nc = cursor;
2663 input_backward(cursor - sc);
2664 while (nc-- > cursor)
2665 input_delete(1);
2666 break;
2667 case 'b': /* "db", "cb" */
2668 case 'B': /* implemented as B */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002669 if (ic == 'b')
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002670 vi_back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002671 else
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002672 vi_Back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002673 while (sc-- > cursor)
2674 input_delete(1);
2675 break;
2676 case ' ': /* "d ", "c " */
2677 input_delete(1);
2678 break;
2679 case '$': /* "d$", "c$" */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002680 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002681 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002682 input_delete(1);
2683 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002684 }
2685 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002686 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002687 case 'p'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002688 input_forward();
2689 /* fallthrough */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002690 case 'P'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002691 put();
2692 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002693 case 'r'|VI_CMDMODE_BIT:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002694//FIXME: unicode case?
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002695 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002696 if (errno) /* error */
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002697 goto return_error_indicator;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002698 if (ic < ' ' || ic > 255) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00002699 beep();
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002700 } else {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002701 command_ps[cursor] = ic;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002702 bb_putchar(ic);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002703 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00002704 }
2705 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002706 case '\x1b': /* ESC */
2707 if (state->flags & VI_MODE) {
2708 /* insert mode --> command mode */
2709 vi_cmdmode = 1;
2710 input_backward(1);
2711 }
2712 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002713#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002714
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002715#if MAX_HISTORY > 0
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002716 case KEYCODE_UP:
2717 if (get_previous_history())
2718 goto rewrite_line;
2719 beep();
2720 break;
2721 case KEYCODE_DOWN:
2722 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002723 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002724 rewrite_line:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002725 /* Rewrite the line with the selected history item */
2726 /* change command */
Denys Vlasenko90a99042009-09-06 02:36:23 +02002727 command_len = load_string(state->history[state->cur_history] ?
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002728 state->history[state->cur_history] : "");
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002729 /* redraw and go to eol (bol, in vi) */
2730 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
2731 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002732#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002733 case KEYCODE_RIGHT:
2734 input_forward();
2735 break;
2736 case KEYCODE_LEFT:
2737 input_backward(1);
2738 break;
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002739 case KEYCODE_CTRL_LEFT:
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002740 case KEYCODE_ALT_LEFT: /* bash doesn't do it */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002741 ctrl_left();
2742 break;
2743 case KEYCODE_CTRL_RIGHT:
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002744 case KEYCODE_ALT_RIGHT: /* bash doesn't do it */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002745 ctrl_right();
2746 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002747 case KEYCODE_HOME:
2748 input_backward(cursor);
2749 break;
2750 case KEYCODE_END:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002751 put_till_end_and_adv_cursor();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002752 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002753
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002754 default:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002755 if (initial_settings.c_cc[VINTR] != 0
2756 && ic_raw == initial_settings.c_cc[VINTR]
2757 ) {
2758 /* Ctrl-C (usually) - stop gathering input */
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002759 command_len = 0;
2760 break_out = -1; /* "do not append '\n'" */
2761 break;
2762 }
2763 if (initial_settings.c_cc[VEOF] != 0
2764 && ic_raw == initial_settings.c_cc[VEOF]
2765 ) {
2766 /* Ctrl-D (usually) - delete one character,
2767 * or exit if len=0 and no chars to delete */
2768 if (command_len == 0) {
2769 errno = 0;
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002770
2771 case -1: /* error (e.g. EIO when tty is destroyed) */
2772 IF_FEATURE_EDITING_VI(return_error_indicator:)
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002773 break_out = command_len = -1;
2774 break;
2775 }
2776 input_delete(0);
2777 break;
2778 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002779// /* Control-V -- force insert of next char */
2780// if (c == CTRL('V')) {
2781// if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002782// goto return_error_indicator;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002783// if (c == 0) {
2784// beep();
2785// break;
2786// }
2787// }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002788 if (ic < ' '
Denys Vlasenko19158a82010-03-26 14:06:56 +01002789 || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2790 || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002791 ) {
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002792 /* If VI_CMDMODE_BIT is set, ic is >= 256
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002793 * and vi mode ignores unexpected chars.
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002794 * Otherwise, we are here if ic is a
2795 * control char or an unhandled ESC sequence,
2796 * which is also ignored.
2797 */
2798 break;
2799 }
2800 if ((int)command_len >= (maxsize - 2)) {
2801 /* Not enough space for the char and EOL */
2802 break;
Paul Fox84bbac52008-01-11 16:50:08 +00002803 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002804
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002805 command_len++;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002806 if (cursor == (command_len - 1)) {
2807 /* We are at the end, append */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002808 command_ps[cursor] = ic;
2809 command_ps[cursor + 1] = BB_NUL;
Denys Vlasenko94043e82010-05-11 14:49:13 +02002810 put_cur_glyph_and_inc_cursor();
Tomas Heinrichaa167552010-03-26 13:13:24 +01002811 if (unicode_bidi_isrtl(ic))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002812 input_backward(1);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002813 } else {
2814 /* In the middle, insert */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002815 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002816
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002817 memmove(command_ps + sc + 1, command_ps + sc,
2818 (command_len - sc) * sizeof(command_ps[0]));
2819 command_ps[sc] = ic;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002820 /* is right-to-left char, or neutral one (e.g. comma) was just added to rtl text? */
2821 if (!isrtl_str())
2822 sc++; /* no */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002823 put_till_end_and_adv_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +00002824 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002825 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002826 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00002827 break;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002828 } /* switch (ic) */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002829
2830 if (break_out)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002831 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002832
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002833#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002834 if (ic_raw != '\t')
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002835 lastWasTab = 0;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002836#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002837 } /* while (1) */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002838
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002839#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002840 if (S.sent_ESC_br6n) {
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002841 /* "sleep 1; busybox ash" + hold [Enter] to trigger.
2842 * We sent "ESC [ 6 n", but got '\n' first, and
2843 * KEYCODE_CURSOR_POS response is now buffered from terminal.
2844 * It's bad already and not much can be done with it
2845 * (it _will_ be visible for the next process to read stdin),
2846 * but without this delay it even shows up on the screen
2847 * as garbage because we restore echo settings with tcsetattr
2848 * before it comes in. UGLY!
2849 */
2850 usleep(20*1000);
2851 }
2852#endif
2853
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002854/* End of bug-catching "command_must_not_be_used" trick */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002855#undef command
2856
Denys Vlasenko19158a82010-03-26 14:06:56 +01002857#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +02002858 command[0] = '\0';
2859 if (command_len > 0)
2860 command_len = save_string(command, maxsize - 1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002861 free(command_ps);
2862#endif
2863
Flemming Madsend96ffda2013-04-07 18:47:24 +02002864 if (command_len > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002865 remember_in_history(command);
Flemming Madsend96ffda2013-04-07 18:47:24 +02002866 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002867
Eric Andersen27bb7902003-12-23 20:24:51 +00002868 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002869 command[command_len++] = '\n';
2870 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00002871 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002872
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002873#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002874 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002875#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002876
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002877 /* restore initial_settings */
Denis Vlasenko202ac502008-11-05 13:20:58 +00002878 tcsetattr_stdin_TCSANOW(&initial_settings);
Ron Yorston23286902018-02-25 20:09:54 +01002879#if ENABLE_FEATURE_EDITING_WINCH
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002880 /* restore SIGWINCH handler */
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002881 sigaction_set(SIGWINCH, &S.SIGWINCH_handler);
Ron Yorston23286902018-02-25 20:09:54 +01002882#endif
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002883 fflush_all();
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002884
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002885 len = command_len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002886 DEINIT_S();
2887
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002888 return len; /* can't return command_len, DEINIT_S() destroys it */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002889}
2890
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002891#else /* !FEATURE_EDITING */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002892
2893#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00002894int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002895{
2896 fputs(prompt, stdout);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002897 fflush_all();
Denys Vlasenkob2320372012-09-27 16:03:49 +02002898 if (!fgets(command, maxsize, stdin))
2899 return -1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002900 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00002901}
2902
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002903#endif /* !FEATURE_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00002904
2905
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002906/*
2907 * Testing
2908 */
2909
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002910#ifdef TEST
2911
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002912#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002913
2914const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002915
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002916int main(int argc, char **argv)
2917{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00002918 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002919 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00002920#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002921 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
2922 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
Denys Vlasenko8187e012017-09-13 22:48:30 +02002923 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002924#else
2925 "% ";
2926#endif
2927
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002928 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00002929 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002930 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002931 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00002932 break;
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002933 buff[l-1] = '\0';
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002934 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00002935 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002936 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002937 return 0;
2938}
2939
Eric Andersenc470f442003-07-28 09:56:35 +00002940#endif /* TEST */