blob: 3bfff008468be3143003ae26f0147dca0cd3fc05 [file] [log] [blame]
Erik Andersenc7c634b2000-03-19 05:28:55 +00001/* vi: set sw=4 ts=4: */
Erik Andersen13456d12000-03-16 08:09:57 +00002/*
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02003 * Command line editing.
Erik Andersen13456d12000-03-16 08:09:57 +00004 *
Eric Andersen81fe1232003-07-29 06:38:40 +00005 * Copyright (c) 1986-2003 may safely be consumed by a BSD or GPL license.
Eric Andersen7467c8d2001-07-12 20:26:32 +00006 * Written by: Vladimir Oleynik <dzo@simtreas.ru>
Eric Andersen5f2c79d2001-02-16 18:36:04 +00007 *
8 * Used ideas:
9 * Adam Rogoyski <rogoyski@cs.utexas.edu>
10 * Dave Cinege <dcinege@psychosis.com>
11 * Jakub Jelinek (c) 1995
Eric Andersen81fe1232003-07-29 06:38:40 +000012 * Erik Andersen <andersen@codepoet.org> (Majorly adjusted for busybox)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000013 *
Erik Andersen13456d12000-03-16 08:09:57 +000014 * This code is 'as is' with no warranty.
Erik Andersen13456d12000-03-16 08:09:57 +000015 */
16
17/*
Denis Vlasenko78ff8192008-06-28 21:03:43 +000018 * Usage and known bugs:
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010019 * Terminal key codes are not extensive, more needs to be added.
20 * This version was created on Debian GNU/Linux 2.x.
Denis Vlasenko78ff8192008-06-28 21:03:43 +000021 * Delete, Backspace, Home, End, and the arrow keys were tested
22 * to work in an Xterm and console. Ctrl-A also works as Home.
23 * Ctrl-E also works as End.
24 *
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010025 * The following readline-like commands are not implemented:
26 * ESC-b -- Move back one word
27 * ESC-f -- Move forward one word
28 * ESC-d -- Delete forward one word
29 * CTL-t -- Transpose two characters
30 *
Denis Vlasenko78ff8192008-06-28 21:03:43 +000031 * lineedit does not know that the terminal escape sequences do not
32 * take up space on the screen. The redisplay code assumes, unless
33 * told otherwise, that each character in the prompt is a printable
34 * character that takes up one character position on the screen.
35 * You need to tell lineedit that some sequences of characters
36 * in the prompt take up no screen space. Compatibly with readline,
37 * use the \[ escape to begin a sequence of non-printing characters,
38 * and the \] escape to signal the end of such a sequence. Example:
39 *
40 * PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +020041 *
42 * Unicode in PS1 is not fully supported: prompt length calulation is wrong,
43 * resulting in line wrap problems with long (multi-line) input.
44 *
45 * Multi-line PS1 (e.g. PS1="\n[\w]\n$ ") has problems with history
46 * browsing: up/down arrows result in scrolling.
47 * It stems from simplistic "cmdedit_y = cmdedit_prmt_len / cmdedit_termw"
48 * calculation of how many lines the prompt takes.
Erik Andersen13456d12000-03-16 08:09:57 +000049 */
Ron Yorstonf23264b2015-05-29 11:31:40 +010050#include "busybox.h"
51#include "NUM_APPLETS.h"
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020052#include "unicode.h"
Alexey Fomenko232ebaa2011-05-20 04:26:29 +020053#ifndef _POSIX_VDISABLE
54# define _POSIX_VDISABLE '\0'
55#endif
56
Glenn L McGrath475820c2004-01-22 12:42:23 +000057
Glenn L McGrath3b251852004-01-03 12:07:32 +000058#ifdef TEST
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020059# define ENABLE_FEATURE_EDITING 0
60# define ENABLE_FEATURE_TAB_COMPLETION 0
61# define ENABLE_FEATURE_USERNAME_COMPLETION 0
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020062#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000063
Eric Andersen5165fbe2001-02-20 06:42:29 +000064
Denis Vlasenko82b39e82007-01-21 19:18:19 +000065/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko38f63192007-01-22 09:03:07 +000066#if ENABLE_FEATURE_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000067
Denis Vlasenko82b39e82007-01-21 19:18:19 +000068
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +020069#define ENABLE_USERNAME_OR_HOMEDIR \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000070 (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +020071#define IF_USERNAME_OR_HOMEDIR(...)
72#if ENABLE_USERNAME_OR_HOMEDIR
73# undef IF_USERNAME_OR_HOMEDIR
74# define IF_USERNAME_OR_HOMEDIR(...) __VA_ARGS__
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000075#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000076
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020077
78#undef CHAR_T
Denys Vlasenko19158a82010-03-26 14:06:56 +010079#if ENABLE_UNICODE_SUPPORT
Tomas Heinricha659b812010-04-29 13:43:39 +020080# define BB_NUL ((wchar_t)0)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020081# define CHAR_T wchar_t
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010082static bool BB_isspace(CHAR_T c) { return ((unsigned)c < 256 && isspace(c)); }
Denys Vlasenko31e2e7b2009-12-12 02:42:35 +010083# if ENABLE_FEATURE_EDITING_VI
Natanael Copa7e6f9312016-08-14 23:30:29 +020084static bool BB_isalnum_or_underscore(CHAR_T c) {
85 return ((unsigned)c < 256 && isalnum(c)) || c == '_';
86}
Denys Vlasenko31e2e7b2009-12-12 02:42:35 +010087# endif
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010088static bool BB_ispunct(CHAR_T c) { return ((unsigned)c < 256 && ispunct(c)); }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020089# undef isspace
90# undef isalnum
91# undef ispunct
92# undef isprint
93# define isspace isspace_must_not_be_used
94# define isalnum isalnum_must_not_be_used
95# define ispunct ispunct_must_not_be_used
96# define isprint isprint_must_not_be_used
97#else
98# define BB_NUL '\0'
99# define CHAR_T char
100# define BB_isspace(c) isspace(c)
Natanael Copa7e6f9312016-08-14 23:30:29 +0200101# if ENABLE_FEATURE_EDITING_VI
102static bool BB_isalnum_or_underscore(CHAR_T c) {
103 return ((unsigned)c < 256 && isalnum(c)) || c == '_';
104}
105# endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200106# define BB_ispunct(c) ispunct(c)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200107#endif
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200108#if ENABLE_UNICODE_PRESERVE_BROKEN
109# define unicode_mark_raw_byte(wc) ((wc) | 0x20000000)
110# define unicode_is_raw_byte(wc) ((wc) & 0x20000000)
111#else
112# define unicode_is_raw_byte(wc) 0
113#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200114
115
Marek Polacek7b181072010-10-28 21:34:56 +0200116#define ESC "\033"
117
118#define SEQ_CLEAR_TILL_END_OF_SCREEN ESC"[J"
119//#define SEQ_CLEAR_TILL_END_OF_LINE ESC"[K"
Tomas Heinricha659b812010-04-29 13:43:39 +0200120
121
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000122enum {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000123 MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN < 0x7ff0
Denis Vlasenko6b404432008-01-07 16:13:14 +0000124 ? CONFIG_FEATURE_EDITING_MAX_LEN
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000125 : 0x7ff0
126};
Robert Griebl350d26b2002-12-03 22:45:46 +0000127
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200128#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000129static const char null_str[] ALIGN1 = "";
130#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +0000131
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000132/* We try to minimize both static and stack usage. */
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000133struct lineedit_statics {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000134 line_input_t *state;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000135
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000136 volatile unsigned cmdedit_termw; /* = 80; */ /* actual terminal width */
137 sighandler_t previous_SIGWINCH_handler;
Mark Whitley4e338752001-01-26 20:42:23 +0000138
Denys Vlasenko020f4062009-05-17 16:44:54 +0200139 unsigned cmdedit_x; /* real x (col) terminal position */
140 unsigned cmdedit_y; /* pseudoreal y (row) terminal position */
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000141 unsigned cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000142
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000143 unsigned cursor;
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +0200144 int command_len; /* must be signed */
145 /* signed maxsize: we want x in "if (x > S.maxsize)"
Denys Vlasenko044b1802009-07-12 02:50:35 +0200146 * to _not_ be promoted to unsigned */
147 int maxsize;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200148 CHAR_T *command_ps;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000149
150 const char *cmdedit_prompt;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000151
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200152#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000153 char *user_buf;
154 char *home_pwd_buf; /* = (char*)null_str; */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000155#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000156
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000157#if ENABLE_FEATURE_TAB_COMPLETION
158 char **matches;
159 unsigned num_matches;
160#endif
161
162#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200163# define DELBUFSIZ 128
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200164 CHAR_T *delptr;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000165 smallint newdelflag; /* whether delbuf should be reused yet */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200166 CHAR_T delbuf[DELBUFSIZ]; /* a place to store deleted characters */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000167#endif
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100168#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +0100169 smallint sent_ESC_br6n;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100170#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000171};
172
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000173/* See lineedit_ptr_hack.c */
174extern struct lineedit_statics *const lineedit_ptr_to_statics;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000175
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000176#define S (*lineedit_ptr_to_statics)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000177#define state (S.state )
178#define cmdedit_termw (S.cmdedit_termw )
179#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
180#define cmdedit_x (S.cmdedit_x )
181#define cmdedit_y (S.cmdedit_y )
182#define cmdedit_prmt_len (S.cmdedit_prmt_len)
183#define cursor (S.cursor )
184#define command_len (S.command_len )
185#define command_ps (S.command_ps )
186#define cmdedit_prompt (S.cmdedit_prompt )
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000187#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000188#define home_pwd_buf (S.home_pwd_buf )
189#define matches (S.matches )
190#define num_matches (S.num_matches )
191#define delptr (S.delptr )
192#define newdelflag (S.newdelflag )
193#define delbuf (S.delbuf )
194
195#define INIT_S() do { \
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000196 (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000197 barrier(); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000198 cmdedit_termw = 80; \
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200199 IF_USERNAME_OR_HOMEDIR(home_pwd_buf = (char*)null_str;) \
Shawn J. Goff46031da2013-02-27 18:30:05 +0100200 IF_FEATURE_EDITING_VI(delptr = delbuf;) \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000201} while (0)
Alexey Fomenko232ebaa2011-05-20 04:26:29 +0200202
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000203static void deinit_S(void)
204{
205#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000206 /* This one is allocated only if FANCY_PROMPT is on
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200207 * (otherwise it points to verbatim prompt (NOT malloced)) */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000208 free((char*)cmdedit_prompt);
209#endif
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +0200210#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000211 free(user_buf);
212 if (home_pwd_buf != null_str)
213 free(home_pwd_buf);
214#endif
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000215 free(lineedit_ptr_to_statics);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000216}
217#define DEINIT_S() deinit_S()
218
Denis Vlasenko2c844952008-04-25 18:44:35 +0000219
Denys Vlasenko19158a82010-03-26 14:06:56 +0100220#if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200221static size_t load_string(const char *src)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200222{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100223 if (unicode_status == UNICODE_ON) {
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200224 ssize_t len = mbstowcs(command_ps, src, S.maxsize - 1);
Denys Vlasenko353680a2011-03-27 01:18:07 +0100225 if (len < 0)
226 len = 0;
227 command_ps[len] = BB_NUL;
228 return len;
229 } else {
230 unsigned i = 0;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200231 while (src[i] && i < S.maxsize - 1) {
232 command_ps[i] = src[i];
Denys Vlasenko353680a2011-03-27 01:18:07 +0100233 i++;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200234 }
235 command_ps[i] = BB_NUL;
Denys Vlasenko353680a2011-03-27 01:18:07 +0100236 return i;
237 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200238}
Tomas Heinricha659b812010-04-29 13:43:39 +0200239static unsigned save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200240{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100241 if (unicode_status == UNICODE_ON) {
Denys Vlasenko94043e82010-05-11 14:49:13 +0200242# if !ENABLE_UNICODE_PRESERVE_BROKEN
Denys Vlasenko353680a2011-03-27 01:18:07 +0100243 ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
244 if (len < 0)
245 len = 0;
246 dst[len] = '\0';
247 return len;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200248# else
Denys Vlasenko353680a2011-03-27 01:18:07 +0100249 unsigned dstpos = 0;
250 unsigned srcpos = 0;
Tomas Heinricha659b812010-04-29 13:43:39 +0200251
Denys Vlasenko353680a2011-03-27 01:18:07 +0100252 maxsize--;
253 while (dstpos < maxsize) {
254 wchar_t wc;
255 int n = srcpos;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200256
Denys Vlasenko353680a2011-03-27 01:18:07 +0100257 /* Convert up to 1st invalid byte (or up to end) */
258 while ((wc = command_ps[srcpos]) != BB_NUL
259 && !unicode_is_raw_byte(wc)
260 ) {
261 srcpos++;
262 }
263 command_ps[srcpos] = BB_NUL;
264 n = wcstombs(dst + dstpos, command_ps + n, maxsize - dstpos);
265 if (n < 0) /* should not happen */
266 break;
267 dstpos += n;
268 if (wc == BB_NUL) /* usually is */
269 break;
270
271 /* We do have invalid byte here! */
272 command_ps[srcpos] = wc; /* restore it */
Tomas Heinricha659b812010-04-29 13:43:39 +0200273 srcpos++;
Denys Vlasenko353680a2011-03-27 01:18:07 +0100274 if (dstpos == maxsize)
275 break;
276 dst[dstpos++] = (char) wc;
Tomas Heinricha659b812010-04-29 13:43:39 +0200277 }
Denys Vlasenko353680a2011-03-27 01:18:07 +0100278 dst[dstpos] = '\0';
279 return dstpos;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200280# endif
Denys Vlasenko353680a2011-03-27 01:18:07 +0100281 } else {
282 unsigned i = 0;
283 while ((dst[i] = command_ps[i]) != 0)
284 i++;
285 return i;
286 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200287}
288/* I thought just fputwc(c, stdout) would work. But no... */
289static void BB_PUTCHAR(wchar_t c)
290{
Denys Vlasenko353680a2011-03-27 01:18:07 +0100291 if (unicode_status == UNICODE_ON) {
292 char buf[MB_CUR_MAX + 1];
293 mbstate_t mbst = { 0 };
294 ssize_t len = wcrtomb(buf, c, &mbst);
295 if (len > 0) {
296 buf[len] = '\0';
297 fputs(buf, stdout);
298 }
299 } else {
300 /* In this case, c is always one byte */
301 putchar(c);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200302 }
303}
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200304# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
305static wchar_t adjust_width_and_validate_wc(unsigned *width_adj, wchar_t wc)
306# else
307static wchar_t adjust_width_and_validate_wc(wchar_t wc)
308# define adjust_width_and_validate_wc(width_adj, wc) \
309 ((*(width_adj))++, adjust_width_and_validate_wc(wc))
310# endif
311{
312 int w = 1;
313
314 if (unicode_status == UNICODE_ON) {
Denys Vlasenko26e2c1d2010-05-16 21:15:03 +0200315 if (wc > CONFIG_LAST_SUPPORTED_WCHAR) {
316 /* note: also true for unicode_is_raw_byte(wc) */
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200317 goto subst;
318 }
319 w = wcwidth(wc);
320 if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0)
321 || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
322 || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
323 ) {
324 subst:
325 w = 1;
326 wc = CONFIG_SUBST_WCHAR;
327 }
328 }
329
330# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
331 *width_adj += w;
332#endif
333 return wc;
334}
335#else /* !UNICODE */
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200336static size_t load_string(const char *src)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200337{
Denys Vlasenkoa669eca2011-07-11 07:36:59 +0200338 safe_strncpy(command_ps, src, S.maxsize);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200339 return strlen(command_ps);
340}
Denys Vlasenko9038d6f2009-07-15 20:02:19 +0200341# if ENABLE_FEATURE_TAB_COMPLETION
Tomas Heinricha659b812010-04-29 13:43:39 +0200342static void save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200343{
344 safe_strncpy(dst, command_ps, maxsize);
345}
Denys Vlasenko7dd0ce42009-07-15 18:27:47 +0200346# endif
347# define BB_PUTCHAR(c) bb_putchar(c)
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200348/* Should never be called: */
349int adjust_width_and_validate_wc(unsigned *width_adj, int wc);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200350#endif
351
352
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000353/* Put 'command_ps[cursor]', cursor++.
354 * Advance cursor on screen. If we reached right margin, scroll text up
355 * and remove terminal margin effect by printing 'next_char' */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000356#define HACK_FOR_WRONG_WIDTH 1
Denys Vlasenko94043e82010-05-11 14:49:13 +0200357static void put_cur_glyph_and_inc_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000358{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200359 CHAR_T c = command_ps[cursor];
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200360 unsigned width = 0;
361 int ofs_to_right;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000362
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200363 if (c == BB_NUL) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000364 /* erase character after end of input string */
365 c = ' ';
Denys Vlasenko94043e82010-05-11 14:49:13 +0200366 } else {
367 /* advance cursor only if we aren't at the end yet */
368 cursor++;
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200369 if (unicode_status == UNICODE_ON) {
370 IF_UNICODE_WIDE_WCHARS(width = cmdedit_x;)
371 c = adjust_width_and_validate_wc(&cmdedit_x, c);
372 IF_UNICODE_WIDE_WCHARS(width = cmdedit_x - width;)
373 } else {
374 cmdedit_x++;
375 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000376 }
Denys Vlasenko94043e82010-05-11 14:49:13 +0200377
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200378 ofs_to_right = cmdedit_x - cmdedit_termw;
379 if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right <= 0) {
380 /* c fits on this line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200381 BB_PUTCHAR(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000382 }
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200383
384 if (ofs_to_right >= 0) {
385 /* we go to the next line */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000386#if HACK_FOR_WRONG_WIDTH
387 /* This works better if our idea of term width is wrong
388 * and it is actually wider (often happens on serial lines).
389 * Printing CR,LF *forces* cursor to next line.
390 * OTOH if terminal width is correct AND terminal does NOT
391 * have automargin (IOW: it is moving cursor to next line
392 * by itself (which is wrong for VT-10x terminals)),
393 * this will break things: there will be one extra empty line */
394 puts("\r"); /* + implicit '\n' */
395#else
Denys Vlasenko94043e82010-05-11 14:49:13 +0200396 /* VT-10x terminals don't wrap cursor to next line when last char
397 * on the line is printed - cursor stays "over" this char.
398 * Need to print _next_ char too (first one to appear on next line)
399 * to make cursor move down to next line.
400 */
401 /* Works ok only if cmdedit_termw is correct. */
402 c = command_ps[cursor];
403 if (c == BB_NUL)
404 c = ' ';
405 BB_PUTCHAR(c);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000406 bb_putchar('\b');
Denis Vlasenko2c844952008-04-25 18:44:35 +0000407#endif
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200408 cmdedit_y++;
409 if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right == 0) {
410 width = 0;
411 } else { /* ofs_to_right > 0 */
412 /* wide char c didn't fit on prev line */
413 BB_PUTCHAR(c);
414 }
415 cmdedit_x = width;
Mark Whitley4e338752001-01-26 20:42:23 +0000416 }
Erik Andersen13456d12000-03-16 08:09:57 +0000417}
418
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000419/* Move to end of line (by printing all chars till the end) */
Denys Vlasenko94043e82010-05-11 14:49:13 +0200420static void put_till_end_and_adv_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000421{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000422 while (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200423 put_cur_glyph_and_inc_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +0000424}
425
426/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000427static void goto_new_line(void)
428{
Denys Vlasenko94043e82010-05-11 14:49:13 +0200429 put_till_end_and_adv_cursor();
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200430 if (cmdedit_x != 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000431 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000432}
433
Rob Landley88621d72006-08-29 19:41:06 +0000434static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000435{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000436 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000437}
438
Denys Vlasenko248c3242010-05-17 00:45:44 +0200439static void put_prompt(void)
440{
441 unsigned w;
442
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200443 fputs(cmdedit_prompt, stdout);
Denys Vlasenko248c3242010-05-17 00:45:44 +0200444 fflush_all();
445 cursor = 0;
446 w = cmdedit_termw; /* read volatile var once */
447 cmdedit_y = cmdedit_prmt_len / w; /* new quasireal y */
448 cmdedit_x = cmdedit_prmt_len % w;
449}
450
Eric Andersenaff114c2004-04-14 17:51:38 +0000451/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000452/* (optimized for slow terminals) */
453static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000454{
455 if (num > cursor)
456 num = cursor;
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200457 if (num == 0)
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000458 return;
459 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000460
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200461 if ((ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS)
462 && unicode_status == UNICODE_ON
463 ) {
464 /* correct NUM to be equal to _screen_ width */
465 int n = num;
466 num = 0;
467 while (--n >= 0)
468 adjust_width_and_validate_wc(&num, command_ps[cursor + n]);
469 if (num == 0)
470 return;
471 }
472
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000473 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000474 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000475 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000476 /* This is longer by 5 bytes on x86.
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000477 * Also gets miscompiled for ARM users
478 * (busybox.net/bugs/view.php?id=2274).
Denis Vlasenko6f043912008-02-18 22:28:03 +0000479 * printf(("\b\b\b\b" + 4) - num);
480 * return;
481 */
482 do {
483 bb_putchar('\b');
484 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000485 return;
486 }
Marek Polacek7b181072010-10-28 21:34:56 +0200487 printf(ESC"[%uD", num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000488 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000489 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000490
491 /* Need to go one or more lines up */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200492 if (ENABLE_UNICODE_WIDE_WCHARS) {
493 /* With wide chars, it is hard to "backtrack"
494 * and reliably figure out where to put cursor.
495 * Example (<> is a wide char; # is an ordinary char, _ cursor):
496 * |prompt: <><> |
497 * |<><><><><><> |
498 * |_ |
499 * and user presses left arrow. num = 1, cmdedit_x = 0,
500 * We need to go up one line, and then - how do we know that
501 * we need to go *10* positions to the right? Because
502 * |prompt: <>#<>|
503 * |<><><>#<><><>|
504 * |_ |
505 * in this situation we need to go *11* positions to the right.
506 *
507 * A simpler thing to do is to redraw everything from the start
508 * up to new cursor position (which is already known):
509 */
510 unsigned sv_cursor;
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200511 /* go to 1st column; go up to first line */
Marek Polacek7b181072010-10-28 21:34:56 +0200512 printf("\r" ESC"[%uA", cmdedit_y);
Denys Vlasenko248c3242010-05-17 00:45:44 +0200513 cmdedit_y = 0;
514 sv_cursor = cursor;
515 put_prompt(); /* sets cursor to 0 */
516 while (cursor < sv_cursor)
517 put_cur_glyph_and_inc_cursor();
518 } else {
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200519 int lines_up;
520 unsigned width;
521 /* num = chars to go back from the beginning of current line: */
Denys Vlasenko248c3242010-05-17 00:45:44 +0200522 num -= cmdedit_x;
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200523 width = cmdedit_termw; /* read volatile var once */
524 /* num=1...w: one line up, w+1...2w: two, etc: */
525 lines_up = 1 + (num - 1) / width;
526 cmdedit_x = (width * cmdedit_y - num) % width;
527 cmdedit_y -= lines_up;
528 /* go to 1st column; go up */
Marek Polacek7b181072010-10-28 21:34:56 +0200529 printf("\r" ESC"[%uA", lines_up);
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200530 /* go to correct column.
Denys Vlasenkobbf1aa12010-05-17 12:33:13 +0200531 * xterm, konsole, Linux VT interpret 0 as 1 below! wow.
532 * need to *make sure* we skip it if cmdedit_x == 0 */
Denys Vlasenko1118d9b2010-05-17 12:30:44 +0200533 if (cmdedit_x)
Marek Polacek7b181072010-10-28 21:34:56 +0200534 printf(ESC"[%uC", cmdedit_x);
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000535 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000536}
537
Eric Andersenaff114c2004-04-14 17:51:38 +0000538/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000539static void redraw(int y, int back_cursor)
540{
Denys Vlasenko9963fe32010-05-17 04:05:53 +0200541 if (y > 0) /* up y lines */
Marek Polacek7b181072010-10-28 21:34:56 +0200542 printf(ESC"[%uA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000543 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000544 put_prompt();
Denys Vlasenko94043e82010-05-11 14:49:13 +0200545 put_till_end_and_adv_cursor();
546 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000547 input_backward(back_cursor);
548}
549
Paul Fox3f11b1b2005-08-04 19:04:46 +0000550/* Delete the char in front of the cursor, optionally saving it
551 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000552#if !ENABLE_FEATURE_EDITING_VI
553static void input_delete(void)
554#define input_delete(save) input_delete()
555#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000556static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000557#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000558{
Mark Whitley4e338752001-01-26 20:42:23 +0000559 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000560
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000561 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000562 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000563
Denis Vlasenko38f63192007-01-22 09:03:07 +0000564#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000565 if (save) {
566 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000567 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000568 newdelflag = 0;
569 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000570 if ((delptr - delbuf) < DELBUFSIZ)
571 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000572 }
573#endif
574
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200575 memmove(command_ps + j, command_ps + j + 1,
Denys Vlasenko9531f7d2009-07-16 14:33:16 +0200576 /* (command_len + 1 [because of NUL]) - (j + 1)
577 * simplified into (command_len - j) */
578 (command_len - j) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000579 command_len--;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200580 put_till_end_and_adv_cursor();
581 /* Last char is still visible, erase it (and more) */
582 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersenc470f442003-07-28 09:56:35 +0000583 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000584}
585
Denis Vlasenko38f63192007-01-22 09:03:07 +0000586#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000587static void put(void)
588{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000589 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000590 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000591
Paul Fox3f11b1b2005-08-04 19:04:46 +0000592 if (j == 0)
593 return;
594 ocursor = cursor;
595 /* open hole and then fill it */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200596 memmove(command_ps + cursor + j, command_ps + cursor,
597 (command_len - cursor + 1) * sizeof(command_ps[0]));
598 memcpy(command_ps + cursor, delbuf, j * sizeof(command_ps[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000599 command_len += j;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200600 put_till_end_and_adv_cursor();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000601 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000602}
603#endif
604
Mark Whitley4e338752001-01-26 20:42:23 +0000605/* Delete the char in back of the cursor */
606static void input_backspace(void)
607{
608 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000609 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000610 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000611 }
612}
613
Eric Andersenaff114c2004-04-14 17:51:38 +0000614/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000615static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000616{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000617 if (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200618 put_cur_glyph_and_inc_cursor();
Erik Andersenf0657d32000-04-12 17:49:52 +0000619}
620
Denis Vlasenko38f63192007-01-22 09:03:07 +0000621#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000622
Mike Shalf3763032010-11-22 03:49:18 +0100623//FIXME:
624//needs to be more clever: currently it thinks that "foo\ b<TAB>
625//matches the file named "foo bar", which is untrue.
626//Also, perhaps "foo b<TAB> needs to complete to "foo bar" <cursor>,
627//not "foo bar <cursor>...
628
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000629static void free_tab_completion_data(void)
630{
631 if (matches) {
632 while (num_matches)
633 free(matches[--num_matches]);
634 free(matches);
635 matches = NULL;
636 }
637}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000638
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000639static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000640{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000641 matches = xrealloc_vector(matches, 4, num_matches);
642 matches[num_matches] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000643 num_matches++;
644}
645
Mike Shalf3763032010-11-22 03:49:18 +0100646# if ENABLE_FEATURE_USERNAME_COMPLETION
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200647/* Replace "~user/..." with "/homedir/...".
648 * The parameter is malloced, free it or return it
649 * unchanged if no user is matched.
650 */
651static char *username_path_completion(char *ud)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000652{
653 struct passwd *entry;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200654 char *tilde_name = ud;
655 char *home = NULL;
656
657 ud++; /* skip ~ */
658 if (*ud == '/') { /* "~/..." */
659 home = home_pwd_buf;
660 } else {
661 /* "~user/..." */
662 ud = strchr(ud, '/');
663 *ud = '\0'; /* "~user" */
664 entry = getpwnam(tilde_name + 1);
665 *ud = '/'; /* restore "~user/..." */
666 if (entry)
667 home = entry->pw_dir;
668 }
669 if (home) {
670 ud = concat_path_file(home, ud);
671 free(tilde_name);
672 tilde_name = ud;
673 }
674 return tilde_name;
675}
676
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200677/* ~use<tab> - find all users with this prefix.
678 * Return the length of the prefix used for matching.
679 */
680static NOINLINE unsigned complete_username(const char *ud)
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200681{
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100682 struct passwd *pw;
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200683 unsigned userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000684
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200685 ud++; /* skip ~ */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000686 userlen = strlen(ud);
687
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200688 setpwent();
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100689 while ((pw = getpwent()) != NULL) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200690 /* Null usernames should result in all users as possible completions. */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100691 if (/* !ud[0] || */ is_prefixed_with(pw->pw_name, ud)) {
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100692 add_match(xasprintf("~%s/", pw->pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000693 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000694 }
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100695 endpwent(); /* don't keep password file open */
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200696
697 return 1 + userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000698}
Mike Shalf3763032010-11-22 03:49:18 +0100699# endif /* FEATURE_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000700
701enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000702 FIND_EXE_ONLY = 0,
703 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000704 FIND_FILE_ONLY = 2,
705};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000706
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200707static int path_parse(char ***p)
Mark Whitley4e338752001-01-26 20:42:23 +0000708{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000709 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000710 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000711 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000712 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000713
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000714 if (state->flags & WITH_PATH_LOOKUP)
715 pth = state->path_lookup;
716 else
717 pth = getenv("PATH");
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200718
719 /* PATH="" or PATH=":"? */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000720 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
721 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000722
Denis Vlasenko253ce002007-01-22 08:34:44 +0000723 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000724 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000725 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000726 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000727 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000728 break;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200729 tmp++;
730 if (*tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000731 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000732 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000733 }
734
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200735 *p = res = xmalloc(npth * sizeof(res[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000736 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000737 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000738 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000739 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000740 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000741 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000742 *tmp++ = '\0'; /* ':' -> '\0' */
743 if (*tmp == '\0')
744 break; /* :<empty> */
745 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000746 }
Mark Whitley4e338752001-01-26 20:42:23 +0000747 return npth;
748}
749
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200750/* Complete command, directory or file name.
751 * Return the length of the prefix used for matching.
752 */
753static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000754{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000755 char *path1[1];
756 char **paths = path1;
757 int npaths;
758 int i;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200759 unsigned pf_len;
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200760 const char *pfind;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200761 char *dirbuf = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +0000762
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000763 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000764 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000765
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200766 pfind = strrchr(command, '/');
767 if (!pfind) {
768 if (type == FIND_EXE_ONLY)
769 npaths = path_parse(&paths);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000770 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000771 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000772 /* point to 'l' in "..../last_component" */
773 pfind++;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200774 /* dirbuf = ".../.../.../" */
775 dirbuf = xstrndup(command, pfind - command);
Mike Shalf3763032010-11-22 03:49:18 +0100776# if ENABLE_FEATURE_USERNAME_COMPLETION
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200777 if (dirbuf[0] == '~') /* ~/... or ~user/... */
778 dirbuf = username_path_completion(dirbuf);
Mike Shalf3763032010-11-22 03:49:18 +0100779# endif
Denys Vlasenko7063e862010-09-02 12:44:39 +0200780 path1[0] = dirbuf;
Mark Whitley4e338752001-01-26 20:42:23 +0000781 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200782 pf_len = strlen(pfind);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000783
Ron Yorstonf23264b2015-05-29 11:31:40 +0100784#if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1
785 if (type == FIND_EXE_ONLY) {
786 const char *p = applet_names;
787
Ron Yorston2b919582016-04-08 11:57:20 +0100788 while (*p) {
Ron Yorstonf23264b2015-05-29 11:31:40 +0100789 if (strncmp(pfind, p, pf_len) == 0)
790 add_match(xstrdup(p));
Ron Yorston2b919582016-04-08 11:57:20 +0100791 while (*p++ != '\0')
792 continue;
Ron Yorstonf23264b2015-05-29 11:31:40 +0100793 }
794 }
795#endif
796
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000797 for (i = 0; i < npaths; i++) {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200798 DIR *dir;
799 struct dirent *next;
800 struct stat st;
801 char *found;
802
Mark Whitley4e338752001-01-26 20:42:23 +0000803 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000804 if (!dir)
805 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000806
807 while ((next = readdir(dir)) != NULL) {
Denys Vlasenko39263632010-09-03 14:11:08 +0200808 unsigned len;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200809 const char *name_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000810
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200811 /* .../<tab>: bash 3.2.0 shows dotfiles, but not . and .. */
812 if (!pfind[0] && DOT_OR_DOTDOT(name_found))
Mark Whitley4e338752001-01-26 20:42:23 +0000813 continue;
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200814 /* match? */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100815 if (!is_prefixed_with(name_found, pfind))
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200816 continue; /* no */
817
818 found = concat_path_file(paths[i], name_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000819 /* NB: stat() first so that we see is it a directory;
820 * but if that fails, use lstat() so that
821 * we still match dangling links */
822 if (stat(found, &st) && lstat(found, &st))
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200823 goto cont; /* hmm, remove in progress? */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000824
Denys Vlasenko39263632010-09-03 14:11:08 +0200825 /* Save only name */
826 len = strlen(name_found);
827 found = xrealloc(found, len + 2); /* +2: for slash and NUL */
828 strcpy(found, name_found);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000829
Mark Whitley4e338752001-01-26 20:42:23 +0000830 if (S_ISDIR(st.st_mode)) {
Denys Vlasenko39263632010-09-03 14:11:08 +0200831 /* name is a directory, add slash */
832 found[len] = '/';
833 found[len + 1] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +0000834 } else {
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200835 /* skip files if looking for dirs only (example: cd) */
Eric Andersenc470f442003-07-28 09:56:35 +0000836 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000837 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000838 }
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200839 /* add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000840 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000841 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000842 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000843 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000844 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000845 closedir(dir);
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200846 } /* for every path */
847
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000848 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000849 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000850 free(paths);
851 }
Denys Vlasenko39263632010-09-03 14:11:08 +0200852 free(dirbuf);
Denys Vlasenko3c460b02010-09-03 12:56:36 +0200853
854 return pf_len;
Erik Andersen6273f652000-03-17 01:12:41 +0000855}
Erik Andersenf0657d32000-04-12 17:49:52 +0000856
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200857/* build_match_prefix:
Denys Vlasenko76939e72010-09-03 14:09:24 +0200858 * On entry, match_buf contains everything up to cursor at the moment <tab>
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200859 * was pressed. This function looks at it, figures out what part of it
860 * constitutes the command/file/directory prefix to use for completion,
Denys Vlasenko76939e72010-09-03 14:09:24 +0200861 * and rewrites match_buf to contain only that part.
Denys Vlasenkob068bd72010-09-02 12:01:11 +0200862 */
Denys Vlasenko76939e72010-09-03 14:09:24 +0200863#define dbg_bmp 0
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200864/* Helpers: */
865/* QUOT is used on elements of int_buf[], which are bytes,
866 * not Unicode chars. Therefore it works correctly even in Unicode mode.
867 */
868#define QUOT (UCHAR_MAX+1)
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200869static void remove_chunk(int16_t *int_buf, int beg, int end)
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200870{
871 /* beg must be <= end */
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200872 if (beg == end)
873 return;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200874
875 while ((int_buf[beg] = int_buf[end]) != 0)
876 beg++, end++;
877
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200878 if (dbg_bmp) {
879 int i;
880 for (i = 0; int_buf[i]; i++)
881 bb_putchar((unsigned char)int_buf[i]);
882 bb_putchar('\n');
883 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200884}
Denys Vlasenko76939e72010-09-03 14:09:24 +0200885/* Caller ensures that match_buf points to a malloced buffer
886 * big enough to hold strlen(match_buf)*2 + 2
887 */
888static NOINLINE int build_match_prefix(char *match_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000889{
890 int i, j;
891 int command_mode;
Denys Vlasenko76939e72010-09-03 14:09:24 +0200892 int16_t *int_buf = (int16_t*)match_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000893
Denys Vlasenko76939e72010-09-03 14:09:24 +0200894 if (dbg_bmp) printf("\n%s\n", match_buf);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200895
Denys Vlasenko76939e72010-09-03 14:09:24 +0200896 /* Copy in reverse order, since they overlap */
897 i = strlen(match_buf);
898 do {
899 int_buf[i] = (unsigned char)match_buf[i];
900 i--;
901 } while (i >= 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000902
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200903 /* Mark every \c as "quoted c" */
Denys Vlasenko76939e72010-09-03 14:09:24 +0200904 for (i = 0; int_buf[i]; i++) {
905 if (int_buf[i] == '\\') {
906 remove_chunk(int_buf, i, i + 1);
907 int_buf[i] |= QUOT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000908 }
Tomas Heinrichb8909c52010-05-16 20:46:53 +0200909 }
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200910 /* Quote-mark "chars" and 'chars', drop delimiters */
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200911 {
912 int in_quote = 0;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200913 i = 0;
914 while (int_buf[i]) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200915 int cur = int_buf[i];
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200916 if (!cur)
917 break;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200918 if (cur == '\'' || cur == '"') {
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200919 if (!in_quote || (cur == in_quote)) {
920 in_quote ^= cur;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200921 remove_chunk(int_buf, i, i + 1);
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200922 continue;
923 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000924 }
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200925 if (in_quote)
926 int_buf[i] = cur | QUOT;
927 i++;
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200928 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000929 }
930
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200931 /* Remove everything up to command delimiters:
932 * ';' ';;' '&' '|' '&&' '||',
933 * but careful with '>&' '<&' '>|'
934 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000935 for (i = 0; int_buf[i]; i++) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200936 int cur = int_buf[i];
937 if (cur == ';' || cur == '&' || cur == '|') {
938 int prev = i ? int_buf[i - 1] : 0;
939 if (cur == '&' && (prev == '>' || prev == '<')) {
940 continue;
941 } else if (cur == '|' && prev == '>') {
942 continue;
943 }
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200944 remove_chunk(int_buf, 0, i + 1 + (cur == int_buf[i + 1]));
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200945 i = -1; /* back to square 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000946 }
947 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200948 /* Remove all `cmd` */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200949 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000950 if (int_buf[i] == '`') {
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200951 for (j = i + 1; int_buf[j]; j++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000952 if (int_buf[j] == '`') {
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200953 /* `cmd` should count as a word:
954 * `cmd` c<tab> should search for files c*,
955 * not commands c*. Therefore we don't drop
956 * `cmd` entirely, we replace it with single `.
957 */
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200958 remove_chunk(int_buf, i, j);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200959 goto next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000960 }
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200961 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200962 /* No closing ` - command mode, remove all up to ` */
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200963 remove_chunk(int_buf, 0, i + 1);
Denys Vlasenko2679e3c2010-09-03 12:53:15 +0200964 break;
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200965 next: ;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000966 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200967 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000968
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200969 /* Remove "cmd (" and "cmd {"
970 * Example: "if { c<tab>"
971 * In this example, c should be matched as command pfx.
972 */
973 for (i = 0; int_buf[i]; i++) {
974 if (int_buf[i] == '(' || int_buf[i] == '{') {
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200975 remove_chunk(int_buf, 0, i + 1);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +0200976 i = -1; /* back to square 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000977 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200978 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000979
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200980 /* Remove leading unquoted spaces */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000981 for (i = 0; int_buf[i]; i++)
982 if (int_buf[i] != ' ')
983 break;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +0200984 remove_chunk(int_buf, 0, i);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000985
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200986 /* Determine completion mode */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000987 command_mode = FIND_EXE_ONLY;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200988 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000989 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
Denys Vlasenko57ea9b42010-09-03 12:51:36 +0200990 if (int_buf[i] == ' '
991 && command_mode == FIND_EXE_ONLY
Denys Vlasenko81254ed2010-09-03 12:59:15 +0200992 && (char)int_buf[0] == 'c'
993 && (char)int_buf[1] == 'd'
994 && i == 2 /* -> int_buf[2] == ' ' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000995 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000996 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000997 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000998 command_mode = FIND_FILE_ONLY;
999 break;
1000 }
1001 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001002 }
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001003 if (dbg_bmp) printf("command_mode(0:exe/1:dir/2:file):%d\n", command_mode);
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001004
1005 /* Remove everything except last word */
Denys Vlasenkob068bd72010-09-02 12:01:11 +02001006 for (i = 0; int_buf[i]; i++) /* quasi-strlen(int_buf) */
1007 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001008 for (--i; i >= 0; i--) {
Denys Vlasenko2679e3c2010-09-03 12:53:15 +02001009 int cur = int_buf[i];
1010 if (cur == ' ' || cur == '<' || cur == '>' || cur == '|' || cur == '&') {
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001011 remove_chunk(int_buf, 0, i + 1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001012 break;
1013 }
1014 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001015
Denys Vlasenko76939e72010-09-03 14:09:24 +02001016 /* Convert back to string of _chars_ */
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001017 i = 0;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001018 while ((match_buf[i] = int_buf[i]) != '\0')
Denys Vlasenko81254ed2010-09-03 12:59:15 +02001019 i++;
Denys Vlasenko9b56bf52010-09-03 13:02:47 +02001020
Denys Vlasenko76939e72010-09-03 14:09:24 +02001021 if (dbg_bmp) printf("final match_buf:'%s'\n", match_buf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001022
1023 return command_mode;
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02001024}
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001025
Glenn L McGrath4d001292003-01-06 01:11:50 +00001026/*
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001027 * Display by column (original idea from ls applet,
1028 * very optimized by me [Vladimir] :)
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001029 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001030static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +00001031{
1032 int ncols, row;
1033 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001034 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001035 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001036 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001037
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001038 /* find the longest file name - use that as the column width */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001039 for (row = 0; row < nrows; row++) {
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001040 l = unicode_strwidth(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001041 if (column_width < l)
1042 column_width = l;
1043 }
1044 column_width += 2; /* min space for columns */
1045 ncols = cmdedit_termw / column_width;
1046
1047 if (ncols > 1) {
1048 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001049 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +00001050 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001051 } else {
1052 ncols = 1;
1053 }
1054 for (row = 0; row < nrows; row++) {
1055 int n = row;
1056 int nc;
1057
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001058 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001059 printf("%s%-*s", matches[n],
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001060 (int)(column_width - unicode_strwidth(matches[n])), ""
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001061 );
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001062 }
Tomas Heinrich11bcf4b2010-06-01 08:33:18 +02001063 if (ENABLE_UNICODE_SUPPORT)
1064 puts(printable_string(NULL, matches[n]));
1065 else
1066 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001067 }
1068}
1069
Mike Shalf3763032010-11-22 03:49:18 +01001070static const char *is_special_char(char c)
1071{
1072 return strchr(" `\"#$%^&*()=+{}[]:;'|\\<>", c);
1073}
1074
1075static char *quote_special_chars(char *found)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001076{
1077 int l = 0;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001078 char *s = xzalloc((strlen(found) + 1) * 2);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001079
1080 while (*found) {
Mike Shalf3763032010-11-22 03:49:18 +01001081 if (is_special_char(*found))
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001082 s[l++] = '\\';
1083 s[l++] = *found++;
1084 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +02001085 /* s[l] = '\0'; - already is */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001086 return s;
1087}
1088
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001089/* Do TAB completion */
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001090static NOINLINE void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001091{
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001092 char *chosen_match;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001093 char *match_buf;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001094 size_t len_found;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001095 /* Length of string used for matching */
1096 unsigned match_pfx_len = match_pfx_len;
1097 int find_type;
Mike Shalf3763032010-11-22 03:49:18 +01001098# if ENABLE_UNICODE_SUPPORT
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001099 /* cursor pos in command converted to multibyte form */
1100 int cursor_mb;
Mike Shalf3763032010-11-22 03:49:18 +01001101# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001102 if (!(state->flags & TAB_COMPLETION))
1103 return;
1104
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001105 if (*lastWasTab) {
1106 /* The last char was a TAB too.
1107 * Print a list of all the available choices.
1108 */
Denys Vlasenkoa46e16e2010-09-03 13:05:51 +02001109 if (num_matches > 0) {
1110 /* cursor will be changed by goto_new_line() */
Denys Vlasenko044b1802009-07-12 02:50:35 +02001111 int sav_cursor = cursor;
Mark Whitley4e338752001-01-26 20:42:23 +00001112 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001113 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001114 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001115 }
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001116 return;
Erik Andersenf0657d32000-04-12 17:49:52 +00001117 }
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001118
1119 *lastWasTab = 1;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001120 chosen_match = NULL;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001121
Denys Vlasenko76939e72010-09-03 14:09:24 +02001122 /* Make a local copy of the string up to the position of the cursor.
1123 * build_match_prefix will expand it into int16_t's, need to allocate
1124 * twice as much as the string_len+1.
1125 * (we then also (ab)use this extra space later - see (**))
1126 */
1127 match_buf = xmalloc(MAX_LINELEN * sizeof(int16_t));
Mike Shalf3763032010-11-22 03:49:18 +01001128# if !ENABLE_UNICODE_SUPPORT
Denys Vlasenko76939e72010-09-03 14:09:24 +02001129 save_string(match_buf, cursor + 1); /* +1 for NUL */
Mike Shalf3763032010-11-22 03:49:18 +01001130# else
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001131 {
1132 CHAR_T wc = command_ps[cursor];
1133 command_ps[cursor] = BB_NUL;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001134 save_string(match_buf, MAX_LINELEN);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001135 command_ps[cursor] = wc;
Denys Vlasenko76939e72010-09-03 14:09:24 +02001136 cursor_mb = strlen(match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001137 }
Mike Shalf3763032010-11-22 03:49:18 +01001138# endif
Denys Vlasenko76939e72010-09-03 14:09:24 +02001139 find_type = build_match_prefix(match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001140
1141 /* Free up any memory already allocated */
1142 free_tab_completion_data();
1143
Mike Shalf3763032010-11-22 03:49:18 +01001144# if ENABLE_FEATURE_USERNAME_COMPLETION
1145 /* If the word starts with ~ and there is no slash in the word,
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001146 * then try completing this word as a username. */
1147 if (state->flags & USERNAME_COMPLETION)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001148 if (match_buf[0] == '~' && strchr(match_buf, '/') == NULL)
1149 match_pfx_len = complete_username(match_buf);
Mike Shalf3763032010-11-22 03:49:18 +01001150# endif
1151 /* If complete_username() did not match,
1152 * try to match a command in $PATH, or a directory, or a file */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001153 if (!matches)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001154 match_pfx_len = complete_cmd_dir_file(match_buf, find_type);
Mike Shalf3763032010-11-22 03:49:18 +01001155
1156 /* Account for backslashes which will be inserted
1157 * by quote_special_chars() later */
1158 {
1159 const char *e = match_buf + strlen(match_buf);
1160 const char *s = e - match_pfx_len;
1161 while (s < e)
1162 if (is_special_char(*s++))
1163 match_pfx_len++;
1164 }
1165
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001166 /* Remove duplicates */
1167 if (matches) {
Mike Shalf3763032010-11-22 03:49:18 +01001168 unsigned i, n = 0;
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001169 qsort_string_vector(matches, num_matches);
1170 for (i = 0; i < num_matches - 1; ++i) {
1171 //if (matches[i] && matches[i+1]) { /* paranoia */
1172 if (strcmp(matches[i], matches[i+1]) == 0) {
1173 free(matches[i]);
1174 //matches[i] = NULL; /* paranoia */
1175 } else {
1176 matches[n++] = matches[i];
1177 }
1178 //}
1179 }
1180 matches[n++] = matches[i];
1181 num_matches = n;
1182 }
Mike Shalf3763032010-11-22 03:49:18 +01001183
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001184 /* Did we find exactly one match? */
1185 if (num_matches != 1) { /* no */
1186 char *cp;
1187 beep();
1188 if (!matches)
Denys Vlasenko76939e72010-09-03 14:09:24 +02001189 goto ret; /* no matches at all */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001190 /* Find common prefix */
1191 chosen_match = xstrdup(matches[0]);
1192 for (cp = chosen_match; *cp; cp++) {
1193 unsigned n;
1194 for (n = 1; n < num_matches; n++) {
1195 if (matches[n][cp - chosen_match] != *cp) {
1196 goto stop;
1197 }
1198 }
1199 }
1200 stop:
1201 if (cp == chosen_match) { /* have unique prefix? */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001202 goto ret; /* no */
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001203 }
1204 *cp = '\0';
Mike Shalf3763032010-11-22 03:49:18 +01001205 cp = quote_special_chars(chosen_match);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001206 free(chosen_match);
1207 chosen_match = cp;
1208 len_found = strlen(chosen_match);
1209 } else { /* exactly one match */
1210 /* Next <tab> is not a double-tab */
1211 *lastWasTab = 0;
1212
Mike Shalf3763032010-11-22 03:49:18 +01001213 chosen_match = quote_special_chars(matches[0]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001214 len_found = strlen(chosen_match);
1215 if (chosen_match[len_found-1] != '/') {
1216 chosen_match[len_found] = ' ';
1217 chosen_match[++len_found] = '\0';
1218 }
1219 }
1220
Mike Shalf3763032010-11-22 03:49:18 +01001221# if !ENABLE_UNICODE_SUPPORT
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001222 /* Have space to place the match? */
1223 /* The result consists of three parts with these lengths: */
1224 /* cursor + (len_found - match_pfx_len) + (command_len - cursor) */
1225 /* it simplifies into: */
1226 if ((int)(len_found - match_pfx_len + command_len) < S.maxsize) {
1227 int pos;
1228 /* save tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001229 strcpy(match_buf, &command_ps[cursor]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001230 /* add match and tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001231 sprintf(&command_ps[cursor], "%s%s", chosen_match + match_pfx_len, match_buf);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001232 command_len = strlen(command_ps);
1233 /* new pos */
1234 pos = cursor + len_found - match_pfx_len;
1235 /* write out the matched command */
1236 redraw(cmdedit_y, command_len - pos);
1237 }
Mike Shalf3763032010-11-22 03:49:18 +01001238# else
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001239 {
Denys Vlasenko76939e72010-09-03 14:09:24 +02001240 /* Use 2nd half of match_buf as scratch space - see (**) */
1241 char *command = match_buf + MAX_LINELEN;
1242 int len = save_string(command, MAX_LINELEN);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001243 /* Have space to place the match? */
1244 /* cursor_mb + (len_found - match_pfx_len) + (len - cursor_mb) */
1245 if ((int)(len_found - match_pfx_len + len) < MAX_LINELEN) {
1246 int pos;
1247 /* save tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001248 strcpy(match_buf, &command[cursor_mb]);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001249 /* where do we want to have cursor after all? */
1250 strcpy(&command[cursor_mb], chosen_match + match_pfx_len);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02001251 len = load_string(command);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001252 /* add match and tail */
Denys Vlasenko76939e72010-09-03 14:09:24 +02001253 sprintf(&command[cursor_mb], "%s%s", chosen_match + match_pfx_len, match_buf);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02001254 command_len = load_string(command);
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001255 /* write out the matched command */
1256 /* paranoia: load_string can return 0 on conv error,
1257 * prevent passing pos = (0 - 12) to redraw */
1258 pos = command_len - len;
1259 redraw(cmdedit_y, pos >= 0 ? pos : 0);
1260 }
1261 }
Mike Shalf3763032010-11-22 03:49:18 +01001262# endif
Denys Vlasenko76939e72010-09-03 14:09:24 +02001263 ret:
Denys Vlasenkoba0e1032010-09-03 14:08:24 +02001264 free(chosen_match);
Denys Vlasenko76939e72010-09-03 14:09:24 +02001265 free(match_buf);
Erik Andersenf0657d32000-04-12 17:49:52 +00001266}
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001267
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02001268#endif /* FEATURE_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001269
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001270
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001271line_input_t* FAST_FUNC new_line_input_t(int flags)
1272{
1273 line_input_t *n = xzalloc(sizeof(*n));
1274 n->flags = flags;
Denys Vlasenko79df4812014-01-10 14:38:26 +01001275#if MAX_HISTORY > 0
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001276 n->max_history = MAX_HISTORY;
Denys Vlasenko79df4812014-01-10 14:38:26 +01001277#endif
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001278 return n;
1279}
1280
1281
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001282#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001283
Flemming Madsend96ffda2013-04-07 18:47:24 +02001284unsigned FAST_FUNC size_from_HISTFILESIZE(const char *hp)
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001285{
1286 int size = MAX_HISTORY;
1287 if (hp) {
1288 size = atoi(hp);
1289 if (size <= 0)
1290 return 1;
1291 if (size > MAX_HISTORY)
1292 return MAX_HISTORY;
1293 }
1294 return size;
1295}
1296
Denis Vlasenko682ad302008-09-27 01:28:56 +00001297static void save_command_ps_at_cur_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001298{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001299 if (command_ps[0] != BB_NUL) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001300 int cur = state->cur_history;
1301 free(state->history[cur]);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001302
Denys Vlasenko19158a82010-03-26 14:06:56 +01001303# if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001304 {
1305 char tbuf[MAX_LINELEN];
1306 save_string(tbuf, sizeof(tbuf));
1307 state->history[cur] = xstrdup(tbuf);
1308 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001309# else
Denis Vlasenko682ad302008-09-27 01:28:56 +00001310 state->history[cur] = xstrdup(command_ps);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001311# endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001312 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001313}
1314
1315/* state->flags is already checked to be nonzero */
1316static int get_previous_history(void)
1317{
1318 if ((state->flags & DO_HISTORY) && state->cur_history) {
1319 save_command_ps_at_cur_history();
1320 state->cur_history--;
1321 return 1;
1322 }
1323 beep();
1324 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001325}
1326
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001327static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001328{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001329 if (state->flags & DO_HISTORY) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001330 if (state->cur_history < state->cnt_history) {
1331 save_command_ps_at_cur_history(); /* save the current history line */
1332 return ++state->cur_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001333 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001334 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001335 beep();
1336 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001337}
Robert Griebl350d26b2002-12-03 22:45:46 +00001338
Flemming Madsend96ffda2013-04-07 18:47:24 +02001339/* Lists command history. Used by shell 'history' builtins */
1340void FAST_FUNC show_history(const line_input_t *st)
1341{
1342 int i;
1343
1344 if (!st)
1345 return;
1346 for (i = 0; i < st->cnt_history; i++)
1347 printf("%4d %s\n", i, st->history[i]);
1348}
1349
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001350# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001351/* We try to ensure that concurrent additions to the history
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001352 * do not overwrite each other.
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001353 * Otherwise shell users get unhappy.
1354 *
1355 * History file is trimmed lazily, when it grows several times longer
1356 * than configured MAX_HISTORY lines.
1357 */
1358
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001359static void free_line_input_t(line_input_t *n)
1360{
1361 int i = n->cnt_history;
1362 while (i > 0)
1363 free(n->history[--i]);
1364 free(n);
1365}
1366
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001367/* state->flags is already checked to be nonzero */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001368static void load_history(line_input_t *st_parm)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001369{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001370 char *temp_h[MAX_HISTORY];
1371 char *line;
Robert Griebl350d26b2002-12-03 22:45:46 +00001372 FILE *fp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001373 unsigned idx, i, line_len;
Robert Griebl350d26b2002-12-03 22:45:46 +00001374
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001375 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +00001376
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001377 fp = fopen_for_read(st_parm->hist_file);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001378 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001379 /* clean up old history */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001380 for (idx = st_parm->cnt_history; idx > 0;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001381 idx--;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001382 free(st_parm->history[idx]);
1383 st_parm->history[idx] = NULL;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001384 }
1385
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001386 /* fill temp_h[], retaining only last MAX_HISTORY lines */
1387 memset(temp_h, 0, sizeof(temp_h));
Denys Vlasenkobede2152011-09-04 16:12:33 +02001388 idx = 0;
Dennis Groenendeee3562012-04-24 22:40:58 +02001389 st_parm->cnt_history_in_file = 0;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001390 while ((line = xmalloc_fgetline(fp)) != NULL) {
1391 if (line[0] == '\0') {
1392 free(line);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001393 continue;
1394 }
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001395 free(temp_h[idx]);
1396 temp_h[idx] = line;
Dennis Groenendeee3562012-04-24 22:40:58 +02001397 st_parm->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001398 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001399 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001400 idx = 0;
Robert Griebl350d26b2002-12-03 22:45:46 +00001401 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001402 fclose(fp);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001403
1404 /* find first non-NULL temp_h[], if any */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001405 if (st_parm->cnt_history_in_file) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001406 while (temp_h[idx] == NULL) {
1407 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001408 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001409 idx = 0;
1410 }
1411 }
1412
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001413 /* copy temp_h[] to st_parm->history[] */
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001414 for (i = 0; i < st_parm->max_history;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001415 line = temp_h[idx];
1416 if (!line)
1417 break;
1418 idx++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001419 if (idx == st_parm->max_history)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001420 idx = 0;
1421 line_len = strlen(line);
1422 if (line_len >= MAX_LINELEN)
1423 line[MAX_LINELEN-1] = '\0';
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001424 st_parm->history[i++] = line;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001425 }
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001426 st_parm->cnt_history = i;
Denys Vlasenkobede2152011-09-04 16:12:33 +02001427 if (ENABLE_FEATURE_EDITING_SAVE_ON_EXIT)
1428 st_parm->cnt_history_in_file = i;
Robert Griebl350d26b2002-12-03 22:45:46 +00001429 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001430}
1431
Denys Vlasenkobede2152011-09-04 16:12:33 +02001432# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1433void save_history(line_input_t *st)
1434{
1435 FILE *fp;
1436
Denys Vlasenkobede2152011-09-04 16:12:33 +02001437 if (!st->hist_file)
1438 return;
1439 if (st->cnt_history <= st->cnt_history_in_file)
1440 return;
1441
1442 fp = fopen(st->hist_file, "a");
1443 if (fp) {
1444 int i, fd;
1445 char *new_name;
1446 line_input_t *st_temp;
1447
1448 for (i = st->cnt_history_in_file; i < st->cnt_history; i++)
1449 fprintf(fp, "%s\n", st->history[i]);
1450 fclose(fp);
1451
1452 /* we may have concurrently written entries from others.
1453 * load them */
1454 st_temp = new_line_input_t(st->flags);
1455 st_temp->hist_file = st->hist_file;
1456 st_temp->max_history = st->max_history;
1457 load_history(st_temp);
1458
1459 /* write out temp file and replace hist_file atomically */
1460 new_name = xasprintf("%s.%u.new", st->hist_file, (int) getpid());
1461 fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1462 if (fd >= 0) {
1463 fp = xfdopen_for_write(fd);
1464 for (i = 0; i < st_temp->cnt_history; i++)
1465 fprintf(fp, "%s\n", st_temp->history[i]);
1466 fclose(fp);
1467 if (rename(new_name, st->hist_file) == 0)
1468 st->cnt_history_in_file = st_temp->cnt_history;
1469 }
1470 free(new_name);
1471 free_line_input_t(st_temp);
1472 }
1473}
1474# else
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001475static void save_history(char *str)
Robert Griebl350d26b2002-12-03 22:45:46 +00001476{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001477 int fd;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001478 int len, len2;
Eric Andersenc470f442003-07-28 09:56:35 +00001479
Denys Vlasenkobede2152011-09-04 16:12:33 +02001480 if (!state->hist_file)
1481 return;
1482
Wolfram Sang2e9aeae2010-11-15 02:58:28 +01001483 fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0600);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001484 if (fd < 0)
1485 return;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001486 xlseek(fd, 0, SEEK_END); /* paranoia */
1487 len = strlen(str);
1488 str[len] = '\n'; /* we (try to) do atomic write */
1489 len2 = full_write(fd, str, len + 1);
1490 str[len] = '\0';
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001491 close(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001492 if (len2 != len + 1)
1493 return; /* "wtf?" */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001494
1495 /* did we write so much that history file needs trimming? */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001496 state->cnt_history_in_file++;
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001497 if (state->cnt_history_in_file > state->max_history * 4) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001498 char *new_name;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001499 line_input_t *st_temp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001500
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001501 /* we may have concurrently written entries from others.
1502 * load them */
1503 st_temp = new_line_input_t(state->flags);
1504 st_temp->hist_file = state->hist_file;
Denys Vlasenkoe3d8d072011-03-31 14:39:38 +02001505 st_temp->max_history = state->max_history;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001506 load_history(st_temp);
1507
1508 /* write out temp file and replace hist_file atomically */
1509 new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
Denys Vlasenko4840ae82011-09-04 15:28:03 +02001510 fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Wolfram Sang2e9aeae2010-11-15 02:58:28 +01001511 if (fd >= 0) {
1512 FILE *fp;
1513 int i;
1514
1515 fp = xfdopen_for_write(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001516 for (i = 0; i < st_temp->cnt_history; i++)
1517 fprintf(fp, "%s\n", st_temp->history[i]);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001518 fclose(fp);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001519 if (rename(new_name, state->hist_file) == 0)
1520 state->cnt_history_in_file = st_temp->cnt_history;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001521 }
1522 free(new_name);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001523 free_line_input_t(st_temp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001524 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001525}
Denys Vlasenkobede2152011-09-04 16:12:33 +02001526# endif
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001527# else
1528# define load_history(a) ((void)0)
1529# define save_history(a) ((void)0)
1530# endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001531
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001532static void remember_in_history(char *str)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001533{
1534 int i;
1535
1536 if (!(state->flags & DO_HISTORY))
1537 return;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001538 if (str[0] == '\0')
1539 return;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001540 i = state->cnt_history;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001541 /* Don't save dupes */
1542 if (i && strcmp(state->history[i-1], str) == 0)
1543 return;
1544
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001545 free(state->history[state->max_history]); /* redundant, paranoia */
1546 state->history[state->max_history] = NULL; /* redundant, paranoia */
Denis Vlasenko682ad302008-09-27 01:28:56 +00001547
1548 /* If history[] is full, remove the oldest command */
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001549 /* we need to keep history[state->max_history] empty, hence >=, not > */
1550 if (i >= state->max_history) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001551 free(state->history[0]);
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001552 for (i = 0; i < state->max_history-1; i++)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001553 state->history[i] = state->history[i+1];
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001554 /* i == state->max_history-1 */
Denys Vlasenkoc0cae522011-11-04 01:09:09 +01001555# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1556 if (state->cnt_history_in_file)
Denys Vlasenkobede2152011-09-04 16:12:33 +02001557 state->cnt_history_in_file--;
Denys Vlasenkoc0cae522011-11-04 01:09:09 +01001558# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001559 }
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001560 /* i <= state->max_history-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001561 state->history[i++] = xstrdup(str);
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02001562 /* i <= state->max_history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001563 state->cur_history = i;
1564 state->cnt_history = i;
Denys Vlasenkobede2152011-09-04 16:12:33 +02001565# if ENABLE_FEATURE_EDITING_SAVEHISTORY && !ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1566 save_history(str);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001567# endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001568}
1569
1570#else /* MAX_HISTORY == 0 */
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001571# define remember_in_history(a) ((void)0)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001572#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001573
1574
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001575#if ENABLE_FEATURE_EDITING_VI
Erik Andersen6273f652000-03-17 01:12:41 +00001576/*
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001577 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001578 */
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001579static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001580vi_Word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001581{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001582 CHAR_T *command = command_ps;
1583
1584 while (cursor < command_len && !BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001585 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001586 if (eat) while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001587 input_forward();
1588}
1589
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001590static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001591vi_word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001592{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001593 CHAR_T *command = command_ps;
1594
Natanael Copa7e6f9312016-08-14 23:30:29 +02001595 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001596 while (cursor < command_len
Natanael Copa7e6f9312016-08-14 23:30:29 +02001597 && (BB_isalnum_or_underscore(command[cursor+1]))
Denys Vlasenko13028922009-07-12 00:51:15 +02001598 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001599 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001600 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001601 } else if (BB_ispunct(command[cursor])) {
1602 while (cursor < command_len && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001603 input_forward();
1604 }
1605
Denis Vlasenko253ce002007-01-22 08:34:44 +00001606 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001607 input_forward();
1608
Denys Vlasenko13028922009-07-12 00:51:15 +02001609 if (eat) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001610 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001611 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001612 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001613}
1614
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001615static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001616vi_End_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001617{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001618 CHAR_T *command = command_ps;
1619
Paul Fox3f11b1b2005-08-04 19:04:46 +00001620 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001621 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001622 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001623 while (cursor < command_len-1 && !BB_isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001624 input_forward();
1625}
1626
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001627static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001628vi_end_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001629{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001630 CHAR_T *command = command_ps;
1631
Denis Vlasenko253ce002007-01-22 08:34:44 +00001632 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001633 return;
1634 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001635 while (cursor < command_len-1 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001636 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001637 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001638 return;
Natanael Copa7e6f9312016-08-14 23:30:29 +02001639 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001640 while (cursor < command_len-1
Natanael Copa7e6f9312016-08-14 23:30:29 +02001641 && (BB_isalnum_or_underscore(command[cursor+1]))
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001642 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001643 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001644 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001645 } else if (BB_ispunct(command[cursor])) {
1646 while (cursor < command_len-1 && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001647 input_forward();
1648 }
1649}
1650
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001651static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001652vi_Back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001653{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001654 CHAR_T *command = command_ps;
1655
1656 while (cursor > 0 && BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001657 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001658 while (cursor > 0 && !BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001659 input_backward(1);
1660}
1661
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001662static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001663vi_back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001664{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001665 CHAR_T *command = command_ps;
1666
Paul Fox3f11b1b2005-08-04 19:04:46 +00001667 if (cursor <= 0)
1668 return;
1669 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001670 while (cursor > 0 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001671 input_backward(1);
1672 if (cursor <= 0)
1673 return;
Natanael Copa7e6f9312016-08-14 23:30:29 +02001674 if (BB_isalnum_or_underscore(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001675 while (cursor > 0
Natanael Copa7e6f9312016-08-14 23:30:29 +02001676 && (BB_isalnum_or_underscore(command[cursor-1]))
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001677 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001678 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001679 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001680 } else if (BB_ispunct(command[cursor])) {
1681 while (cursor > 0 && BB_ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001682 input_backward(1);
1683 }
1684}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001685#endif
1686
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001687/* Modelled after bash 4.0 behavior of Ctrl-<arrow> */
1688static void ctrl_left(void)
1689{
1690 CHAR_T *command = command_ps;
1691
1692 while (1) {
1693 CHAR_T c;
1694
1695 input_backward(1);
1696 if (cursor == 0)
1697 break;
1698 c = command[cursor];
1699 if (c != ' ' && !BB_ispunct(c)) {
1700 /* we reached a "word" delimited by spaces/punct.
1701 * go to its beginning */
1702 while (1) {
1703 c = command[cursor - 1];
1704 if (c == ' ' || BB_ispunct(c))
1705 break;
1706 input_backward(1);
1707 if (cursor == 0)
1708 break;
1709 }
1710 break;
1711 }
1712 }
1713}
1714static void ctrl_right(void)
1715{
1716 CHAR_T *command = command_ps;
1717
1718 while (1) {
1719 CHAR_T c;
1720
1721 c = command[cursor];
1722 if (c == BB_NUL)
1723 break;
1724 if (c != ' ' && !BB_ispunct(c)) {
1725 /* we reached a "word" delimited by spaces/punct.
1726 * go to its end + 1 */
1727 while (1) {
1728 input_forward();
1729 c = command[cursor];
1730 if (c == BB_NUL || c == ' ' || BB_ispunct(c))
1731 break;
1732 }
1733 break;
1734 }
1735 input_forward();
1736 }
1737}
1738
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001739
1740/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001741 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001742 */
1743
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001744#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
1745static void ask_terminal(void)
1746{
1747 /* Ask terminal where is the cursor now.
1748 * lineedit_read_key handles response and corrects
1749 * our idea of current cursor position.
1750 * Testcase: run "echo -n long_line_long_line_long_line",
1751 * then type in a long, wrapping command and try to
1752 * delete it using backspace key.
1753 * Note: we print it _after_ prompt, because
1754 * prompt may contain CR. Example: PS1='\[\r\n\]\w '
1755 */
1756 /* Problem: if there is buffered input on stdin,
1757 * the response will be delivered later,
1758 * possibly to an unsuspecting application.
1759 * Testcase: "sleep 1; busybox ash" + press and hold [Enter].
1760 * Result:
1761 * ~/srcdevel/bbox/fix/busybox.t4 #
1762 * ~/srcdevel/bbox/fix/busybox.t4 #
1763 * ^[[59;34~/srcdevel/bbox/fix/busybox.t4 # <-- garbage
1764 * ~/srcdevel/bbox/fix/busybox.t4 #
1765 *
1766 * Checking for input with poll only makes the race narrower,
1767 * I still can trigger it. Strace:
1768 *
1769 * write(1, "~/srcdevel/bbox/fix/busybox.t4 # ", 33) = 33
1770 * poll([{fd=0, events=POLLIN}], 1, 0) = 0 (Timeout) <-- no input exists
1771 * write(1, "\33[6n", 4) = 4 <-- send the ESC sequence, quick!
Alexey Fomenko232ebaa2011-05-20 04:26:29 +02001772 * poll([{fd=0, events=POLLIN}], 1, -1) = 1 ([{fd=0, revents=POLLIN}])
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001773 * read(0, "\n", 1) = 1 <-- oh crap, user's input got in first
1774 */
Denys Vlasenko451add42010-07-25 00:06:41 +02001775 struct pollfd pfd;
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001776
Denys Vlasenko451add42010-07-25 00:06:41 +02001777 pfd.fd = STDIN_FILENO;
1778 pfd.events = POLLIN;
1779 if (safe_poll(&pfd, 1, 0) == 0) {
1780 S.sent_ESC_br6n = 1;
Marek Polacek7b181072010-10-28 21:34:56 +02001781 fputs(ESC"[6n", stdout);
Denys Vlasenko451add42010-07-25 00:06:41 +02001782 fflush_all(); /* make terminal see it ASAP! */
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001783 }
1784}
1785#else
1786#define ask_terminal() ((void)0)
1787#endif
1788
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001789/* Called just once at read_line_input() init time */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001790#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001791static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001792{
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001793 const char *p;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001794 cmdedit_prompt = prmt_ptr;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001795 p = strrchr(prmt_ptr, '\n');
Denys Vlasenko1b9ac212013-08-20 16:13:05 +02001796 cmdedit_prmt_len = unicode_strwidth(p ? p+1 : prmt_ptr);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001797 put_prompt();
1798}
1799#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001800static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001801{
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001802 int prmt_size = 0;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001803 char *prmt_mem_ptr = xzalloc(1);
Denys Vlasenko1d145692013-03-29 13:09:05 +01001804# if ENABLE_USERNAME_OR_HOMEDIR
1805 char *cwd_buf = NULL;
1806# endif
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001807 char flg_not_length = '[';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001808 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001809
Denys Vlasenko7a180432013-08-19 16:44:05 +02001810 /*cmdedit_prmt_len = 0; - already is */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001811
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001812 cbuf[1] = '\0'; /* never changes */
1813
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001814 while (*prmt_ptr) {
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001815 char timebuf[sizeof("HH:MM:SS")];
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001816 char *free_me = NULL;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001817 char *pbuf;
1818 char c;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001819
1820 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001821 c = *prmt_ptr++;
1822 if (c == '\\') {
Denys Vlasenko1d145692013-03-29 13:09:05 +01001823 const char *cp;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001824 int l;
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001825/*
1826 * Supported via bb_process_escape_sequence:
1827 * \a ASCII bell character (07)
1828 * \e ASCII escape character (033)
1829 * \n newline
1830 * \r carriage return
1831 * \\ backslash
1832 * \nnn char with octal code nnn
1833 * Supported:
1834 * \$ if the effective UID is 0, a #, otherwise a $
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001835 * \w current working directory, with $HOME abbreviated with a tilde
1836 * Note: we do not support $PROMPT_DIRTRIM=n feature
Denys Vlasenko1d145692013-03-29 13:09:05 +01001837 * \W basename of the current working directory, with $HOME abbreviated with a tilde
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001838 * \h hostname up to the first '.'
1839 * \H hostname
1840 * \u username
1841 * \[ begin a sequence of non-printing characters
1842 * \] end a sequence of non-printing characters
Denys Vlasenko1d145692013-03-29 13:09:05 +01001843 * \T current time in 12-hour HH:MM:SS format
1844 * \@ current time in 12-hour am/pm format
1845 * \A current time in 24-hour HH:MM format
1846 * \t current time in 24-hour HH:MM:SS format
1847 * (all of the above work as \A)
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001848 * Not supported:
Denys Vlasenko1d145692013-03-29 13:09:05 +01001849 * \! history number of this command
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001850 * \# command number of this command
1851 * \j number of jobs currently managed by the shell
1852 * \l basename of the shell's terminal device name
1853 * \s name of the shell, the basename of $0 (the portion following the final slash)
1854 * \V release of bash, version + patch level (e.g., 2.00.0)
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001855 * \d date in "Weekday Month Date" format (e.g., "Tue May 26")
1856 * \D{format}
1857 * format is passed to strftime(3).
1858 * An empty format results in a locale-specific time representation.
1859 * The braces are required.
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001860 * Mishandled by bb_process_escape_sequence:
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001861 * \v version of bash (e.g., 2.00)
1862 */
Denys Vlasenko1d145692013-03-29 13:09:05 +01001863 cp = prmt_ptr;
1864 c = *cp;
1865 if (c != 't') /* don't treat \t as tab */
1866 c = bb_process_escape_sequence(&prmt_ptr);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001867 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001868 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001869 break;
1870 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001871
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001872 switch (c) {
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02001873# if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001874 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001875 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001876 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001877# endif
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001878 case 'H':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001879 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001880 pbuf = free_me = safe_gethostname();
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001881 if (c == 'h')
1882 strchrnul(pbuf, '.')[0] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001883 break;
1884 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001885 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001886 break;
Denys Vlasenko1d145692013-03-29 13:09:05 +01001887 case 'T': /* 12-hour HH:MM:SS format */
1888 case '@': /* 12-hour am/pm format */
1889 case 'A': /* 24-hour HH:MM format */
1890 case 't': /* 24-hour HH:MM:SS format */
1891 /* We show all of them as 24-hour HH:MM */
1892 strftime_HHMMSS(timebuf, sizeof(timebuf), NULL)[-3] = '\0';
1893 pbuf = timebuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001894 break;
Denys Vlasenko1d145692013-03-29 13:09:05 +01001895# if ENABLE_USERNAME_OR_HOMEDIR
1896 case 'w': /* current dir */
1897 case 'W': /* basename of cur dir */
1898 if (!cwd_buf) {
1899 cwd_buf = xrealloc_getcwd_or_warn(NULL);
1900 if (!cwd_buf)
1901 cwd_buf = (char *)bb_msg_unknown;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001902 else if (home_pwd_buf[0]) {
1903 char *after_home_user;
1904
Denys Vlasenko1d145692013-03-29 13:09:05 +01001905 /* /home/user[/something] -> ~[/something] */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001906 after_home_user = is_prefixed_with(cwd_buf, home_pwd_buf);
1907 if (after_home_user
1908 && (*after_home_user == '/' || *after_home_user == '\0')
Denys Vlasenko1d145692013-03-29 13:09:05 +01001909 ) {
1910 cwd_buf[0] = '~';
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001911 overlapping_strcpy(cwd_buf + 1, after_home_user);
Denys Vlasenko1d145692013-03-29 13:09:05 +01001912 }
1913 }
1914 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001915 pbuf = cwd_buf;
Denys Vlasenko1d145692013-03-29 13:09:05 +01001916 if (c == 'w')
1917 break;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001918 cp = strrchr(pbuf, '/');
Denys Vlasenko8172d052013-03-29 13:21:53 +01001919 if (cp)
Denys Vlasenko1d145692013-03-29 13:09:05 +01001920 pbuf = (char*)cp + 1;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001921 break;
Denys Vlasenko1d145692013-03-29 13:09:05 +01001922# endif
Denys Vlasenko6c852bf2013-03-28 13:20:12 +01001923// bb_process_escape_sequence does this now:
1924// case 'e': case 'E': /* \e \E = \033 */
1925// c = '\033';
1926// break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001927 case 'x': case 'X': {
1928 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001929 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001930 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001931 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001932 buf2[l] = '\0';
1933 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001934 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001935 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001936 break;
1937 }
1938 prmt_ptr++;
1939 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001940 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001941 if (c == 0)
1942 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001943 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001944 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001945 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001946 case '[': case ']':
1947 if (c == flg_not_length) {
Denys Vlasenko7a180432013-08-19 16:44:05 +02001948 /* Toggle '['/']' hex 5b/5d */
1949 flg_not_length ^= 6;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001950 continue;
1951 }
1952 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001953 } /* switch */
1954 } /* if */
1955 } /* if */
1956 cbuf[0] = c;
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001957 {
1958 int n = strlen(pbuf);
1959 prmt_size += n;
1960 if (c == '\n')
1961 cmdedit_prmt_len = 0;
1962 else if (flg_not_length != ']') {
1963#if 0 /*ENABLE_UNICODE_SUPPORT*/
1964/* Won't work, pbuf is one BYTE string here instead of an one Unicode char string. */
1965/* FIXME */
Denys Vlasenko1b9ac212013-08-20 16:13:05 +02001966 cmdedit_prmt_len += unicode_strwidth(pbuf);
Denys Vlasenko7a180432013-08-19 16:44:05 +02001967#else
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001968 cmdedit_prmt_len += n;
Denys Vlasenko7a180432013-08-19 16:44:05 +02001969#endif
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001970 }
Denys Vlasenko7a180432013-08-19 16:44:05 +02001971 }
Denys Vlasenkoe66a56d2013-08-19 16:45:04 +02001972 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_size+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001973 free(free_me);
1974 } /* while */
1975
Denys Vlasenko1d145692013-03-29 13:09:05 +01001976# if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001977 if (cwd_buf != (char *)bb_msg_unknown)
1978 free(cwd_buf);
Denys Vlasenko1d145692013-03-29 13:09:05 +01001979# endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001980 cmdedit_prompt = prmt_mem_ptr;
1981 put_prompt();
1982}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001983#endif
1984
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001985static void cmdedit_setwidth(unsigned w, int redraw_flg)
1986{
1987 cmdedit_termw = w;
1988 if (redraw_flg) {
1989 /* new y for current cursor */
1990 int new_y = (cursor + cmdedit_prmt_len) / w;
1991 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001992 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001993 fflush_all();
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001994 }
1995}
1996
1997static void win_changed(int nsig)
1998{
Denys Vlasenko55241fa2010-07-18 22:53:06 +02001999 int sv_errno = errno;
Denis Vlasenko55995022008-05-18 22:28:26 +00002000 unsigned width;
Alexey Fomenko232ebaa2011-05-20 04:26:29 +02002001
Denys Vlasenko451add42010-07-25 00:06:41 +02002002 get_terminal_width_height(0, &width, NULL);
Alexey Fomenko232ebaa2011-05-20 04:26:29 +02002003//FIXME: cmdedit_setwidth() -> redraw() -> printf() -> KABOOM! (we are in signal handler!)
2004 cmdedit_setwidth(width, /*redraw_flg:*/ nsig);
2005
Denys Vlasenko55241fa2010-07-18 22:53:06 +02002006 errno = sv_errno;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002007}
2008
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002009static int lineedit_read_key(char *read_key_buffer, int timeout)
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002010{
Denys Vlasenko020f4062009-05-17 16:44:54 +02002011 int64_t ic;
Denys Vlasenko19158a82010-03-26 14:06:56 +01002012#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002013 char unicode_buf[MB_CUR_MAX + 1];
2014 int unicode_idx = 0;
2015#endif
Denys Vlasenko020f4062009-05-17 16:44:54 +02002016
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002017 while (1) {
2018 /* Wait for input. TIMEOUT = -1 makes read_key wait even
2019 * on nonblocking stdin, TIMEOUT = 50 makes sure we won't
2020 * insist on full MB_CUR_MAX buffer to declare input like
2021 * "\xff\n",pause,"ls\n" invalid and thus won't lose "ls".
2022 *
2023 * Note: read_key sets errno to 0 on success.
2024 */
2025 ic = read_key(STDIN_FILENO, read_key_buffer, timeout);
2026 if (errno) {
Denys Vlasenko19158a82010-03-26 14:06:56 +01002027#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002028 if (errno == EAGAIN && unicode_idx != 0)
2029 goto pushback;
Denys Vlasenko1f6d2302009-10-29 03:45:26 +01002030#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002031 break;
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +02002032 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002033
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002034#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
2035 if ((int32_t)ic == KEYCODE_CURSOR_POS
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002036 && S.sent_ESC_br6n
Denys Vlasenko020f4062009-05-17 16:44:54 +02002037 ) {
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002038 S.sent_ESC_br6n = 0;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002039 if (cursor == 0) { /* otherwise it may be bogus */
2040 int col = ((ic >> 32) & 0x7fff) - 1;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002041 /*
2042 * Is col > cmdedit_prmt_len?
2043 * If yes (terminal says cursor is farther to the right
2044 * of where we think it should be),
2045 * the prompt wasn't printed starting at col 1,
2046 * there was additional text before it.
2047 */
2048 if ((int)(col - cmdedit_prmt_len) > 0) {
2049 /* Fix our understanding of current x position */
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002050 cmdedit_x += (col - cmdedit_prmt_len);
2051 while (cmdedit_x >= cmdedit_termw) {
2052 cmdedit_x -= cmdedit_termw;
2053 cmdedit_y++;
2054 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02002055 }
2056 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002057 continue;
Denys Vlasenko020f4062009-05-17 16:44:54 +02002058 }
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002059#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002060
Denys Vlasenko19158a82010-03-26 14:06:56 +01002061#if ENABLE_UNICODE_SUPPORT
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002062 if (unicode_status == UNICODE_ON) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002063 wchar_t wc;
2064
2065 if ((int32_t)ic < 0) /* KEYCODE_xxx */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002066 break;
2067 // TODO: imagine sequence like: 0xff,<left-arrow>: we are currently losing 0xff...
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002068
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002069 unicode_buf[unicode_idx++] = ic;
2070 unicode_buf[unicode_idx] = '\0';
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002071 if (mbstowcs(&wc, unicode_buf, 1) != 1) {
2072 /* Not (yet?) a valid unicode char */
2073 if (unicode_idx < MB_CUR_MAX) {
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002074 timeout = 50;
2075 continue;
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002076 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002077 pushback:
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002078 /* Invalid sequence. Save all "bad bytes" except first */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002079 read_key_ungets(read_key_buffer, unicode_buf + 1, unicode_idx - 1);
Tomas Heinricha659b812010-04-29 13:43:39 +02002080# if !ENABLE_UNICODE_PRESERVE_BROKEN
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002081 ic = CONFIG_SUBST_WCHAR;
Tomas Heinricha659b812010-04-29 13:43:39 +02002082# else
Tomas Heinrichb8909c52010-05-16 20:46:53 +02002083 ic = unicode_mark_raw_byte(unicode_buf[0]);
Tomas Heinricha659b812010-04-29 13:43:39 +02002084# endif
Tomas Heinrichd2b04052010-03-09 14:09:24 +01002085 } else {
2086 /* Valid unicode char, return its code */
2087 ic = wc;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002088 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002089 }
2090#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002091 break;
2092 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002093
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002094 return ic;
2095}
2096
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002097#if ENABLE_UNICODE_BIDI_SUPPORT
2098static int isrtl_str(void)
2099{
2100 int idx = cursor;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002101
2102 while (idx < command_len && unicode_bidi_is_neutral_wchar(command_ps[idx]))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002103 idx++;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002104 return unicode_bidi_isrtl(command_ps[idx]);
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002105}
2106#else
2107# define isrtl_str() 0
2108#endif
2109
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002110/* leave out the "vi-mode"-only case labels if vi editing isn't
2111 * configured. */
Denys Vlasenko13028922009-07-12 00:51:15 +02002112#define vi_case(caselabel) IF_FEATURE_EDITING_VI(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00002113
2114/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002115#undef CTRL
2116#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00002117
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002118enum {
2119 VI_CMDMODE_BIT = 0x40000000,
2120 /* 0x80000000 bit flags KEYCODE_xxx */
2121};
2122
2123#if ENABLE_FEATURE_REVERSE_SEARCH
2124/* Mimic readline Ctrl-R reverse history search.
2125 * When invoked, it shows the following prompt:
2126 * (reverse-i-search)'': user_input [cursor pos unchanged by Ctrl-R]
2127 * and typing results in search being performed:
2128 * (reverse-i-search)'tmp': cd /tmp [cursor under t in /tmp]
2129 * Search is performed by looking at progressively older lines in history.
2130 * Ctrl-R again searches for the next match in history.
2131 * Backspace deletes last matched char.
2132 * Control keys exit search and return to normal editing (at current history line).
2133 */
2134static int32_t reverse_i_search(void)
2135{
2136 char match_buf[128]; /* for user input */
2137 char read_key_buffer[KEYCODE_BUFFER_SIZE];
2138 const char *matched_history_line;
2139 const char *saved_prompt;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002140 unsigned saved_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002141 int32_t ic;
2142
2143 matched_history_line = NULL;
2144 read_key_buffer[0] = 0;
2145 match_buf[0] = '\0';
2146
2147 /* Save and replace the prompt */
2148 saved_prompt = cmdedit_prompt;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002149 saved_prmt_len = cmdedit_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002150 goto set_prompt;
2151
2152 while (1) {
2153 int h;
2154 unsigned match_buf_len = strlen(match_buf);
2155
2156 fflush_all();
2157//FIXME: correct timeout?
2158 ic = lineedit_read_key(read_key_buffer, -1);
2159
2160 switch (ic) {
2161 case CTRL('R'): /* searching for the next match */
2162 break;
2163
2164 case '\b':
2165 case '\x7f':
2166 /* Backspace */
2167 if (unicode_status == UNICODE_ON) {
2168 while (match_buf_len != 0) {
2169 uint8_t c = match_buf[--match_buf_len];
2170 if ((c & 0xc0) != 0x80) /* start of UTF-8 char? */
2171 break; /* yes */
2172 }
2173 } else {
2174 if (match_buf_len != 0)
2175 match_buf_len--;
2176 }
2177 match_buf[match_buf_len] = '\0';
2178 break;
2179
2180 default:
2181 if (ic < ' '
2182 || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2183 || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
2184 ) {
2185 goto ret;
2186 }
2187
2188 /* Append this char */
2189#if ENABLE_UNICODE_SUPPORT
2190 if (unicode_status == UNICODE_ON) {
2191 mbstate_t mbstate = { 0 };
2192 char buf[MB_CUR_MAX + 1];
2193 int len = wcrtomb(buf, ic, &mbstate);
2194 if (len > 0) {
2195 buf[len] = '\0';
2196 if (match_buf_len + len < sizeof(match_buf))
2197 strcpy(match_buf + match_buf_len, buf);
2198 }
2199 } else
2200#endif
2201 if (match_buf_len < sizeof(match_buf) - 1) {
2202 match_buf[match_buf_len] = ic;
2203 match_buf[match_buf_len + 1] = '\0';
2204 }
2205 break;
2206 } /* switch (ic) */
2207
2208 /* Search in history for match_buf */
2209 h = state->cur_history;
2210 if (ic == CTRL('R'))
2211 h--;
2212 while (h >= 0) {
2213 if (state->history[h]) {
2214 char *match = strstr(state->history[h], match_buf);
2215 if (match) {
2216 state->cur_history = h;
2217 matched_history_line = state->history[h];
2218 command_len = load_string(matched_history_line);
2219 cursor = match - matched_history_line;
2220//FIXME: cursor position for Unicode case
2221
2222 free((char*)cmdedit_prompt);
2223 set_prompt:
2224 cmdedit_prompt = xasprintf("(reverse-i-search)'%s': ", match_buf);
Denys Vlasenko1b9ac212013-08-20 16:13:05 +02002225 cmdedit_prmt_len = unicode_strwidth(cmdedit_prompt);
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002226 goto do_redraw;
2227 }
2228 }
2229 h--;
2230 }
2231
2232 /* Not found */
2233 match_buf[match_buf_len] = '\0';
2234 beep();
2235 continue;
2236
2237 do_redraw:
2238 redraw(cmdedit_y, command_len - cursor);
2239 } /* while (1) */
2240
2241 ret:
2242 if (matched_history_line)
2243 command_len = load_string(matched_history_line);
2244
2245 free((char*)cmdedit_prompt);
2246 cmdedit_prompt = saved_prompt;
Denys Vlasenko7a180432013-08-19 16:44:05 +02002247 cmdedit_prmt_len = saved_prmt_len;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002248 redraw(cmdedit_y, command_len - cursor);
2249
2250 return ic;
2251}
2252#endif
2253
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02002254/* maxsize must be >= 2.
2255 * Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00002256 * -1 on read errors or EOF, or on bare Ctrl-D,
2257 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00002258 * >0 length of input string, including terminating '\n'
2259 */
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002260int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize, int timeout)
Erik Andersen13456d12000-03-16 08:09:57 +00002261{
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002262 int len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002263#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002264 smallint lastWasTab = 0;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002265#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002266 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00002267#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002268 smallint vi_cmdmode = 0;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002269#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002270 struct termios initial_settings;
2271 struct termios new_settings;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002272 char read_key_buffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002273
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002274 INIT_S();
2275
2276 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
Denys Vlasenkod598a8d2014-12-10 17:22:13 +01002277 || (initial_settings.c_lflag & (ECHO|ICANON)) == ICANON
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002278 ) {
Denys Vlasenkod598a8d2014-12-10 17:22:13 +01002279 /* Happens when e.g. stty -echo was run before.
2280 * But if ICANON is not set, we don't come here.
2281 * (example: interactive python ^Z-backgrounded,
2282 * tty is still in "raw mode").
2283 */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002284 parse_and_put_prompt(prompt);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002285 /* fflush_all(); - done by parse_and_put_prompt */
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00002286 if (fgets(command, maxsize, stdin) == NULL)
2287 len = -1; /* EOF or error */
2288 else
2289 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002290 DEINIT_S();
2291 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00002292 }
2293
Denys Vlasenko28055022010-01-04 20:49:58 +01002294 init_unicode();
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02002295
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002296// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00002297 if (maxsize > MAX_LINELEN)
2298 maxsize = MAX_LINELEN;
Denys Vlasenko044b1802009-07-12 02:50:35 +02002299 S.maxsize = maxsize;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002300
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02002301 /* With zero flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00002302 state = st ? st : (line_input_t*) &const_int_0;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002303#if MAX_HISTORY > 0
2304# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denys Vlasenkoe45af7a2011-09-04 16:15:24 +02002305 if (state->hist_file)
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00002306 if (state->cnt_history == 0)
2307 load_history(state);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002308# endif
Denis Vlasenko3c385cd2008-11-02 00:41:05 +00002309 if (state->flags & DO_HISTORY)
2310 state->cur_history = state->cnt_history;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02002311#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002312
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002313 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00002314 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002315 command_len = 0;
Denys Vlasenko19158a82010-03-26 14:06:56 +01002316#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002317 command_ps = xzalloc(maxsize * sizeof(command_ps[0]));
2318#else
Mark Whitley4e338752001-01-26 20:42:23 +00002319 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00002320 command[0] = '\0';
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002321#endif
2322#define command command_must_not_be_used
Mark Whitley4e338752001-01-26 20:42:23 +00002323
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002324 new_settings = initial_settings;
Denys Vlasenko7ce209b2012-01-15 22:58:06 +01002325 /* ~ICANON: unbuffered input (most c_cc[] are disabled, VMIN/VTIME are enabled) */
2326 /* ~ECHO, ~ECHONL: turn off echoing, including newline echoing */
2327 /* ~ISIG: turn off INTR (ctrl-C), QUIT, SUSP */
2328 new_settings.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG);
2329 /* reads would block only if < 1 char is available */
Eric Andersen34506362001-08-02 05:02:46 +00002330 new_settings.c_cc[VMIN] = 1;
Denys Vlasenko7ce209b2012-01-15 22:58:06 +01002331 /* no timeout (reads block forever) */
Eric Andersen34506362001-08-02 05:02:46 +00002332 new_settings.c_cc[VTIME] = 0;
Denys Vlasenko7ce209b2012-01-15 22:58:06 +01002333 /* Should be not needed if ISIG is off: */
2334 /* Turn off CTRL-C */
2335 /* new_settings.c_cc[VINTR] = _POSIX_VDISABLE; */
Denis Vlasenko202ac502008-11-05 13:20:58 +00002336 tcsetattr_stdin_TCSANOW(&new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00002337
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002338#if ENABLE_USERNAME_OR_HOMEDIR
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002339 {
2340 struct passwd *entry;
2341
2342 entry = getpwuid(geteuid());
2343 if (entry) {
2344 user_buf = xstrdup(entry->pw_name);
2345 home_pwd_buf = xstrdup(entry->pw_dir);
2346 }
2347 }
2348#endif
Denis Vlasenko682ad302008-09-27 01:28:56 +00002349
2350#if 0
Denys Vlasenko2c4de5b2011-03-31 13:16:52 +02002351 for (i = 0; i <= state->max_history; i++)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002352 bb_error_msg("history[%d]:'%s'", i, state->history[i]);
Denis Vlasenko682ad302008-09-27 01:28:56 +00002353 bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
2354#endif
2355
Denys Vlasenko13ad9062009-11-11 03:19:30 +01002356 /* Print out the command prompt, optionally ask where cursor is */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002357 parse_and_put_prompt(prompt);
Denys Vlasenko13ad9062009-11-11 03:19:30 +01002358 ask_terminal();
Eric Andersenb3dc3b82001-01-04 11:08:45 +00002359
Alexey Fomenko232ebaa2011-05-20 04:26:29 +02002360 /* Install window resize handler (NB: after *all* init is complete) */
2361//FIXME: save entire sigaction!
2362 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
2363 win_changed(0); /* get initial window size */
2364
Denys Vlasenkoc396fe62009-05-17 19:28:14 +02002365 read_key_buffer[0] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002366 while (1) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002367 /*
2368 * The emacs and vi modes share much of the code in the big
2369 * command loop. Commands entered when in vi's command mode
2370 * (aka "escape mode") get an extra bit added to distinguish
2371 * them - this keeps them from being self-inserted. This
2372 * clutters the big switch a bit, but keeps all the code
2373 * in one place.
2374 */
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002375 int32_t ic, ic_raw;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002376
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002377 fflush_all();
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002378 ic = ic_raw = lineedit_read_key(read_key_buffer, timeout);
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002379
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002380#if ENABLE_FEATURE_REVERSE_SEARCH
2381 again:
2382#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00002383#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00002384 newdelflag = 1;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002385 if (vi_cmdmode) {
2386 /* btw, since KEYCODE_xxx are all < 0, this doesn't
2387 * change ic if it contains one of them: */
2388 ic |= VI_CMDMODE_BIT;
2389 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00002390#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002391
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002392 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00002393 case '\n':
2394 case '\r':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002395 vi_case('\n'|VI_CMDMODE_BIT:)
2396 vi_case('\r'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002397 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002398 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00002399 break_out = 1;
2400 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002401 case CTRL('A'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002402 vi_case('0'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002403 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002404 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00002405 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002406 case CTRL('B'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002407 vi_case('h'|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002408 vi_case('\b'|VI_CMDMODE_BIT:) /* ^H */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002409 vi_case('\x7f'|VI_CMDMODE_BIT:) /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002410 input_backward(1); /* Move back one character */
Erik Andersenf0657d32000-04-12 17:49:52 +00002411 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002412 case CTRL('E'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002413 vi_case('$'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002414 /* Control-e -- End of line */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002415 put_till_end_and_adv_cursor();
Erik Andersenc7c634b2000-03-19 05:28:55 +00002416 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002417 case CTRL('F'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002418 vi_case('l'|VI_CMDMODE_BIT:)
2419 vi_case(' '|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002420 input_forward(); /* Move forward one character */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002421 break;
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002422 case '\b': /* ^H */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002423 case '\x7f': /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002424 if (!isrtl_str())
2425 input_backspace();
2426 else
2427 input_delete(0);
2428 break;
2429 case KEYCODE_DELETE:
2430 if (!isrtl_str())
2431 input_delete(0);
2432 else
2433 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00002434 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002435#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00002436 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002437 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002438 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002439#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002440 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00002441 /* Control-k -- clear to end of line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002442 command_ps[cursor] = BB_NUL;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002443 command_len = cursor;
Denys Vlasenko94043e82010-05-11 14:49:13 +02002444 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen65a07302002-04-13 13:26:49 +00002445 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002446 case CTRL('L'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002447 vi_case(CTRL('L')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002448 /* Control-l -- clear screen */
Marek Polacek7b181072010-10-28 21:34:56 +02002449 printf(ESC"[H"); /* cursor to top,left */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002450 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00002451 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002452#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002453 case CTRL('N'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002454 vi_case(CTRL('N')|VI_CMDMODE_BIT:)
2455 vi_case('j'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002456 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002457 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002458 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00002459 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002460 case CTRL('P'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002461 vi_case(CTRL('P')|VI_CMDMODE_BIT:)
2462 vi_case('k'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002463 /* Control-p -- Get previous command from history */
Denis Vlasenko682ad302008-09-27 01:28:56 +00002464 if (get_previous_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002465 goto rewrite_line;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002466 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002467#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002468 case CTRL('U'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002469 vi_case(CTRL('U')|VI_CMDMODE_BIT:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002470 /* Control-U -- Clear line before cursor */
2471 if (cursor) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002472 command_len -= cursor;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002473 memmove(command_ps, command_ps + cursor,
2474 (command_len + 1) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002475 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002476 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002477 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002478 case CTRL('W'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002479 vi_case(CTRL('W')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002480 /* Control-W -- Remove the last word */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002481 while (cursor > 0 && BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002482 input_backspace();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002483 while (cursor > 0 && !BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002484 input_backspace();
2485 break;
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002486#if ENABLE_FEATURE_REVERSE_SEARCH
2487 case CTRL('R'):
2488 ic = ic_raw = reverse_i_search();
2489 goto again;
2490#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002491
Denis Vlasenko38f63192007-01-22 09:03:07 +00002492#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002493 case 'i'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002494 vi_cmdmode = 0;
2495 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002496 case 'I'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002497 input_backward(cursor);
2498 vi_cmdmode = 0;
2499 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002500 case 'a'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002501 input_forward();
2502 vi_cmdmode = 0;
2503 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002504 case 'A'|VI_CMDMODE_BIT:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002505 put_till_end_and_adv_cursor();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002506 vi_cmdmode = 0;
2507 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002508 case 'x'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002509 input_delete(1);
2510 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002511 case 'X'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002512 if (cursor > 0) {
2513 input_backward(1);
2514 input_delete(1);
2515 }
2516 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002517 case 'W'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002518 vi_Word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002519 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002520 case 'w'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002521 vi_word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002522 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002523 case 'E'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002524 vi_End_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002525 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002526 case 'e'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002527 vi_end_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002528 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002529 case 'B'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002530 vi_Back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002531 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002532 case 'b'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002533 vi_back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002534 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002535 case 'C'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002536 vi_cmdmode = 0;
2537 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002538 case 'D'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002539 goto clear_to_eol;
2540
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002541 case 'c'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002542 vi_cmdmode = 0;
2543 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002544 case 'd'|VI_CMDMODE_BIT: {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002545 int nc, sc;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002546
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002547 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002548 if (errno) /* error */
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002549 goto return_error_indicator;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002550 if (ic == ic_raw) { /* "cc", "dd" */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002551 input_backward(cursor);
2552 goto clear_to_eol;
2553 break;
2554 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002555
2556 sc = cursor;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002557 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002558 case 'w':
2559 case 'W':
2560 case 'e':
2561 case 'E':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002562 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002563 case 'w': /* "dw", "cw" */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002564 vi_word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002565 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002566 case 'W': /* 'dW', 'cW' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002567 vi_Word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002568 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002569 case 'e': /* 'de', 'ce' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002570 vi_end_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002571 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002572 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002573 case 'E': /* 'dE', 'cE' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002574 vi_End_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002575 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002576 break;
2577 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002578 nc = cursor;
2579 input_backward(cursor - sc);
2580 while (nc-- > cursor)
2581 input_delete(1);
2582 break;
2583 case 'b': /* "db", "cb" */
2584 case 'B': /* implemented as B */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002585 if (ic == 'b')
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002586 vi_back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002587 else
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002588 vi_Back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002589 while (sc-- > cursor)
2590 input_delete(1);
2591 break;
2592 case ' ': /* "d ", "c " */
2593 input_delete(1);
2594 break;
2595 case '$': /* "d$", "c$" */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002596 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002597 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002598 input_delete(1);
2599 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002600 }
2601 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002602 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002603 case 'p'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002604 input_forward();
2605 /* fallthrough */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002606 case 'P'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002607 put();
2608 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002609 case 'r'|VI_CMDMODE_BIT:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002610//FIXME: unicode case?
Denys Vlasenko66c5b122011-02-08 05:07:02 +01002611 ic = lineedit_read_key(read_key_buffer, timeout);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002612 if (errno) /* error */
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002613 goto return_error_indicator;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002614 if (ic < ' ' || ic > 255) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00002615 beep();
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002616 } else {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002617 command_ps[cursor] = ic;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002618 bb_putchar(ic);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002619 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00002620 }
2621 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002622 case '\x1b': /* ESC */
2623 if (state->flags & VI_MODE) {
2624 /* insert mode --> command mode */
2625 vi_cmdmode = 1;
2626 input_backward(1);
2627 }
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002628 /* Handle a few ESC-<key> combinations the same way
2629 * standard readline bindings (IOW: bash) do.
2630 * Often, Alt-<key> generates ESC-<key>.
2631 */
Ron Yorston20cd31a2014-12-12 08:29:41 +00002632 ic = lineedit_read_key(read_key_buffer, 50);
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002633 switch (ic) {
2634 //case KEYCODE_LEFT: - bash doesn't do this
2635 case 'b':
Denys Vlasenko56443cd2012-04-20 15:07:22 +02002636 ctrl_left();
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002637 break;
2638 //case KEYCODE_RIGHT: - bash doesn't do this
2639 case 'f':
2640 ctrl_right();
2641 break;
2642 //case KEYCODE_DELETE: - bash doesn't do this
2643 case 'd': /* Alt-D */
2644 {
2645 /* Delete word forward */
2646 int nc, sc = cursor;
2647 ctrl_right();
Cliff Frey49195652012-08-07 17:59:40 +02002648 nc = cursor - sc;
2649 input_backward(nc);
2650 while (--nc >= 0)
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002651 input_delete(1);
2652 break;
2653 }
2654 case '\b': /* Alt-Backspace(?) */
2655 case '\x7f': /* Alt-Backspace(?) */
2656 //case 'w': - bash doesn't do this
2657 {
2658 /* Delete word backward */
2659 int sc = cursor;
2660 ctrl_left();
2661 while (sc-- > cursor)
2662 input_delete(1);
2663 break;
2664 }
2665 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002666 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002667#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002668
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002669#if MAX_HISTORY > 0
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002670 case KEYCODE_UP:
2671 if (get_previous_history())
2672 goto rewrite_line;
2673 beep();
2674 break;
2675 case KEYCODE_DOWN:
2676 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002677 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002678 rewrite_line:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002679 /* Rewrite the line with the selected history item */
2680 /* change command */
Denys Vlasenko90a99042009-09-06 02:36:23 +02002681 command_len = load_string(state->history[state->cur_history] ?
Denys Vlasenkoa669eca2011-07-11 07:36:59 +02002682 state->history[state->cur_history] : "");
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002683 /* redraw and go to eol (bol, in vi) */
2684 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
2685 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002686#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002687 case KEYCODE_RIGHT:
2688 input_forward();
2689 break;
2690 case KEYCODE_LEFT:
2691 input_backward(1);
2692 break;
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002693 case KEYCODE_CTRL_LEFT:
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002694 case KEYCODE_ALT_LEFT: /* bash doesn't do it */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002695 ctrl_left();
2696 break;
2697 case KEYCODE_CTRL_RIGHT:
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +01002698 case KEYCODE_ALT_RIGHT: /* bash doesn't do it */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002699 ctrl_right();
2700 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002701 case KEYCODE_HOME:
2702 input_backward(cursor);
2703 break;
2704 case KEYCODE_END:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002705 put_till_end_and_adv_cursor();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002706 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002707
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002708 default:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002709 if (initial_settings.c_cc[VINTR] != 0
2710 && ic_raw == initial_settings.c_cc[VINTR]
2711 ) {
2712 /* Ctrl-C (usually) - stop gathering input */
2713 goto_new_line();
2714 command_len = 0;
2715 break_out = -1; /* "do not append '\n'" */
2716 break;
2717 }
2718 if (initial_settings.c_cc[VEOF] != 0
2719 && ic_raw == initial_settings.c_cc[VEOF]
2720 ) {
2721 /* Ctrl-D (usually) - delete one character,
2722 * or exit if len=0 and no chars to delete */
2723 if (command_len == 0) {
2724 errno = 0;
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002725
2726 case -1: /* error (e.g. EIO when tty is destroyed) */
2727 IF_FEATURE_EDITING_VI(return_error_indicator:)
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002728 break_out = command_len = -1;
2729 break;
2730 }
2731 input_delete(0);
2732 break;
2733 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002734// /* Control-V -- force insert of next char */
2735// if (c == CTRL('V')) {
2736// if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denys Vlasenkod55f5992010-09-07 18:40:53 +02002737// goto return_error_indicator;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002738// if (c == 0) {
2739// beep();
2740// break;
2741// }
2742// }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002743 if (ic < ' '
Denys Vlasenko19158a82010-03-26 14:06:56 +01002744 || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2745 || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002746 ) {
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002747 /* If VI_CMDMODE_BIT is set, ic is >= 256
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002748 * and vi mode ignores unexpected chars.
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002749 * Otherwise, we are here if ic is a
2750 * control char or an unhandled ESC sequence,
2751 * which is also ignored.
2752 */
2753 break;
2754 }
2755 if ((int)command_len >= (maxsize - 2)) {
2756 /* Not enough space for the char and EOL */
2757 break;
Paul Fox84bbac52008-01-11 16:50:08 +00002758 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002759
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002760 command_len++;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002761 if (cursor == (command_len - 1)) {
2762 /* We are at the end, append */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002763 command_ps[cursor] = ic;
2764 command_ps[cursor + 1] = BB_NUL;
Denys Vlasenko94043e82010-05-11 14:49:13 +02002765 put_cur_glyph_and_inc_cursor();
Tomas Heinrichaa167552010-03-26 13:13:24 +01002766 if (unicode_bidi_isrtl(ic))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002767 input_backward(1);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002768 } else {
2769 /* In the middle, insert */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002770 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002771
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002772 memmove(command_ps + sc + 1, command_ps + sc,
2773 (command_len - sc) * sizeof(command_ps[0]));
2774 command_ps[sc] = ic;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002775 /* is right-to-left char, or neutral one (e.g. comma) was just added to rtl text? */
2776 if (!isrtl_str())
2777 sc++; /* no */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002778 put_till_end_and_adv_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +00002779 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002780 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002781 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00002782 break;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002783 } /* switch (ic) */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002784
2785 if (break_out)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002786 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002787
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002788#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002789 if (ic_raw != '\t')
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002790 lastWasTab = 0;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002791#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002792 } /* while (1) */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002793
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002794#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002795 if (S.sent_ESC_br6n) {
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002796 /* "sleep 1; busybox ash" + hold [Enter] to trigger.
2797 * We sent "ESC [ 6 n", but got '\n' first, and
2798 * KEYCODE_CURSOR_POS response is now buffered from terminal.
2799 * It's bad already and not much can be done with it
2800 * (it _will_ be visible for the next process to read stdin),
2801 * but without this delay it even shows up on the screen
2802 * as garbage because we restore echo settings with tcsetattr
2803 * before it comes in. UGLY!
2804 */
2805 usleep(20*1000);
2806 }
2807#endif
2808
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002809/* End of bug-catching "command_must_not_be_used" trick */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002810#undef command
2811
Denys Vlasenko19158a82010-03-26 14:06:56 +01002812#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +02002813 command[0] = '\0';
2814 if (command_len > 0)
2815 command_len = save_string(command, maxsize - 1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002816 free(command_ps);
2817#endif
2818
Flemming Madsend96ffda2013-04-07 18:47:24 +02002819 if (command_len > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002820 remember_in_history(command);
Flemming Madsend96ffda2013-04-07 18:47:24 +02002821 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002822
Eric Andersen27bb7902003-12-23 20:24:51 +00002823 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002824 command[command_len++] = '\n';
2825 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00002826 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002827
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002828#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002829 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002830#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002831
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002832 /* restore initial_settings */
Denis Vlasenko202ac502008-11-05 13:20:58 +00002833 tcsetattr_stdin_TCSANOW(&initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002834 /* restore SIGWINCH handler */
2835 signal(SIGWINCH, previous_SIGWINCH_handler);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002836 fflush_all();
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002837
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002838 len = command_len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002839 DEINIT_S();
2840
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002841 return len; /* can't return command_len, DEINIT_S() destroys it */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002842}
2843
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002844#else /* !FEATURE_EDITING */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002845
2846#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00002847int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002848{
2849 fputs(prompt, stdout);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002850 fflush_all();
Denys Vlasenkob2320372012-09-27 16:03:49 +02002851 if (!fgets(command, maxsize, stdin))
2852 return -1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002853 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00002854}
2855
Denys Vlasenkob9e35dc2010-07-18 22:21:24 +02002856#endif /* !FEATURE_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00002857
2858
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002859/*
2860 * Testing
2861 */
2862
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002863#ifdef TEST
2864
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002865#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002866
2867const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002868
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002869int main(int argc, char **argv)
2870{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00002871 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002872 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00002873#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002874 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
2875 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
2876 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002877#else
2878 "% ";
2879#endif
2880
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002881 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00002882 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002883 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002884 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00002885 break;
Denys Vlasenko57ea9b42010-09-03 12:51:36 +02002886 buff[l-1] = '\0';
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002887 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00002888 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002889 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002890 return 0;
2891}
2892
Eric Andersenc470f442003-07-28 09:56:35 +00002893#endif /* TEST */