blob: b942f540ad36ca9f02328895dfe4b5ffab4290c3 [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 Vlasenkoa97a7952020-12-16 11:14:08 +010060#if !ENABLE_SHELL_ASH && !ENABLE_SHELL_HUSH
61/* so far only shells use these features */
62# undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
63# undef ENABLE_FEATURE_TAB_COMPLETION
64# undef ENABLE_FEATURE_USERNAME_COMPLETION
65# define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
66# define ENABLE_FEATURE_TAB_COMPLETION 0
67# define ENABLE_FEATURE_USERNAME_COMPLETION 0
68#endif
69
70
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +020071#define ENABLE_USERNAME_OR_HOMEDIR \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000072 (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +020073#if ENABLE_USERNAME_OR_HOMEDIR
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +020074# define IF_USERNAME_OR_HOMEDIR(...) __VA_ARGS__
Denys Vlasenkoa97a7952020-12-16 11:14:08 +010075#else
76# define IF_USERNAME_OR_HOMEDIR(...) /*nothing*/
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000077#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000078
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020079
80#undef CHAR_T
Denys Vlasenko19158a82010-03-26 14:06:56 +010081#if ENABLE_UNICODE_SUPPORT
Tomas Heinricha659b812010-04-29 13:43:39 +020082# define BB_NUL ((wchar_t)0)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020083# define CHAR_T wchar_t
Denys Vlasenko8c317f02019-05-14 17:26:47 +020084static bool BB_isspace(CHAR_T c)
85{
86 return ((unsigned)c < 256 && isspace(c));
87}
Denys Vlasenko31e2e7b2009-12-12 02:42:35 +010088# if ENABLE_FEATURE_EDITING_VI
Denys Vlasenko8c317f02019-05-14 17:26:47 +020089static bool BB_isalnum_or_underscore(CHAR_T c)
90{
Natanael Copa7e6f9312016-08-14 23:30:29 +020091 return ((unsigned)c < 256 && isalnum(c)) || c == '_';
92}
Denys Vlasenko31e2e7b2009-12-12 02:42:35 +010093# endif
Denys Vlasenko8c317f02019-05-14 17:26:47 +020094static bool BB_ispunct(CHAR_T c)
95{
96 return ((unsigned)c < 256 && ispunct(c));
97}
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020098# undef isspace
99# undef isalnum
100# undef ispunct
101# undef isprint
102# define isspace isspace_must_not_be_used
103# define isalnum isalnum_must_not_be_used
104# define ispunct ispunct_must_not_be_used
105# define isprint isprint_must_not_be_used
106#else
107# define BB_NUL '\0'
108# define CHAR_T char
109# define BB_isspace(c) isspace(c)
Natanael Copa7e6f9312016-08-14 23:30:29 +0200110# if ENABLE_FEATURE_EDITING_VI
Denys Vlasenko82d1c1f2017-12-31 17:30:02 +0100111static bool BB_isalnum_or_underscore(CHAR_T c)
112{
Denys Vlasenkod5314e72020-06-24 09:31:30 +0200113 return isalnum(c) || c == '_';
Natanael Copa7e6f9312016-08-14 23:30:29 +0200114}
115# endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200116# define BB_ispunct(c) ispunct(c)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200117#endif
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200118#if ENABLE_UNICODE_PRESERVE_BROKEN
119# define unicode_mark_raw_byte(wc) ((wc) | 0x20000000)
120# define unicode_is_raw_byte(wc) ((wc) & 0x20000000)
121#else
122# define unicode_is_raw_byte(wc) 0
123#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200124
125
Marek Polacek7b181072010-10-28 21:34:56 +0200126#define ESC "\033"
127
128#define SEQ_CLEAR_TILL_END_OF_SCREEN ESC"[J"
129//#define SEQ_CLEAR_TILL_END_OF_LINE ESC"[K"
Tomas Heinricha659b812010-04-29 13:43:39 +0200130
131
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000132enum {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000133 MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN < 0x7ff0
Denis Vlasenko6b404432008-01-07 16:13:14 +0000134 ? CONFIG_FEATURE_EDITING_MAX_LEN
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000135 : 0x7ff0
136};
Robert Griebl350d26b2002-12-03 22:45:46 +0000137
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000138/* We try to minimize both static and stack usage. */
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000139struct lineedit_statics {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000140 line_input_t *state;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000141
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100142 unsigned cmdedit_termw; /* = 80; */ /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +0000143
Denys Vlasenko020f4062009-05-17 16:44:54 +0200144 unsigned cmdedit_x; /* real x (col) terminal position */
145 unsigned cmdedit_y; /* pseudoreal y (row) terminal position */
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200146 unsigned cmdedit_prmt_len; /* on-screen length of last/sole prompt line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000147
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000148 unsigned cursor;
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +0200149 int command_len; /* must be signed */
150 /* signed maxsize: we want x in "if (x > S.maxsize)"
Denys Vlasenko044b1802009-07-12 02:50:35 +0200151 * to _not_ be promoted to unsigned */
152 int maxsize;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200153 CHAR_T *command_ps;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000154
155 const char *cmdedit_prompt;
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200156 const char *prompt_last_line; /* last/sole prompt line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000157
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200158#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000159 char *user_buf;
Denys Vlasenkof4fcd742021-10-11 23:08:31 +0200160 char *home_pwd_buf;
161 smallint got_user_strings;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000162#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000163
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000164#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000165 unsigned num_matches;
Denys Vlasenkof4fcd742021-10-11 23:08:31 +0200166 char **matches;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000167#endif
168
Ron Yorston23286902018-02-25 20:09:54 +0100169#if ENABLE_FEATURE_EDITING_WINCH
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100170 unsigned SIGWINCH_saved;
171 volatile unsigned SIGWINCH_count;
172 volatile smallint ok_to_redraw;
Ron Yorston23286902018-02-25 20:09:54 +0100173#endif
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100174
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000175#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200176# define DELBUFSIZ 128
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000177 smallint newdelflag; /* whether delbuf should be reused yet */
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100178 CHAR_T *delptr;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200179 CHAR_T delbuf[DELBUFSIZ]; /* a place to store deleted characters */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000180#endif
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100181#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +0100182 smallint sent_ESC_br6n;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100183#endif
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100184
Ron Yorston23286902018-02-25 20:09:54 +0100185#if ENABLE_FEATURE_EDITING_WINCH
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100186 /* Largish struct, keeping it last results in smaller code */
187 struct sigaction SIGWINCH_handler;
Ron Yorston23286902018-02-25 20:09:54 +0100188#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000189};
190
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000191/* See lineedit_ptr_hack.c */
YU Jincheng1f925032021-09-29 17:37:26 +0800192extern struct lineedit_statics *BB_GLOBAL_CONST lineedit_ptr_to_statics;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000193
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000194#define S (*lineedit_ptr_to_statics)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000195#define state (S.state )
196#define cmdedit_termw (S.cmdedit_termw )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000197#define cmdedit_x (S.cmdedit_x )
198#define cmdedit_y (S.cmdedit_y )
199#define cmdedit_prmt_len (S.cmdedit_prmt_len)
200#define cursor (S.cursor )
201#define command_len (S.command_len )
202#define command_ps (S.command_ps )
203#define cmdedit_prompt (S.cmdedit_prompt )
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200204#define prompt_last_line (S.prompt_last_line)
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000205#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000206#define home_pwd_buf (S.home_pwd_buf )
Denys Vlasenkof4fcd742021-10-11 23:08:31 +0200207#define got_user_strings (S.got_user_strings)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000208#define num_matches (S.num_matches )
Denys Vlasenkof4fcd742021-10-11 23:08:31 +0200209#define matches (S.matches )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000210#define delptr (S.delptr )
211#define newdelflag (S.newdelflag )
212#define delbuf (S.delbuf )
213
214#define INIT_S() do { \
YU Jincheng5156b242021-10-10 02:19:51 +0800215 XZALLOC_CONST_PTR(&lineedit_ptr_to_statics, sizeof(S)); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000216} while (0)
Alexey Fomenko232ebaa2011-05-20 04:26:29 +0200217
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000218static void deinit_S(void)
219{
220#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000221 /* This one is allocated only if FANCY_PROMPT is on
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200222 * (otherwise it points to verbatim prompt (NOT malloced)) */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000223 free((char*)cmdedit_prompt);
224#endif
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200225#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000226 free(user_buf);
Denys Vlasenkof4fcd742021-10-11 23:08:31 +0200227 free(home_pwd_buf);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000228#endif
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000229 free(lineedit_ptr_to_statics);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000230}
231#define DEINIT_S() deinit_S()
232
Denis Vlasenko2c844952008-04-25 18:44:35 +0000233
Denys Vlasenkof4fcd742021-10-11 23:08:31 +0200234#if ENABLE_USERNAME_OR_HOMEDIR
235/* Call getpwuid() only if necessary.
236 * E.g. if PS1=':', no user database reading is needed to generate prompt.
237 * (Unfortunately, default PS1='\w \$' needs it, \w abbreviates homedir
238 * as ~/... - for that it needs to *know* the homedir...)
239 */
240static void get_user_strings(void)
241{
242 struct passwd *entry;
243
244 got_user_strings = 1;
245 entry = getpwuid(geteuid());
246 if (entry) {
247 user_buf = xstrdup(entry->pw_name);
248 home_pwd_buf = xstrdup(entry->pw_dir);
249 }
250}
251
Denys Vlasenkof4fcd742021-10-11 23:08:31 +0200252static NOINLINE const char *get_homedir_or_NULL(void)
253{
Ron Yorston7d1c7d82022-03-24 12:17:25 +0000254 const char *home;
255
256# if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH
257 home = state->sh_get_var ? state->sh_get_var("HOME") : getenv("HOME");
258# else
259 home = getenv("HOME");
260# endif
261 if (home != NULL && home[0] != '\0')
262 return home;
263
Denys Vlasenkof4fcd742021-10-11 23:08:31 +0200264 if (!got_user_strings)
265 get_user_strings();
266 return home_pwd_buf;
267}
268#endif
269
Denys Vlasenko27be0e82023-01-03 08:28:16 +0100270#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
271static const char *get_username_str(void)
272{
273 if (!got_user_strings)
274 get_user_strings();
275 return user_buf ? user_buf : "";
276 /* btw, bash uses "I have no name!" string if uid has no entry */
277}
278#endif
279
Denys Vlasenko19158a82010-03-26 14:06:56 +0100280#if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200281static size_t load_string(const char *src)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200282{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100283 if (unicode_status == UNICODE_ON) {
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200284 ssize_t len = mbstowcs(command_ps, src, S.maxsize - 1);
Denys Vlasenko353680a2011-03-27 01:18:07 +0100285 if (len < 0)
286 len = 0;
287 command_ps[len] = BB_NUL;
288 return len;
289 } else {
290 unsigned i = 0;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200291 while (src[i] && i < S.maxsize - 1) {
292 command_ps[i] = src[i];
Denys Vlasenko353680a2011-03-27 01:18:07 +0100293 i++;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200294 }
295 command_ps[i] = BB_NUL;
Denys Vlasenko353680a2011-03-27 01:18:07 +0100296 return i;
297 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200298}
Tomas Heinricha659b812010-04-29 13:43:39 +0200299static unsigned save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200300{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100301 if (unicode_status == UNICODE_ON) {
Denys Vlasenko94043e82010-05-11 14:49:13 +0200302# if !ENABLE_UNICODE_PRESERVE_BROKEN
Denys Vlasenko353680a2011-03-27 01:18:07 +0100303 ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
304 if (len < 0)
305 len = 0;
306 dst[len] = '\0';
307 return len;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200308# else
Denys Vlasenko353680a2011-03-27 01:18:07 +0100309 unsigned dstpos = 0;
310 unsigned srcpos = 0;
Tomas Heinricha659b812010-04-29 13:43:39 +0200311
Denys Vlasenko353680a2011-03-27 01:18:07 +0100312 maxsize--;
313 while (dstpos < maxsize) {
314 wchar_t wc;
315 int n = srcpos;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200316
Denys Vlasenko353680a2011-03-27 01:18:07 +0100317 /* Convert up to 1st invalid byte (or up to end) */
318 while ((wc = command_ps[srcpos]) != BB_NUL
319 && !unicode_is_raw_byte(wc)
320 ) {
321 srcpos++;
322 }
323 command_ps[srcpos] = BB_NUL;
324 n = wcstombs(dst + dstpos, command_ps + n, maxsize - dstpos);
325 if (n < 0) /* should not happen */
326 break;
327 dstpos += n;
328 if (wc == BB_NUL) /* usually is */
329 break;
330
331 /* We do have invalid byte here! */
332 command_ps[srcpos] = wc; /* restore it */
Tomas Heinricha659b812010-04-29 13:43:39 +0200333 srcpos++;
Denys Vlasenko353680a2011-03-27 01:18:07 +0100334 if (dstpos == maxsize)
335 break;
336 dst[dstpos++] = (char) wc;
Tomas Heinricha659b812010-04-29 13:43:39 +0200337 }
Denys Vlasenko353680a2011-03-27 01:18:07 +0100338 dst[dstpos] = '\0';
339 return dstpos;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200340# endif
Denys Vlasenko353680a2011-03-27 01:18:07 +0100341 } else {
342 unsigned i = 0;
343 while ((dst[i] = command_ps[i]) != 0)
344 i++;
345 return i;
346 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200347}
348/* I thought just fputwc(c, stdout) would work. But no... */
349static void BB_PUTCHAR(wchar_t c)
350{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100351 if (unicode_status == UNICODE_ON) {
352 char buf[MB_CUR_MAX + 1];
353 mbstate_t mbst = { 0 };
354 ssize_t len = wcrtomb(buf, c, &mbst);
355 if (len > 0) {
356 buf[len] = '\0';
Ron Yorstoncad3fc72021-02-03 20:47:14 +0100357 fputs_stdout(buf);
Denys Vlasenko353680a2011-03-27 01:18:07 +0100358 }
359 } else {
360 /* In this case, c is always one byte */
361 putchar(c);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200362 }
363}
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200364# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
365static wchar_t adjust_width_and_validate_wc(unsigned *width_adj, wchar_t wc)
366# else
367static wchar_t adjust_width_and_validate_wc(wchar_t wc)
368# define adjust_width_and_validate_wc(width_adj, wc) \
369 ((*(width_adj))++, adjust_width_and_validate_wc(wc))
370# endif
371{
372 int w = 1;
373
374 if (unicode_status == UNICODE_ON) {
Denys Vlasenko26e2c1d2010-05-16 21:15:03 +0200375 if (wc > CONFIG_LAST_SUPPORTED_WCHAR) {
376 /* note: also true for unicode_is_raw_byte(wc) */
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200377 goto subst;
378 }
379 w = wcwidth(wc);
380 if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0)
381 || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
382 || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
383 ) {
384 subst:
385 w = 1;
386 wc = CONFIG_SUBST_WCHAR;
387 }
388 }
389
390# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
391 *width_adj += w;
392#endif
393 return wc;
394}
395#else /* !UNICODE */
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200396static size_t load_string(const char *src)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200397{
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200398 safe_strncpy(command_ps, src, S.maxsize);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200399 return strlen(command_ps);
400}
Denys Vlasenko9038d6f2009-07-15 20:02:19 +0200401# if ENABLE_FEATURE_TAB_COMPLETION
Tomas Heinricha659b812010-04-29 13:43:39 +0200402static void save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200403{
404 safe_strncpy(dst, command_ps, maxsize);
405}
Denys Vlasenko7dd0ce42009-07-15 18:27:47 +0200406# endif
407# define BB_PUTCHAR(c) bb_putchar(c)
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200408/* Should never be called: */
409int adjust_width_and_validate_wc(unsigned *width_adj, int wc);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200410#endif
411
412
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000413/* Put 'command_ps[cursor]', cursor++.
414 * Advance cursor on screen. If we reached right margin, scroll text up
415 * and remove terminal margin effect by printing 'next_char' */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000416#define HACK_FOR_WRONG_WIDTH 1
Denys Vlasenko94043e82010-05-11 14:49:13 +0200417static void put_cur_glyph_and_inc_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000418{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200419 CHAR_T c = command_ps[cursor];
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200420 unsigned width = 0;
421 int ofs_to_right;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000422
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200423 if (c == BB_NUL) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000424 /* erase character after end of input string */
425 c = ' ';
Denys Vlasenko94043e82010-05-11 14:49:13 +0200426 } else {
427 /* advance cursor only if we aren't at the end yet */
428 cursor++;
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200429 if (unicode_status == UNICODE_ON) {
430 IF_UNICODE_WIDE_WCHARS(width = cmdedit_x;)
431 c = adjust_width_and_validate_wc(&cmdedit_x, c);
432 IF_UNICODE_WIDE_WCHARS(width = cmdedit_x - width;)
433 } else {
434 cmdedit_x++;
435 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000436 }
Denys Vlasenko94043e82010-05-11 14:49:13 +0200437
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200438 ofs_to_right = cmdedit_x - cmdedit_termw;
439 if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right <= 0) {
440 /* c fits on this line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200441 BB_PUTCHAR(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000442 }
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200443
444 if (ofs_to_right >= 0) {
445 /* we go to the next line */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000446#if HACK_FOR_WRONG_WIDTH
447 /* This works better if our idea of term width is wrong
448 * and it is actually wider (often happens on serial lines).
449 * Printing CR,LF *forces* cursor to next line.
450 * OTOH if terminal width is correct AND terminal does NOT
451 * have automargin (IOW: it is moving cursor to next line
452 * by itself (which is wrong for VT-10x terminals)),
453 * this will break things: there will be one extra empty line */
454 puts("\r"); /* + implicit '\n' */
455#else
Denys Vlasenko94043e82010-05-11 14:49:13 +0200456 /* VT-10x terminals don't wrap cursor to next line when last char
457 * on the line is printed - cursor stays "over" this char.
458 * Need to print _next_ char too (first one to appear on next line)
459 * to make cursor move down to next line.
460 */
461 /* Works ok only if cmdedit_termw is correct. */
462 c = command_ps[cursor];
463 if (c == BB_NUL)
464 c = ' ';
465 BB_PUTCHAR(c);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000466 bb_putchar('\b');
Denis Vlasenko2c844952008-04-25 18:44:35 +0000467#endif
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200468 cmdedit_y++;
469 if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right == 0) {
470 width = 0;
471 } else { /* ofs_to_right > 0 */
472 /* wide char c didn't fit on prev line */
473 BB_PUTCHAR(c);
474 }
475 cmdedit_x = width;
Mark Whitley4e338752001-01-26 20:42:23 +0000476 }
Erik Andersen13456d12000-03-16 08:09:57 +0000477}
478
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000479/* Move to end of line (by printing all chars till the end) */
Denys Vlasenko94043e82010-05-11 14:49:13 +0200480static void put_till_end_and_adv_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000481{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000482 while (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200483 put_cur_glyph_and_inc_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +0000484}
485
486/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000487static void goto_new_line(void)
488{
Denys Vlasenko94043e82010-05-11 14:49:13 +0200489 put_till_end_and_adv_cursor();
Denys Vlasenkof5018da2018-04-06 17:58:21 +0200490 /* "cursor == 0" is only if prompt is "" and user input is empty */
491 if (cursor == 0 || cmdedit_x != 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000492 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000493}
494
Rob Landley88621d72006-08-29 19:41:06 +0000495static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000496{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000497 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000498}
499
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200500/* Full or last/sole prompt line, reset edit cursor, calculate terminal cursor.
501 * cmdedit_y is always calculated for the last/sole prompt line.
502 */
503static void put_prompt_custom(bool is_full)
Denys Vlasenko248c3242010-05-17 00:45:44 +0200504{
Ron Yorstoncad3fc72021-02-03 20:47:14 +0100505 fputs_stdout((is_full ? cmdedit_prompt : prompt_last_line));
Denys Vlasenko248c3242010-05-17 00:45:44 +0200506 cursor = 0;
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100507 cmdedit_y = cmdedit_prmt_len / cmdedit_termw; /* new quasireal y */
508 cmdedit_x = cmdedit_prmt_len % cmdedit_termw;
Denys Vlasenko248c3242010-05-17 00:45:44 +0200509}
510
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200511#define put_prompt_last_line() put_prompt_custom(0)
512#define put_prompt() put_prompt_custom(1)
513
Eric Andersenaff114c2004-04-14 17:51:38 +0000514/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000515/* (optimized for slow terminals) */
516static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000517{
518 if (num > cursor)
519 num = cursor;
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200520 if (num == 0)
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000521 return;
522 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000523
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200524 if ((ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS)
525 && unicode_status == UNICODE_ON
526 ) {
527 /* correct NUM to be equal to _screen_ width */
528 int n = num;
529 num = 0;
530 while (--n >= 0)
531 adjust_width_and_validate_wc(&num, command_ps[cursor + n]);
532 if (num == 0)
533 return;
534 }
535
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000536 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000537 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000538 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000539 /* This is longer by 5 bytes on x86.
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000540 * Also gets miscompiled for ARM users
541 * (busybox.net/bugs/view.php?id=2274).
Denis Vlasenko6f043912008-02-18 22:28:03 +0000542 * printf(("\b\b\b\b" + 4) - num);
543 * return;
544 */
545 do {
546 bb_putchar('\b');
547 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000548 return;
549 }
Marek Polacek7b181072010-10-28 21:34:56 +0200550 printf(ESC"[%uD", num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000551 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000552 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000553
554 /* Need to go one or more lines up */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200555 if (ENABLE_UNICODE_WIDE_WCHARS) {
556 /* With wide chars, it is hard to "backtrack"
557 * and reliably figure out where to put cursor.
558 * Example (<> is a wide char; # is an ordinary char, _ cursor):
559 * |prompt: <><> |
560 * |<><><><><><> |
561 * |_ |
562 * and user presses left arrow. num = 1, cmdedit_x = 0,
563 * We need to go up one line, and then - how do we know that
564 * we need to go *10* positions to the right? Because
565 * |prompt: <>#<>|
566 * |<><><>#<><><>|
567 * |_ |
568 * in this situation we need to go *11* positions to the right.
569 *
570 * A simpler thing to do is to redraw everything from the start
571 * up to new cursor position (which is already known):
572 */
573 unsigned sv_cursor;
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200574 /* go to 1st column; go up to first line */
Marek Polacek7b181072010-10-28 21:34:56 +0200575 printf("\r" ESC"[%uA", cmdedit_y);
Denys Vlasenko248c3242010-05-17 00:45:44 +0200576 cmdedit_y = 0;
577 sv_cursor = cursor;
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200578 put_prompt_last_line(); /* sets cursor to 0 */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200579 while (cursor < sv_cursor)
580 put_cur_glyph_and_inc_cursor();
581 } else {
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200582 int lines_up;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200583 /* num = chars to go back from the beginning of current line: */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200584 num -= cmdedit_x;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200585 /* num=1...w: one line up, w+1...2w: two, etc: */
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100586 lines_up = 1 + (num - 1) / cmdedit_termw;
587 cmdedit_x = (cmdedit_termw * cmdedit_y - num) % cmdedit_termw;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200588 cmdedit_y -= lines_up;
589 /* go to 1st column; go up */
Marek Polacek7b181072010-10-28 21:34:56 +0200590 printf("\r" ESC"[%uA", lines_up);
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200591 /* go to correct column.
Denys Vlasenkobbf1aa12010-05-17 12:33:13 +0200592 * xterm, konsole, Linux VT interpret 0 as 1 below! wow.
593 * need to *make sure* we skip it if cmdedit_x == 0 */
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200594 if (cmdedit_x)
Marek Polacek7b181072010-10-28 21:34:56 +0200595 printf(ESC"[%uC", cmdedit_x);
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000596 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000597}
598
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200599/* See redraw and draw_full below */
600static void draw_custom(int y, int back_cursor, bool is_full)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000601{
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200602 if (y > 0) /* up y lines */
Marek Polacek7b181072010-10-28 21:34:56 +0200603 printf(ESC"[%uA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000604 bb_putchar('\r');
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200605 put_prompt_custom(is_full);
Denys Vlasenko94043e82010-05-11 14:49:13 +0200606 put_till_end_and_adv_cursor();
607 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000608 input_backward(back_cursor);
609}
610
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200611/* Move y lines up, draw last/sole prompt line, editor line[s], and clear tail.
612 * goal: redraw the prompt+input+cursor in-place, overwriting the previous */
613#define redraw(y, back_cursor) draw_custom((y), (back_cursor), 0)
614
615/* Like above, but without moving up, and while using all the prompt lines.
616 * goal: draw a full prompt+input+cursor unrelated to a previous position.
617 * note: cmdedit_y always ends up relating to the last/sole prompt line */
618#define draw_full(back_cursor) draw_custom(0, (back_cursor), 1)
619
Paul Fox3f11b1b2005-08-04 19:04:46 +0000620/* Delete the char in front of the cursor, optionally saving it
621 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000622#if !ENABLE_FEATURE_EDITING_VI
623static void input_delete(void)
624#define input_delete(save) input_delete()
625#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000626static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000627#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000628{
Mark Whitley4e338752001-01-26 20:42:23 +0000629 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000630
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000631 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000632 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000633
Denis Vlasenko38f63192007-01-22 09:03:07 +0000634#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000635 if (save) {
636 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000637 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000638 newdelflag = 0;
639 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000640 if ((delptr - delbuf) < DELBUFSIZ)
641 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000642 }
643#endif
644
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200645 memmove(command_ps + j, command_ps + j + 1,
Denys Vlasenko9531f7d2009-07-16 14:33:16 +0200646 /* (command_len + 1 [because of NUL]) - (j + 1)
647 * simplified into (command_len - j) */
648 (command_len - j) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000649 command_len--;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200650 put_till_end_and_adv_cursor();
651 /* Last char is still visible, erase it (and more) */
652 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersenc470f442003-07-28 09:56:35 +0000653 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000654}
655
Denis Vlasenko38f63192007-01-22 09:03:07 +0000656#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000657static void put(void)
658{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000659 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000660 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000661
Paul Fox3f11b1b2005-08-04 19:04:46 +0000662 if (j == 0)
663 return;
664 ocursor = cursor;
665 /* open hole and then fill it */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200666 memmove(command_ps + cursor + j, command_ps + cursor,
667 (command_len - cursor + 1) * sizeof(command_ps[0]));
668 memcpy(command_ps + cursor, delbuf, j * sizeof(command_ps[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000669 command_len += j;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200670 put_till_end_and_adv_cursor();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000671 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000672}
673#endif
674
Mark Whitley4e338752001-01-26 20:42:23 +0000675/* Delete the char in back of the cursor */
676static void input_backspace(void)
677{
678 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000679 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000680 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000681 }
682}
683
Eric Andersenaff114c2004-04-14 17:51:38 +0000684/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000685static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000686{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000687 if (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200688 put_cur_glyph_and_inc_cursor();
Erik Andersenf0657d32000-04-12 17:49:52 +0000689}
690
Denis Vlasenko38f63192007-01-22 09:03:07 +0000691#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000692
Mike Shalf3763032010-11-22 03:49:18 +0100693//FIXME:
694//needs to be more clever: currently it thinks that "foo\ b<TAB>
695//matches the file named "foo bar", which is untrue.
696//Also, perhaps "foo b<TAB> needs to complete to "foo bar" <cursor>,
697//not "foo bar <cursor>...
698
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000699static void free_tab_completion_data(void)
700{
701 if (matches) {
702 while (num_matches)
703 free(matches[--num_matches]);
704 free(matches);
705 matches = NULL;
706 }
707}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000708
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000709static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000710{
Denys Vlasenkoc3797d42017-11-07 18:09:29 +0100711 unsigned char *p = (unsigned char*)matched;
712 while (*p) {
713 /* ESC attack fix: drop any string with control chars */
714 if (*p < ' '
715 || (!ENABLE_UNICODE_SUPPORT && *p >= 0x7f)
716 || (ENABLE_UNICODE_SUPPORT && *p == 0x7f)
717 ) {
718 free(matched);
719 return;
720 }
721 p++;
722 }
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000723 matches = xrealloc_vector(matches, 4, num_matches);
724 matches[num_matches] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000725 num_matches++;
726}
727
Mike Shalf3763032010-11-22 03:49:18 +0100728# if ENABLE_FEATURE_USERNAME_COMPLETION
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200729/* Replace "~user/..." with "/homedir/...".
730 * The parameter is malloced, free it or return it
731 * unchanged if no user is matched.
732 */
733static char *username_path_completion(char *ud)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000734{
735 struct passwd *entry;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200736 char *tilde_name = ud;
Denys Vlasenkof4fcd742021-10-11 23:08:31 +0200737 const char *home = NULL;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200738
739 ud++; /* skip ~ */
740 if (*ud == '/') { /* "~/..." */
Denys Vlasenkof4fcd742021-10-11 23:08:31 +0200741 home = get_homedir_or_NULL();
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200742 } else {
743 /* "~user/..." */
744 ud = strchr(ud, '/');
745 *ud = '\0'; /* "~user" */
746 entry = getpwnam(tilde_name + 1);
747 *ud = '/'; /* restore "~user/..." */
748 if (entry)
749 home = entry->pw_dir;
750 }
751 if (home) {
752 ud = concat_path_file(home, ud);
753 free(tilde_name);
754 tilde_name = ud;
755 }
756 return tilde_name;
757}
758
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200759/* ~use<tab> - find all users with this prefix.
760 * Return the length of the prefix used for matching.
761 */
762static NOINLINE unsigned complete_username(const char *ud)
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200763{
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100764 struct passwd *pw;
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200765 unsigned userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000766
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200767 ud++; /* skip ~ */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000768 userlen = strlen(ud);
769
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200770 setpwent();
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100771 while ((pw = getpwent()) != NULL) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200772 /* Null usernames should result in all users as possible completions. */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100773 if (/* !ud[0] || */ is_prefixed_with(pw->pw_name, ud)) {
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100774 add_match(xasprintf("~%s/", pw->pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000775 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000776 }
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100777 endpwent(); /* don't keep password file open */
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200778
779 return 1 + userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000780}
Mike Shalf3763032010-11-22 03:49:18 +0100781# endif /* FEATURE_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000782
783enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000784 FIND_EXE_ONLY = 0,
785 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000786 FIND_FILE_ONLY = 2,
787};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000788
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100789static unsigned path_parse(char ***p)
Mark Whitley4e338752001-01-26 20:42:23 +0000790{
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100791 unsigned npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000792 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000793 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000794 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000795
Denys Vlasenkoa97a7952020-12-16 11:14:08 +0100796# if EDITING_HAS_path_lookup
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000797 if (state->flags & WITH_PATH_LOOKUP)
798 pth = state->path_lookup;
799 else
Denys Vlasenkoa97a7952020-12-16 11:14:08 +0100800# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000801 pth = getenv("PATH");
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200802
803 /* PATH="" or PATH=":"? */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000804 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
805 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000806
Denis Vlasenko253ce002007-01-22 08:34:44 +0000807 tmp = (char*)pth;
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100808 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000809 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000810 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000811 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000812 break;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200813 tmp++;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000814 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000815 }
816
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100817 *p = res = xzalloc((npth + 1) * sizeof(res[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000818 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000819 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000820 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000821 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000822 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000823 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000824 *tmp++ = '\0'; /* ':' -> '\0' */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000825 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000826 }
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100827 /* special case: "match subdirectories of the current directory" */
828 /*res[npth++] = NULL; - filled by xzalloc() */
Mark Whitley4e338752001-01-26 20:42:23 +0000829 return npth;
830}
831
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200832/* Complete command, directory or file name.
833 * Return the length of the prefix used for matching.
834 */
835static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000836{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000837 char *path1[1];
838 char **paths = path1;
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100839 unsigned npaths;
840 unsigned i;
841 unsigned baselen;
842 const char *basecmd;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200843 char *dirbuf = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +0000844
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000845 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000846 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000847
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100848 basecmd = strrchr(command, '/');
849 if (!basecmd) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200850 if (type == FIND_EXE_ONLY)
851 npaths = path_parse(&paths);
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100852 basecmd = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000853 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000854 /* point to 'l' in "..../last_component" */
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100855 basecmd++;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200856 /* dirbuf = ".../.../.../" */
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100857 dirbuf = xstrndup(command, basecmd - command);
Mike Shalf3763032010-11-22 03:49:18 +0100858# if ENABLE_FEATURE_USERNAME_COMPLETION
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200859 if (dirbuf[0] == '~') /* ~/... or ~user/... */
860 dirbuf = username_path_completion(dirbuf);
Mike Shalf3763032010-11-22 03:49:18 +0100861# endif
Denys Vlasenko7063e862010-09-02 12:44:39 +0200862 path1[0] = dirbuf;
Mark Whitley4e338752001-01-26 20:42:23 +0000863 }
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100864 baselen = strlen(basecmd);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000865
Denys Vlasenko13360522016-10-24 01:25:05 +0200866 if (type == FIND_EXE_ONLY && !dirbuf) {
Ron Yorston9e2a5662020-01-21 16:01:58 +0000867# if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1
Ron Yorston37788982018-11-17 17:48:14 +0000868 const char *p = applet_names;
Ron Yorston2b919582016-04-08 11:57:20 +0100869 while (*p) {
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100870 if (strncmp(basecmd, p, baselen) == 0)
Ron Yorstonf23264b2015-05-29 11:31:40 +0100871 add_match(xstrdup(p));
Ron Yorston37788982018-11-17 17:48:14 +0000872 while (*p++ != '\0')
Ron Yorston2b919582016-04-08 11:57:20 +0100873 continue;
Ron Yorstonf23264b2015-05-29 11:31:40 +0100874 }
Denys Vlasenkof128bdb2017-07-29 00:59:24 +0200875# endif
Ron Yorston7d1c7d82022-03-24 12:17:25 +0000876# if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH
Ron Yorston9e2a5662020-01-21 16:01:58 +0000877 if (state->get_exe_name) {
878 i = 0;
879 for (;;) {
880 const char *b = state->get_exe_name(i++);
881 if (!b)
882 break;
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100883 if (strncmp(basecmd, b, baselen) == 0)
Ron Yorston9e2a5662020-01-21 16:01:58 +0000884 add_match(xstrdup(b));
885 }
886 }
887# endif
888 }
Ron Yorstonf23264b2015-05-29 11:31:40 +0100889
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000890 for (i = 0; i < npaths; i++) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200891 DIR *dir;
892 struct dirent *next;
893 struct stat st;
894 char *found;
Ron Yorston760b6272021-02-18 09:50:29 +0000895 const char *lpath;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200896
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100897 if (paths[i] == NULL) { /* path_parse()'s last component? */
898 /* in PATH completion, current dir's subdir names
899 * can be completions (but only subdirs, not files).
900 */
Ron Yorston8baa6432020-12-11 12:34:21 +0000901 type = FIND_DIR_ONLY;
902 paths[i] = (char *)".";
903 }
904
Ron Yorston760b6272021-02-18 09:50:29 +0000905 lpath = *paths[i] ? paths[i] : ".";
906 dir = opendir(lpath);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000907 if (!dir)
908 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000909
910 while ((next = readdir(dir)) != NULL) {
Denys Vlasenko39263632010-09-03 14:11:08 +0200911 unsigned len;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200912 const char *name_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000913
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200914 /* .../<tab>: bash 3.2.0 shows dotfiles, but not . and .. */
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100915 if (!basecmd[0] && DOT_OR_DOTDOT(name_found))
Mark Whitley4e338752001-01-26 20:42:23 +0000916 continue;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200917 /* match? */
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100918 if (strncmp(basecmd, name_found, baselen) != 0)
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200919 continue; /* no */
920
Ron Yorston760b6272021-02-18 09:50:29 +0000921 found = concat_path_file(lpath, name_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000922 /* NB: stat() first so that we see is it a directory;
923 * but if that fails, use lstat() so that
924 * we still match dangling links */
925 if (stat(found, &st) && lstat(found, &st))
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200926 goto cont; /* hmm, remove in progress? */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000927
Denys Vlasenko39263632010-09-03 14:11:08 +0200928 /* Save only name */
929 len = strlen(name_found);
930 found = xrealloc(found, len + 2); /* +2: for slash and NUL */
931 strcpy(found, name_found);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000932
Mark Whitley4e338752001-01-26 20:42:23 +0000933 if (S_ISDIR(st.st_mode)) {
Ron Yorston8506dd62020-12-10 14:44:57 +0000934 /* skip directories if searching PATH */
935 if (type == FIND_EXE_ONLY && !dirbuf)
936 goto cont;
Denys Vlasenko39263632010-09-03 14:11:08 +0200937 /* name is a directory, add slash */
938 found[len] = '/';
939 found[len + 1] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +0000940 } else {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200941 /* skip files if looking for dirs only (example: cd) */
Eric Andersenc470f442003-07-28 09:56:35 +0000942 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000943 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000944 }
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200945 /* add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000946 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000947 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000948 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000949 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000950 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000951 closedir(dir);
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200952 } /* for every path */
953
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000954 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000955 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000956 free(paths);
957 }
Denys Vlasenko39263632010-09-03 14:11:08 +0200958 free(dirbuf);
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200959
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100960 return baselen;
Erik Andersen6273f652000-03-17 01:12:41 +0000961}
Erik Andersenf0657d32000-04-12 17:49:52 +0000962
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200963/* build_match_prefix:
Denys Vlasenko76939e72010-09-03 14:09:24 +0200964 * On entry, match_buf contains everything up to cursor at the moment <tab>
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200965 * was pressed. This function looks at it, figures out what part of it
966 * constitutes the command/file/directory prefix to use for completion,
Denys Vlasenko76939e72010-09-03 14:09:24 +0200967 * and rewrites match_buf to contain only that part.
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200968 */
Denys Vlasenko76939e72010-09-03 14:09:24 +0200969#define dbg_bmp 0
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200970/* Helpers: */
971/* QUOT is used on elements of int_buf[], which are bytes,
972 * not Unicode chars. Therefore it works correctly even in Unicode mode.
973 */
974#define QUOT (UCHAR_MAX+1)
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200975static void remove_chunk(int16_t *int_buf, int beg, int end)
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200976{
977 /* beg must be <= end */
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200978 if (beg == end)
979 return;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200980
981 while ((int_buf[beg] = int_buf[end]) != 0)
982 beg++, end++;
983
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200984 if (dbg_bmp) {
985 int i;
986 for (i = 0; int_buf[i]; i++)
987 bb_putchar((unsigned char)int_buf[i]);
988 bb_putchar('\n');
989 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200990}
Denys Vlasenko76939e72010-09-03 14:09:24 +0200991/* Caller ensures that match_buf points to a malloced buffer
992 * big enough to hold strlen(match_buf)*2 + 2
993 */
994static NOINLINE int build_match_prefix(char *match_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000995{
996 int i, j;
997 int command_mode;
Denys Vlasenko76939e72010-09-03 14:09:24 +0200998 int16_t *int_buf = (int16_t*)match_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000999
Denys Vlasenko76939e72010-09-03 14:09:24 +02001000 if (dbg_bmp) printf("\n%s\n", match_buf);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001001
Denys Vlasenko76939e72010-09-03 14:09:24 +02001002 /* Copy in reverse order, since they overlap */
1003 i = strlen(match_buf);
1004 do {
1005 int_buf[i] = (unsigned char)match_buf[i];
1006 i--;
1007 } while (i >= 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001008
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001009 /* Mark every \c as "quoted c" */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001010 for (i = 0; int_buf[i]; i++) {
1011 if (int_buf[i] == '\\') {
1012 remove_chunk(int_buf, i, i + 1);
1013 int_buf[i] |= QUOT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001014 }
Tomas Heinrichb8909c52010-05-16 20:46:53 +02001015 }
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001016 /* Quote-mark "chars" and 'chars', drop delimiters */
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001017 {
1018 int in_quote = 0;
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001019 i = 0;
1020 while (int_buf[i]) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001021 int cur = int_buf[i];
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001022 if (!cur)
1023 break;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001024 if (cur == '\'' || cur == '"') {
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001025 if (!in_quote || (cur == in_quote)) {
1026 in_quote ^= cur;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001027 remove_chunk(int_buf, i, i + 1);
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001028 continue;
1029 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001030 }
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001031 if (in_quote)
1032 int_buf[i] = cur | QUOT;
1033 i++;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001034 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001035 }
1036
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001037 /* Remove everything up to command delimiters:
1038 * ';' ';;' '&' '|' '&&' '||',
1039 * but careful with '>&' '<&' '>|'
1040 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001041 for (i = 0; int_buf[i]; i++) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001042 int cur = int_buf[i];
1043 if (cur == ';' || cur == '&' || cur == '|') {
1044 int prev = i ? int_buf[i - 1] : 0;
1045 if (cur == '&' && (prev == '>' || prev == '<')) {
1046 continue;
1047 } else if (cur == '|' && prev == '>') {
1048 continue;
1049 }
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001050 remove_chunk(int_buf, 0, i + 1 + (cur == int_buf[i + 1]));
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001051 i = -1; /* back to square 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001052 }
1053 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001054 /* Remove all `cmd` */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001055 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001056 if (int_buf[i] == '`') {
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001057 for (j = i + 1; int_buf[j]; j++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001058 if (int_buf[j] == '`') {
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001059 /* `cmd` should count as a word:
1060 * `cmd` c<tab> should search for files c*,
1061 * not commands c*. Therefore we don't drop
1062 * `cmd` entirely, we replace it with single `.
1063 */
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001064 remove_chunk(int_buf, i, j);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001065 goto next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001066 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001067 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001068 /* No closing ` - command mode, remove all up to ` */
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001069 remove_chunk(int_buf, 0, i + 1);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001070 break;
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001071 next: ;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001072 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001073 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001074
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001075 /* Remove "cmd (" and "cmd {"
1076 * Example: "if { c<tab>"
1077 * In this example, c should be matched as command pfx.
1078 */
1079 for (i = 0; int_buf[i]; i++) {
1080 if (int_buf[i] == '(' || int_buf[i] == '{') {
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001081 remove_chunk(int_buf, 0, i + 1);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001082 i = -1; /* back to square 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001083 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001084 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001085
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001086 /* Remove leading unquoted spaces */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001087 for (i = 0; int_buf[i]; i++)
1088 if (int_buf[i] != ' ')
1089 break;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001090 remove_chunk(int_buf, 0, i);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001091
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001092 /* Determine completion mode */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001093 command_mode = FIND_EXE_ONLY;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001094 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001095 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001096 if (int_buf[i] == ' '
1097 && command_mode == FIND_EXE_ONLY
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001098 && (char)int_buf[0] == 'c'
1099 && (char)int_buf[1] == 'd'
1100 && i == 2 /* -> int_buf[2] == ' ' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001101 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001102 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001103 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001104 command_mode = FIND_FILE_ONLY;
1105 break;
1106 }
1107 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001108 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001109 if (dbg_bmp) printf("command_mode(0:exe/1:dir/2:file):%d\n", command_mode);
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001110
1111 /* Remove everything except last word */
Denys Vlasenkob068bd72010-09-02 12:01:11 +02001112 for (i = 0; int_buf[i]; i++) /* quasi-strlen(int_buf) */
1113 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001114 for (--i; i >= 0; i--) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001115 int cur = int_buf[i];
Natanael Copa7323bca2021-04-09 17:02:00 +02001116 if (cur == ' ' || cur == '<' || cur == '>' || cur == '|' || cur == '&' || cur == '=') {
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001117 remove_chunk(int_buf, 0, i + 1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001118 break;
1119 }
1120 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001121
Denys Vlasenko76939e72010-09-03 14:09:24 +02001122 /* Convert back to string of _chars_ */
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001123 i = 0;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001124 while ((match_buf[i] = int_buf[i]) != '\0')
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001125 i++;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001126
Denys Vlasenko76939e72010-09-03 14:09:24 +02001127 if (dbg_bmp) printf("final match_buf:'%s'\n", match_buf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001128
1129 return command_mode;
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02001130}
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001131
Glenn L McGrath4d001292003-01-06 01:11:50 +00001132/*
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001133 * Display by column (original idea from ls applet,
1134 * very optimized by me [Vladimir] :)
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001135 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001136static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +00001137{
1138 int ncols, row;
1139 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001140 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001141 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001142 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001143
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001144 /* find the longest file name - use that as the column width */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001145 for (row = 0; row < nrows; row++) {
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001146 l = unicode_strwidth(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001147 if (column_width < l)
1148 column_width = l;
1149 }
1150 column_width += 2; /* min space for columns */
1151 ncols = cmdedit_termw / column_width;
1152
1153 if (ncols > 1) {
1154 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001155 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +00001156 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001157 } else {
1158 ncols = 1;
1159 }
1160 for (row = 0; row < nrows; row++) {
1161 int n = row;
1162 int nc;
1163
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001164 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001165 printf("%s%-*s", matches[n],
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001166 (int)(column_width - unicode_strwidth(matches[n])), ""
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001167 );
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001168 }
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001169 if (ENABLE_UNICODE_SUPPORT)
Denys Vlasenko349d72c2018-09-30 16:56:56 +02001170 puts(printable_string(matches[n]));
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001171 else
1172 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001173 }
1174}
1175
Mike Shalf3763032010-11-22 03:49:18 +01001176static const char *is_special_char(char c)
1177{
Denys Vlasenko6d2463a2021-09-17 17:32:30 +02001178 // {: It's mandatory to escape { only if entire name is "{"
1179 // (otherwise it's not special. Example: file named "{ "
1180 // can be escaped simply as "{\ "; "{a" or "a{" need no escaping),
1181 // or if shell supports brace expansion
1182 // (ash doesn't, hush optionally does).
1183 // (): unlike {, shell treats () specially even in contexts
1184 // where they clearly are not valid (e.g. "echo )" is an error).
1185 // #: needs escaping to not start a shell comment.
1186 return strchr(" `'\"\\#$~?*[{()&;|<>", c);
1187 // Used to also have %^=+}]: but not necessary to escape?
Mike Shalf3763032010-11-22 03:49:18 +01001188}
1189
1190static char *quote_special_chars(char *found)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001191{
1192 int l = 0;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001193 char *s = xzalloc((strlen(found) + 1) * 2);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001194
1195 while (*found) {
Mike Shalf3763032010-11-22 03:49:18 +01001196 if (is_special_char(*found))
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001197 s[l++] = '\\';
1198 s[l++] = *found++;
1199 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001200 /* s[l] = '\0'; - already is */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001201 return s;
1202}
1203
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001204/* Do TAB completion */
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001205static NOINLINE void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001206{
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001207 char *chosen_match;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001208 char *match_buf;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001209 size_t len_found;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001210 /* Length of string used for matching */
1211 unsigned match_pfx_len = match_pfx_len;
1212 int find_type;
Mike Shalf3763032010-11-22 03:49:18 +01001213# if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001214 /* cursor pos in command converted to multibyte form */
1215 int cursor_mb;
Mike Shalf3763032010-11-22 03:49:18 +01001216# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001217 if (!(state->flags & TAB_COMPLETION))
1218 return;
1219
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001220 if (*lastWasTab) {
1221 /* The last char was a TAB too.
1222 * Print a list of all the available choices.
1223 */
Denys Vlasenkoa46e16e2010-09-03 13:05:51 +02001224 if (num_matches > 0) {
1225 /* cursor will be changed by goto_new_line() */
Denys Vlasenko044b1802009-07-12 02:50:35 +02001226 int sav_cursor = cursor;
Mark Whitley4e338752001-01-26 20:42:23 +00001227 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001228 showfiles();
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001229 draw_full(command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001230 }
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001231 return;
Erik Andersenf0657d32000-04-12 17:49:52 +00001232 }
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001233
1234 *lastWasTab = 1;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001235 chosen_match = NULL;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001236
Denys Vlasenko76939e72010-09-03 14:09:24 +02001237 /* Make a local copy of the string up to the position of the cursor.
1238 * build_match_prefix will expand it into int16_t's, need to allocate
1239 * twice as much as the string_len+1.
1240 * (we then also (ab)use this extra space later - see (**))
1241 */
1242 match_buf = xmalloc(MAX_LINELEN * sizeof(int16_t));
Mike Shalf3763032010-11-22 03:49:18 +01001243# if !ENABLE_UNICODE_SUPPORT
Denys Vlasenko76939e72010-09-03 14:09:24 +02001244 save_string(match_buf, cursor + 1); /* +1 for NUL */
Mike Shalf3763032010-11-22 03:49:18 +01001245# else
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001246 {
1247 CHAR_T wc = command_ps[cursor];
1248 command_ps[cursor] = BB_NUL;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001249 save_string(match_buf, MAX_LINELEN);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001250 command_ps[cursor] = wc;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001251 cursor_mb = strlen(match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001252 }
Mike Shalf3763032010-11-22 03:49:18 +01001253# endif
Denys Vlasenko76939e72010-09-03 14:09:24 +02001254 find_type = build_match_prefix(match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001255
1256 /* Free up any memory already allocated */
1257 free_tab_completion_data();
1258
Mike Shalf3763032010-11-22 03:49:18 +01001259# if ENABLE_FEATURE_USERNAME_COMPLETION
1260 /* If the word starts with ~ and there is no slash in the word,
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001261 * then try completing this word as a username. */
1262 if (state->flags & USERNAME_COMPLETION)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001263 if (match_buf[0] == '~' && strchr(match_buf, '/') == NULL)
1264 match_pfx_len = complete_username(match_buf);
Mike Shalf3763032010-11-22 03:49:18 +01001265# endif
1266 /* If complete_username() did not match,
1267 * try to match a command in $PATH, or a directory, or a file */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001268 if (!matches)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001269 match_pfx_len = complete_cmd_dir_file(match_buf, find_type);
Mike Shalf3763032010-11-22 03:49:18 +01001270
1271 /* Account for backslashes which will be inserted
1272 * by quote_special_chars() later */
1273 {
1274 const char *e = match_buf + strlen(match_buf);
1275 const char *s = e - match_pfx_len;
1276 while (s < e)
1277 if (is_special_char(*s++))
1278 match_pfx_len++;
1279 }
1280
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001281 /* Remove duplicates */
1282 if (matches) {
Mike Shalf3763032010-11-22 03:49:18 +01001283 unsigned i, n = 0;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001284 qsort_string_vector(matches, num_matches);
1285 for (i = 0; i < num_matches - 1; ++i) {
1286 //if (matches[i] && matches[i+1]) { /* paranoia */
1287 if (strcmp(matches[i], matches[i+1]) == 0) {
1288 free(matches[i]);
1289 //matches[i] = NULL; /* paranoia */
1290 } else {
1291 matches[n++] = matches[i];
1292 }
1293 //}
1294 }
1295 matches[n++] = matches[i];
1296 num_matches = n;
1297 }
Mike Shalf3763032010-11-22 03:49:18 +01001298
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001299 /* Did we find exactly one match? */
1300 if (num_matches != 1) { /* no */
1301 char *cp;
1302 beep();
1303 if (!matches)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001304 goto ret; /* no matches at all */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001305 /* Find common prefix */
1306 chosen_match = xstrdup(matches[0]);
1307 for (cp = chosen_match; *cp; cp++) {
1308 unsigned n;
1309 for (n = 1; n < num_matches; n++) {
1310 if (matches[n][cp - chosen_match] != *cp) {
1311 goto stop;
1312 }
1313 }
1314 }
1315 stop:
1316 if (cp == chosen_match) { /* have unique prefix? */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001317 goto ret; /* no */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001318 }
1319 *cp = '\0';
Mike Shalf3763032010-11-22 03:49:18 +01001320 cp = quote_special_chars(chosen_match);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001321 free(chosen_match);
1322 chosen_match = cp;
1323 len_found = strlen(chosen_match);
1324 } else { /* exactly one match */
1325 /* Next <tab> is not a double-tab */
1326 *lastWasTab = 0;
1327
Mike Shalf3763032010-11-22 03:49:18 +01001328 chosen_match = quote_special_chars(matches[0]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001329 len_found = strlen(chosen_match);
1330 if (chosen_match[len_found-1] != '/') {
1331 chosen_match[len_found] = ' ';
1332 chosen_match[++len_found] = '\0';
1333 }
1334 }
1335
Mike Shalf3763032010-11-22 03:49:18 +01001336# if !ENABLE_UNICODE_SUPPORT
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001337 /* Have space to place the match? */
1338 /* The result consists of three parts with these lengths: */
1339 /* cursor + (len_found - match_pfx_len) + (command_len - cursor) */
1340 /* it simplifies into: */
1341 if ((int)(len_found - match_pfx_len + command_len) < S.maxsize) {
1342 int pos;
1343 /* save tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001344 strcpy(match_buf, &command_ps[cursor]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001345 /* add match and tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001346 sprintf(&command_ps[cursor], "%s%s", chosen_match + match_pfx_len, match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001347 command_len = strlen(command_ps);
1348 /* new pos */
1349 pos = cursor + len_found - match_pfx_len;
1350 /* write out the matched command */
1351 redraw(cmdedit_y, command_len - pos);
1352 }
Mike Shalf3763032010-11-22 03:49:18 +01001353# else
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001354 {
Denys Vlasenko76939e72010-09-03 14:09:24 +02001355 /* Use 2nd half of match_buf as scratch space - see (**) */
1356 char *command = match_buf + MAX_LINELEN;
1357 int len = save_string(command, MAX_LINELEN);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001358 /* Have space to place the match? */
1359 /* cursor_mb + (len_found - match_pfx_len) + (len - cursor_mb) */
1360 if ((int)(len_found - match_pfx_len + len) < MAX_LINELEN) {
1361 int pos;
1362 /* save tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001363 strcpy(match_buf, &command[cursor_mb]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001364 /* where do we want to have cursor after all? */
1365 strcpy(&command[cursor_mb], chosen_match + match_pfx_len);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02001366 len = load_string(command);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001367 /* add match and tail */
Denys Vlasenkofe2d8062021-04-14 17:52:18 +02001368 stpcpy(stpcpy(&command[cursor_mb], chosen_match + match_pfx_len), match_buf);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02001369 command_len = load_string(command);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001370 /* write out the matched command */
1371 /* paranoia: load_string can return 0 on conv error,
1372 * prevent passing pos = (0 - 12) to redraw */
1373 pos = command_len - len;
1374 redraw(cmdedit_y, pos >= 0 ? pos : 0);
1375 }
1376 }
Mike Shalf3763032010-11-22 03:49:18 +01001377# endif
Denys Vlasenko76939e72010-09-03 14:09:24 +02001378 ret:
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001379 free(chosen_match);
Denys Vlasenko76939e72010-09-03 14:09:24 +02001380 free(match_buf);
Erik Andersenf0657d32000-04-12 17:49:52 +00001381}
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001382
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001383#endif /* FEATURE_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001384
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001385
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001386line_input_t* FAST_FUNC new_line_input_t(int flags)
1387{
1388 line_input_t *n = xzalloc(sizeof(*n));
1389 n->flags = flags;
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02001390 n->timeout = -1;
Denys Vlasenko79df4812014-01-10 14:38:26 +01001391#if MAX_HISTORY > 0
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001392 n->max_history = MAX_HISTORY;
Denys Vlasenko79df4812014-01-10 14:38:26 +01001393#endif
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001394 return n;
1395}
1396
1397
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001398#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001399
Flemming Madsend96ffda2013-04-07 18:47:24 +02001400unsigned FAST_FUNC size_from_HISTFILESIZE(const char *hp)
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001401{
1402 int size = MAX_HISTORY;
1403 if (hp) {
1404 size = atoi(hp);
1405 if (size <= 0)
1406 return 1;
1407 if (size > MAX_HISTORY)
1408 return MAX_HISTORY;
1409 }
1410 return size;
1411}
1412
Denis Vlasenko682ad302008-09-27 01:28:56 +00001413static void save_command_ps_at_cur_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001414{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001415 if (command_ps[0] != BB_NUL) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001416 int cur = state->cur_history;
1417 free(state->history[cur]);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001418
Denys Vlasenko19158a82010-03-26 14:06:56 +01001419# if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001420 {
1421 char tbuf[MAX_LINELEN];
1422 save_string(tbuf, sizeof(tbuf));
1423 state->history[cur] = xstrdup(tbuf);
1424 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001425# else
Denis Vlasenko682ad302008-09-27 01:28:56 +00001426 state->history[cur] = xstrdup(command_ps);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001427# endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001428 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001429}
1430
1431/* state->flags is already checked to be nonzero */
1432static int get_previous_history(void)
1433{
1434 if ((state->flags & DO_HISTORY) && state->cur_history) {
1435 save_command_ps_at_cur_history();
1436 state->cur_history--;
1437 return 1;
1438 }
1439 beep();
1440 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001441}
1442
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001443static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001444{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001445 if (state->flags & DO_HISTORY) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001446 if (state->cur_history < state->cnt_history) {
1447 save_command_ps_at_cur_history(); /* save the current history line */
1448 return ++state->cur_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001449 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001450 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001451 beep();
1452 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001453}
Robert Griebl350d26b2002-12-03 22:45:46 +00001454
Flemming Madsend96ffda2013-04-07 18:47:24 +02001455/* Lists command history. Used by shell 'history' builtins */
1456void FAST_FUNC show_history(const line_input_t *st)
1457{
1458 int i;
1459
1460 if (!st)
1461 return;
1462 for (i = 0; i < st->cnt_history; i++)
1463 printf("%4d %s\n", i, st->history[i]);
1464}
1465
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01001466# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denys Vlasenko3d27d432018-12-27 18:03:20 +01001467void FAST_FUNC free_line_input_t(line_input_t *n)
1468{
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01001469 if (n) {
1470 int i = n->cnt_history;
1471 while (i > 0)
1472 free(n->history[--i]);
1473 free(n);
1474 }
Denys Vlasenko3d27d432018-12-27 18:03:20 +01001475}
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01001476# else
1477/* #defined to free() in libbb.h */
1478# endif
Denys Vlasenko3d27d432018-12-27 18:03:20 +01001479
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001480# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001481/* We try to ensure that concurrent additions to the history
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001482 * do not overwrite each other.
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001483 * Otherwise shell users get unhappy.
1484 *
1485 * History file is trimmed lazily, when it grows several times longer
1486 * than configured MAX_HISTORY lines.
1487 */
1488
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001489/* state->flags is already checked to be nonzero */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001490static void load_history(line_input_t *st_parm)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001491{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001492 char *temp_h[MAX_HISTORY];
1493 char *line;
Robert Griebl350d26b2002-12-03 22:45:46 +00001494 FILE *fp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001495 unsigned idx, i, line_len;
Robert Griebl350d26b2002-12-03 22:45:46 +00001496
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001497 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +00001498
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001499 fp = fopen_for_read(st_parm->hist_file);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001500 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001501 /* clean up old history */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001502 for (idx = st_parm->cnt_history; idx > 0;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001503 idx--;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001504 free(st_parm->history[idx]);
1505 st_parm->history[idx] = NULL;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001506 }
1507
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001508 /* fill temp_h[], retaining only last MAX_HISTORY lines */
1509 memset(temp_h, 0, sizeof(temp_h));
Denys Vlasenkobede2152011-09-04 16:12:33 +02001510 idx = 0;
Dennis Groenendeee3562012-04-24 22:40:58 +02001511 st_parm->cnt_history_in_file = 0;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001512 while ((line = xmalloc_fgetline(fp)) != NULL) {
1513 if (line[0] == '\0') {
1514 free(line);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001515 continue;
1516 }
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001517 free(temp_h[idx]);
1518 temp_h[idx] = line;
Dennis Groenendeee3562012-04-24 22:40:58 +02001519 st_parm->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001520 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001521 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001522 idx = 0;
Robert Griebl350d26b2002-12-03 22:45:46 +00001523 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001524 fclose(fp);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001525
1526 /* find first non-NULL temp_h[], if any */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001527 if (st_parm->cnt_history_in_file) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001528 while (temp_h[idx] == NULL) {
1529 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001530 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001531 idx = 0;
1532 }
1533 }
1534
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001535 /* copy temp_h[] to st_parm->history[] */
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001536 for (i = 0; i < st_parm->max_history;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001537 line = temp_h[idx];
1538 if (!line)
1539 break;
1540 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001541 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001542 idx = 0;
1543 line_len = strlen(line);
1544 if (line_len >= MAX_LINELEN)
1545 line[MAX_LINELEN-1] = '\0';
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001546 st_parm->history[i++] = line;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001547 }
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001548 st_parm->cnt_history = i;
Denys Vlasenkobede2152011-09-04 16:12:33 +02001549 if (ENABLE_FEATURE_EDITING_SAVE_ON_EXIT)
1550 st_parm->cnt_history_in_file = i;
Robert Griebl350d26b2002-12-03 22:45:46 +00001551 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001552}
1553
Denys Vlasenkobede2152011-09-04 16:12:33 +02001554# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1555void save_history(line_input_t *st)
1556{
1557 FILE *fp;
1558
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01001559 if (!st || !st->hist_file)
Denys Vlasenkobede2152011-09-04 16:12:33 +02001560 return;
1561 if (st->cnt_history <= st->cnt_history_in_file)
1562 return;
1563
1564 fp = fopen(st->hist_file, "a");
1565 if (fp) {
1566 int i, fd;
1567 char *new_name;
1568 line_input_t *st_temp;
1569
1570 for (i = st->cnt_history_in_file; i < st->cnt_history; i++)
1571 fprintf(fp, "%s\n", st->history[i]);
1572 fclose(fp);
1573
1574 /* we may have concurrently written entries from others.
1575 * load them */
1576 st_temp = new_line_input_t(st->flags);
1577 st_temp->hist_file = st->hist_file;
1578 st_temp->max_history = st->max_history;
1579 load_history(st_temp);
1580
1581 /* write out temp file and replace hist_file atomically */
1582 new_name = xasprintf("%s.%u.new", st->hist_file, (int) getpid());
1583 fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1584 if (fd >= 0) {
1585 fp = xfdopen_for_write(fd);
1586 for (i = 0; i < st_temp->cnt_history; i++)
1587 fprintf(fp, "%s\n", st_temp->history[i]);
1588 fclose(fp);
1589 if (rename(new_name, st->hist_file) == 0)
1590 st->cnt_history_in_file = st_temp->cnt_history;
1591 }
1592 free(new_name);
1593 free_line_input_t(st_temp);
1594 }
1595}
1596# else
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001597static void save_history(char *str)
Robert Griebl350d26b2002-12-03 22:45:46 +00001598{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001599 int fd;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001600 int len, len2;
Eric Andersenc470f442003-07-28 09:56:35 +00001601
Denys Vlasenkobede2152011-09-04 16:12:33 +02001602 if (!state->hist_file)
1603 return;
1604
Wolfram Sang2e9aeae2010-11-15 02:58:28 +01001605 fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0600);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001606 if (fd < 0)
1607 return;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001608 xlseek(fd, 0, SEEK_END); /* paranoia */
1609 len = strlen(str);
1610 str[len] = '\n'; /* we (try to) do atomic write */
1611 len2 = full_write(fd, str, len + 1);
1612 str[len] = '\0';
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001613 close(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001614 if (len2 != len + 1)
1615 return; /* "wtf?" */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001616
1617 /* did we write so much that history file needs trimming? */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001618 state->cnt_history_in_file++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001619 if (state->cnt_history_in_file > state->max_history * 4) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001620 char *new_name;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001621 line_input_t *st_temp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001622
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001623 /* we may have concurrently written entries from others.
1624 * load them */
1625 st_temp = new_line_input_t(state->flags);
1626 st_temp->hist_file = state->hist_file;
Denys Vlasenkoe3d8d072011-03-31 14:39:38 +02001627 st_temp->max_history = state->max_history;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001628 load_history(st_temp);
1629
1630 /* write out temp file and replace hist_file atomically */
1631 new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
Denys Vlasenko4840ae82011-09-04 15:28:03 +02001632 fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Wolfram Sang2e9aeae2010-11-15 02:58:28 +01001633 if (fd >= 0) {
1634 FILE *fp;
1635 int i;
1636
1637 fp = xfdopen_for_write(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001638 for (i = 0; i < st_temp->cnt_history; i++)
1639 fprintf(fp, "%s\n", st_temp->history[i]);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001640 fclose(fp);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001641 if (rename(new_name, state->hist_file) == 0)
1642 state->cnt_history_in_file = st_temp->cnt_history;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001643 }
1644 free(new_name);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001645 free_line_input_t(st_temp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001646 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001647}
Denys Vlasenkobede2152011-09-04 16:12:33 +02001648# endif
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001649# else
1650# define load_history(a) ((void)0)
1651# define save_history(a) ((void)0)
1652# endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001653
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001654static void remember_in_history(char *str)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001655{
1656 int i;
1657
1658 if (!(state->flags & DO_HISTORY))
1659 return;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001660 if (str[0] == '\0')
1661 return;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001662 i = state->cnt_history;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001663 /* Don't save dupes */
1664 if (i && strcmp(state->history[i-1], str) == 0)
1665 return;
1666
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001667 free(state->history[state->max_history]); /* redundant, paranoia */
1668 state->history[state->max_history] = NULL; /* redundant, paranoia */
Denis Vlasenko682ad302008-09-27 01:28:56 +00001669
1670 /* If history[] is full, remove the oldest command */
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001671 /* we need to keep history[state->max_history] empty, hence >=, not > */
1672 if (i >= state->max_history) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001673 free(state->history[0]);
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001674 for (i = 0; i < state->max_history-1; i++)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001675 state->history[i] = state->history[i+1];
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001676 /* i == state->max_history-1 */
Denys Vlasenkoc0cae522011-11-04 01:09:09 +01001677# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1678 if (state->cnt_history_in_file)
Denys Vlasenkobede2152011-09-04 16:12:33 +02001679 state->cnt_history_in_file--;
Denys Vlasenkoc0cae522011-11-04 01:09:09 +01001680# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001681 }
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001682 /* i <= state->max_history-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001683 state->history[i++] = xstrdup(str);
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001684 /* i <= state->max_history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001685 state->cur_history = i;
1686 state->cnt_history = i;
Denys Vlasenkobede2152011-09-04 16:12:33 +02001687# if ENABLE_FEATURE_EDITING_SAVEHISTORY && !ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1688 save_history(str);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001689# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001690}
1691
1692#else /* MAX_HISTORY == 0 */
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001693# define remember_in_history(a) ((void)0)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001694#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001695
1696
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001697#if ENABLE_FEATURE_EDITING_VI
Erik Andersen6273f652000-03-17 01:12:41 +00001698/*
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001699 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001700 */
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001701static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001702vi_Word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001703{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001704 CHAR_T *command = command_ps;
1705
1706 while (cursor < command_len && !BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001707 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001708 if (eat) while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001709 input_forward();
1710}
1711
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001712static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001713vi_word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001714{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001715 CHAR_T *command = command_ps;
1716
Natanael Copa7e6f9312016-08-14 23:30:29 +02001717 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001718 while (cursor < command_len
Natanael Copa7e6f9312016-08-14 23:30:29 +02001719 && (BB_isalnum_or_underscore(command[cursor+1]))
Denys Vlasenko13028922009-07-12 00:51:15 +02001720 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001721 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001722 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001723 } else if (BB_ispunct(command[cursor])) {
1724 while (cursor < command_len && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001725 input_forward();
1726 }
1727
Denis Vlasenko253ce002007-01-22 08:34:44 +00001728 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001729 input_forward();
1730
Denys Vlasenko13028922009-07-12 00:51:15 +02001731 if (eat) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001732 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001733 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001734 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001735}
1736
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001737static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001738vi_End_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001739{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001740 CHAR_T *command = command_ps;
1741
Paul Fox3f11b1b2005-08-04 19:04:46 +00001742 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001743 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001744 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001745 while (cursor < command_len-1 && !BB_isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001746 input_forward();
1747}
1748
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001749static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001750vi_end_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001751{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001752 CHAR_T *command = command_ps;
1753
Denis Vlasenko253ce002007-01-22 08:34:44 +00001754 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001755 return;
1756 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001757 while (cursor < command_len-1 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001758 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001759 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001760 return;
Natanael Copa7e6f9312016-08-14 23:30:29 +02001761 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001762 while (cursor < command_len-1
Natanael Copa7e6f9312016-08-14 23:30:29 +02001763 && (BB_isalnum_or_underscore(command[cursor+1]))
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001764 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001765 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001766 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001767 } else if (BB_ispunct(command[cursor])) {
1768 while (cursor < command_len-1 && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001769 input_forward();
1770 }
1771}
1772
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001773static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001774vi_Back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001775{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001776 CHAR_T *command = command_ps;
1777
1778 while (cursor > 0 && BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001779 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001780 while (cursor > 0 && !BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001781 input_backward(1);
1782}
1783
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001784static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001785vi_back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001786{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001787 CHAR_T *command = command_ps;
1788
Paul Fox3f11b1b2005-08-04 19:04:46 +00001789 if (cursor <= 0)
1790 return;
1791 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001792 while (cursor > 0 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001793 input_backward(1);
1794 if (cursor <= 0)
1795 return;
Natanael Copa7e6f9312016-08-14 23:30:29 +02001796 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001797 while (cursor > 0
Natanael Copa7e6f9312016-08-14 23:30:29 +02001798 && (BB_isalnum_or_underscore(command[cursor-1]))
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001799 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001800 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001801 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001802 } else if (BB_ispunct(command[cursor])) {
1803 while (cursor > 0 && BB_ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001804 input_backward(1);
1805 }
1806}
Denys Vlasenko627821e2021-09-25 19:36:35 +02001807#endif /* ENABLE_FEATURE_EDITING_VI */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001808
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001809/* Modelled after bash 4.0 behavior of Ctrl-<arrow> */
1810static void ctrl_left(void)
1811{
1812 CHAR_T *command = command_ps;
1813
1814 while (1) {
1815 CHAR_T c;
1816
1817 input_backward(1);
1818 if (cursor == 0)
1819 break;
1820 c = command[cursor];
1821 if (c != ' ' && !BB_ispunct(c)) {
1822 /* we reached a "word" delimited by spaces/punct.
1823 * go to its beginning */
1824 while (1) {
1825 c = command[cursor - 1];
1826 if (c == ' ' || BB_ispunct(c))
1827 break;
1828 input_backward(1);
1829 if (cursor == 0)
1830 break;
1831 }
1832 break;
1833 }
1834 }
1835}
1836static void ctrl_right(void)
1837{
1838 CHAR_T *command = command_ps;
1839
1840 while (1) {
1841 CHAR_T c;
1842
1843 c = command[cursor];
1844 if (c == BB_NUL)
1845 break;
1846 if (c != ' ' && !BB_ispunct(c)) {
1847 /* we reached a "word" delimited by spaces/punct.
1848 * go to its end + 1 */
1849 while (1) {
1850 input_forward();
1851 c = command[cursor];
1852 if (c == BB_NUL || c == ' ' || BB_ispunct(c))
1853 break;
1854 }
1855 break;
1856 }
1857 input_forward();
1858 }
1859}
1860
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001861
1862/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001863 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001864 */
1865
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001866#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
1867static void ask_terminal(void)
1868{
1869 /* Ask terminal where is the cursor now.
1870 * lineedit_read_key handles response and corrects
1871 * our idea of current cursor position.
1872 * Testcase: run "echo -n long_line_long_line_long_line",
1873 * then type in a long, wrapping command and try to
1874 * delete it using backspace key.
1875 * Note: we print it _after_ prompt, because
1876 * prompt may contain CR. Example: PS1='\[\r\n\]\w '
1877 */
1878 /* Problem: if there is buffered input on stdin,
1879 * the response will be delivered later,
1880 * possibly to an unsuspecting application.
1881 * Testcase: "sleep 1; busybox ash" + press and hold [Enter].
1882 * Result:
1883 * ~/srcdevel/bbox/fix/busybox.t4 #
1884 * ~/srcdevel/bbox/fix/busybox.t4 #
1885 * ^[[59;34~/srcdevel/bbox/fix/busybox.t4 # <-- garbage
1886 * ~/srcdevel/bbox/fix/busybox.t4 #
1887 *
1888 * Checking for input with poll only makes the race narrower,
1889 * I still can trigger it. Strace:
1890 *
1891 * write(1, "~/srcdevel/bbox/fix/busybox.t4 # ", 33) = 33
1892 * poll([{fd=0, events=POLLIN}], 1, 0) = 0 (Timeout) <-- no input exists
1893 * write(1, "\33[6n", 4) = 4 <-- send the ESC sequence, quick!
Alexey Fomenko232ebaa2011-05-20 04:26:29 +02001894 * poll([{fd=0, events=POLLIN}], 1, -1) = 1 ([{fd=0, revents=POLLIN}])
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001895 * read(0, "\n", 1) = 1 <-- oh crap, user's input got in first
1896 */
Denys Vlasenko451add42010-07-25 00:06:41 +02001897 struct pollfd pfd;
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001898
Denys Vlasenko451add42010-07-25 00:06:41 +02001899 pfd.fd = STDIN_FILENO;
1900 pfd.events = POLLIN;
1901 if (safe_poll(&pfd, 1, 0) == 0) {
1902 S.sent_ESC_br6n = 1;
Ron Yorstoncad3fc72021-02-03 20:47:14 +01001903 fputs_stdout(ESC"[6n");
Denys Vlasenko451add42010-07-25 00:06:41 +02001904 fflush_all(); /* make terminal see it ASAP! */
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001905 }
1906}
1907#else
Denys Vlasenko627821e2021-09-25 19:36:35 +02001908# define ask_terminal() ((void)0)
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001909#endif
1910
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001911/* Note about multi-line PS1 (e.g. "\n\w \u@\h\n> ") and prompt redrawing:
1912 *
1913 * If the prompt has any newlines, after we print it once we use only its last
1914 * line to redraw in-place, which makes it simpler to calculate how many lines
1915 * we should move the cursor up to align the redraw (cmdedit_y). The earlier
1916 * prompt lines just stay on screen and we redraw below them.
1917 *
1918 * Use cases for all prompt lines beyond the initial draw:
1919 * - After clear-screen (^L) or after displaying tab-completion choices, we
1920 * print the full prompt, as it isn't redrawn in-place.
1921 * - During terminal resize we could try to redraw all lines, but we don't,
1922 * because it requires delicate alignment, it's good enough with only the
1923 * last line, and doing it wrong is arguably worse than not doing it at all.
1924 *
1925 * Terminology wise, if it doesn't mention "full", then it means the last/sole
1926 * prompt line. We use the prompt (last/sole line) while redrawing in-place,
1927 * and the full where we need a fresh one unrelated to an earlier position.
1928 *
1929 * If PS1 is not multiline, the last/sole line and the full are the same string.
1930 */
1931
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001932/* Called just once at read_line_input() init time */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001933#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001934static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001935{
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001936 const char *p;
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001937 cmdedit_prompt = prompt_last_line = prmt_ptr;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001938 p = strrchr(prmt_ptr, '\n');
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001939 if (p)
1940 prompt_last_line = p + 1;
1941 cmdedit_prmt_len = unicode_strwidth(prompt_last_line);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001942 put_prompt();
1943}
1944#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001945static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001946{
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001947 int prmt_size = 0;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001948 char *prmt_mem_ptr = xzalloc(1);
Denys Vlasenko1d145692013-03-29 13:09:05 +01001949 char *cwd_buf = NULL;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001950 char flg_not_length = '[';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001951 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001952
Denys Vlasenko7a180432013-08-19 16:44:05 +02001953 /*cmdedit_prmt_len = 0; - already is */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001954
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001955 cbuf[1] = '\0'; /* never changes */
1956
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001957 while (*prmt_ptr) {
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001958 char timebuf[sizeof("HH:MM:SS")];
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001959 char *free_me = NULL;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001960 char *pbuf;
1961 char c;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001962
1963 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001964 c = *prmt_ptr++;
1965 if (c == '\\') {
Denys Vlasenko1d145692013-03-29 13:09:05 +01001966 const char *cp;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001967 int l;
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001968/*
1969 * Supported via bb_process_escape_sequence:
1970 * \a ASCII bell character (07)
1971 * \e ASCII escape character (033)
1972 * \n newline
1973 * \r carriage return
1974 * \\ backslash
1975 * \nnn char with octal code nnn
1976 * Supported:
1977 * \$ if the effective UID is 0, a #, otherwise a $
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001978 * \w current working directory, with $HOME abbreviated with a tilde
1979 * Note: we do not support $PROMPT_DIRTRIM=n feature
Denys Vlasenko1d145692013-03-29 13:09:05 +01001980 * \W basename of the current working directory, with $HOME abbreviated with a tilde
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001981 * \h hostname up to the first '.'
1982 * \H hostname
1983 * \u username
1984 * \[ begin a sequence of non-printing characters
1985 * \] end a sequence of non-printing characters
Denys Vlasenko1d145692013-03-29 13:09:05 +01001986 * \T current time in 12-hour HH:MM:SS format
1987 * \@ current time in 12-hour am/pm format
1988 * \A current time in 24-hour HH:MM format
1989 * \t current time in 24-hour HH:MM:SS format
1990 * (all of the above work as \A)
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001991 * Not supported:
Denys Vlasenko1d145692013-03-29 13:09:05 +01001992 * \! history number of this command
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001993 * \# command number of this command
1994 * \j number of jobs currently managed by the shell
1995 * \l basename of the shell's terminal device name
1996 * \s name of the shell, the basename of $0 (the portion following the final slash)
1997 * \V release of bash, version + patch level (e.g., 2.00.0)
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001998 * \d date in "Weekday Month Date" format (e.g., "Tue May 26")
1999 * \D{format}
2000 * format is passed to strftime(3).
2001 * An empty format results in a locale-specific time representation.
2002 * The braces are required.
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01002003 * Mishandled by bb_process_escape_sequence:
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01002004 * \v version of bash (e.g., 2.00)
2005 */
Denys Vlasenko1d145692013-03-29 13:09:05 +01002006 cp = prmt_ptr;
2007 c = *cp;
2008 if (c != 't') /* don't treat \t as tab */
2009 c = bb_process_escape_sequence(&prmt_ptr);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002010 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002011 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002012 break;
2013 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002014
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002015 switch (c) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002016 case 'u':
Denys Vlasenkof4fcd742021-10-11 23:08:31 +02002017 pbuf = (char*)get_username_str();
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002018 break;
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01002019 case 'H':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002020 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00002021 pbuf = free_me = safe_gethostname();
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01002022 if (c == 'h')
2023 strchrnul(pbuf, '.')[0] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002024 break;
2025 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002026 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002027 break;
Denys Vlasenko1d145692013-03-29 13:09:05 +01002028 case 'T': /* 12-hour HH:MM:SS format */
2029 case '@': /* 12-hour am/pm format */
2030 case 'A': /* 24-hour HH:MM format */
2031 case 't': /* 24-hour HH:MM:SS format */
2032 /* We show all of them as 24-hour HH:MM */
2033 strftime_HHMMSS(timebuf, sizeof(timebuf), NULL)[-3] = '\0';
2034 pbuf = timebuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002035 break;
Denys Vlasenko1d145692013-03-29 13:09:05 +01002036 case 'w': /* current dir */
2037 case 'W': /* basename of cur dir */
2038 if (!cwd_buf) {
Denys Vlasenkof4fcd742021-10-11 23:08:31 +02002039 const char *home;
Denys Vlasenko27be0e82023-01-03 08:28:16 +01002040# if EDITING_HAS_sh_get_var
Ron Yorston298ac952022-06-28 13:40:49 +01002041 cwd_buf = state->sh_get_var
2042 ? xstrdup(state->sh_get_var("PWD"))
2043 : xrealloc_getcwd_or_warn(NULL);
Denys Vlasenko27be0e82023-01-03 08:28:16 +01002044# else
Denys Vlasenko1d145692013-03-29 13:09:05 +01002045 cwd_buf = xrealloc_getcwd_or_warn(NULL);
Denys Vlasenko27be0e82023-01-03 08:28:16 +01002046# endif
Denys Vlasenko1d145692013-03-29 13:09:05 +01002047 if (!cwd_buf)
2048 cwd_buf = (char *)bb_msg_unknown;
Denys Vlasenkof4fcd742021-10-11 23:08:31 +02002049 else if ((home = get_homedir_or_NULL()) != NULL && home[0]) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01002050 char *after_home_user;
2051
Denys Vlasenko1d145692013-03-29 13:09:05 +01002052 /* /home/user[/something] -> ~[/something] */
Denys Vlasenkof4fcd742021-10-11 23:08:31 +02002053 after_home_user = is_prefixed_with(cwd_buf, home);
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01002054 if (after_home_user
2055 && (*after_home_user == '/' || *after_home_user == '\0')
Denys Vlasenko1d145692013-03-29 13:09:05 +01002056 ) {
2057 cwd_buf[0] = '~';
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01002058 overlapping_strcpy(cwd_buf + 1, after_home_user);
Denys Vlasenko1d145692013-03-29 13:09:05 +01002059 }
2060 }
2061 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002062 pbuf = cwd_buf;
Denys Vlasenko1d145692013-03-29 13:09:05 +01002063 if (c == 'w')
2064 break;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00002065 cp = strrchr(pbuf, '/');
Denys Vlasenko8172d052013-03-29 13:21:53 +01002066 if (cp)
Denys Vlasenko1d145692013-03-29 13:09:05 +01002067 pbuf = (char*)cp + 1;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002068 break;
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01002069// bb_process_escape_sequence does this now:
2070// case 'e': case 'E': /* \e \E = \033 */
2071// c = '\033';
2072// break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002073 case 'x': case 'X': {
2074 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002075 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002076 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002077 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002078 buf2[l] = '\0';
2079 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002080 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002081 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002082 break;
2083 }
2084 prmt_ptr++;
2085 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002086 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002087 if (c == 0)
2088 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002089 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002090 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002091 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002092 case '[': case ']':
2093 if (c == flg_not_length) {
Denys Vlasenko7a180432013-08-19 16:44:05 +02002094 /* Toggle '['/']' hex 5b/5d */
2095 flg_not_length ^= 6;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002096 continue;
2097 }
2098 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002099 } /* switch */
2100 } /* if */
2101 } /* if */
2102 cbuf[0] = c;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002103 {
2104 int n = strlen(pbuf);
2105 prmt_size += n;
2106 if (c == '\n')
2107 cmdedit_prmt_len = 0;
2108 else if (flg_not_length != ']') {
Denys Vlasenko627821e2021-09-25 19:36:35 +02002109# if ENABLE_UNICODE_SUPPORT
Audun-Marius Gangstø9bf44992020-11-21 12:26:39 +01002110 if (n == 1) {
2111 /* Only count single-byte characters and the first of multi-byte characters */
2112 if ((unsigned char)*pbuf < 0x80 /* single byte character */
2113 || (unsigned char)*pbuf >= 0xc0 /* first of multi-byte characters */
2114 ) {
2115 cmdedit_prmt_len += n;
2116 }
2117 } else {
2118 cmdedit_prmt_len += unicode_strwidth(pbuf);
2119 }
Denys Vlasenko627821e2021-09-25 19:36:35 +02002120# else
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002121 cmdedit_prmt_len += n;
Denys Vlasenko627821e2021-09-25 19:36:35 +02002122# endif
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002123 }
Denys Vlasenko7a180432013-08-19 16:44:05 +02002124 }
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002125 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_size+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002126 free(free_me);
2127 } /* while */
2128
2129 if (cwd_buf != (char *)bb_msg_unknown)
2130 free(cwd_buf);
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002131 /* see comment (above this function) about multiline prompt redrawing */
2132 cmdedit_prompt = prompt_last_line = prmt_mem_ptr;
2133 prmt_ptr = strrchr(cmdedit_prompt, '\n');
2134 if (prmt_ptr)
2135 prompt_last_line = prmt_ptr + 1;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002136 put_prompt();
2137}
Denys Vlasenkoa97a7952020-12-16 11:14:08 +01002138#endif /* FEATURE_EDITING_FANCY_PROMPT */
Paul Fox3f11b1b2005-08-04 19:04:46 +00002139
Ron Yorston23286902018-02-25 20:09:54 +01002140#if ENABLE_FEATURE_EDITING_WINCH
Denys Vlasenko038a9772016-11-28 01:10:16 +01002141static void cmdedit_setwidth(void)
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002142{
Denys Vlasenko038a9772016-11-28 01:10:16 +01002143 int new_y;
2144
2145 cmdedit_termw = get_terminal_width(STDIN_FILENO);
2146 /* new y for current cursor */
2147 new_y = (cursor + cmdedit_prmt_len) / cmdedit_termw;
2148 /* redraw */
2149 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002150}
2151
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002152static void win_changed(int nsig UNUSED_PARAM)
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002153{
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002154 if (S.ok_to_redraw) {
2155 /* We are in read_key(), safe to redraw immediately */
2156 int sv_errno = errno;
Denys Vlasenko038a9772016-11-28 01:10:16 +01002157 cmdedit_setwidth();
2158 fflush_all();
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002159 errno = sv_errno;
2160 } else {
2161 /* Signal main loop that redraw is necessary */
2162 S.SIGWINCH_count++;
2163 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002164}
Ron Yorston23286902018-02-25 20:09:54 +01002165#endif
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002166
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002167static int lineedit_read_key(char *read_key_buffer, int timeout)
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002168{
Denys Vlasenko020f4062009-05-17 16:44:54 +02002169 int64_t ic;
Denys Vlasenko19158a82010-03-26 14:06:56 +01002170#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002171 char unicode_buf[MB_CUR_MAX + 1];
2172 int unicode_idx = 0;
2173#endif
Denys Vlasenko020f4062009-05-17 16:44:54 +02002174
Denys Vlasenko038a9772016-11-28 01:10:16 +01002175 fflush_all();
Denys Vlasenko1e825ac2022-01-18 00:31:27 +01002176 for (;;) {
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002177 /* Wait for input. TIMEOUT = -1 makes read_key wait even
2178 * on nonblocking stdin, TIMEOUT = 50 makes sure we won't
2179 * insist on full MB_CUR_MAX buffer to declare input like
2180 * "\xff\n",pause,"ls\n" invalid and thus won't lose "ls".
2181 *
Denys Vlasenko12566e72022-01-17 03:02:40 +01002182 * If LI_INTERRUPTIBLE, return -1 if got EINTR in poll()
Denys Vlasenko93e08982023-01-26 12:56:33 +01002183 * inside read_key and bb_got_signal became != 0,
2184 * or if bb_got_signal != 0 (IOW: if signal
Denys Vlasenko12566e72022-01-17 03:02:40 +01002185 * arrived before poll() is reached).
2186 *
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002187 * Note: read_key sets errno to 0 on success.
2188 */
Denys Vlasenko1e825ac2022-01-18 00:31:27 +01002189 for (;;) {
Denys Vlasenko12566e72022-01-17 03:02:40 +01002190 if ((state->flags & LI_INTERRUPTIBLE) && bb_got_signal) {
2191 errno = EINTR;
2192 return -1;
2193 }
2194//FIXME: still races here with signals, but small window to poll() inside read_key
2195 IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 1;)
Denys Vlasenko1e825ac2022-01-18 00:31:27 +01002196 /* errno = 0; - read_key does this itself */
Denys Vlasenko12566e72022-01-17 03:02:40 +01002197 ic = read_key(STDIN_FILENO, read_key_buffer, timeout);
2198 IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 0;)
Denys Vlasenko1e825ac2022-01-18 00:31:27 +01002199 if (errno != EINTR)
2200 break;
Denys Vlasenko93e08982023-01-26 12:56:33 +01002201 /* It was EINTR. Repeat read_key() unless... */
Denys Vlasenko1e825ac2022-01-18 00:31:27 +01002202 if (state->flags & LI_INTERRUPTIBLE) {
Denys Vlasenko93e08982023-01-26 12:56:33 +01002203 /* LI_INTERRUPTIBLE bails out on EINTR
2204 * if bb_got_signal became nonzero.
2205 * (It may stay zero: for example, our SIGWINCH
2206 * handler does not set it. This is used for signals
2207 * which should not interrupt line editing).
Denys Vlasenko1e825ac2022-01-18 00:31:27 +01002208 */
Denys Vlasenko93e08982023-01-26 12:56:33 +01002209 if (bb_got_signal != 0)
2210 goto ret; /* will return -1 */
Denys Vlasenko1e825ac2022-01-18 00:31:27 +01002211 }
2212 }
Denys Vlasenko12566e72022-01-17 03:02:40 +01002213
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002214 if (errno) {
Denys Vlasenko19158a82010-03-26 14:06:56 +01002215#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002216 if (errno == EAGAIN && unicode_idx != 0)
2217 goto pushback;
Denys Vlasenko1f6d2302009-10-29 03:45:26 +01002218#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002219 break;
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +02002220 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002221
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002222#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
2223 if ((int32_t)ic == KEYCODE_CURSOR_POS
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002224 && S.sent_ESC_br6n
Denys Vlasenko020f4062009-05-17 16:44:54 +02002225 ) {
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002226 S.sent_ESC_br6n = 0;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002227 if (cursor == 0) { /* otherwise it may be bogus */
2228 int col = ((ic >> 32) & 0x7fff) - 1;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002229 /*
2230 * Is col > cmdedit_prmt_len?
2231 * If yes (terminal says cursor is farther to the right
2232 * of where we think it should be),
2233 * the prompt wasn't printed starting at col 1,
2234 * there was additional text before it.
2235 */
2236 if ((int)(col - cmdedit_prmt_len) > 0) {
2237 /* Fix our understanding of current x position */
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002238 cmdedit_x += (col - cmdedit_prmt_len);
2239 while (cmdedit_x >= cmdedit_termw) {
2240 cmdedit_x -= cmdedit_termw;
2241 cmdedit_y++;
2242 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02002243 }
2244 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002245 continue;
Denys Vlasenko020f4062009-05-17 16:44:54 +02002246 }
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002247#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002248
Denys Vlasenko19158a82010-03-26 14:06:56 +01002249#if ENABLE_UNICODE_SUPPORT
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002250 if (unicode_status == UNICODE_ON) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002251 wchar_t wc;
2252
2253 if ((int32_t)ic < 0) /* KEYCODE_xxx */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002254 break;
2255 // TODO: imagine sequence like: 0xff,<left-arrow>: we are currently losing 0xff...
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002256
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002257 unicode_buf[unicode_idx++] = ic;
2258 unicode_buf[unicode_idx] = '\0';
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002259 if (mbstowcs(&wc, unicode_buf, 1) != 1) {
2260 /* Not (yet?) a valid unicode char */
2261 if (unicode_idx < MB_CUR_MAX) {
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002262 timeout = 50;
2263 continue;
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002264 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002265 pushback:
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002266 /* Invalid sequence. Save all "bad bytes" except first */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002267 read_key_ungets(read_key_buffer, unicode_buf + 1, unicode_idx - 1);
Tomas Heinricha659b812010-04-29 13:43:39 +02002268# if !ENABLE_UNICODE_PRESERVE_BROKEN
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002269 ic = CONFIG_SUBST_WCHAR;
Tomas Heinricha659b812010-04-29 13:43:39 +02002270# else
Tomas Heinrichb8909c52010-05-16 20:46:53 +02002271 ic = unicode_mark_raw_byte(unicode_buf[0]);
Tomas Heinricha659b812010-04-29 13:43:39 +02002272# endif
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002273 } else {
2274 /* Valid unicode char, return its code */
2275 ic = wc;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002276 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002277 }
2278#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002279 break;
2280 }
Denys Vlasenko1e825ac2022-01-18 00:31:27 +01002281 ret:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002282 return ic;
2283}
2284
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002285#if ENABLE_UNICODE_BIDI_SUPPORT
2286static int isrtl_str(void)
2287{
2288 int idx = cursor;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002289
2290 while (idx < command_len && unicode_bidi_is_neutral_wchar(command_ps[idx]))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002291 idx++;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002292 return unicode_bidi_isrtl(command_ps[idx]);
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002293}
2294#else
2295# define isrtl_str() 0
2296#endif
2297
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002298/* leave out the "vi-mode"-only case labels if vi editing isn't
2299 * configured. */
Denys Vlasenko13028922009-07-12 00:51:15 +02002300#define vi_case(caselabel) IF_FEATURE_EDITING_VI(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00002301
2302/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002303#undef CTRL
2304#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00002305
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002306enum {
2307 VI_CMDMODE_BIT = 0x40000000,
2308 /* 0x80000000 bit flags KEYCODE_xxx */
2309};
2310
2311#if ENABLE_FEATURE_REVERSE_SEARCH
2312/* Mimic readline Ctrl-R reverse history search.
2313 * When invoked, it shows the following prompt:
2314 * (reverse-i-search)'': user_input [cursor pos unchanged by Ctrl-R]
2315 * and typing results in search being performed:
2316 * (reverse-i-search)'tmp': cd /tmp [cursor under t in /tmp]
2317 * Search is performed by looking at progressively older lines in history.
2318 * Ctrl-R again searches for the next match in history.
2319 * Backspace deletes last matched char.
2320 * Control keys exit search and return to normal editing (at current history line).
2321 */
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002322static int32_t reverse_i_search(int timeout)
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002323{
2324 char match_buf[128]; /* for user input */
2325 char read_key_buffer[KEYCODE_BUFFER_SIZE];
2326 const char *matched_history_line;
2327 const char *saved_prompt;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002328 unsigned saved_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002329 int32_t ic;
2330
2331 matched_history_line = NULL;
2332 read_key_buffer[0] = 0;
2333 match_buf[0] = '\0';
2334
2335 /* Save and replace the prompt */
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002336 saved_prompt = prompt_last_line;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002337 saved_prmt_len = cmdedit_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002338 goto set_prompt;
2339
2340 while (1) {
2341 int h;
2342 unsigned match_buf_len = strlen(match_buf);
2343
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002344//FIXME: correct timeout? (i.e. count it down?)
2345 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002346
2347 switch (ic) {
2348 case CTRL('R'): /* searching for the next match */
2349 break;
2350
2351 case '\b':
2352 case '\x7f':
2353 /* Backspace */
2354 if (unicode_status == UNICODE_ON) {
2355 while (match_buf_len != 0) {
2356 uint8_t c = match_buf[--match_buf_len];
2357 if ((c & 0xc0) != 0x80) /* start of UTF-8 char? */
2358 break; /* yes */
2359 }
2360 } else {
2361 if (match_buf_len != 0)
2362 match_buf_len--;
2363 }
2364 match_buf[match_buf_len] = '\0';
2365 break;
2366
2367 default:
2368 if (ic < ' '
2369 || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2370 || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
2371 ) {
2372 goto ret;
2373 }
2374
2375 /* Append this char */
Denys Vlasenko627821e2021-09-25 19:36:35 +02002376# if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002377 if (unicode_status == UNICODE_ON) {
2378 mbstate_t mbstate = { 0 };
2379 char buf[MB_CUR_MAX + 1];
2380 int len = wcrtomb(buf, ic, &mbstate);
2381 if (len > 0) {
2382 buf[len] = '\0';
2383 if (match_buf_len + len < sizeof(match_buf))
2384 strcpy(match_buf + match_buf_len, buf);
2385 }
2386 } else
Denys Vlasenko627821e2021-09-25 19:36:35 +02002387# endif
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002388 if (match_buf_len < sizeof(match_buf) - 1) {
2389 match_buf[match_buf_len] = ic;
2390 match_buf[match_buf_len + 1] = '\0';
2391 }
2392 break;
2393 } /* switch (ic) */
2394
2395 /* Search in history for match_buf */
2396 h = state->cur_history;
2397 if (ic == CTRL('R'))
2398 h--;
2399 while (h >= 0) {
2400 if (state->history[h]) {
2401 char *match = strstr(state->history[h], match_buf);
2402 if (match) {
2403 state->cur_history = h;
2404 matched_history_line = state->history[h];
2405 command_len = load_string(matched_history_line);
2406 cursor = match - matched_history_line;
2407//FIXME: cursor position for Unicode case
2408
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002409 free((char*)prompt_last_line);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002410 set_prompt:
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002411 prompt_last_line = xasprintf("(reverse-i-search)'%s': ", match_buf);
2412 cmdedit_prmt_len = unicode_strwidth(prompt_last_line);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002413 goto do_redraw;
2414 }
2415 }
2416 h--;
2417 }
2418
2419 /* Not found */
2420 match_buf[match_buf_len] = '\0';
2421 beep();
2422 continue;
2423
2424 do_redraw:
2425 redraw(cmdedit_y, command_len - cursor);
2426 } /* while (1) */
2427
2428 ret:
2429 if (matched_history_line)
2430 command_len = load_string(matched_history_line);
2431
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002432 free((char*)prompt_last_line);
2433 prompt_last_line = saved_prompt;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002434 cmdedit_prmt_len = saved_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002435 redraw(cmdedit_y, command_len - cursor);
2436
2437 return ic;
2438}
Denys Vlasenko627821e2021-09-25 19:36:35 +02002439#endif /* ENABLE_FEATURE_REVERSE_SEARCH */
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002440
Denys Vlasenko23427a62018-12-08 15:45:46 +01002441#if ENABLE_FEATURE_EDITING_WINCH
Denys Vlasenko136fe9b2018-12-08 13:49:15 +01002442static void sigaction2(int sig, struct sigaction *act)
2443{
2444 // Grr... gcc 8.1.1:
2445 // "passing argument 3 to restrict-qualified parameter aliases with argument 2"
2446 // dance around that...
2447 struct sigaction *oact FIX_ALIASING;
2448 oact = act;
2449 sigaction(sig, act, oact);
2450}
Denys Vlasenko23427a62018-12-08 15:45:46 +01002451#endif
Denys Vlasenko136fe9b2018-12-08 13:49:15 +01002452
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02002453/* maxsize must be >= 2.
2454 * Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00002455 * -1 on read errors or EOF, or on bare Ctrl-D,
2456 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denys Vlasenko4b89d512016-11-25 03:41:03 +01002457 * (in both cases the cursor remains on the input line, '\n' is not printed)
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00002458 * >0 length of input string, including terminating '\n'
2459 */
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002460int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize)
Erik Andersen13456d12000-03-16 08:09:57 +00002461{
Denys Vlasenkoaaaaaa52017-09-15 17:14:01 +02002462 int len, n;
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002463 int timeout;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002464#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002465 smallint lastWasTab = 0;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002466#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002467 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00002468#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002469 smallint vi_cmdmode = 0;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002470#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002471 struct termios initial_settings;
2472 struct termios new_settings;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002473 char read_key_buffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002474
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002475 INIT_S();
Denys Vlasenko96717d92020-12-21 22:50:23 +01002476 //command_len = 0; - done by INIT_S()
2477 //cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
2478 cmdedit_termw = 80;
Denys Vlasenko96717d92020-12-21 22:50:23 +01002479 IF_FEATURE_EDITING_VI(delptr = delbuf;)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002480
Denys Vlasenkoaaaaaa52017-09-15 17:14:01 +02002481 n = get_termios_and_make_raw(STDIN_FILENO, &new_settings, &initial_settings, 0
2482 | TERMIOS_CLEAR_ISIG /* turn off INTR (ctrl-C), QUIT, SUSP */
2483 );
2484 if (n != 0 || (initial_settings.c_lflag & (ECHO|ICANON)) == ICANON) {
Denys Vlasenkod598a8d2014-12-10 17:22:13 +01002485 /* Happens when e.g. stty -echo was run before.
2486 * But if ICANON is not set, we don't come here.
2487 * (example: interactive python ^Z-backgrounded,
2488 * tty is still in "raw mode").
2489 */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002490 parse_and_put_prompt(prompt);
Denys Vlasenko038a9772016-11-28 01:10:16 +01002491 fflush_all();
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00002492 if (fgets(command, maxsize, stdin) == NULL)
2493 len = -1; /* EOF or error */
2494 else
2495 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002496 DEINIT_S();
2497 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00002498 }
2499
Denys Vlasenko28055022010-01-04 20:49:58 +01002500 init_unicode();
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02002501
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002502// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00002503 if (maxsize > MAX_LINELEN)
2504 maxsize = MAX_LINELEN;
Denys Vlasenko044b1802009-07-12 02:50:35 +02002505 S.maxsize = maxsize;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002506
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002507 timeout = -1;
2508 /* Make state->flags == 0 if st is NULL.
2509 * With zeroed flags, no other fields are ever referenced.
2510 */
2511 state = (line_input_t*) &const_int_0;
2512 if (st) {
2513 state = st;
2514 timeout = st->timeout;
2515 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002516#if MAX_HISTORY > 0
Denys Vlasenko779f96a2019-02-04 16:16:30 +01002517 if (state->flags & DO_HISTORY) {
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002518# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denys Vlasenko779f96a2019-02-04 16:16:30 +01002519 if (state->hist_file)
2520 if (state->cnt_history == 0)
2521 load_history(state);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002522# endif
Denis Vlasenko3c385cd2008-11-02 00:41:05 +00002523 state->cur_history = state->cnt_history;
Denys Vlasenko779f96a2019-02-04 16:16:30 +01002524 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002525#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002526
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002527 /* prepare before init handlers */
Denys Vlasenko19158a82010-03-26 14:06:56 +01002528#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002529 command_ps = xzalloc(maxsize * sizeof(command_ps[0]));
2530#else
Mark Whitley4e338752001-01-26 20:42:23 +00002531 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00002532 command[0] = '\0';
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002533#endif
2534#define command command_must_not_be_used
Mark Whitley4e338752001-01-26 20:42:23 +00002535
Denis Vlasenko202ac502008-11-05 13:20:58 +00002536 tcsetattr_stdin_TCSANOW(&new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00002537
Denis Vlasenko682ad302008-09-27 01:28:56 +00002538#if 0
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02002539 for (i = 0; i <= state->max_history; i++)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002540 bb_error_msg("history[%d]:'%s'", i, state->history[i]);
Denis Vlasenko682ad302008-09-27 01:28:56 +00002541 bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
2542#endif
2543
Denys Vlasenko0a677222017-11-08 13:38:12 +01002544 /* Get width (before printing prompt) */
2545 cmdedit_termw = get_terminal_width(STDIN_FILENO);
Denys Vlasenko13ad9062009-11-11 03:19:30 +01002546 /* Print out the command prompt, optionally ask where cursor is */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002547 parse_and_put_prompt(prompt);
Denys Vlasenko13ad9062009-11-11 03:19:30 +01002548 ask_terminal();
Eric Andersenb3dc3b82001-01-04 11:08:45 +00002549
Ron Yorston23286902018-02-25 20:09:54 +01002550#if ENABLE_FEATURE_EDITING_WINCH
Alexey Fomenko232ebaa2011-05-20 04:26:29 +02002551 /* Install window resize handler (NB: after *all* init is complete) */
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002552 S.SIGWINCH_handler.sa_handler = win_changed;
2553 S.SIGWINCH_handler.sa_flags = SA_RESTART;
Denys Vlasenko136fe9b2018-12-08 13:49:15 +01002554 sigaction2(SIGWINCH, &S.SIGWINCH_handler);
Ron Yorston23286902018-02-25 20:09:54 +01002555#endif
Denys Vlasenkoc396fe62009-05-17 19:28:14 +02002556 read_key_buffer[0] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002557 while (1) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002558 /*
2559 * The emacs and vi modes share much of the code in the big
2560 * command loop. Commands entered when in vi's command mode
2561 * (aka "escape mode") get an extra bit added to distinguish
2562 * them - this keeps them from being self-inserted. This
2563 * clutters the big switch a bit, but keeps all the code
2564 * in one place.
2565 */
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002566 int32_t ic, ic_raw;
Ron Yorston23286902018-02-25 20:09:54 +01002567#if ENABLE_FEATURE_EDITING_WINCH
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002568 unsigned count;
2569
2570 count = S.SIGWINCH_count;
2571 if (S.SIGWINCH_saved != count) {
2572 S.SIGWINCH_saved = count;
Denys Vlasenko038a9772016-11-28 01:10:16 +01002573 cmdedit_setwidth();
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002574 }
Ron Yorston23286902018-02-25 20:09:54 +01002575#endif
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002576 ic = ic_raw = lineedit_read_key(read_key_buffer, timeout);
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002577
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002578#if ENABLE_FEATURE_REVERSE_SEARCH
2579 again:
2580#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00002581#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00002582 newdelflag = 1;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002583 if (vi_cmdmode) {
2584 /* btw, since KEYCODE_xxx are all < 0, this doesn't
2585 * change ic if it contains one of them: */
2586 ic |= VI_CMDMODE_BIT;
2587 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00002588#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002589
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002590 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00002591 case '\n':
2592 case '\r':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002593 vi_case('\n'|VI_CMDMODE_BIT:)
2594 vi_case('\r'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002595 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002596 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00002597 break_out = 1;
2598 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002599 case CTRL('A'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002600 vi_case('0'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002601 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002602 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00002603 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002604 case CTRL('B'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002605 vi_case('h'|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002606 vi_case('\b'|VI_CMDMODE_BIT:) /* ^H */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002607 vi_case('\x7f'|VI_CMDMODE_BIT:) /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002608 input_backward(1); /* Move back one character */
Erik Andersenf0657d32000-04-12 17:49:52 +00002609 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002610 case CTRL('E'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002611 vi_case('$'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002612 /* Control-e -- End of line */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002613 put_till_end_and_adv_cursor();
Erik Andersenc7c634b2000-03-19 05:28:55 +00002614 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002615 case CTRL('F'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002616 vi_case('l'|VI_CMDMODE_BIT:)
2617 vi_case(' '|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002618 input_forward(); /* Move forward one character */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002619 break;
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002620 case '\b': /* ^H */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002621 case '\x7f': /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002622 if (!isrtl_str())
2623 input_backspace();
2624 else
2625 input_delete(0);
2626 break;
2627 case KEYCODE_DELETE:
2628 if (!isrtl_str())
2629 input_delete(0);
2630 else
2631 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00002632 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002633#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00002634 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002635 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002636 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002637#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002638 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00002639 /* Control-k -- clear to end of line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002640 command_ps[cursor] = BB_NUL;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002641 command_len = cursor;
Denys Vlasenko94043e82010-05-11 14:49:13 +02002642 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen65a07302002-04-13 13:26:49 +00002643 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002644 case CTRL('L'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002645 vi_case(CTRL('L')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002646 /* Control-l -- clear screen */
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002647 /* cursor to top,left; clear to the end of screen */
2648 printf(ESC"[H" ESC"[J");
2649 draw_full(command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00002650 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002651#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002652 case CTRL('N'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002653 vi_case(CTRL('N')|VI_CMDMODE_BIT:)
2654 vi_case('j'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002655 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002656 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002657 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00002658 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002659 case CTRL('P'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002660 vi_case(CTRL('P')|VI_CMDMODE_BIT:)
2661 vi_case('k'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002662 /* Control-p -- Get previous command from history */
Denis Vlasenko682ad302008-09-27 01:28:56 +00002663 if (get_previous_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002664 goto rewrite_line;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002665 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002666#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002667 case CTRL('U'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002668 vi_case(CTRL('U')|VI_CMDMODE_BIT:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002669 /* Control-U -- Clear line before cursor */
2670 if (cursor) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002671 command_len -= cursor;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002672 memmove(command_ps, command_ps + cursor,
2673 (command_len + 1) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002674 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002675 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002676 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002677 case CTRL('W'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002678 vi_case(CTRL('W')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002679 /* Control-W -- Remove the last word */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002680 while (cursor > 0 && BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002681 input_backspace();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002682 while (cursor > 0 && !BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002683 input_backspace();
2684 break;
Rostislav Skudnov2e4ef382016-11-24 15:04:00 +01002685 case KEYCODE_ALT_D: {
2686 /* Delete word forward */
2687 int nc, sc = cursor;
2688 ctrl_right();
2689 nc = cursor - sc;
2690 input_backward(nc);
2691 while (--nc >= 0)
2692 input_delete(1);
2693 break;
2694 }
2695 case KEYCODE_ALT_BACKSPACE: {
2696 /* Delete word backward */
2697 int sc = cursor;
2698 ctrl_left();
2699 while (sc-- > cursor)
2700 input_delete(1);
2701 break;
2702 }
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002703#if ENABLE_FEATURE_REVERSE_SEARCH
2704 case CTRL('R'):
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002705 ic = ic_raw = reverse_i_search(timeout);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002706 goto again;
2707#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002708
Denis Vlasenko38f63192007-01-22 09:03:07 +00002709#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002710 case 'i'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002711 vi_cmdmode = 0;
2712 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002713 case 'I'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002714 input_backward(cursor);
2715 vi_cmdmode = 0;
2716 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002717 case 'a'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002718 input_forward();
2719 vi_cmdmode = 0;
2720 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002721 case 'A'|VI_CMDMODE_BIT:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002722 put_till_end_and_adv_cursor();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002723 vi_cmdmode = 0;
2724 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002725 case 'x'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002726 input_delete(1);
2727 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002728 case 'X'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002729 if (cursor > 0) {
2730 input_backward(1);
2731 input_delete(1);
2732 }
2733 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002734 case 'W'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002735 vi_Word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002736 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002737 case 'w'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002738 vi_word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002739 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002740 case 'E'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002741 vi_End_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002742 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002743 case 'e'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002744 vi_end_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002745 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002746 case 'B'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002747 vi_Back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002748 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002749 case 'b'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002750 vi_back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002751 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002752 case 'C'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002753 vi_cmdmode = 0;
2754 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002755 case 'D'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002756 goto clear_to_eol;
2757
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002758 case 'c'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002759 vi_cmdmode = 0;
2760 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002761 case 'd'|VI_CMDMODE_BIT: {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002762 int nc, sc;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002763
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002764 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002765 if (errno) /* error */
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002766 goto return_error_indicator;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002767 if (ic == ic_raw) { /* "cc", "dd" */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002768 input_backward(cursor);
2769 goto clear_to_eol;
2770 break;
2771 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002772
2773 sc = cursor;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002774 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002775 case 'w':
2776 case 'W':
2777 case 'e':
2778 case 'E':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002779 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002780 case 'w': /* "dw", "cw" */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002781 vi_word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002782 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002783 case 'W': /* 'dW', 'cW' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002784 vi_Word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002785 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002786 case 'e': /* 'de', 'ce' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002787 vi_end_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002788 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002789 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002790 case 'E': /* 'dE', 'cE' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002791 vi_End_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002792 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002793 break;
2794 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002795 nc = cursor;
2796 input_backward(cursor - sc);
2797 while (nc-- > cursor)
2798 input_delete(1);
2799 break;
2800 case 'b': /* "db", "cb" */
2801 case 'B': /* implemented as B */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002802 if (ic == 'b')
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002803 vi_back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002804 else
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002805 vi_Back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002806 while (sc-- > cursor)
2807 input_delete(1);
2808 break;
2809 case ' ': /* "d ", "c " */
2810 input_delete(1);
2811 break;
2812 case '$': /* "d$", "c$" */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002813 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002814 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002815 input_delete(1);
2816 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002817 }
2818 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002819 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002820 case 'p'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002821 input_forward();
2822 /* fallthrough */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002823 case 'P'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002824 put();
2825 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002826 case 'r'|VI_CMDMODE_BIT:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002827//FIXME: unicode case?
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002828 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002829 if (errno) /* error */
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002830 goto return_error_indicator;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002831 if (ic < ' ' || ic > 255) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00002832 beep();
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002833 } else {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002834 command_ps[cursor] = ic;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002835 bb_putchar(ic);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002836 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00002837 }
2838 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002839 case '\x1b': /* ESC */
2840 if (state->flags & VI_MODE) {
2841 /* insert mode --> command mode */
2842 vi_cmdmode = 1;
2843 input_backward(1);
2844 }
2845 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002846#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002847
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002848#if MAX_HISTORY > 0
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002849 case KEYCODE_UP:
2850 if (get_previous_history())
2851 goto rewrite_line;
2852 beep();
2853 break;
2854 case KEYCODE_DOWN:
2855 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002856 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002857 rewrite_line:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002858 /* Rewrite the line with the selected history item */
2859 /* change command */
Denys Vlasenko90a99042009-09-06 02:36:23 +02002860 command_len = load_string(state->history[state->cur_history] ?
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002861 state->history[state->cur_history] : "");
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002862 /* redraw and go to eol (bol, in vi) */
2863 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
2864 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002865#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002866 case KEYCODE_RIGHT:
2867 input_forward();
2868 break;
2869 case KEYCODE_LEFT:
2870 input_backward(1);
2871 break;
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002872 case KEYCODE_CTRL_LEFT:
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002873 case KEYCODE_ALT_LEFT: /* bash doesn't do it */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002874 ctrl_left();
2875 break;
2876 case KEYCODE_CTRL_RIGHT:
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002877 case KEYCODE_ALT_RIGHT: /* bash doesn't do it */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002878 ctrl_right();
2879 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002880 case KEYCODE_HOME:
2881 input_backward(cursor);
2882 break;
2883 case KEYCODE_END:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002884 put_till_end_and_adv_cursor();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002885 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002886
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002887 default:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002888 if (initial_settings.c_cc[VINTR] != 0
2889 && ic_raw == initial_settings.c_cc[VINTR]
2890 ) {
2891 /* Ctrl-C (usually) - stop gathering input */
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002892 command_len = 0;
2893 break_out = -1; /* "do not append '\n'" */
2894 break;
2895 }
2896 if (initial_settings.c_cc[VEOF] != 0
2897 && ic_raw == initial_settings.c_cc[VEOF]
2898 ) {
2899 /* Ctrl-D (usually) - delete one character,
2900 * or exit if len=0 and no chars to delete */
2901 if (command_len == 0) {
2902 errno = 0;
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002903
2904 case -1: /* error (e.g. EIO when tty is destroyed) */
2905 IF_FEATURE_EDITING_VI(return_error_indicator:)
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002906 break_out = command_len = -1;
2907 break;
2908 }
2909 input_delete(0);
2910 break;
2911 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002912// /* Control-V -- force insert of next char */
2913// if (c == CTRL('V')) {
2914// if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002915// goto return_error_indicator;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002916// if (c == 0) {
2917// beep();
2918// break;
2919// }
2920// }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002921 if (ic < ' '
Denys Vlasenko19158a82010-03-26 14:06:56 +01002922 || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2923 || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002924 ) {
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002925 /* If VI_CMDMODE_BIT is set, ic is >= 256
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002926 * and vi mode ignores unexpected chars.
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002927 * Otherwise, we are here if ic is a
2928 * control char or an unhandled ESC sequence,
2929 * which is also ignored.
2930 */
2931 break;
2932 }
2933 if ((int)command_len >= (maxsize - 2)) {
2934 /* Not enough space for the char and EOL */
2935 break;
Paul Fox84bbac52008-01-11 16:50:08 +00002936 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002937
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002938 command_len++;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002939 if (cursor == (command_len - 1)) {
2940 /* We are at the end, append */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002941 command_ps[cursor] = ic;
2942 command_ps[cursor + 1] = BB_NUL;
Denys Vlasenko94043e82010-05-11 14:49:13 +02002943 put_cur_glyph_and_inc_cursor();
Tomas Heinrichaa167552010-03-26 13:13:24 +01002944 if (unicode_bidi_isrtl(ic))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002945 input_backward(1);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002946 } else {
2947 /* In the middle, insert */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002948 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002949
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002950 memmove(command_ps + sc + 1, command_ps + sc,
2951 (command_len - sc) * sizeof(command_ps[0]));
2952 command_ps[sc] = ic;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002953 /* is right-to-left char, or neutral one (e.g. comma) was just added to rtl text? */
2954 if (!isrtl_str())
2955 sc++; /* no */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002956 put_till_end_and_adv_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +00002957 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002958 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002959 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00002960 break;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002961 } /* switch (ic) */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002962
2963 if (break_out)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002964 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002965
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002966#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002967 if (ic_raw != '\t')
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002968 lastWasTab = 0;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002969#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002970 } /* while (1) */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002971
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002972#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002973 if (S.sent_ESC_br6n) {
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002974 /* "sleep 1; busybox ash" + hold [Enter] to trigger.
2975 * We sent "ESC [ 6 n", but got '\n' first, and
2976 * KEYCODE_CURSOR_POS response is now buffered from terminal.
2977 * It's bad already and not much can be done with it
2978 * (it _will_ be visible for the next process to read stdin),
2979 * but without this delay it even shows up on the screen
2980 * as garbage because we restore echo settings with tcsetattr
2981 * before it comes in. UGLY!
2982 */
2983 usleep(20*1000);
Denys Vlasenkofae73322020-12-21 21:55:03 +01002984// MAYBE? tcflush(STDIN_FILENO, TCIFLUSH); /* flushes data received but not read */
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002985 }
2986#endif
2987
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002988/* End of bug-catching "command_must_not_be_used" trick */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002989#undef command
2990
Denys Vlasenko19158a82010-03-26 14:06:56 +01002991#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +02002992 command[0] = '\0';
2993 if (command_len > 0)
2994 command_len = save_string(command, maxsize - 1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002995 free(command_ps);
2996#endif
2997
Flemming Madsend96ffda2013-04-07 18:47:24 +02002998 if (command_len > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002999 remember_in_history(command);
Flemming Madsend96ffda2013-04-07 18:47:24 +02003000 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00003001
Eric Andersen27bb7902003-12-23 20:24:51 +00003002 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003003 command[command_len++] = '\n';
3004 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00003005 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00003006
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00003007#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00003008 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00003009#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00003010
Denis Vlasenko6258fd32007-01-22 07:30:26 +00003011 /* restore initial_settings */
Denis Vlasenko202ac502008-11-05 13:20:58 +00003012 tcsetattr_stdin_TCSANOW(&initial_settings);
Ron Yorston23286902018-02-25 20:09:54 +01003013#if ENABLE_FEATURE_EDITING_WINCH
Denis Vlasenko6258fd32007-01-22 07:30:26 +00003014 /* restore SIGWINCH handler */
Denys Vlasenkobff71d32016-11-27 22:25:07 +01003015 sigaction_set(SIGWINCH, &S.SIGWINCH_handler);
Ron Yorston23286902018-02-25 20:09:54 +01003016#endif
Denys Vlasenko8131eea2009-11-02 14:19:51 +01003017 fflush_all();
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00003018
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00003019 len = command_len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00003020 DEINIT_S();
3021
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00003022 return len; /* can't return command_len, DEINIT_S() destroys it */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003023}
3024
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02003025#else /* !FEATURE_EDITING */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003026
3027#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00003028int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003029{
Ron Yorstoncad3fc72021-02-03 20:47:14 +01003030 fputs_stdout(prompt);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01003031 fflush_all();
Denys Vlasenkob2320372012-09-27 16:03:49 +02003032 if (!fgets(command, maxsize, stdin))
3033 return -1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003034 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00003035}
3036
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02003037#endif /* !FEATURE_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00003038
3039
Denis Vlasenko82b39e82007-01-21 19:18:19 +00003040/*
3041 * Testing
3042 */
3043
Eric Andersen5f2c79d2001-02-16 18:36:04 +00003044#ifdef TEST
3045
Eric Andersenf9ff8a72001-03-15 20:51:09 +00003046#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00003047
3048const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00003049
Eric Andersen5f2c79d2001-02-16 18:36:04 +00003050int main(int argc, char **argv)
3051{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00003052 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00003053 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00003054#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00003055 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
3056 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
Denys Vlasenko8187e012017-09-13 22:48:30 +02003057 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00003058#else
3059 "% ";
3060#endif
3061
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00003062 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00003063 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003064 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00003065 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00003066 break;
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02003067 buff[l-1] = '\0';
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003068 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00003069 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003070 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00003071 return 0;
3072}
3073
Eric Andersenc470f442003-07-28 09:56:35 +00003074#endif /* TEST */