blob: f76afd37dfcb8f18c32f430c5142f041482d03f4 [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
252static const char *get_username_str(void)
253{
254 if (!got_user_strings)
255 get_user_strings();
256 return user_buf ? user_buf : "";
257 /* btw, bash uses "I have no name!" string if uid has no entry */
258}
259
260static NOINLINE const char *get_homedir_or_NULL(void)
261{
262 if (!got_user_strings)
263 get_user_strings();
264 return home_pwd_buf;
265}
266#endif
267
Denys Vlasenko19158a82010-03-26 14:06:56 +0100268#if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200269static size_t load_string(const char *src)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200270{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100271 if (unicode_status == UNICODE_ON) {
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200272 ssize_t len = mbstowcs(command_ps, src, S.maxsize - 1);
Denys Vlasenko353680a2011-03-27 01:18:07 +0100273 if (len < 0)
274 len = 0;
275 command_ps[len] = BB_NUL;
276 return len;
277 } else {
278 unsigned i = 0;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200279 while (src[i] && i < S.maxsize - 1) {
280 command_ps[i] = src[i];
Denys Vlasenko353680a2011-03-27 01:18:07 +0100281 i++;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200282 }
283 command_ps[i] = BB_NUL;
Denys Vlasenko353680a2011-03-27 01:18:07 +0100284 return i;
285 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200286}
Tomas Heinricha659b812010-04-29 13:43:39 +0200287static unsigned save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200288{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100289 if (unicode_status == UNICODE_ON) {
Denys Vlasenko94043e82010-05-11 14:49:13 +0200290# if !ENABLE_UNICODE_PRESERVE_BROKEN
Denys Vlasenko353680a2011-03-27 01:18:07 +0100291 ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
292 if (len < 0)
293 len = 0;
294 dst[len] = '\0';
295 return len;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200296# else
Denys Vlasenko353680a2011-03-27 01:18:07 +0100297 unsigned dstpos = 0;
298 unsigned srcpos = 0;
Tomas Heinricha659b812010-04-29 13:43:39 +0200299
Denys Vlasenko353680a2011-03-27 01:18:07 +0100300 maxsize--;
301 while (dstpos < maxsize) {
302 wchar_t wc;
303 int n = srcpos;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200304
Denys Vlasenko353680a2011-03-27 01:18:07 +0100305 /* Convert up to 1st invalid byte (or up to end) */
306 while ((wc = command_ps[srcpos]) != BB_NUL
307 && !unicode_is_raw_byte(wc)
308 ) {
309 srcpos++;
310 }
311 command_ps[srcpos] = BB_NUL;
312 n = wcstombs(dst + dstpos, command_ps + n, maxsize - dstpos);
313 if (n < 0) /* should not happen */
314 break;
315 dstpos += n;
316 if (wc == BB_NUL) /* usually is */
317 break;
318
319 /* We do have invalid byte here! */
320 command_ps[srcpos] = wc; /* restore it */
Tomas Heinricha659b812010-04-29 13:43:39 +0200321 srcpos++;
Denys Vlasenko353680a2011-03-27 01:18:07 +0100322 if (dstpos == maxsize)
323 break;
324 dst[dstpos++] = (char) wc;
Tomas Heinricha659b812010-04-29 13:43:39 +0200325 }
Denys Vlasenko353680a2011-03-27 01:18:07 +0100326 dst[dstpos] = '\0';
327 return dstpos;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200328# endif
Denys Vlasenko353680a2011-03-27 01:18:07 +0100329 } else {
330 unsigned i = 0;
331 while ((dst[i] = command_ps[i]) != 0)
332 i++;
333 return i;
334 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200335}
336/* I thought just fputwc(c, stdout) would work. But no... */
337static void BB_PUTCHAR(wchar_t c)
338{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100339 if (unicode_status == UNICODE_ON) {
340 char buf[MB_CUR_MAX + 1];
341 mbstate_t mbst = { 0 };
342 ssize_t len = wcrtomb(buf, c, &mbst);
343 if (len > 0) {
344 buf[len] = '\0';
Ron Yorstoncad3fc72021-02-03 20:47:14 +0100345 fputs_stdout(buf);
Denys Vlasenko353680a2011-03-27 01:18:07 +0100346 }
347 } else {
348 /* In this case, c is always one byte */
349 putchar(c);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200350 }
351}
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200352# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
353static wchar_t adjust_width_and_validate_wc(unsigned *width_adj, wchar_t wc)
354# else
355static wchar_t adjust_width_and_validate_wc(wchar_t wc)
356# define adjust_width_and_validate_wc(width_adj, wc) \
357 ((*(width_adj))++, adjust_width_and_validate_wc(wc))
358# endif
359{
360 int w = 1;
361
362 if (unicode_status == UNICODE_ON) {
Denys Vlasenko26e2c1d2010-05-16 21:15:03 +0200363 if (wc > CONFIG_LAST_SUPPORTED_WCHAR) {
364 /* note: also true for unicode_is_raw_byte(wc) */
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200365 goto subst;
366 }
367 w = wcwidth(wc);
368 if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0)
369 || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
370 || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
371 ) {
372 subst:
373 w = 1;
374 wc = CONFIG_SUBST_WCHAR;
375 }
376 }
377
378# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
379 *width_adj += w;
380#endif
381 return wc;
382}
383#else /* !UNICODE */
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200384static size_t load_string(const char *src)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200385{
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200386 safe_strncpy(command_ps, src, S.maxsize);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200387 return strlen(command_ps);
388}
Denys Vlasenko9038d6f2009-07-15 20:02:19 +0200389# if ENABLE_FEATURE_TAB_COMPLETION
Tomas Heinricha659b812010-04-29 13:43:39 +0200390static void save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200391{
392 safe_strncpy(dst, command_ps, maxsize);
393}
Denys Vlasenko7dd0ce42009-07-15 18:27:47 +0200394# endif
395# define BB_PUTCHAR(c) bb_putchar(c)
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200396/* Should never be called: */
397int adjust_width_and_validate_wc(unsigned *width_adj, int wc);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200398#endif
399
400
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000401/* Put 'command_ps[cursor]', cursor++.
402 * Advance cursor on screen. If we reached right margin, scroll text up
403 * and remove terminal margin effect by printing 'next_char' */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000404#define HACK_FOR_WRONG_WIDTH 1
Denys Vlasenko94043e82010-05-11 14:49:13 +0200405static void put_cur_glyph_and_inc_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000406{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200407 CHAR_T c = command_ps[cursor];
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200408 unsigned width = 0;
409 int ofs_to_right;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000410
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200411 if (c == BB_NUL) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000412 /* erase character after end of input string */
413 c = ' ';
Denys Vlasenko94043e82010-05-11 14:49:13 +0200414 } else {
415 /* advance cursor only if we aren't at the end yet */
416 cursor++;
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200417 if (unicode_status == UNICODE_ON) {
418 IF_UNICODE_WIDE_WCHARS(width = cmdedit_x;)
419 c = adjust_width_and_validate_wc(&cmdedit_x, c);
420 IF_UNICODE_WIDE_WCHARS(width = cmdedit_x - width;)
421 } else {
422 cmdedit_x++;
423 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000424 }
Denys Vlasenko94043e82010-05-11 14:49:13 +0200425
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200426 ofs_to_right = cmdedit_x - cmdedit_termw;
427 if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right <= 0) {
428 /* c fits on this line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200429 BB_PUTCHAR(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000430 }
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200431
432 if (ofs_to_right >= 0) {
433 /* we go to the next line */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000434#if HACK_FOR_WRONG_WIDTH
435 /* This works better if our idea of term width is wrong
436 * and it is actually wider (often happens on serial lines).
437 * Printing CR,LF *forces* cursor to next line.
438 * OTOH if terminal width is correct AND terminal does NOT
439 * have automargin (IOW: it is moving cursor to next line
440 * by itself (which is wrong for VT-10x terminals)),
441 * this will break things: there will be one extra empty line */
442 puts("\r"); /* + implicit '\n' */
443#else
Denys Vlasenko94043e82010-05-11 14:49:13 +0200444 /* VT-10x terminals don't wrap cursor to next line when last char
445 * on the line is printed - cursor stays "over" this char.
446 * Need to print _next_ char too (first one to appear on next line)
447 * to make cursor move down to next line.
448 */
449 /* Works ok only if cmdedit_termw is correct. */
450 c = command_ps[cursor];
451 if (c == BB_NUL)
452 c = ' ';
453 BB_PUTCHAR(c);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000454 bb_putchar('\b');
Denis Vlasenko2c844952008-04-25 18:44:35 +0000455#endif
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200456 cmdedit_y++;
457 if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right == 0) {
458 width = 0;
459 } else { /* ofs_to_right > 0 */
460 /* wide char c didn't fit on prev line */
461 BB_PUTCHAR(c);
462 }
463 cmdedit_x = width;
Mark Whitley4e338752001-01-26 20:42:23 +0000464 }
Erik Andersen13456d12000-03-16 08:09:57 +0000465}
466
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000467/* Move to end of line (by printing all chars till the end) */
Denys Vlasenko94043e82010-05-11 14:49:13 +0200468static void put_till_end_and_adv_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000469{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000470 while (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200471 put_cur_glyph_and_inc_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +0000472}
473
474/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000475static void goto_new_line(void)
476{
Denys Vlasenko94043e82010-05-11 14:49:13 +0200477 put_till_end_and_adv_cursor();
Denys Vlasenkof5018da2018-04-06 17:58:21 +0200478 /* "cursor == 0" is only if prompt is "" and user input is empty */
479 if (cursor == 0 || cmdedit_x != 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000480 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000481}
482
Rob Landley88621d72006-08-29 19:41:06 +0000483static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000484{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000485 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000486}
487
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200488/* Full or last/sole prompt line, reset edit cursor, calculate terminal cursor.
489 * cmdedit_y is always calculated for the last/sole prompt line.
490 */
491static void put_prompt_custom(bool is_full)
Denys Vlasenko248c3242010-05-17 00:45:44 +0200492{
Ron Yorstoncad3fc72021-02-03 20:47:14 +0100493 fputs_stdout((is_full ? cmdedit_prompt : prompt_last_line));
Denys Vlasenko248c3242010-05-17 00:45:44 +0200494 cursor = 0;
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100495 cmdedit_y = cmdedit_prmt_len / cmdedit_termw; /* new quasireal y */
496 cmdedit_x = cmdedit_prmt_len % cmdedit_termw;
Denys Vlasenko248c3242010-05-17 00:45:44 +0200497}
498
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200499#define put_prompt_last_line() put_prompt_custom(0)
500#define put_prompt() put_prompt_custom(1)
501
Eric Andersenaff114c2004-04-14 17:51:38 +0000502/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000503/* (optimized for slow terminals) */
504static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000505{
506 if (num > cursor)
507 num = cursor;
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200508 if (num == 0)
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000509 return;
510 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000511
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200512 if ((ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS)
513 && unicode_status == UNICODE_ON
514 ) {
515 /* correct NUM to be equal to _screen_ width */
516 int n = num;
517 num = 0;
518 while (--n >= 0)
519 adjust_width_and_validate_wc(&num, command_ps[cursor + n]);
520 if (num == 0)
521 return;
522 }
523
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000524 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000525 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000526 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000527 /* This is longer by 5 bytes on x86.
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000528 * Also gets miscompiled for ARM users
529 * (busybox.net/bugs/view.php?id=2274).
Denis Vlasenko6f043912008-02-18 22:28:03 +0000530 * printf(("\b\b\b\b" + 4) - num);
531 * return;
532 */
533 do {
534 bb_putchar('\b');
535 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000536 return;
537 }
Marek Polacek7b181072010-10-28 21:34:56 +0200538 printf(ESC"[%uD", num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000539 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000540 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000541
542 /* Need to go one or more lines up */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200543 if (ENABLE_UNICODE_WIDE_WCHARS) {
544 /* With wide chars, it is hard to "backtrack"
545 * and reliably figure out where to put cursor.
546 * Example (<> is a wide char; # is an ordinary char, _ cursor):
547 * |prompt: <><> |
548 * |<><><><><><> |
549 * |_ |
550 * and user presses left arrow. num = 1, cmdedit_x = 0,
551 * We need to go up one line, and then - how do we know that
552 * we need to go *10* positions to the right? Because
553 * |prompt: <>#<>|
554 * |<><><>#<><><>|
555 * |_ |
556 * in this situation we need to go *11* positions to the right.
557 *
558 * A simpler thing to do is to redraw everything from the start
559 * up to new cursor position (which is already known):
560 */
561 unsigned sv_cursor;
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200562 /* go to 1st column; go up to first line */
Marek Polacek7b181072010-10-28 21:34:56 +0200563 printf("\r" ESC"[%uA", cmdedit_y);
Denys Vlasenko248c3242010-05-17 00:45:44 +0200564 cmdedit_y = 0;
565 sv_cursor = cursor;
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200566 put_prompt_last_line(); /* sets cursor to 0 */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200567 while (cursor < sv_cursor)
568 put_cur_glyph_and_inc_cursor();
569 } else {
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200570 int lines_up;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200571 /* num = chars to go back from the beginning of current line: */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200572 num -= cmdedit_x;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200573 /* num=1...w: one line up, w+1...2w: two, etc: */
Denys Vlasenkobff71d32016-11-27 22:25:07 +0100574 lines_up = 1 + (num - 1) / cmdedit_termw;
575 cmdedit_x = (cmdedit_termw * cmdedit_y - num) % cmdedit_termw;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200576 cmdedit_y -= lines_up;
577 /* go to 1st column; go up */
Marek Polacek7b181072010-10-28 21:34:56 +0200578 printf("\r" ESC"[%uA", lines_up);
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200579 /* go to correct column.
Denys Vlasenkobbf1aa12010-05-17 12:33:13 +0200580 * xterm, konsole, Linux VT interpret 0 as 1 below! wow.
581 * need to *make sure* we skip it if cmdedit_x == 0 */
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200582 if (cmdedit_x)
Marek Polacek7b181072010-10-28 21:34:56 +0200583 printf(ESC"[%uC", cmdedit_x);
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000584 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000585}
586
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200587/* See redraw and draw_full below */
588static void draw_custom(int y, int back_cursor, bool is_full)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000589{
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200590 if (y > 0) /* up y lines */
Marek Polacek7b181072010-10-28 21:34:56 +0200591 printf(ESC"[%uA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000592 bb_putchar('\r');
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200593 put_prompt_custom(is_full);
Denys Vlasenko94043e82010-05-11 14:49:13 +0200594 put_till_end_and_adv_cursor();
595 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000596 input_backward(back_cursor);
597}
598
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +0200599/* Move y lines up, draw last/sole prompt line, editor line[s], and clear tail.
600 * goal: redraw the prompt+input+cursor in-place, overwriting the previous */
601#define redraw(y, back_cursor) draw_custom((y), (back_cursor), 0)
602
603/* Like above, but without moving up, and while using all the prompt lines.
604 * goal: draw a full prompt+input+cursor unrelated to a previous position.
605 * note: cmdedit_y always ends up relating to the last/sole prompt line */
606#define draw_full(back_cursor) draw_custom(0, (back_cursor), 1)
607
Paul Fox3f11b1b2005-08-04 19:04:46 +0000608/* Delete the char in front of the cursor, optionally saving it
609 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000610#if !ENABLE_FEATURE_EDITING_VI
611static void input_delete(void)
612#define input_delete(save) input_delete()
613#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000614static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000615#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000616{
Mark Whitley4e338752001-01-26 20:42:23 +0000617 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000618
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000619 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000620 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000621
Denis Vlasenko38f63192007-01-22 09:03:07 +0000622#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000623 if (save) {
624 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000625 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000626 newdelflag = 0;
627 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000628 if ((delptr - delbuf) < DELBUFSIZ)
629 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000630 }
631#endif
632
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200633 memmove(command_ps + j, command_ps + j + 1,
Denys Vlasenko9531f7d2009-07-16 14:33:16 +0200634 /* (command_len + 1 [because of NUL]) - (j + 1)
635 * simplified into (command_len - j) */
636 (command_len - j) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000637 command_len--;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200638 put_till_end_and_adv_cursor();
639 /* Last char is still visible, erase it (and more) */
640 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersenc470f442003-07-28 09:56:35 +0000641 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000642}
643
Denis Vlasenko38f63192007-01-22 09:03:07 +0000644#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000645static void put(void)
646{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000647 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000648 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000649
Paul Fox3f11b1b2005-08-04 19:04:46 +0000650 if (j == 0)
651 return;
652 ocursor = cursor;
653 /* open hole and then fill it */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200654 memmove(command_ps + cursor + j, command_ps + cursor,
655 (command_len - cursor + 1) * sizeof(command_ps[0]));
656 memcpy(command_ps + cursor, delbuf, j * sizeof(command_ps[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000657 command_len += j;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200658 put_till_end_and_adv_cursor();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000659 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000660}
661#endif
662
Mark Whitley4e338752001-01-26 20:42:23 +0000663/* Delete the char in back of the cursor */
664static void input_backspace(void)
665{
666 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000667 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000668 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000669 }
670}
671
Eric Andersenaff114c2004-04-14 17:51:38 +0000672/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000673static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000674{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000675 if (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200676 put_cur_glyph_and_inc_cursor();
Erik Andersenf0657d32000-04-12 17:49:52 +0000677}
678
Denis Vlasenko38f63192007-01-22 09:03:07 +0000679#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000680
Mike Shalf3763032010-11-22 03:49:18 +0100681//FIXME:
682//needs to be more clever: currently it thinks that "foo\ b<TAB>
683//matches the file named "foo bar", which is untrue.
684//Also, perhaps "foo b<TAB> needs to complete to "foo bar" <cursor>,
685//not "foo bar <cursor>...
686
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000687static void free_tab_completion_data(void)
688{
689 if (matches) {
690 while (num_matches)
691 free(matches[--num_matches]);
692 free(matches);
693 matches = NULL;
694 }
695}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000696
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000697static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000698{
Denys Vlasenkoc3797d42017-11-07 18:09:29 +0100699 unsigned char *p = (unsigned char*)matched;
700 while (*p) {
701 /* ESC attack fix: drop any string with control chars */
702 if (*p < ' '
703 || (!ENABLE_UNICODE_SUPPORT && *p >= 0x7f)
704 || (ENABLE_UNICODE_SUPPORT && *p == 0x7f)
705 ) {
706 free(matched);
707 return;
708 }
709 p++;
710 }
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000711 matches = xrealloc_vector(matches, 4, num_matches);
712 matches[num_matches] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000713 num_matches++;
714}
715
Mike Shalf3763032010-11-22 03:49:18 +0100716# if ENABLE_FEATURE_USERNAME_COMPLETION
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200717/* Replace "~user/..." with "/homedir/...".
718 * The parameter is malloced, free it or return it
719 * unchanged if no user is matched.
720 */
721static char *username_path_completion(char *ud)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000722{
723 struct passwd *entry;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200724 char *tilde_name = ud;
Denys Vlasenkof4fcd742021-10-11 23:08:31 +0200725 const char *home = NULL;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200726
727 ud++; /* skip ~ */
728 if (*ud == '/') { /* "~/..." */
Denys Vlasenkof4fcd742021-10-11 23:08:31 +0200729 home = get_homedir_or_NULL();
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200730 } else {
731 /* "~user/..." */
732 ud = strchr(ud, '/');
733 *ud = '\0'; /* "~user" */
734 entry = getpwnam(tilde_name + 1);
735 *ud = '/'; /* restore "~user/..." */
736 if (entry)
737 home = entry->pw_dir;
738 }
739 if (home) {
740 ud = concat_path_file(home, ud);
741 free(tilde_name);
742 tilde_name = ud;
743 }
744 return tilde_name;
745}
746
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200747/* ~use<tab> - find all users with this prefix.
748 * Return the length of the prefix used for matching.
749 */
750static NOINLINE unsigned complete_username(const char *ud)
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200751{
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100752 struct passwd *pw;
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200753 unsigned userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000754
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200755 ud++; /* skip ~ */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000756 userlen = strlen(ud);
757
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200758 setpwent();
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100759 while ((pw = getpwent()) != NULL) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200760 /* Null usernames should result in all users as possible completions. */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100761 if (/* !ud[0] || */ is_prefixed_with(pw->pw_name, ud)) {
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100762 add_match(xasprintf("~%s/", pw->pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000763 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000764 }
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100765 endpwent(); /* don't keep password file open */
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200766
767 return 1 + userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000768}
Mike Shalf3763032010-11-22 03:49:18 +0100769# endif /* FEATURE_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000770
771enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000772 FIND_EXE_ONLY = 0,
773 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000774 FIND_FILE_ONLY = 2,
775};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000776
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100777static unsigned path_parse(char ***p)
Mark Whitley4e338752001-01-26 20:42:23 +0000778{
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100779 unsigned npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000780 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000781 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000782 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000783
Denys Vlasenkoa97a7952020-12-16 11:14:08 +0100784# if EDITING_HAS_path_lookup
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000785 if (state->flags & WITH_PATH_LOOKUP)
786 pth = state->path_lookup;
787 else
Denys Vlasenkoa97a7952020-12-16 11:14:08 +0100788# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000789 pth = getenv("PATH");
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200790
791 /* PATH="" or PATH=":"? */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000792 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
793 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000794
Denis Vlasenko253ce002007-01-22 08:34:44 +0000795 tmp = (char*)pth;
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100796 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000797 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000798 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000799 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000800 break;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200801 tmp++;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000802 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000803 }
804
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100805 *p = res = xzalloc((npth + 1) * sizeof(res[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000806 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000807 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000808 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000809 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000810 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000811 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000812 *tmp++ = '\0'; /* ':' -> '\0' */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000813 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000814 }
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100815 /* special case: "match subdirectories of the current directory" */
816 /*res[npth++] = NULL; - filled by xzalloc() */
Mark Whitley4e338752001-01-26 20:42:23 +0000817 return npth;
818}
819
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200820/* Complete command, directory or file name.
821 * Return the length of the prefix used for matching.
822 */
823static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000824{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000825 char *path1[1];
826 char **paths = path1;
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100827 unsigned npaths;
828 unsigned i;
829 unsigned baselen;
830 const char *basecmd;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200831 char *dirbuf = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +0000832
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000833 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000834 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000835
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100836 basecmd = strrchr(command, '/');
837 if (!basecmd) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200838 if (type == FIND_EXE_ONLY)
839 npaths = path_parse(&paths);
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100840 basecmd = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000841 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000842 /* point to 'l' in "..../last_component" */
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100843 basecmd++;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200844 /* dirbuf = ".../.../.../" */
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100845 dirbuf = xstrndup(command, basecmd - command);
Mike Shalf3763032010-11-22 03:49:18 +0100846# if ENABLE_FEATURE_USERNAME_COMPLETION
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200847 if (dirbuf[0] == '~') /* ~/... or ~user/... */
848 dirbuf = username_path_completion(dirbuf);
Mike Shalf3763032010-11-22 03:49:18 +0100849# endif
Denys Vlasenko7063e862010-09-02 12:44:39 +0200850 path1[0] = dirbuf;
Mark Whitley4e338752001-01-26 20:42:23 +0000851 }
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100852 baselen = strlen(basecmd);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000853
Denys Vlasenko13360522016-10-24 01:25:05 +0200854 if (type == FIND_EXE_ONLY && !dirbuf) {
Ron Yorston9e2a5662020-01-21 16:01:58 +0000855# if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1
Ron Yorston37788982018-11-17 17:48:14 +0000856 const char *p = applet_names;
Ron Yorston2b919582016-04-08 11:57:20 +0100857 while (*p) {
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100858 if (strncmp(basecmd, p, baselen) == 0)
Ron Yorstonf23264b2015-05-29 11:31:40 +0100859 add_match(xstrdup(p));
Ron Yorston37788982018-11-17 17:48:14 +0000860 while (*p++ != '\0')
Ron Yorston2b919582016-04-08 11:57:20 +0100861 continue;
Ron Yorstonf23264b2015-05-29 11:31:40 +0100862 }
Denys Vlasenkof128bdb2017-07-29 00:59:24 +0200863# endif
Ron Yorston9e2a5662020-01-21 16:01:58 +0000864# if EDITING_HAS_get_exe_name
865 if (state->get_exe_name) {
866 i = 0;
867 for (;;) {
868 const char *b = state->get_exe_name(i++);
869 if (!b)
870 break;
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100871 if (strncmp(basecmd, b, baselen) == 0)
Ron Yorston9e2a5662020-01-21 16:01:58 +0000872 add_match(xstrdup(b));
873 }
874 }
875# endif
876 }
Ron Yorstonf23264b2015-05-29 11:31:40 +0100877
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000878 for (i = 0; i < npaths; i++) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200879 DIR *dir;
880 struct dirent *next;
881 struct stat st;
882 char *found;
Ron Yorston760b6272021-02-18 09:50:29 +0000883 const char *lpath;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200884
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100885 if (paths[i] == NULL) { /* path_parse()'s last component? */
886 /* in PATH completion, current dir's subdir names
887 * can be completions (but only subdirs, not files).
888 */
Ron Yorston8baa6432020-12-11 12:34:21 +0000889 type = FIND_DIR_ONLY;
890 paths[i] = (char *)".";
891 }
892
Ron Yorston760b6272021-02-18 09:50:29 +0000893 lpath = *paths[i] ? paths[i] : ".";
894 dir = opendir(lpath);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000895 if (!dir)
896 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000897
898 while ((next = readdir(dir)) != NULL) {
Denys Vlasenko39263632010-09-03 14:11:08 +0200899 unsigned len;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200900 const char *name_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000901
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200902 /* .../<tab>: bash 3.2.0 shows dotfiles, but not . and .. */
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100903 if (!basecmd[0] && DOT_OR_DOTDOT(name_found))
Mark Whitley4e338752001-01-26 20:42:23 +0000904 continue;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200905 /* match? */
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100906 if (strncmp(basecmd, name_found, baselen) != 0)
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200907 continue; /* no */
908
Ron Yorston760b6272021-02-18 09:50:29 +0000909 found = concat_path_file(lpath, name_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000910 /* NB: stat() first so that we see is it a directory;
911 * but if that fails, use lstat() so that
912 * we still match dangling links */
913 if (stat(found, &st) && lstat(found, &st))
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200914 goto cont; /* hmm, remove in progress? */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000915
Denys Vlasenko39263632010-09-03 14:11:08 +0200916 /* Save only name */
917 len = strlen(name_found);
918 found = xrealloc(found, len + 2); /* +2: for slash and NUL */
919 strcpy(found, name_found);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000920
Mark Whitley4e338752001-01-26 20:42:23 +0000921 if (S_ISDIR(st.st_mode)) {
Ron Yorston8506dd62020-12-10 14:44:57 +0000922 /* skip directories if searching PATH */
923 if (type == FIND_EXE_ONLY && !dirbuf)
924 goto cont;
Denys Vlasenko39263632010-09-03 14:11:08 +0200925 /* name is a directory, add slash */
926 found[len] = '/';
927 found[len + 1] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +0000928 } else {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200929 /* skip files if looking for dirs only (example: cd) */
Eric Andersenc470f442003-07-28 09:56:35 +0000930 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000931 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000932 }
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200933 /* add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000934 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000935 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000936 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000937 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000938 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000939 closedir(dir);
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200940 } /* for every path */
941
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000942 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000943 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000944 free(paths);
945 }
Denys Vlasenko39263632010-09-03 14:11:08 +0200946 free(dirbuf);
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200947
Denys Vlasenko1d180cd2020-12-16 10:59:20 +0100948 return baselen;
Erik Andersen6273f652000-03-17 01:12:41 +0000949}
Erik Andersenf0657d32000-04-12 17:49:52 +0000950
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200951/* build_match_prefix:
Denys Vlasenko76939e72010-09-03 14:09:24 +0200952 * On entry, match_buf contains everything up to cursor at the moment <tab>
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200953 * was pressed. This function looks at it, figures out what part of it
954 * constitutes the command/file/directory prefix to use for completion,
Denys Vlasenko76939e72010-09-03 14:09:24 +0200955 * and rewrites match_buf to contain only that part.
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200956 */
Denys Vlasenko76939e72010-09-03 14:09:24 +0200957#define dbg_bmp 0
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200958/* Helpers: */
959/* QUOT is used on elements of int_buf[], which are bytes,
960 * not Unicode chars. Therefore it works correctly even in Unicode mode.
961 */
962#define QUOT (UCHAR_MAX+1)
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200963static void remove_chunk(int16_t *int_buf, int beg, int end)
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200964{
965 /* beg must be <= end */
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200966 if (beg == end)
967 return;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200968
969 while ((int_buf[beg] = int_buf[end]) != 0)
970 beg++, end++;
971
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200972 if (dbg_bmp) {
973 int i;
974 for (i = 0; int_buf[i]; i++)
975 bb_putchar((unsigned char)int_buf[i]);
976 bb_putchar('\n');
977 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200978}
Denys Vlasenko76939e72010-09-03 14:09:24 +0200979/* Caller ensures that match_buf points to a malloced buffer
980 * big enough to hold strlen(match_buf)*2 + 2
981 */
982static NOINLINE int build_match_prefix(char *match_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000983{
984 int i, j;
985 int command_mode;
Denys Vlasenko76939e72010-09-03 14:09:24 +0200986 int16_t *int_buf = (int16_t*)match_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000987
Denys Vlasenko76939e72010-09-03 14:09:24 +0200988 if (dbg_bmp) printf("\n%s\n", match_buf);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200989
Denys Vlasenko76939e72010-09-03 14:09:24 +0200990 /* Copy in reverse order, since they overlap */
991 i = strlen(match_buf);
992 do {
993 int_buf[i] = (unsigned char)match_buf[i];
994 i--;
995 } while (i >= 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000996
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200997 /* Mark every \c as "quoted c" */
Denys Vlasenko76939e72010-09-03 14:09:24 +0200998 for (i = 0; int_buf[i]; i++) {
999 if (int_buf[i] == '\\') {
1000 remove_chunk(int_buf, i, i + 1);
1001 int_buf[i] |= QUOT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001002 }
Tomas Heinrichb8909c52010-05-16 20:46:53 +02001003 }
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001004 /* Quote-mark "chars" and 'chars', drop delimiters */
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001005 {
1006 int in_quote = 0;
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001007 i = 0;
1008 while (int_buf[i]) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001009 int cur = int_buf[i];
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001010 if (!cur)
1011 break;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001012 if (cur == '\'' || cur == '"') {
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001013 if (!in_quote || (cur == in_quote)) {
1014 in_quote ^= cur;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001015 remove_chunk(int_buf, i, i + 1);
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001016 continue;
1017 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001018 }
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001019 if (in_quote)
1020 int_buf[i] = cur | QUOT;
1021 i++;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001022 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001023 }
1024
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001025 /* Remove everything up to command delimiters:
1026 * ';' ';;' '&' '|' '&&' '||',
1027 * but careful with '>&' '<&' '>|'
1028 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001029 for (i = 0; int_buf[i]; i++) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001030 int cur = int_buf[i];
1031 if (cur == ';' || cur == '&' || cur == '|') {
1032 int prev = i ? int_buf[i - 1] : 0;
1033 if (cur == '&' && (prev == '>' || prev == '<')) {
1034 continue;
1035 } else if (cur == '|' && prev == '>') {
1036 continue;
1037 }
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001038 remove_chunk(int_buf, 0, i + 1 + (cur == int_buf[i + 1]));
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001039 i = -1; /* back to square 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001040 }
1041 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001042 /* Remove all `cmd` */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001043 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001044 if (int_buf[i] == '`') {
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001045 for (j = i + 1; int_buf[j]; j++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001046 if (int_buf[j] == '`') {
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001047 /* `cmd` should count as a word:
1048 * `cmd` c<tab> should search for files c*,
1049 * not commands c*. Therefore we don't drop
1050 * `cmd` entirely, we replace it with single `.
1051 */
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001052 remove_chunk(int_buf, i, j);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001053 goto next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001054 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001055 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001056 /* No closing ` - command mode, remove all up to ` */
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001057 remove_chunk(int_buf, 0, i + 1);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001058 break;
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001059 next: ;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001060 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001061 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001062
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001063 /* Remove "cmd (" and "cmd {"
1064 * Example: "if { c<tab>"
1065 * In this example, c should be matched as command pfx.
1066 */
1067 for (i = 0; int_buf[i]; i++) {
1068 if (int_buf[i] == '(' || int_buf[i] == '{') {
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001069 remove_chunk(int_buf, 0, i + 1);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001070 i = -1; /* back to square 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001071 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001072 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001073
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001074 /* Remove leading unquoted spaces */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001075 for (i = 0; int_buf[i]; i++)
1076 if (int_buf[i] != ' ')
1077 break;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001078 remove_chunk(int_buf, 0, i);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001079
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001080 /* Determine completion mode */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001081 command_mode = FIND_EXE_ONLY;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001082 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001083 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001084 if (int_buf[i] == ' '
1085 && command_mode == FIND_EXE_ONLY
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001086 && (char)int_buf[0] == 'c'
1087 && (char)int_buf[1] == 'd'
1088 && i == 2 /* -> int_buf[2] == ' ' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001089 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001090 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001091 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001092 command_mode = FIND_FILE_ONLY;
1093 break;
1094 }
1095 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001096 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001097 if (dbg_bmp) printf("command_mode(0:exe/1:dir/2:file):%d\n", command_mode);
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001098
1099 /* Remove everything except last word */
Denys Vlasenkob068bd72010-09-02 12:01:11 +02001100 for (i = 0; int_buf[i]; i++) /* quasi-strlen(int_buf) */
1101 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001102 for (--i; i >= 0; i--) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001103 int cur = int_buf[i];
Natanael Copa7323bca2021-04-09 17:02:00 +02001104 if (cur == ' ' || cur == '<' || cur == '>' || cur == '|' || cur == '&' || cur == '=') {
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001105 remove_chunk(int_buf, 0, i + 1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001106 break;
1107 }
1108 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001109
Denys Vlasenko76939e72010-09-03 14:09:24 +02001110 /* Convert back to string of _chars_ */
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001111 i = 0;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001112 while ((match_buf[i] = int_buf[i]) != '\0')
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001113 i++;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001114
Denys Vlasenko76939e72010-09-03 14:09:24 +02001115 if (dbg_bmp) printf("final match_buf:'%s'\n", match_buf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001116
1117 return command_mode;
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02001118}
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001119
Glenn L McGrath4d001292003-01-06 01:11:50 +00001120/*
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001121 * Display by column (original idea from ls applet,
1122 * very optimized by me [Vladimir] :)
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001123 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001124static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +00001125{
1126 int ncols, row;
1127 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001128 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001129 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001130 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001131
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001132 /* find the longest file name - use that as the column width */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001133 for (row = 0; row < nrows; row++) {
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001134 l = unicode_strwidth(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001135 if (column_width < l)
1136 column_width = l;
1137 }
1138 column_width += 2; /* min space for columns */
1139 ncols = cmdedit_termw / column_width;
1140
1141 if (ncols > 1) {
1142 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001143 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +00001144 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001145 } else {
1146 ncols = 1;
1147 }
1148 for (row = 0; row < nrows; row++) {
1149 int n = row;
1150 int nc;
1151
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001152 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001153 printf("%s%-*s", matches[n],
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001154 (int)(column_width - unicode_strwidth(matches[n])), ""
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001155 );
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001156 }
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001157 if (ENABLE_UNICODE_SUPPORT)
Denys Vlasenko349d72c2018-09-30 16:56:56 +02001158 puts(printable_string(matches[n]));
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001159 else
1160 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001161 }
1162}
1163
Mike Shalf3763032010-11-22 03:49:18 +01001164static const char *is_special_char(char c)
1165{
Denys Vlasenko6d2463a2021-09-17 17:32:30 +02001166 // {: It's mandatory to escape { only if entire name is "{"
1167 // (otherwise it's not special. Example: file named "{ "
1168 // can be escaped simply as "{\ "; "{a" or "a{" need no escaping),
1169 // or if shell supports brace expansion
1170 // (ash doesn't, hush optionally does).
1171 // (): unlike {, shell treats () specially even in contexts
1172 // where they clearly are not valid (e.g. "echo )" is an error).
1173 // #: needs escaping to not start a shell comment.
1174 return strchr(" `'\"\\#$~?*[{()&;|<>", c);
1175 // Used to also have %^=+}]: but not necessary to escape?
Mike Shalf3763032010-11-22 03:49:18 +01001176}
1177
1178static char *quote_special_chars(char *found)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001179{
1180 int l = 0;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001181 char *s = xzalloc((strlen(found) + 1) * 2);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001182
1183 while (*found) {
Mike Shalf3763032010-11-22 03:49:18 +01001184 if (is_special_char(*found))
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001185 s[l++] = '\\';
1186 s[l++] = *found++;
1187 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001188 /* s[l] = '\0'; - already is */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001189 return s;
1190}
1191
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001192/* Do TAB completion */
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001193static NOINLINE void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001194{
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001195 char *chosen_match;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001196 char *match_buf;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001197 size_t len_found;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001198 /* Length of string used for matching */
1199 unsigned match_pfx_len = match_pfx_len;
1200 int find_type;
Mike Shalf3763032010-11-22 03:49:18 +01001201# if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001202 /* cursor pos in command converted to multibyte form */
1203 int cursor_mb;
Mike Shalf3763032010-11-22 03:49:18 +01001204# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001205 if (!(state->flags & TAB_COMPLETION))
1206 return;
1207
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001208 if (*lastWasTab) {
1209 /* The last char was a TAB too.
1210 * Print a list of all the available choices.
1211 */
Denys Vlasenkoa46e16e2010-09-03 13:05:51 +02001212 if (num_matches > 0) {
1213 /* cursor will be changed by goto_new_line() */
Denys Vlasenko044b1802009-07-12 02:50:35 +02001214 int sav_cursor = cursor;
Mark Whitley4e338752001-01-26 20:42:23 +00001215 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001216 showfiles();
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001217 draw_full(command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001218 }
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001219 return;
Erik Andersenf0657d32000-04-12 17:49:52 +00001220 }
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001221
1222 *lastWasTab = 1;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001223 chosen_match = NULL;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001224
Denys Vlasenko76939e72010-09-03 14:09:24 +02001225 /* Make a local copy of the string up to the position of the cursor.
1226 * build_match_prefix will expand it into int16_t's, need to allocate
1227 * twice as much as the string_len+1.
1228 * (we then also (ab)use this extra space later - see (**))
1229 */
1230 match_buf = xmalloc(MAX_LINELEN * sizeof(int16_t));
Mike Shalf3763032010-11-22 03:49:18 +01001231# if !ENABLE_UNICODE_SUPPORT
Denys Vlasenko76939e72010-09-03 14:09:24 +02001232 save_string(match_buf, cursor + 1); /* +1 for NUL */
Mike Shalf3763032010-11-22 03:49:18 +01001233# else
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001234 {
1235 CHAR_T wc = command_ps[cursor];
1236 command_ps[cursor] = BB_NUL;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001237 save_string(match_buf, MAX_LINELEN);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001238 command_ps[cursor] = wc;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001239 cursor_mb = strlen(match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001240 }
Mike Shalf3763032010-11-22 03:49:18 +01001241# endif
Denys Vlasenko76939e72010-09-03 14:09:24 +02001242 find_type = build_match_prefix(match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001243
1244 /* Free up any memory already allocated */
1245 free_tab_completion_data();
1246
Mike Shalf3763032010-11-22 03:49:18 +01001247# if ENABLE_FEATURE_USERNAME_COMPLETION
1248 /* If the word starts with ~ and there is no slash in the word,
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001249 * then try completing this word as a username. */
1250 if (state->flags & USERNAME_COMPLETION)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001251 if (match_buf[0] == '~' && strchr(match_buf, '/') == NULL)
1252 match_pfx_len = complete_username(match_buf);
Mike Shalf3763032010-11-22 03:49:18 +01001253# endif
1254 /* If complete_username() did not match,
1255 * try to match a command in $PATH, or a directory, or a file */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001256 if (!matches)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001257 match_pfx_len = complete_cmd_dir_file(match_buf, find_type);
Mike Shalf3763032010-11-22 03:49:18 +01001258
1259 /* Account for backslashes which will be inserted
1260 * by quote_special_chars() later */
1261 {
1262 const char *e = match_buf + strlen(match_buf);
1263 const char *s = e - match_pfx_len;
1264 while (s < e)
1265 if (is_special_char(*s++))
1266 match_pfx_len++;
1267 }
1268
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001269 /* Remove duplicates */
1270 if (matches) {
Mike Shalf3763032010-11-22 03:49:18 +01001271 unsigned i, n = 0;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001272 qsort_string_vector(matches, num_matches);
1273 for (i = 0; i < num_matches - 1; ++i) {
1274 //if (matches[i] && matches[i+1]) { /* paranoia */
1275 if (strcmp(matches[i], matches[i+1]) == 0) {
1276 free(matches[i]);
1277 //matches[i] = NULL; /* paranoia */
1278 } else {
1279 matches[n++] = matches[i];
1280 }
1281 //}
1282 }
1283 matches[n++] = matches[i];
1284 num_matches = n;
1285 }
Mike Shalf3763032010-11-22 03:49:18 +01001286
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001287 /* Did we find exactly one match? */
1288 if (num_matches != 1) { /* no */
1289 char *cp;
1290 beep();
1291 if (!matches)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001292 goto ret; /* no matches at all */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001293 /* Find common prefix */
1294 chosen_match = xstrdup(matches[0]);
1295 for (cp = chosen_match; *cp; cp++) {
1296 unsigned n;
1297 for (n = 1; n < num_matches; n++) {
1298 if (matches[n][cp - chosen_match] != *cp) {
1299 goto stop;
1300 }
1301 }
1302 }
1303 stop:
1304 if (cp == chosen_match) { /* have unique prefix? */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001305 goto ret; /* no */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001306 }
1307 *cp = '\0';
Mike Shalf3763032010-11-22 03:49:18 +01001308 cp = quote_special_chars(chosen_match);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001309 free(chosen_match);
1310 chosen_match = cp;
1311 len_found = strlen(chosen_match);
1312 } else { /* exactly one match */
1313 /* Next <tab> is not a double-tab */
1314 *lastWasTab = 0;
1315
Mike Shalf3763032010-11-22 03:49:18 +01001316 chosen_match = quote_special_chars(matches[0]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001317 len_found = strlen(chosen_match);
1318 if (chosen_match[len_found-1] != '/') {
1319 chosen_match[len_found] = ' ';
1320 chosen_match[++len_found] = '\0';
1321 }
1322 }
1323
Mike Shalf3763032010-11-22 03:49:18 +01001324# if !ENABLE_UNICODE_SUPPORT
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001325 /* Have space to place the match? */
1326 /* The result consists of three parts with these lengths: */
1327 /* cursor + (len_found - match_pfx_len) + (command_len - cursor) */
1328 /* it simplifies into: */
1329 if ((int)(len_found - match_pfx_len + command_len) < S.maxsize) {
1330 int pos;
1331 /* save tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001332 strcpy(match_buf, &command_ps[cursor]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001333 /* add match and tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001334 sprintf(&command_ps[cursor], "%s%s", chosen_match + match_pfx_len, match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001335 command_len = strlen(command_ps);
1336 /* new pos */
1337 pos = cursor + len_found - match_pfx_len;
1338 /* write out the matched command */
1339 redraw(cmdedit_y, command_len - pos);
1340 }
Mike Shalf3763032010-11-22 03:49:18 +01001341# else
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001342 {
Denys Vlasenko76939e72010-09-03 14:09:24 +02001343 /* Use 2nd half of match_buf as scratch space - see (**) */
1344 char *command = match_buf + MAX_LINELEN;
1345 int len = save_string(command, MAX_LINELEN);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001346 /* Have space to place the match? */
1347 /* cursor_mb + (len_found - match_pfx_len) + (len - cursor_mb) */
1348 if ((int)(len_found - match_pfx_len + len) < MAX_LINELEN) {
1349 int pos;
1350 /* save tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001351 strcpy(match_buf, &command[cursor_mb]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001352 /* where do we want to have cursor after all? */
1353 strcpy(&command[cursor_mb], chosen_match + match_pfx_len);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02001354 len = load_string(command);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001355 /* add match and tail */
Denys Vlasenkofe2d8062021-04-14 17:52:18 +02001356 stpcpy(stpcpy(&command[cursor_mb], chosen_match + match_pfx_len), match_buf);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02001357 command_len = load_string(command);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001358 /* write out the matched command */
1359 /* paranoia: load_string can return 0 on conv error,
1360 * prevent passing pos = (0 - 12) to redraw */
1361 pos = command_len - len;
1362 redraw(cmdedit_y, pos >= 0 ? pos : 0);
1363 }
1364 }
Mike Shalf3763032010-11-22 03:49:18 +01001365# endif
Denys Vlasenko76939e72010-09-03 14:09:24 +02001366 ret:
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001367 free(chosen_match);
Denys Vlasenko76939e72010-09-03 14:09:24 +02001368 free(match_buf);
Erik Andersenf0657d32000-04-12 17:49:52 +00001369}
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001370
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001371#endif /* FEATURE_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001372
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001373
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001374line_input_t* FAST_FUNC new_line_input_t(int flags)
1375{
1376 line_input_t *n = xzalloc(sizeof(*n));
1377 n->flags = flags;
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02001378 n->timeout = -1;
Denys Vlasenko79df4812014-01-10 14:38:26 +01001379#if MAX_HISTORY > 0
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001380 n->max_history = MAX_HISTORY;
Denys Vlasenko79df4812014-01-10 14:38:26 +01001381#endif
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001382 return n;
1383}
1384
1385
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001386#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001387
Flemming Madsend96ffda2013-04-07 18:47:24 +02001388unsigned FAST_FUNC size_from_HISTFILESIZE(const char *hp)
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001389{
1390 int size = MAX_HISTORY;
1391 if (hp) {
1392 size = atoi(hp);
1393 if (size <= 0)
1394 return 1;
1395 if (size > MAX_HISTORY)
1396 return MAX_HISTORY;
1397 }
1398 return size;
1399}
1400
Denis Vlasenko682ad302008-09-27 01:28:56 +00001401static void save_command_ps_at_cur_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001402{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001403 if (command_ps[0] != BB_NUL) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001404 int cur = state->cur_history;
1405 free(state->history[cur]);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001406
Denys Vlasenko19158a82010-03-26 14:06:56 +01001407# if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001408 {
1409 char tbuf[MAX_LINELEN];
1410 save_string(tbuf, sizeof(tbuf));
1411 state->history[cur] = xstrdup(tbuf);
1412 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001413# else
Denis Vlasenko682ad302008-09-27 01:28:56 +00001414 state->history[cur] = xstrdup(command_ps);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001415# endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001416 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001417}
1418
1419/* state->flags is already checked to be nonzero */
1420static int get_previous_history(void)
1421{
1422 if ((state->flags & DO_HISTORY) && state->cur_history) {
1423 save_command_ps_at_cur_history();
1424 state->cur_history--;
1425 return 1;
1426 }
1427 beep();
1428 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001429}
1430
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001431static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001432{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001433 if (state->flags & DO_HISTORY) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001434 if (state->cur_history < state->cnt_history) {
1435 save_command_ps_at_cur_history(); /* save the current history line */
1436 return ++state->cur_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001437 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001438 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001439 beep();
1440 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001441}
Robert Griebl350d26b2002-12-03 22:45:46 +00001442
Flemming Madsend96ffda2013-04-07 18:47:24 +02001443/* Lists command history. Used by shell 'history' builtins */
1444void FAST_FUNC show_history(const line_input_t *st)
1445{
1446 int i;
1447
1448 if (!st)
1449 return;
1450 for (i = 0; i < st->cnt_history; i++)
1451 printf("%4d %s\n", i, st->history[i]);
1452}
1453
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01001454# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denys Vlasenko3d27d432018-12-27 18:03:20 +01001455void FAST_FUNC free_line_input_t(line_input_t *n)
1456{
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01001457 if (n) {
1458 int i = n->cnt_history;
1459 while (i > 0)
1460 free(n->history[--i]);
1461 free(n);
1462 }
Denys Vlasenko3d27d432018-12-27 18:03:20 +01001463}
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01001464# else
1465/* #defined to free() in libbb.h */
1466# endif
Denys Vlasenko3d27d432018-12-27 18:03:20 +01001467
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001468# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001469/* We try to ensure that concurrent additions to the history
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001470 * do not overwrite each other.
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001471 * Otherwise shell users get unhappy.
1472 *
1473 * History file is trimmed lazily, when it grows several times longer
1474 * than configured MAX_HISTORY lines.
1475 */
1476
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001477/* state->flags is already checked to be nonzero */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001478static void load_history(line_input_t *st_parm)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001479{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001480 char *temp_h[MAX_HISTORY];
1481 char *line;
Robert Griebl350d26b2002-12-03 22:45:46 +00001482 FILE *fp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001483 unsigned idx, i, line_len;
Robert Griebl350d26b2002-12-03 22:45:46 +00001484
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001485 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +00001486
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001487 fp = fopen_for_read(st_parm->hist_file);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001488 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001489 /* clean up old history */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001490 for (idx = st_parm->cnt_history; idx > 0;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001491 idx--;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001492 free(st_parm->history[idx]);
1493 st_parm->history[idx] = NULL;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001494 }
1495
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001496 /* fill temp_h[], retaining only last MAX_HISTORY lines */
1497 memset(temp_h, 0, sizeof(temp_h));
Denys Vlasenkobede2152011-09-04 16:12:33 +02001498 idx = 0;
Dennis Groenendeee3562012-04-24 22:40:58 +02001499 st_parm->cnt_history_in_file = 0;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001500 while ((line = xmalloc_fgetline(fp)) != NULL) {
1501 if (line[0] == '\0') {
1502 free(line);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001503 continue;
1504 }
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001505 free(temp_h[idx]);
1506 temp_h[idx] = line;
Dennis Groenendeee3562012-04-24 22:40:58 +02001507 st_parm->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001508 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001509 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001510 idx = 0;
Robert Griebl350d26b2002-12-03 22:45:46 +00001511 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001512 fclose(fp);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001513
1514 /* find first non-NULL temp_h[], if any */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001515 if (st_parm->cnt_history_in_file) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001516 while (temp_h[idx] == NULL) {
1517 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001518 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001519 idx = 0;
1520 }
1521 }
1522
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001523 /* copy temp_h[] to st_parm->history[] */
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001524 for (i = 0; i < st_parm->max_history;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001525 line = temp_h[idx];
1526 if (!line)
1527 break;
1528 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001529 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001530 idx = 0;
1531 line_len = strlen(line);
1532 if (line_len >= MAX_LINELEN)
1533 line[MAX_LINELEN-1] = '\0';
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001534 st_parm->history[i++] = line;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001535 }
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001536 st_parm->cnt_history = i;
Denys Vlasenkobede2152011-09-04 16:12:33 +02001537 if (ENABLE_FEATURE_EDITING_SAVE_ON_EXIT)
1538 st_parm->cnt_history_in_file = i;
Robert Griebl350d26b2002-12-03 22:45:46 +00001539 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001540}
1541
Denys Vlasenkobede2152011-09-04 16:12:33 +02001542# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1543void save_history(line_input_t *st)
1544{
1545 FILE *fp;
1546
Denys Vlasenko00eb23b2020-12-21 21:36:58 +01001547 if (!st || !st->hist_file)
Denys Vlasenkobede2152011-09-04 16:12:33 +02001548 return;
1549 if (st->cnt_history <= st->cnt_history_in_file)
1550 return;
1551
1552 fp = fopen(st->hist_file, "a");
1553 if (fp) {
1554 int i, fd;
1555 char *new_name;
1556 line_input_t *st_temp;
1557
1558 for (i = st->cnt_history_in_file; i < st->cnt_history; i++)
1559 fprintf(fp, "%s\n", st->history[i]);
1560 fclose(fp);
1561
1562 /* we may have concurrently written entries from others.
1563 * load them */
1564 st_temp = new_line_input_t(st->flags);
1565 st_temp->hist_file = st->hist_file;
1566 st_temp->max_history = st->max_history;
1567 load_history(st_temp);
1568
1569 /* write out temp file and replace hist_file atomically */
1570 new_name = xasprintf("%s.%u.new", st->hist_file, (int) getpid());
1571 fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1572 if (fd >= 0) {
1573 fp = xfdopen_for_write(fd);
1574 for (i = 0; i < st_temp->cnt_history; i++)
1575 fprintf(fp, "%s\n", st_temp->history[i]);
1576 fclose(fp);
1577 if (rename(new_name, st->hist_file) == 0)
1578 st->cnt_history_in_file = st_temp->cnt_history;
1579 }
1580 free(new_name);
1581 free_line_input_t(st_temp);
1582 }
1583}
1584# else
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001585static void save_history(char *str)
Robert Griebl350d26b2002-12-03 22:45:46 +00001586{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001587 int fd;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001588 int len, len2;
Eric Andersenc470f442003-07-28 09:56:35 +00001589
Denys Vlasenkobede2152011-09-04 16:12:33 +02001590 if (!state->hist_file)
1591 return;
1592
Wolfram Sang2e9aeae2010-11-15 02:58:28 +01001593 fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0600);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001594 if (fd < 0)
1595 return;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001596 xlseek(fd, 0, SEEK_END); /* paranoia */
1597 len = strlen(str);
1598 str[len] = '\n'; /* we (try to) do atomic write */
1599 len2 = full_write(fd, str, len + 1);
1600 str[len] = '\0';
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001601 close(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001602 if (len2 != len + 1)
1603 return; /* "wtf?" */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001604
1605 /* did we write so much that history file needs trimming? */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001606 state->cnt_history_in_file++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001607 if (state->cnt_history_in_file > state->max_history * 4) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001608 char *new_name;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001609 line_input_t *st_temp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001610
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001611 /* we may have concurrently written entries from others.
1612 * load them */
1613 st_temp = new_line_input_t(state->flags);
1614 st_temp->hist_file = state->hist_file;
Denys Vlasenkoe3d8d072011-03-31 14:39:38 +02001615 st_temp->max_history = state->max_history;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001616 load_history(st_temp);
1617
1618 /* write out temp file and replace hist_file atomically */
1619 new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
Denys Vlasenko4840ae82011-09-04 15:28:03 +02001620 fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Wolfram Sang2e9aeae2010-11-15 02:58:28 +01001621 if (fd >= 0) {
1622 FILE *fp;
1623 int i;
1624
1625 fp = xfdopen_for_write(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001626 for (i = 0; i < st_temp->cnt_history; i++)
1627 fprintf(fp, "%s\n", st_temp->history[i]);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001628 fclose(fp);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001629 if (rename(new_name, state->hist_file) == 0)
1630 state->cnt_history_in_file = st_temp->cnt_history;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001631 }
1632 free(new_name);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001633 free_line_input_t(st_temp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001634 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001635}
Denys Vlasenkobede2152011-09-04 16:12:33 +02001636# endif
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001637# else
1638# define load_history(a) ((void)0)
1639# define save_history(a) ((void)0)
1640# endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001641
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001642static void remember_in_history(char *str)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001643{
1644 int i;
1645
1646 if (!(state->flags & DO_HISTORY))
1647 return;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001648 if (str[0] == '\0')
1649 return;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001650 i = state->cnt_history;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001651 /* Don't save dupes */
1652 if (i && strcmp(state->history[i-1], str) == 0)
1653 return;
1654
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001655 free(state->history[state->max_history]); /* redundant, paranoia */
1656 state->history[state->max_history] = NULL; /* redundant, paranoia */
Denis Vlasenko682ad302008-09-27 01:28:56 +00001657
1658 /* If history[] is full, remove the oldest command */
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001659 /* we need to keep history[state->max_history] empty, hence >=, not > */
1660 if (i >= state->max_history) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001661 free(state->history[0]);
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001662 for (i = 0; i < state->max_history-1; i++)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001663 state->history[i] = state->history[i+1];
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001664 /* i == state->max_history-1 */
Denys Vlasenkoc0cae522011-11-04 01:09:09 +01001665# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1666 if (state->cnt_history_in_file)
Denys Vlasenkobede2152011-09-04 16:12:33 +02001667 state->cnt_history_in_file--;
Denys Vlasenkoc0cae522011-11-04 01:09:09 +01001668# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001669 }
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001670 /* i <= state->max_history-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001671 state->history[i++] = xstrdup(str);
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001672 /* i <= state->max_history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001673 state->cur_history = i;
1674 state->cnt_history = i;
Denys Vlasenkobede2152011-09-04 16:12:33 +02001675# if ENABLE_FEATURE_EDITING_SAVEHISTORY && !ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1676 save_history(str);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001677# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001678}
1679
1680#else /* MAX_HISTORY == 0 */
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001681# define remember_in_history(a) ((void)0)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001682#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001683
1684
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001685#if ENABLE_FEATURE_EDITING_VI
Erik Andersen6273f652000-03-17 01:12:41 +00001686/*
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001687 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001688 */
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001689static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001690vi_Word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001691{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001692 CHAR_T *command = command_ps;
1693
1694 while (cursor < command_len && !BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001695 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001696 if (eat) while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001697 input_forward();
1698}
1699
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001700static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001701vi_word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001702{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001703 CHAR_T *command = command_ps;
1704
Natanael Copa7e6f9312016-08-14 23:30:29 +02001705 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001706 while (cursor < command_len
Natanael Copa7e6f9312016-08-14 23:30:29 +02001707 && (BB_isalnum_or_underscore(command[cursor+1]))
Denys Vlasenko13028922009-07-12 00:51:15 +02001708 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001709 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001710 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001711 } else if (BB_ispunct(command[cursor])) {
1712 while (cursor < command_len && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001713 input_forward();
1714 }
1715
Denis Vlasenko253ce002007-01-22 08:34:44 +00001716 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001717 input_forward();
1718
Denys Vlasenko13028922009-07-12 00:51:15 +02001719 if (eat) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001720 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001721 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001722 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001723}
1724
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001725static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001726vi_End_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001727{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001728 CHAR_T *command = command_ps;
1729
Paul Fox3f11b1b2005-08-04 19:04:46 +00001730 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001731 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001732 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001733 while (cursor < command_len-1 && !BB_isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001734 input_forward();
1735}
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
Denis Vlasenko253ce002007-01-22 08:34:44 +00001742 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001743 return;
1744 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001745 while (cursor < command_len-1 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001746 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001747 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001748 return;
Natanael Copa7e6f9312016-08-14 23:30:29 +02001749 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001750 while (cursor < command_len-1
Natanael Copa7e6f9312016-08-14 23:30:29 +02001751 && (BB_isalnum_or_underscore(command[cursor+1]))
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001752 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001753 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001754 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001755 } else if (BB_ispunct(command[cursor])) {
1756 while (cursor < command_len-1 && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001757 input_forward();
1758 }
1759}
1760
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001761static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001762vi_Back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001763{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001764 CHAR_T *command = command_ps;
1765
1766 while (cursor > 0 && BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001767 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001768 while (cursor > 0 && !BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001769 input_backward(1);
1770}
1771
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001772static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001773vi_back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001774{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001775 CHAR_T *command = command_ps;
1776
Paul Fox3f11b1b2005-08-04 19:04:46 +00001777 if (cursor <= 0)
1778 return;
1779 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001780 while (cursor > 0 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001781 input_backward(1);
1782 if (cursor <= 0)
1783 return;
Natanael Copa7e6f9312016-08-14 23:30:29 +02001784 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001785 while (cursor > 0
Natanael Copa7e6f9312016-08-14 23:30:29 +02001786 && (BB_isalnum_or_underscore(command[cursor-1]))
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001787 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001788 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001789 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001790 } else if (BB_ispunct(command[cursor])) {
1791 while (cursor > 0 && BB_ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001792 input_backward(1);
1793 }
1794}
Denys Vlasenko627821e2021-09-25 19:36:35 +02001795#endif /* ENABLE_FEATURE_EDITING_VI */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001796
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001797/* Modelled after bash 4.0 behavior of Ctrl-<arrow> */
1798static void ctrl_left(void)
1799{
1800 CHAR_T *command = command_ps;
1801
1802 while (1) {
1803 CHAR_T c;
1804
1805 input_backward(1);
1806 if (cursor == 0)
1807 break;
1808 c = command[cursor];
1809 if (c != ' ' && !BB_ispunct(c)) {
1810 /* we reached a "word" delimited by spaces/punct.
1811 * go to its beginning */
1812 while (1) {
1813 c = command[cursor - 1];
1814 if (c == ' ' || BB_ispunct(c))
1815 break;
1816 input_backward(1);
1817 if (cursor == 0)
1818 break;
1819 }
1820 break;
1821 }
1822 }
1823}
1824static void ctrl_right(void)
1825{
1826 CHAR_T *command = command_ps;
1827
1828 while (1) {
1829 CHAR_T c;
1830
1831 c = command[cursor];
1832 if (c == BB_NUL)
1833 break;
1834 if (c != ' ' && !BB_ispunct(c)) {
1835 /* we reached a "word" delimited by spaces/punct.
1836 * go to its end + 1 */
1837 while (1) {
1838 input_forward();
1839 c = command[cursor];
1840 if (c == BB_NUL || c == ' ' || BB_ispunct(c))
1841 break;
1842 }
1843 break;
1844 }
1845 input_forward();
1846 }
1847}
1848
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001849
1850/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001851 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001852 */
1853
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001854#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
1855static void ask_terminal(void)
1856{
1857 /* Ask terminal where is the cursor now.
1858 * lineedit_read_key handles response and corrects
1859 * our idea of current cursor position.
1860 * Testcase: run "echo -n long_line_long_line_long_line",
1861 * then type in a long, wrapping command and try to
1862 * delete it using backspace key.
1863 * Note: we print it _after_ prompt, because
1864 * prompt may contain CR. Example: PS1='\[\r\n\]\w '
1865 */
1866 /* Problem: if there is buffered input on stdin,
1867 * the response will be delivered later,
1868 * possibly to an unsuspecting application.
1869 * Testcase: "sleep 1; busybox ash" + press and hold [Enter].
1870 * Result:
1871 * ~/srcdevel/bbox/fix/busybox.t4 #
1872 * ~/srcdevel/bbox/fix/busybox.t4 #
1873 * ^[[59;34~/srcdevel/bbox/fix/busybox.t4 # <-- garbage
1874 * ~/srcdevel/bbox/fix/busybox.t4 #
1875 *
1876 * Checking for input with poll only makes the race narrower,
1877 * I still can trigger it. Strace:
1878 *
1879 * write(1, "~/srcdevel/bbox/fix/busybox.t4 # ", 33) = 33
1880 * poll([{fd=0, events=POLLIN}], 1, 0) = 0 (Timeout) <-- no input exists
1881 * write(1, "\33[6n", 4) = 4 <-- send the ESC sequence, quick!
Alexey Fomenko232ebaa2011-05-20 04:26:29 +02001882 * poll([{fd=0, events=POLLIN}], 1, -1) = 1 ([{fd=0, revents=POLLIN}])
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001883 * read(0, "\n", 1) = 1 <-- oh crap, user's input got in first
1884 */
Denys Vlasenko451add42010-07-25 00:06:41 +02001885 struct pollfd pfd;
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001886
Denys Vlasenko451add42010-07-25 00:06:41 +02001887 pfd.fd = STDIN_FILENO;
1888 pfd.events = POLLIN;
1889 if (safe_poll(&pfd, 1, 0) == 0) {
1890 S.sent_ESC_br6n = 1;
Ron Yorstoncad3fc72021-02-03 20:47:14 +01001891 fputs_stdout(ESC"[6n");
Denys Vlasenko451add42010-07-25 00:06:41 +02001892 fflush_all(); /* make terminal see it ASAP! */
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001893 }
1894}
1895#else
Denys Vlasenko627821e2021-09-25 19:36:35 +02001896# define ask_terminal() ((void)0)
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001897#endif
1898
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001899/* Note about multi-line PS1 (e.g. "\n\w \u@\h\n> ") and prompt redrawing:
1900 *
1901 * If the prompt has any newlines, after we print it once we use only its last
1902 * line to redraw in-place, which makes it simpler to calculate how many lines
1903 * we should move the cursor up to align the redraw (cmdedit_y). The earlier
1904 * prompt lines just stay on screen and we redraw below them.
1905 *
1906 * Use cases for all prompt lines beyond the initial draw:
1907 * - After clear-screen (^L) or after displaying tab-completion choices, we
1908 * print the full prompt, as it isn't redrawn in-place.
1909 * - During terminal resize we could try to redraw all lines, but we don't,
1910 * because it requires delicate alignment, it's good enough with only the
1911 * last line, and doing it wrong is arguably worse than not doing it at all.
1912 *
1913 * Terminology wise, if it doesn't mention "full", then it means the last/sole
1914 * prompt line. We use the prompt (last/sole line) while redrawing in-place,
1915 * and the full where we need a fresh one unrelated to an earlier position.
1916 *
1917 * If PS1 is not multiline, the last/sole line and the full are the same string.
1918 */
1919
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001920/* Called just once at read_line_input() init time */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001921#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001922static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001923{
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001924 const char *p;
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001925 cmdedit_prompt = prompt_last_line = prmt_ptr;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001926 p = strrchr(prmt_ptr, '\n');
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02001927 if (p)
1928 prompt_last_line = p + 1;
1929 cmdedit_prmt_len = unicode_strwidth(prompt_last_line);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001930 put_prompt();
1931}
1932#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001933static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001934{
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001935 int prmt_size = 0;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001936 char *prmt_mem_ptr = xzalloc(1);
Denys Vlasenko1d145692013-03-29 13:09:05 +01001937 char *cwd_buf = NULL;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001938 char flg_not_length = '[';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001939 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001940
Denys Vlasenko7a180432013-08-19 16:44:05 +02001941 /*cmdedit_prmt_len = 0; - already is */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001942
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001943 cbuf[1] = '\0'; /* never changes */
1944
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001945 while (*prmt_ptr) {
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001946 char timebuf[sizeof("HH:MM:SS")];
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001947 char *free_me = NULL;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001948 char *pbuf;
1949 char c;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001950
1951 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001952 c = *prmt_ptr++;
1953 if (c == '\\') {
Denys Vlasenko1d145692013-03-29 13:09:05 +01001954 const char *cp;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001955 int l;
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001956/*
1957 * Supported via bb_process_escape_sequence:
1958 * \a ASCII bell character (07)
1959 * \e ASCII escape character (033)
1960 * \n newline
1961 * \r carriage return
1962 * \\ backslash
1963 * \nnn char with octal code nnn
1964 * Supported:
1965 * \$ if the effective UID is 0, a #, otherwise a $
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001966 * \w current working directory, with $HOME abbreviated with a tilde
1967 * Note: we do not support $PROMPT_DIRTRIM=n feature
Denys Vlasenko1d145692013-03-29 13:09:05 +01001968 * \W basename of the current working directory, with $HOME abbreviated with a tilde
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001969 * \h hostname up to the first '.'
1970 * \H hostname
1971 * \u username
1972 * \[ begin a sequence of non-printing characters
1973 * \] end a sequence of non-printing characters
Denys Vlasenko1d145692013-03-29 13:09:05 +01001974 * \T current time in 12-hour HH:MM:SS format
1975 * \@ current time in 12-hour am/pm format
1976 * \A current time in 24-hour HH:MM format
1977 * \t current time in 24-hour HH:MM:SS format
1978 * (all of the above work as \A)
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001979 * Not supported:
Denys Vlasenko1d145692013-03-29 13:09:05 +01001980 * \! history number of this command
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001981 * \# command number of this command
1982 * \j number of jobs currently managed by the shell
1983 * \l basename of the shell's terminal device name
1984 * \s name of the shell, the basename of $0 (the portion following the final slash)
1985 * \V release of bash, version + patch level (e.g., 2.00.0)
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001986 * \d date in "Weekday Month Date" format (e.g., "Tue May 26")
1987 * \D{format}
1988 * format is passed to strftime(3).
1989 * An empty format results in a locale-specific time representation.
1990 * The braces are required.
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001991 * Mishandled by bb_process_escape_sequence:
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001992 * \v version of bash (e.g., 2.00)
1993 */
Denys Vlasenko1d145692013-03-29 13:09:05 +01001994 cp = prmt_ptr;
1995 c = *cp;
1996 if (c != 't') /* don't treat \t as tab */
1997 c = bb_process_escape_sequence(&prmt_ptr);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001998 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001999 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002000 break;
2001 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002002
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002003 switch (c) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002004 case 'u':
Denys Vlasenkof4fcd742021-10-11 23:08:31 +02002005 pbuf = (char*)get_username_str();
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002006 break;
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01002007 case 'H':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002008 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00002009 pbuf = free_me = safe_gethostname();
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01002010 if (c == 'h')
2011 strchrnul(pbuf, '.')[0] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002012 break;
2013 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002014 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002015 break;
Denys Vlasenko1d145692013-03-29 13:09:05 +01002016 case 'T': /* 12-hour HH:MM:SS format */
2017 case '@': /* 12-hour am/pm format */
2018 case 'A': /* 24-hour HH:MM format */
2019 case 't': /* 24-hour HH:MM:SS format */
2020 /* We show all of them as 24-hour HH:MM */
2021 strftime_HHMMSS(timebuf, sizeof(timebuf), NULL)[-3] = '\0';
2022 pbuf = timebuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002023 break;
Denys Vlasenko1d145692013-03-29 13:09:05 +01002024 case 'w': /* current dir */
2025 case 'W': /* basename of cur dir */
2026 if (!cwd_buf) {
Denys Vlasenkof4fcd742021-10-11 23:08:31 +02002027 const char *home;
Denys Vlasenko1d145692013-03-29 13:09:05 +01002028 cwd_buf = xrealloc_getcwd_or_warn(NULL);
2029 if (!cwd_buf)
2030 cwd_buf = (char *)bb_msg_unknown;
Denys Vlasenkof4fcd742021-10-11 23:08:31 +02002031 else if ((home = get_homedir_or_NULL()) != NULL && home[0]) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01002032 char *after_home_user;
2033
Denys Vlasenko1d145692013-03-29 13:09:05 +01002034 /* /home/user[/something] -> ~[/something] */
Denys Vlasenkof4fcd742021-10-11 23:08:31 +02002035 after_home_user = is_prefixed_with(cwd_buf, home);
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01002036 if (after_home_user
2037 && (*after_home_user == '/' || *after_home_user == '\0')
Denys Vlasenko1d145692013-03-29 13:09:05 +01002038 ) {
2039 cwd_buf[0] = '~';
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01002040 overlapping_strcpy(cwd_buf + 1, after_home_user);
Denys Vlasenko1d145692013-03-29 13:09:05 +01002041 }
2042 }
2043 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002044 pbuf = cwd_buf;
Denys Vlasenko1d145692013-03-29 13:09:05 +01002045 if (c == 'w')
2046 break;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00002047 cp = strrchr(pbuf, '/');
Denys Vlasenko8172d052013-03-29 13:21:53 +01002048 if (cp)
Denys Vlasenko1d145692013-03-29 13:09:05 +01002049 pbuf = (char*)cp + 1;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002050 break;
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01002051// bb_process_escape_sequence does this now:
2052// case 'e': case 'E': /* \e \E = \033 */
2053// c = '\033';
2054// break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002055 case 'x': case 'X': {
2056 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002057 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002058 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002059 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002060 buf2[l] = '\0';
2061 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002062 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002063 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002064 break;
2065 }
2066 prmt_ptr++;
2067 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002068 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002069 if (c == 0)
2070 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002071 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002072 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002073 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002074 case '[': case ']':
2075 if (c == flg_not_length) {
Denys Vlasenko7a180432013-08-19 16:44:05 +02002076 /* Toggle '['/']' hex 5b/5d */
2077 flg_not_length ^= 6;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002078 continue;
2079 }
2080 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002081 } /* switch */
2082 } /* if */
2083 } /* if */
2084 cbuf[0] = c;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002085 {
2086 int n = strlen(pbuf);
2087 prmt_size += n;
2088 if (c == '\n')
2089 cmdedit_prmt_len = 0;
2090 else if (flg_not_length != ']') {
Denys Vlasenko627821e2021-09-25 19:36:35 +02002091# if ENABLE_UNICODE_SUPPORT
Audun-Marius Gangstø9bf44992020-11-21 12:26:39 +01002092 if (n == 1) {
2093 /* Only count single-byte characters and the first of multi-byte characters */
2094 if ((unsigned char)*pbuf < 0x80 /* single byte character */
2095 || (unsigned char)*pbuf >= 0xc0 /* first of multi-byte characters */
2096 ) {
2097 cmdedit_prmt_len += n;
2098 }
2099 } else {
2100 cmdedit_prmt_len += unicode_strwidth(pbuf);
2101 }
Denys Vlasenko627821e2021-09-25 19:36:35 +02002102# else
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002103 cmdedit_prmt_len += n;
Denys Vlasenko627821e2021-09-25 19:36:35 +02002104# endif
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002105 }
Denys Vlasenko7a180432013-08-19 16:44:05 +02002106 }
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02002107 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_size+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00002108 free(free_me);
2109 } /* while */
2110
2111 if (cwd_buf != (char *)bb_msg_unknown)
2112 free(cwd_buf);
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002113 /* see comment (above this function) about multiline prompt redrawing */
2114 cmdedit_prompt = prompt_last_line = prmt_mem_ptr;
2115 prmt_ptr = strrchr(cmdedit_prompt, '\n');
2116 if (prmt_ptr)
2117 prompt_last_line = prmt_ptr + 1;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002118 put_prompt();
2119}
Denys Vlasenkoa97a7952020-12-16 11:14:08 +01002120#endif /* FEATURE_EDITING_FANCY_PROMPT */
Paul Fox3f11b1b2005-08-04 19:04:46 +00002121
Ron Yorston23286902018-02-25 20:09:54 +01002122#if ENABLE_FEATURE_EDITING_WINCH
Denys Vlasenko038a9772016-11-28 01:10:16 +01002123static void cmdedit_setwidth(void)
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002124{
Denys Vlasenko038a9772016-11-28 01:10:16 +01002125 int new_y;
2126
2127 cmdedit_termw = get_terminal_width(STDIN_FILENO);
2128 /* new y for current cursor */
2129 new_y = (cursor + cmdedit_prmt_len) / cmdedit_termw;
2130 /* redraw */
2131 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002132}
2133
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002134static void win_changed(int nsig UNUSED_PARAM)
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002135{
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002136 if (S.ok_to_redraw) {
2137 /* We are in read_key(), safe to redraw immediately */
2138 int sv_errno = errno;
Denys Vlasenko038a9772016-11-28 01:10:16 +01002139 cmdedit_setwidth();
2140 fflush_all();
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002141 errno = sv_errno;
2142 } else {
2143 /* Signal main loop that redraw is necessary */
2144 S.SIGWINCH_count++;
2145 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002146}
Ron Yorston23286902018-02-25 20:09:54 +01002147#endif
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002148
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002149static int lineedit_read_key(char *read_key_buffer, int timeout)
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002150{
Denys Vlasenko020f4062009-05-17 16:44:54 +02002151 int64_t ic;
Denys Vlasenko19158a82010-03-26 14:06:56 +01002152#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002153 char unicode_buf[MB_CUR_MAX + 1];
2154 int unicode_idx = 0;
2155#endif
Denys Vlasenko020f4062009-05-17 16:44:54 +02002156
Denys Vlasenko038a9772016-11-28 01:10:16 +01002157 fflush_all();
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002158 while (1) {
2159 /* Wait for input. TIMEOUT = -1 makes read_key wait even
2160 * on nonblocking stdin, TIMEOUT = 50 makes sure we won't
2161 * insist on full MB_CUR_MAX buffer to declare input like
2162 * "\xff\n",pause,"ls\n" invalid and thus won't lose "ls".
2163 *
Denys Vlasenko12566e72022-01-17 03:02:40 +01002164 * If LI_INTERRUPTIBLE, return -1 if got EINTR in poll()
2165 * inside read_key, or if bb_got_signal != 0 (IOW: if signal
2166 * arrived before poll() is reached).
2167 *
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002168 * Note: read_key sets errno to 0 on success.
2169 */
Denys Vlasenko12566e72022-01-17 03:02:40 +01002170 do {
2171 if ((state->flags & LI_INTERRUPTIBLE) && bb_got_signal) {
2172 errno = EINTR;
2173 return -1;
2174 }
2175//FIXME: still races here with signals, but small window to poll() inside read_key
2176 IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 1;)
2177 ic = read_key(STDIN_FILENO, read_key_buffer, timeout);
2178 IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 0;)
2179 } while (!(state->flags & LI_INTERRUPTIBLE) && errno == EINTR);
2180
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002181 if (errno) {
Denys Vlasenko12566e72022-01-17 03:02:40 +01002182 /* LI_INTERRUPTIBLE can bail out with EINTR here,
2183 * but nothing really guarantees that bb_got_signal
2184 * is nonzero. Follow the least surprise principle:
2185 */
2186 if (errno == EINTR && bb_got_signal == 0)
2187 bb_got_signal = 255; /* something nonzero */
Denys Vlasenko19158a82010-03-26 14:06:56 +01002188#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002189 if (errno == EAGAIN && unicode_idx != 0)
2190 goto pushback;
Denys Vlasenko1f6d2302009-10-29 03:45:26 +01002191#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002192 break;
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +02002193 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002194
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002195#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
2196 if ((int32_t)ic == KEYCODE_CURSOR_POS
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002197 && S.sent_ESC_br6n
Denys Vlasenko020f4062009-05-17 16:44:54 +02002198 ) {
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002199 S.sent_ESC_br6n = 0;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002200 if (cursor == 0) { /* otherwise it may be bogus */
2201 int col = ((ic >> 32) & 0x7fff) - 1;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002202 /*
2203 * Is col > cmdedit_prmt_len?
2204 * If yes (terminal says cursor is farther to the right
2205 * of where we think it should be),
2206 * the prompt wasn't printed starting at col 1,
2207 * there was additional text before it.
2208 */
2209 if ((int)(col - cmdedit_prmt_len) > 0) {
2210 /* Fix our understanding of current x position */
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002211 cmdedit_x += (col - cmdedit_prmt_len);
2212 while (cmdedit_x >= cmdedit_termw) {
2213 cmdedit_x -= cmdedit_termw;
2214 cmdedit_y++;
2215 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02002216 }
2217 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002218 continue;
Denys Vlasenko020f4062009-05-17 16:44:54 +02002219 }
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002220#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002221
Denys Vlasenko19158a82010-03-26 14:06:56 +01002222#if ENABLE_UNICODE_SUPPORT
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002223 if (unicode_status == UNICODE_ON) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002224 wchar_t wc;
2225
2226 if ((int32_t)ic < 0) /* KEYCODE_xxx */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002227 break;
2228 // TODO: imagine sequence like: 0xff,<left-arrow>: we are currently losing 0xff...
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002229
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002230 unicode_buf[unicode_idx++] = ic;
2231 unicode_buf[unicode_idx] = '\0';
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002232 if (mbstowcs(&wc, unicode_buf, 1) != 1) {
2233 /* Not (yet?) a valid unicode char */
2234 if (unicode_idx < MB_CUR_MAX) {
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002235 timeout = 50;
2236 continue;
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002237 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002238 pushback:
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002239 /* Invalid sequence. Save all "bad bytes" except first */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002240 read_key_ungets(read_key_buffer, unicode_buf + 1, unicode_idx - 1);
Tomas Heinricha659b812010-04-29 13:43:39 +02002241# if !ENABLE_UNICODE_PRESERVE_BROKEN
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002242 ic = CONFIG_SUBST_WCHAR;
Tomas Heinricha659b812010-04-29 13:43:39 +02002243# else
Tomas Heinrichb8909c52010-05-16 20:46:53 +02002244 ic = unicode_mark_raw_byte(unicode_buf[0]);
Tomas Heinricha659b812010-04-29 13:43:39 +02002245# endif
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002246 } else {
2247 /* Valid unicode char, return its code */
2248 ic = wc;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002249 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002250 }
2251#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002252 break;
2253 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002254
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002255 return ic;
2256}
2257
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002258#if ENABLE_UNICODE_BIDI_SUPPORT
2259static int isrtl_str(void)
2260{
2261 int idx = cursor;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002262
2263 while (idx < command_len && unicode_bidi_is_neutral_wchar(command_ps[idx]))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002264 idx++;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002265 return unicode_bidi_isrtl(command_ps[idx]);
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002266}
2267#else
2268# define isrtl_str() 0
2269#endif
2270
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002271/* leave out the "vi-mode"-only case labels if vi editing isn't
2272 * configured. */
Denys Vlasenko13028922009-07-12 00:51:15 +02002273#define vi_case(caselabel) IF_FEATURE_EDITING_VI(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00002274
2275/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002276#undef CTRL
2277#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00002278
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002279enum {
2280 VI_CMDMODE_BIT = 0x40000000,
2281 /* 0x80000000 bit flags KEYCODE_xxx */
2282};
2283
2284#if ENABLE_FEATURE_REVERSE_SEARCH
2285/* Mimic readline Ctrl-R reverse history search.
2286 * When invoked, it shows the following prompt:
2287 * (reverse-i-search)'': user_input [cursor pos unchanged by Ctrl-R]
2288 * and typing results in search being performed:
2289 * (reverse-i-search)'tmp': cd /tmp [cursor under t in /tmp]
2290 * Search is performed by looking at progressively older lines in history.
2291 * Ctrl-R again searches for the next match in history.
2292 * Backspace deletes last matched char.
2293 * Control keys exit search and return to normal editing (at current history line).
2294 */
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002295static int32_t reverse_i_search(int timeout)
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002296{
2297 char match_buf[128]; /* for user input */
2298 char read_key_buffer[KEYCODE_BUFFER_SIZE];
2299 const char *matched_history_line;
2300 const char *saved_prompt;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002301 unsigned saved_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002302 int32_t ic;
2303
2304 matched_history_line = NULL;
2305 read_key_buffer[0] = 0;
2306 match_buf[0] = '\0';
2307
2308 /* Save and replace the prompt */
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002309 saved_prompt = prompt_last_line;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002310 saved_prmt_len = cmdedit_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002311 goto set_prompt;
2312
2313 while (1) {
2314 int h;
2315 unsigned match_buf_len = strlen(match_buf);
2316
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002317//FIXME: correct timeout? (i.e. count it down?)
2318 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002319
2320 switch (ic) {
2321 case CTRL('R'): /* searching for the next match */
2322 break;
2323
2324 case '\b':
2325 case '\x7f':
2326 /* Backspace */
2327 if (unicode_status == UNICODE_ON) {
2328 while (match_buf_len != 0) {
2329 uint8_t c = match_buf[--match_buf_len];
2330 if ((c & 0xc0) != 0x80) /* start of UTF-8 char? */
2331 break; /* yes */
2332 }
2333 } else {
2334 if (match_buf_len != 0)
2335 match_buf_len--;
2336 }
2337 match_buf[match_buf_len] = '\0';
2338 break;
2339
2340 default:
2341 if (ic < ' '
2342 || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2343 || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
2344 ) {
2345 goto ret;
2346 }
2347
2348 /* Append this char */
Denys Vlasenko627821e2021-09-25 19:36:35 +02002349# if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002350 if (unicode_status == UNICODE_ON) {
2351 mbstate_t mbstate = { 0 };
2352 char buf[MB_CUR_MAX + 1];
2353 int len = wcrtomb(buf, ic, &mbstate);
2354 if (len > 0) {
2355 buf[len] = '\0';
2356 if (match_buf_len + len < sizeof(match_buf))
2357 strcpy(match_buf + match_buf_len, buf);
2358 }
2359 } else
Denys Vlasenko627821e2021-09-25 19:36:35 +02002360# endif
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002361 if (match_buf_len < sizeof(match_buf) - 1) {
2362 match_buf[match_buf_len] = ic;
2363 match_buf[match_buf_len + 1] = '\0';
2364 }
2365 break;
2366 } /* switch (ic) */
2367
2368 /* Search in history for match_buf */
2369 h = state->cur_history;
2370 if (ic == CTRL('R'))
2371 h--;
2372 while (h >= 0) {
2373 if (state->history[h]) {
2374 char *match = strstr(state->history[h], match_buf);
2375 if (match) {
2376 state->cur_history = h;
2377 matched_history_line = state->history[h];
2378 command_len = load_string(matched_history_line);
2379 cursor = match - matched_history_line;
2380//FIXME: cursor position for Unicode case
2381
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002382 free((char*)prompt_last_line);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002383 set_prompt:
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002384 prompt_last_line = xasprintf("(reverse-i-search)'%s': ", match_buf);
2385 cmdedit_prmt_len = unicode_strwidth(prompt_last_line);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002386 goto do_redraw;
2387 }
2388 }
2389 h--;
2390 }
2391
2392 /* Not found */
2393 match_buf[match_buf_len] = '\0';
2394 beep();
2395 continue;
2396
2397 do_redraw:
2398 redraw(cmdedit_y, command_len - cursor);
2399 } /* while (1) */
2400
2401 ret:
2402 if (matched_history_line)
2403 command_len = load_string(matched_history_line);
2404
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002405 free((char*)prompt_last_line);
2406 prompt_last_line = saved_prompt;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002407 cmdedit_prmt_len = saved_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002408 redraw(cmdedit_y, command_len - cursor);
2409
2410 return ic;
2411}
Denys Vlasenko627821e2021-09-25 19:36:35 +02002412#endif /* ENABLE_FEATURE_REVERSE_SEARCH */
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002413
Denys Vlasenko23427a62018-12-08 15:45:46 +01002414#if ENABLE_FEATURE_EDITING_WINCH
Denys Vlasenko136fe9b2018-12-08 13:49:15 +01002415static void sigaction2(int sig, struct sigaction *act)
2416{
2417 // Grr... gcc 8.1.1:
2418 // "passing argument 3 to restrict-qualified parameter aliases with argument 2"
2419 // dance around that...
2420 struct sigaction *oact FIX_ALIASING;
2421 oact = act;
2422 sigaction(sig, act, oact);
2423}
Denys Vlasenko23427a62018-12-08 15:45:46 +01002424#endif
Denys Vlasenko136fe9b2018-12-08 13:49:15 +01002425
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02002426/* maxsize must be >= 2.
2427 * Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00002428 * -1 on read errors or EOF, or on bare Ctrl-D,
2429 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denys Vlasenko4b89d512016-11-25 03:41:03 +01002430 * (in both cases the cursor remains on the input line, '\n' is not printed)
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00002431 * >0 length of input string, including terminating '\n'
2432 */
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002433int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize)
Erik Andersen13456d12000-03-16 08:09:57 +00002434{
Denys Vlasenkoaaaaaa52017-09-15 17:14:01 +02002435 int len, n;
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002436 int timeout;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002437#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002438 smallint lastWasTab = 0;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002439#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002440 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00002441#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002442 smallint vi_cmdmode = 0;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002443#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002444 struct termios initial_settings;
2445 struct termios new_settings;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002446 char read_key_buffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002447
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002448 INIT_S();
Denys Vlasenko96717d92020-12-21 22:50:23 +01002449 //command_len = 0; - done by INIT_S()
2450 //cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
2451 cmdedit_termw = 80;
Denys Vlasenko96717d92020-12-21 22:50:23 +01002452 IF_FEATURE_EDITING_VI(delptr = delbuf;)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002453
Denys Vlasenkoaaaaaa52017-09-15 17:14:01 +02002454 n = get_termios_and_make_raw(STDIN_FILENO, &new_settings, &initial_settings, 0
2455 | TERMIOS_CLEAR_ISIG /* turn off INTR (ctrl-C), QUIT, SUSP */
2456 );
2457 if (n != 0 || (initial_settings.c_lflag & (ECHO|ICANON)) == ICANON) {
Denys Vlasenkod598a8d2014-12-10 17:22:13 +01002458 /* Happens when e.g. stty -echo was run before.
2459 * But if ICANON is not set, we don't come here.
2460 * (example: interactive python ^Z-backgrounded,
2461 * tty is still in "raw mode").
2462 */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002463 parse_and_put_prompt(prompt);
Denys Vlasenko038a9772016-11-28 01:10:16 +01002464 fflush_all();
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00002465 if (fgets(command, maxsize, stdin) == NULL)
2466 len = -1; /* EOF or error */
2467 else
2468 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002469 DEINIT_S();
2470 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00002471 }
2472
Denys Vlasenko28055022010-01-04 20:49:58 +01002473 init_unicode();
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02002474
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002475// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00002476 if (maxsize > MAX_LINELEN)
2477 maxsize = MAX_LINELEN;
Denys Vlasenko044b1802009-07-12 02:50:35 +02002478 S.maxsize = maxsize;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002479
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002480 timeout = -1;
2481 /* Make state->flags == 0 if st is NULL.
2482 * With zeroed flags, no other fields are ever referenced.
2483 */
2484 state = (line_input_t*) &const_int_0;
2485 if (st) {
2486 state = st;
2487 timeout = st->timeout;
2488 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002489#if MAX_HISTORY > 0
Denys Vlasenko779f96a2019-02-04 16:16:30 +01002490 if (state->flags & DO_HISTORY) {
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002491# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denys Vlasenko779f96a2019-02-04 16:16:30 +01002492 if (state->hist_file)
2493 if (state->cnt_history == 0)
2494 load_history(state);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002495# endif
Denis Vlasenko3c385cd2008-11-02 00:41:05 +00002496 state->cur_history = state->cnt_history;
Denys Vlasenko779f96a2019-02-04 16:16:30 +01002497 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002498#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002499
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002500 /* prepare before init handlers */
Denys Vlasenko19158a82010-03-26 14:06:56 +01002501#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002502 command_ps = xzalloc(maxsize * sizeof(command_ps[0]));
2503#else
Mark Whitley4e338752001-01-26 20:42:23 +00002504 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00002505 command[0] = '\0';
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002506#endif
2507#define command command_must_not_be_used
Mark Whitley4e338752001-01-26 20:42:23 +00002508
Denis Vlasenko202ac502008-11-05 13:20:58 +00002509 tcsetattr_stdin_TCSANOW(&new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00002510
Denis Vlasenko682ad302008-09-27 01:28:56 +00002511#if 0
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02002512 for (i = 0; i <= state->max_history; i++)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002513 bb_error_msg("history[%d]:'%s'", i, state->history[i]);
Denis Vlasenko682ad302008-09-27 01:28:56 +00002514 bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
2515#endif
2516
Denys Vlasenko0a677222017-11-08 13:38:12 +01002517 /* Get width (before printing prompt) */
2518 cmdedit_termw = get_terminal_width(STDIN_FILENO);
Denys Vlasenko13ad9062009-11-11 03:19:30 +01002519 /* Print out the command prompt, optionally ask where cursor is */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002520 parse_and_put_prompt(prompt);
Denys Vlasenko13ad9062009-11-11 03:19:30 +01002521 ask_terminal();
Eric Andersenb3dc3b82001-01-04 11:08:45 +00002522
Ron Yorston23286902018-02-25 20:09:54 +01002523#if ENABLE_FEATURE_EDITING_WINCH
Alexey Fomenko232ebaa2011-05-20 04:26:29 +02002524 /* Install window resize handler (NB: after *all* init is complete) */
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002525 S.SIGWINCH_handler.sa_handler = win_changed;
2526 S.SIGWINCH_handler.sa_flags = SA_RESTART;
Denys Vlasenko136fe9b2018-12-08 13:49:15 +01002527 sigaction2(SIGWINCH, &S.SIGWINCH_handler);
Ron Yorston23286902018-02-25 20:09:54 +01002528#endif
Denys Vlasenkoc396fe62009-05-17 19:28:14 +02002529 read_key_buffer[0] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002530 while (1) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002531 /*
2532 * The emacs and vi modes share much of the code in the big
2533 * command loop. Commands entered when in vi's command mode
2534 * (aka "escape mode") get an extra bit added to distinguish
2535 * them - this keeps them from being self-inserted. This
2536 * clutters the big switch a bit, but keeps all the code
2537 * in one place.
2538 */
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002539 int32_t ic, ic_raw;
Ron Yorston23286902018-02-25 20:09:54 +01002540#if ENABLE_FEATURE_EDITING_WINCH
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002541 unsigned count;
2542
2543 count = S.SIGWINCH_count;
2544 if (S.SIGWINCH_saved != count) {
2545 S.SIGWINCH_saved = count;
Denys Vlasenko038a9772016-11-28 01:10:16 +01002546 cmdedit_setwidth();
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002547 }
Ron Yorston23286902018-02-25 20:09:54 +01002548#endif
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002549 ic = ic_raw = lineedit_read_key(read_key_buffer, timeout);
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002550
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002551#if ENABLE_FEATURE_REVERSE_SEARCH
2552 again:
2553#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00002554#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00002555 newdelflag = 1;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002556 if (vi_cmdmode) {
2557 /* btw, since KEYCODE_xxx are all < 0, this doesn't
2558 * change ic if it contains one of them: */
2559 ic |= VI_CMDMODE_BIT;
2560 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00002561#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002562
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002563 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00002564 case '\n':
2565 case '\r':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002566 vi_case('\n'|VI_CMDMODE_BIT:)
2567 vi_case('\r'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002568 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002569 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00002570 break_out = 1;
2571 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002572 case CTRL('A'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002573 vi_case('0'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002574 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002575 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00002576 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002577 case CTRL('B'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002578 vi_case('h'|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002579 vi_case('\b'|VI_CMDMODE_BIT:) /* ^H */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002580 vi_case('\x7f'|VI_CMDMODE_BIT:) /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002581 input_backward(1); /* Move back one character */
Erik Andersenf0657d32000-04-12 17:49:52 +00002582 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002583 case CTRL('E'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002584 vi_case('$'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002585 /* Control-e -- End of line */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002586 put_till_end_and_adv_cursor();
Erik Andersenc7c634b2000-03-19 05:28:55 +00002587 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002588 case CTRL('F'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002589 vi_case('l'|VI_CMDMODE_BIT:)
2590 vi_case(' '|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002591 input_forward(); /* Move forward one character */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002592 break;
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002593 case '\b': /* ^H */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002594 case '\x7f': /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002595 if (!isrtl_str())
2596 input_backspace();
2597 else
2598 input_delete(0);
2599 break;
2600 case KEYCODE_DELETE:
2601 if (!isrtl_str())
2602 input_delete(0);
2603 else
2604 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00002605 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002606#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00002607 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002608 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002609 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002610#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002611 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00002612 /* Control-k -- clear to end of line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002613 command_ps[cursor] = BB_NUL;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002614 command_len = cursor;
Denys Vlasenko94043e82010-05-11 14:49:13 +02002615 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen65a07302002-04-13 13:26:49 +00002616 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002617 case CTRL('L'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002618 vi_case(CTRL('L')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002619 /* Control-l -- clear screen */
Avi Halachmi0fd5dbb2017-10-12 16:38:35 +02002620 /* cursor to top,left; clear to the end of screen */
2621 printf(ESC"[H" ESC"[J");
2622 draw_full(command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00002623 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002624#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002625 case CTRL('N'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002626 vi_case(CTRL('N')|VI_CMDMODE_BIT:)
2627 vi_case('j'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002628 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002629 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002630 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00002631 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002632 case CTRL('P'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002633 vi_case(CTRL('P')|VI_CMDMODE_BIT:)
2634 vi_case('k'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002635 /* Control-p -- Get previous command from history */
Denis Vlasenko682ad302008-09-27 01:28:56 +00002636 if (get_previous_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002637 goto rewrite_line;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002638 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002639#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002640 case CTRL('U'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002641 vi_case(CTRL('U')|VI_CMDMODE_BIT:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002642 /* Control-U -- Clear line before cursor */
2643 if (cursor) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002644 command_len -= cursor;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002645 memmove(command_ps, command_ps + cursor,
2646 (command_len + 1) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002647 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002648 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002649 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002650 case CTRL('W'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002651 vi_case(CTRL('W')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002652 /* Control-W -- Remove the last word */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002653 while (cursor > 0 && BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002654 input_backspace();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002655 while (cursor > 0 && !BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002656 input_backspace();
2657 break;
Rostislav Skudnov2e4ef382016-11-24 15:04:00 +01002658 case KEYCODE_ALT_D: {
2659 /* Delete word forward */
2660 int nc, sc = cursor;
2661 ctrl_right();
2662 nc = cursor - sc;
2663 input_backward(nc);
2664 while (--nc >= 0)
2665 input_delete(1);
2666 break;
2667 }
2668 case KEYCODE_ALT_BACKSPACE: {
2669 /* Delete word backward */
2670 int sc = cursor;
2671 ctrl_left();
2672 while (sc-- > cursor)
2673 input_delete(1);
2674 break;
2675 }
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002676#if ENABLE_FEATURE_REVERSE_SEARCH
2677 case CTRL('R'):
Denys Vlasenko84ea60e2017-08-02 17:27:28 +02002678 ic = ic_raw = reverse_i_search(timeout);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002679 goto again;
2680#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002681
Denis Vlasenko38f63192007-01-22 09:03:07 +00002682#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002683 case 'i'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002684 vi_cmdmode = 0;
2685 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002686 case 'I'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002687 input_backward(cursor);
2688 vi_cmdmode = 0;
2689 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002690 case 'a'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002691 input_forward();
2692 vi_cmdmode = 0;
2693 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002694 case 'A'|VI_CMDMODE_BIT:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002695 put_till_end_and_adv_cursor();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002696 vi_cmdmode = 0;
2697 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002698 case 'x'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002699 input_delete(1);
2700 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002701 case 'X'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002702 if (cursor > 0) {
2703 input_backward(1);
2704 input_delete(1);
2705 }
2706 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002707 case 'W'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002708 vi_Word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002709 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002710 case 'w'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002711 vi_word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002712 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002713 case 'E'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002714 vi_End_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002715 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002716 case 'e'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002717 vi_end_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002718 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002719 case 'B'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002720 vi_Back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002721 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002722 case 'b'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002723 vi_back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002724 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002725 case 'C'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002726 vi_cmdmode = 0;
2727 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002728 case 'D'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002729 goto clear_to_eol;
2730
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002731 case 'c'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002732 vi_cmdmode = 0;
2733 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002734 case 'd'|VI_CMDMODE_BIT: {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002735 int nc, sc;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002736
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002737 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002738 if (errno) /* error */
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002739 goto return_error_indicator;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002740 if (ic == ic_raw) { /* "cc", "dd" */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002741 input_backward(cursor);
2742 goto clear_to_eol;
2743 break;
2744 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002745
2746 sc = cursor;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002747 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002748 case 'w':
2749 case 'W':
2750 case 'e':
2751 case 'E':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002752 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002753 case 'w': /* "dw", "cw" */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002754 vi_word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002755 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002756 case 'W': /* 'dW', 'cW' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002757 vi_Word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002758 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002759 case 'e': /* 'de', 'ce' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002760 vi_end_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002761 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002762 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002763 case 'E': /* 'dE', 'cE' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002764 vi_End_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002765 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002766 break;
2767 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002768 nc = cursor;
2769 input_backward(cursor - sc);
2770 while (nc-- > cursor)
2771 input_delete(1);
2772 break;
2773 case 'b': /* "db", "cb" */
2774 case 'B': /* implemented as B */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002775 if (ic == 'b')
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002776 vi_back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002777 else
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002778 vi_Back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002779 while (sc-- > cursor)
2780 input_delete(1);
2781 break;
2782 case ' ': /* "d ", "c " */
2783 input_delete(1);
2784 break;
2785 case '$': /* "d$", "c$" */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002786 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002787 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002788 input_delete(1);
2789 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002790 }
2791 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002792 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002793 case 'p'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002794 input_forward();
2795 /* fallthrough */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002796 case 'P'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002797 put();
2798 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002799 case 'r'|VI_CMDMODE_BIT:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002800//FIXME: unicode case?
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002801 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002802 if (errno) /* error */
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002803 goto return_error_indicator;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002804 if (ic < ' ' || ic > 255) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00002805 beep();
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002806 } else {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002807 command_ps[cursor] = ic;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002808 bb_putchar(ic);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002809 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00002810 }
2811 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002812 case '\x1b': /* ESC */
2813 if (state->flags & VI_MODE) {
2814 /* insert mode --> command mode */
2815 vi_cmdmode = 1;
2816 input_backward(1);
2817 }
2818 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002819#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002820
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002821#if MAX_HISTORY > 0
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002822 case KEYCODE_UP:
2823 if (get_previous_history())
2824 goto rewrite_line;
2825 beep();
2826 break;
2827 case KEYCODE_DOWN:
2828 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002829 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002830 rewrite_line:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002831 /* Rewrite the line with the selected history item */
2832 /* change command */
Denys Vlasenko90a99042009-09-06 02:36:23 +02002833 command_len = load_string(state->history[state->cur_history] ?
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002834 state->history[state->cur_history] : "");
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002835 /* redraw and go to eol (bol, in vi) */
2836 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
2837 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002838#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002839 case KEYCODE_RIGHT:
2840 input_forward();
2841 break;
2842 case KEYCODE_LEFT:
2843 input_backward(1);
2844 break;
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002845 case KEYCODE_CTRL_LEFT:
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002846 case KEYCODE_ALT_LEFT: /* bash doesn't do it */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002847 ctrl_left();
2848 break;
2849 case KEYCODE_CTRL_RIGHT:
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002850 case KEYCODE_ALT_RIGHT: /* bash doesn't do it */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002851 ctrl_right();
2852 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002853 case KEYCODE_HOME:
2854 input_backward(cursor);
2855 break;
2856 case KEYCODE_END:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002857 put_till_end_and_adv_cursor();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002858 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002859
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002860 default:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002861 if (initial_settings.c_cc[VINTR] != 0
2862 && ic_raw == initial_settings.c_cc[VINTR]
2863 ) {
2864 /* Ctrl-C (usually) - stop gathering input */
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002865 command_len = 0;
2866 break_out = -1; /* "do not append '\n'" */
2867 break;
2868 }
2869 if (initial_settings.c_cc[VEOF] != 0
2870 && ic_raw == initial_settings.c_cc[VEOF]
2871 ) {
2872 /* Ctrl-D (usually) - delete one character,
2873 * or exit if len=0 and no chars to delete */
2874 if (command_len == 0) {
2875 errno = 0;
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002876
2877 case -1: /* error (e.g. EIO when tty is destroyed) */
2878 IF_FEATURE_EDITING_VI(return_error_indicator:)
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002879 break_out = command_len = -1;
2880 break;
2881 }
2882 input_delete(0);
2883 break;
2884 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002885// /* Control-V -- force insert of next char */
2886// if (c == CTRL('V')) {
2887// if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002888// goto return_error_indicator;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002889// if (c == 0) {
2890// beep();
2891// break;
2892// }
2893// }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002894 if (ic < ' '
Denys Vlasenko19158a82010-03-26 14:06:56 +01002895 || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2896 || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002897 ) {
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002898 /* If VI_CMDMODE_BIT is set, ic is >= 256
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002899 * and vi mode ignores unexpected chars.
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002900 * Otherwise, we are here if ic is a
2901 * control char or an unhandled ESC sequence,
2902 * which is also ignored.
2903 */
2904 break;
2905 }
2906 if ((int)command_len >= (maxsize - 2)) {
2907 /* Not enough space for the char and EOL */
2908 break;
Paul Fox84bbac52008-01-11 16:50:08 +00002909 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002910
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002911 command_len++;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002912 if (cursor == (command_len - 1)) {
2913 /* We are at the end, append */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002914 command_ps[cursor] = ic;
2915 command_ps[cursor + 1] = BB_NUL;
Denys Vlasenko94043e82010-05-11 14:49:13 +02002916 put_cur_glyph_and_inc_cursor();
Tomas Heinrichaa167552010-03-26 13:13:24 +01002917 if (unicode_bidi_isrtl(ic))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002918 input_backward(1);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002919 } else {
2920 /* In the middle, insert */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002921 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002922
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002923 memmove(command_ps + sc + 1, command_ps + sc,
2924 (command_len - sc) * sizeof(command_ps[0]));
2925 command_ps[sc] = ic;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002926 /* is right-to-left char, or neutral one (e.g. comma) was just added to rtl text? */
2927 if (!isrtl_str())
2928 sc++; /* no */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002929 put_till_end_and_adv_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +00002930 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002931 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002932 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00002933 break;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002934 } /* switch (ic) */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002935
2936 if (break_out)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002937 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002938
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002939#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002940 if (ic_raw != '\t')
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002941 lastWasTab = 0;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002942#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002943 } /* while (1) */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002944
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002945#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002946 if (S.sent_ESC_br6n) {
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002947 /* "sleep 1; busybox ash" + hold [Enter] to trigger.
2948 * We sent "ESC [ 6 n", but got '\n' first, and
2949 * KEYCODE_CURSOR_POS response is now buffered from terminal.
2950 * It's bad already and not much can be done with it
2951 * (it _will_ be visible for the next process to read stdin),
2952 * but without this delay it even shows up on the screen
2953 * as garbage because we restore echo settings with tcsetattr
2954 * before it comes in. UGLY!
2955 */
2956 usleep(20*1000);
Denys Vlasenkofae73322020-12-21 21:55:03 +01002957// MAYBE? tcflush(STDIN_FILENO, TCIFLUSH); /* flushes data received but not read */
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002958 }
2959#endif
2960
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002961/* End of bug-catching "command_must_not_be_used" trick */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002962#undef command
2963
Denys Vlasenko19158a82010-03-26 14:06:56 +01002964#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +02002965 command[0] = '\0';
2966 if (command_len > 0)
2967 command_len = save_string(command, maxsize - 1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002968 free(command_ps);
2969#endif
2970
Flemming Madsend96ffda2013-04-07 18:47:24 +02002971 if (command_len > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002972 remember_in_history(command);
Flemming Madsend96ffda2013-04-07 18:47:24 +02002973 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002974
Eric Andersen27bb7902003-12-23 20:24:51 +00002975 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002976 command[command_len++] = '\n';
2977 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00002978 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002979
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002980#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002981 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002982#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002983
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002984 /* restore initial_settings */
Denis Vlasenko202ac502008-11-05 13:20:58 +00002985 tcsetattr_stdin_TCSANOW(&initial_settings);
Ron Yorston23286902018-02-25 20:09:54 +01002986#if ENABLE_FEATURE_EDITING_WINCH
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002987 /* restore SIGWINCH handler */
Denys Vlasenkobff71d32016-11-27 22:25:07 +01002988 sigaction_set(SIGWINCH, &S.SIGWINCH_handler);
Ron Yorston23286902018-02-25 20:09:54 +01002989#endif
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002990 fflush_all();
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002991
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002992 len = command_len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002993 DEINIT_S();
2994
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002995 return len; /* can't return command_len, DEINIT_S() destroys it */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002996}
2997
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002998#else /* !FEATURE_EDITING */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002999
3000#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00003001int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003002{
Ron Yorstoncad3fc72021-02-03 20:47:14 +01003003 fputs_stdout(prompt);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01003004 fflush_all();
Denys Vlasenkob2320372012-09-27 16:03:49 +02003005 if (!fgets(command, maxsize, stdin))
3006 return -1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003007 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00003008}
3009
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02003010#endif /* !FEATURE_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00003011
3012
Denis Vlasenko82b39e82007-01-21 19:18:19 +00003013/*
3014 * Testing
3015 */
3016
Eric Andersen5f2c79d2001-02-16 18:36:04 +00003017#ifdef TEST
3018
Eric Andersenf9ff8a72001-03-15 20:51:09 +00003019#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00003020
3021const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00003022
Eric Andersen5f2c79d2001-02-16 18:36:04 +00003023int main(int argc, char **argv)
3024{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00003025 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00003026 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00003027#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00003028 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
3029 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
Denys Vlasenko8187e012017-09-13 22:48:30 +02003030 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00003031#else
3032 "% ";
3033#endif
3034
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00003035 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00003036 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003037 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00003038 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00003039 break;
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02003040 buff[l-1] = '\0';
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003041 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00003042 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003043 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00003044 return 0;
3045}
3046
Eric Andersenc470f442003-07-28 09:56:35 +00003047#endif /* TEST */