blob: 7bb3f2e356489bfd083e45efd20f3a7f7199c657 [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/*
Eric Andersenaff114c2004-04-14 17:51:38 +00003 * Termios command line History and 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\] '
Erik Andersen13456d12000-03-16 08:09:57 +000041 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000042#include "libbb.h"
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020043#include "unicode.h"
Glenn L McGrath475820c2004-01-22 12:42:23 +000044
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000045/* FIXME: obsolete CONFIG item? */
46#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
47
Glenn L McGrath3b251852004-01-03 12:07:32 +000048#ifdef TEST
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020049# define ENABLE_FEATURE_EDITING 0
50# define ENABLE_FEATURE_TAB_COMPLETION 0
51# define ENABLE_FEATURE_USERNAME_COMPLETION 0
52# define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
53#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000054
Eric Andersen5165fbe2001-02-20 06:42:29 +000055
Denis Vlasenko82b39e82007-01-21 19:18:19 +000056/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko38f63192007-01-22 09:03:07 +000057#if ENABLE_FEATURE_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000058
Denis Vlasenko82b39e82007-01-21 19:18:19 +000059
Denis Vlasenko7e46cf72006-12-23 01:21:55 +000060#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000061 (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000062#define IF_FEATURE_GETUSERNAME_AND_HOMEDIR(...)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000063#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000064#undef IF_FEATURE_GETUSERNAME_AND_HOMEDIR
65#define IF_FEATURE_GETUSERNAME_AND_HOMEDIR(...) __VA_ARGS__
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000066#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000067
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020068
69#undef CHAR_T
70#if ENABLE_FEATURE_ASSUME_UNICODE
71# define BB_NUL L'\0'
72# define CHAR_T wchar_t
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010073static bool BB_isspace(CHAR_T c) { return ((unsigned)c < 256 && isspace(c)); }
74static bool BB_isalnum(CHAR_T c) { return ((unsigned)c < 256 && isalnum(c)); }
75static bool BB_ispunct(CHAR_T c) { return ((unsigned)c < 256 && ispunct(c)); }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020076# undef isspace
77# undef isalnum
78# undef ispunct
79# undef isprint
80# define isspace isspace_must_not_be_used
81# define isalnum isalnum_must_not_be_used
82# define ispunct ispunct_must_not_be_used
83# define isprint isprint_must_not_be_used
84#else
85# define BB_NUL '\0'
86# define CHAR_T char
87# define BB_isspace(c) isspace(c)
88# define BB_isalnum(c) isalnum(c)
89# define BB_ispunct(c) ispunct(c)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020090#endif
91
92
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000093enum {
94 /* We use int16_t for positions, need to limit line len */
95 MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN < 0x7ff0
Denis Vlasenko6b404432008-01-07 16:13:14 +000096 ? CONFIG_FEATURE_EDITING_MAX_LEN
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000097 : 0x7ff0
98};
Robert Griebl350d26b2002-12-03 22:45:46 +000099
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000100#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
101static const char null_str[] ALIGN1 = "";
102#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +0000103
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000104/* We try to minimize both static and stack usage. */
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000105struct lineedit_statics {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000106 line_input_t *state;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000107
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000108 volatile unsigned cmdedit_termw; /* = 80; */ /* actual terminal width */
109 sighandler_t previous_SIGWINCH_handler;
Mark Whitley4e338752001-01-26 20:42:23 +0000110
Denys Vlasenko020f4062009-05-17 16:44:54 +0200111 unsigned cmdedit_x; /* real x (col) terminal position */
112 unsigned cmdedit_y; /* pseudoreal y (row) terminal position */
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000113 unsigned cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000114
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000115 unsigned cursor;
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +0200116 int command_len; /* must be signed */
117 /* signed maxsize: we want x in "if (x > S.maxsize)"
Denys Vlasenko044b1802009-07-12 02:50:35 +0200118 * to _not_ be promoted to unsigned */
119 int maxsize;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200120 CHAR_T *command_ps;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000121
122 const char *cmdedit_prompt;
Denis Vlasenko38f63192007-01-22 09:03:07 +0000123#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000124 int num_ok_lines; /* = 1; */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000125#endif
126
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000127#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000128 char *user_buf;
129 char *home_pwd_buf; /* = (char*)null_str; */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000130#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000131
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000132#if ENABLE_FEATURE_TAB_COMPLETION
133 char **matches;
134 unsigned num_matches;
135#endif
136
137#if ENABLE_FEATURE_EDITING_VI
138#define DELBUFSIZ 128
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200139 CHAR_T *delptr;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000140 smallint newdelflag; /* whether delbuf should be reused yet */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200141 CHAR_T delbuf[DELBUFSIZ]; /* a place to store deleted characters */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000142#endif
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100143#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +0100144 smallint sent_ESC_br6n;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100145#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000146
147 /* Formerly these were big buffers on stack: */
148#if ENABLE_FEATURE_TAB_COMPLETION
149 char exe_n_cwd_tab_completion__dirbuf[MAX_LINELEN];
150 char input_tab__matchBuf[MAX_LINELEN];
151 int16_t find_match__int_buf[MAX_LINELEN + 1]; /* need to have 9 bits at least */
152 int16_t find_match__pos_buf[MAX_LINELEN + 1];
153#endif
154};
155
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000156/* See lineedit_ptr_hack.c */
157extern struct lineedit_statics *const lineedit_ptr_to_statics;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000158
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000159#define S (*lineedit_ptr_to_statics)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000160#define state (S.state )
161#define cmdedit_termw (S.cmdedit_termw )
162#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
163#define cmdedit_x (S.cmdedit_x )
164#define cmdedit_y (S.cmdedit_y )
165#define cmdedit_prmt_len (S.cmdedit_prmt_len)
166#define cursor (S.cursor )
167#define command_len (S.command_len )
168#define command_ps (S.command_ps )
169#define cmdedit_prompt (S.cmdedit_prompt )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000170#define num_ok_lines (S.num_ok_lines )
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000171#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000172#define home_pwd_buf (S.home_pwd_buf )
173#define matches (S.matches )
174#define num_matches (S.num_matches )
175#define delptr (S.delptr )
176#define newdelflag (S.newdelflag )
177#define delbuf (S.delbuf )
178
179#define INIT_S() do { \
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000180 (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000181 barrier(); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000182 cmdedit_termw = 80; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000183 IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
184 IF_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000185} while (0)
186static void deinit_S(void)
187{
188#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000189 /* This one is allocated only if FANCY_PROMPT is on
190 * (otherwise it points to verbatim prompt (NOT malloced) */
191 free((char*)cmdedit_prompt);
192#endif
193#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
194 free(user_buf);
195 if (home_pwd_buf != null_str)
196 free(home_pwd_buf);
197#endif
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000198 free(lineedit_ptr_to_statics);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000199}
200#define DEINIT_S() deinit_S()
201
Denis Vlasenko2c844952008-04-25 18:44:35 +0000202
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200203#if ENABLE_FEATURE_ASSUME_UNICODE
204static size_t load_string(const char *src, int maxsize)
205{
206 ssize_t len = mbstowcs(command_ps, src, maxsize - 1);
207 if (len < 0)
208 len = 0;
209 command_ps[len] = L'\0';
210 return len;
211}
212static size_t save_string(char *dst, int maxsize)
213{
214 ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
215 if (len < 0)
216 len = 0;
217 dst[len] = '\0';
218 return len;
219}
220/* I thought just fputwc(c, stdout) would work. But no... */
221static void BB_PUTCHAR(wchar_t c)
222{
223 char buf[MB_CUR_MAX + 1];
224 mbstate_t mbst = { 0 };
225 ssize_t len = wcrtomb(buf, c, &mbst);
226
227 if (len > 0) {
228 buf[len] = '\0';
229 fputs(buf, stdout);
230 }
231}
232#else
233static size_t load_string(const char *src, int maxsize)
234{
235 safe_strncpy(command_ps, src, maxsize);
236 return strlen(command_ps);
237}
Denys Vlasenko9038d6f2009-07-15 20:02:19 +0200238# if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200239static void save_string(char *dst, int maxsize)
240{
241 safe_strncpy(dst, command_ps, maxsize);
242}
Denys Vlasenko7dd0ce42009-07-15 18:27:47 +0200243# endif
244# define BB_PUTCHAR(c) bb_putchar(c)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200245#endif
246
247
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000248/* Put 'command_ps[cursor]', cursor++.
249 * Advance cursor on screen. If we reached right margin, scroll text up
250 * and remove terminal margin effect by printing 'next_char' */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000251#define HACK_FOR_WRONG_WIDTH 1
252#if HACK_FOR_WRONG_WIDTH
253static void cmdedit_set_out_char(void)
254#define cmdedit_set_out_char(next_char) cmdedit_set_out_char()
255#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000256static void cmdedit_set_out_char(int next_char)
Denis Vlasenko2c844952008-04-25 18:44:35 +0000257#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000258{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200259 CHAR_T c = command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000260
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200261 if (c == BB_NUL) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000262 /* erase character after end of input string */
263 c = ' ';
264 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000265#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000266 /* Display non-printable characters in reverse */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200267 if (!BB_isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000268 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000269 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000270 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000271 c += '@';
272 if (c == 127)
273 c = '?';
274 printf("\033[7m%c\033[0m", c);
275 } else
276#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000277 {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200278 BB_PUTCHAR(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000279 }
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000280 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000281 /* terminal is scrolled down */
282 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000283 cmdedit_x = 0;
Denis Vlasenko2c844952008-04-25 18:44:35 +0000284#if HACK_FOR_WRONG_WIDTH
285 /* This works better if our idea of term width is wrong
286 * and it is actually wider (often happens on serial lines).
287 * Printing CR,LF *forces* cursor to next line.
288 * OTOH if terminal width is correct AND terminal does NOT
289 * have automargin (IOW: it is moving cursor to next line
290 * by itself (which is wrong for VT-10x terminals)),
291 * this will break things: there will be one extra empty line */
292 puts("\r"); /* + implicit '\n' */
293#else
294 /* Works ok only if cmdedit_termw is correct */
Mark Whitley4e338752001-01-26 20:42:23 +0000295 /* destroy "(auto)margin" */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000296 bb_putchar(next_char);
297 bb_putchar('\b');
Denis Vlasenko2c844952008-04-25 18:44:35 +0000298#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000299 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200300// Huh? What if command_ps[cursor] == BB_NUL (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000301 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000302}
303
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000304/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000305static void input_end(void)
306{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000307 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000308 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000309}
310
311/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000312static void goto_new_line(void)
313{
Mark Whitley4e338752001-01-26 20:42:23 +0000314 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000315 if (cmdedit_x)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000316 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000317}
318
319
Rob Landley88621d72006-08-29 19:41:06 +0000320static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000321{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000322 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000323 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000324}
Eric Andersen81fe1232003-07-29 06:38:40 +0000325
Rob Landley88621d72006-08-29 19:41:06 +0000326static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000327{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000328 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000329}
330
Eric Andersenaff114c2004-04-14 17:51:38 +0000331/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000332/* (optimized for slow terminals) */
333static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000334{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000335 int count_y;
336
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000337 if (num > cursor)
338 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000339 if (!num)
340 return;
341 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000342
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000343 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000344 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000345 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000346 /* This is longer by 5 bytes on x86.
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000347 * Also gets miscompiled for ARM users
348 * (busybox.net/bugs/view.php?id=2274).
Denis Vlasenko6f043912008-02-18 22:28:03 +0000349 * printf(("\b\b\b\b" + 4) - num);
350 * return;
351 */
352 do {
353 bb_putchar('\b');
354 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000355 return;
356 }
357 printf("\033[%uD", num);
358 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000359 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000360
361 /* Need to go one or more lines up */
362 num -= cmdedit_x;
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000363 {
364 unsigned w = cmdedit_termw; /* volatile var */
365 count_y = 1 + (num / w);
366 cmdedit_y -= count_y;
367 cmdedit_x = w * count_y - num;
368 }
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000369 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000370 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000371}
372
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000373static void put_prompt(void)
374{
Denys Vlasenko13ad9062009-11-11 03:19:30 +0100375 unsigned w;
376
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000377 out1str(cmdedit_prompt);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100378 fflush_all();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000379 cursor = 0;
Denys Vlasenko13ad9062009-11-11 03:19:30 +0100380 w = cmdedit_termw; /* read volatile var once */
381 cmdedit_y = cmdedit_prmt_len / w; /* new quasireal y */
382 cmdedit_x = cmdedit_prmt_len % w;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000383}
384
Eric Andersenaff114c2004-04-14 17:51:38 +0000385/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000386static void redraw(int y, int back_cursor)
387{
Denys Vlasenko044b1802009-07-12 02:50:35 +0200388 if (y > 0) /* up to start y */
389 printf("\033[%uA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000390 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000391 put_prompt();
Denys Vlasenko044b1802009-07-12 02:50:35 +0200392 input_end(); /* rewrite */
393 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000394 input_backward(back_cursor);
395}
396
Paul Fox3f11b1b2005-08-04 19:04:46 +0000397/* Delete the char in front of the cursor, optionally saving it
398 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000399#if !ENABLE_FEATURE_EDITING_VI
400static void input_delete(void)
401#define input_delete(save) input_delete()
402#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000403static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000404#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000405{
Mark Whitley4e338752001-01-26 20:42:23 +0000406 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000407
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000408 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000409 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000410
Denis Vlasenko38f63192007-01-22 09:03:07 +0000411#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000412 if (save) {
413 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000414 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000415 newdelflag = 0;
416 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000417 if ((delptr - delbuf) < DELBUFSIZ)
418 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000419 }
420#endif
421
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200422 memmove(command_ps + j, command_ps + j + 1,
Denys Vlasenko9531f7d2009-07-16 14:33:16 +0200423 /* (command_len + 1 [because of NUL]) - (j + 1)
424 * simplified into (command_len - j) */
425 (command_len - j) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000426 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000427 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000428 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000429 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000430}
431
Denis Vlasenko38f63192007-01-22 09:03:07 +0000432#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000433static void put(void)
434{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000435 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000436 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000437
Paul Fox3f11b1b2005-08-04 19:04:46 +0000438 if (j == 0)
439 return;
440 ocursor = cursor;
441 /* open hole and then fill it */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200442 memmove(command_ps + cursor + j, command_ps + cursor,
443 (command_len - cursor + 1) * sizeof(command_ps[0]));
444 memcpy(command_ps + cursor, delbuf, j * sizeof(command_ps[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000445 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000446 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000447 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000448}
449#endif
450
Mark Whitley4e338752001-01-26 20:42:23 +0000451/* Delete the char in back of the cursor */
452static void input_backspace(void)
453{
454 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000455 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000456 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000457 }
458}
459
Eric Andersenaff114c2004-04-14 17:51:38 +0000460/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000461static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000462{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000463 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000464 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000465}
466
Denis Vlasenko38f63192007-01-22 09:03:07 +0000467#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000468
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000469static void free_tab_completion_data(void)
470{
471 if (matches) {
472 while (num_matches)
473 free(matches[--num_matches]);
474 free(matches);
475 matches = NULL;
476 }
477}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000478
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000479static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000480{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000481 matches = xrealloc_vector(matches, 4, num_matches);
482 matches[num_matches] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000483 num_matches++;
484}
485
Denis Vlasenko38f63192007-01-22 09:03:07 +0000486#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000487static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000488{
489 struct passwd *entry;
490 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000491
Eric Andersenc470f442003-07-28 09:56:35 +0000492 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000493 userlen = strlen(ud);
494
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000495 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000496 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000497 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000498
Eric Andersenc470f442003-07-28 09:56:35 +0000499 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000500 home = home_pwd_buf;
501 } else {
502 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000503 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000504 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000505 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000506 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000507 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000508 ud = temp;
509 if (entry)
510 home = entry->pw_dir;
511 }
512 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000513 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000514 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000515 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000516 }
517 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518 } else {
519 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000520 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000521 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000522 struct passwd pwd;
523 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000524
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000525 setpwent();
526 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000527 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000528 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
529 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000530 }
531 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000532 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000533 }
534}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000535#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000536
537enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000538 FIND_EXE_ONLY = 0,
539 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000540 FIND_FILE_ONLY = 2,
541};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000542
Mark Whitley4e338752001-01-26 20:42:23 +0000543static int path_parse(char ***p, int flags)
544{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000545 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000546 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000547 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000548 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000549
Mark Whitley4e338752001-01-26 20:42:23 +0000550 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000551 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000552 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000553
554 if (state->flags & WITH_PATH_LOOKUP)
555 pth = state->path_lookup;
556 else
557 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000558 /* PATH=<empty> or PATH=:<empty> */
559 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
560 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000561
Denis Vlasenko253ce002007-01-22 08:34:44 +0000562 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000563 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000564 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000565 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000566 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000567 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000568 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000569 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000570 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000571 }
572
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000573 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000574 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000575 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000576 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000577 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000578 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000579 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000580 *tmp++ = '\0'; /* ':' -> '\0' */
581 if (*tmp == '\0')
582 break; /* :<empty> */
583 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000584 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000585 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000586 return npth;
587}
588
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000589static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000590{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000591 DIR *dir;
592 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000593 struct stat st;
594 char *path1[1];
595 char **paths = path1;
596 int npaths;
597 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000598 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000599 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000600/* char dirbuf[MAX_LINELEN]; */
601#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000602
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000603 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000604 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000605
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000606 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000607 /* no dir, if flags==EXE_ONLY - get paths, else "." */
608 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000609 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000610 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000611 /* dirbuf = ".../.../.../" */
612 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000613#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000614 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000615 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000616#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000617 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000618 /* point to 'l' in "..../last_component" */
619 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000620 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000621
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000622 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000623 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000624 if (!dir)
625 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000626
627 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000628 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000629 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000630
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000631 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000632 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000633 continue;
634 /* not see .name without .match */
Denis Vlasenkob5202712008-04-24 04:42:52 +0000635 if (*str_found == '.' && *pfind == '\0') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000636 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000637 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000638 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000639 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000640 found = concat_path_file(paths[i], str_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000641 /* hmm, remove in progress? */
642 /* NB: stat() first so that we see is it a directory;
643 * but if that fails, use lstat() so that
644 * we still match dangling links */
645 if (stat(found, &st) && lstat(found, &st))
Eric Andersene5dfced2001-04-09 22:48:12 +0000646 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000647 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000648 if (paths[i] != dirbuf)
Denis Vlasenkob5202712008-04-24 04:42:52 +0000649 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000650
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000651 len1 = strlen(found);
652 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000653 found[len1] = '\0';
654 found[len1+1] = '\0';
655
Mark Whitley4e338752001-01-26 20:42:23 +0000656 if (S_ISDIR(st.st_mode)) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000657 /* name is a directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000658 if (found[len1-1] != '/') {
659 found[len1] = '/';
660 }
Mark Whitley4e338752001-01-26 20:42:23 +0000661 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000662 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000663 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000664 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000665 }
Mark Whitley4e338752001-01-26 20:42:23 +0000666 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000667 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000668 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000669 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000670 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000671 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000672 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000673 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000674 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000675 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000676 free(paths);
677 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000678#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000679}
Erik Andersenf0657d32000-04-12 17:49:52 +0000680
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200681/* QUOT is used on elements of int_buf[], which are bytes,
682 * not Unicode chars. Therefore it works correctly even in Unicode mode.
683 */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000684#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000685
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200686#define int_buf (S.find_match__int_buf)
687#define pos_buf (S.find_match__pos_buf)
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +0200688/* is must be <= in */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200689static void collapse_pos(int is, int in)
690{
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +0200691 memmove(int_buf+is, int_buf+in, (MAX_LINELEN+1-in)*sizeof(int_buf[0]));
692 memmove(pos_buf+is, pos_buf+in, (MAX_LINELEN+1-in)*sizeof(pos_buf[0]));
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200693}
Denys Vlasenko044b1802009-07-12 02:50:35 +0200694static NOINLINE int find_match(char *matchBuf, int *len_with_quotes)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000695{
696 int i, j;
697 int command_mode;
698 int c, c2;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200699/* Were local, but it uses too much stack */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000700/* int16_t int_buf[MAX_LINELEN + 1]; */
701/* int16_t pos_buf[MAX_LINELEN + 1]; */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000702
703 /* set to integer dimension characters and own positions */
704 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000705 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000706 if (int_buf[i] == 0) {
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200707 pos_buf[i] = -1; /* end-fo-line indicator */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000708 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000709 }
710 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000711 }
712
713 /* mask \+symbol and convert '\t' to ' ' */
714 for (i = j = 0; matchBuf[i]; i++, j++)
715 if (matchBuf[i] == '\\') {
716 collapse_pos(j, j + 1);
717 int_buf[j] |= QUOT;
718 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000719#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200720 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000721 int_buf[j] = ' ' | QUOT;
722#endif
723 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000724#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000725 else if (matchBuf[i] == '\t')
726 int_buf[j] = ' ';
727#endif
728
729 /* mask "symbols" or 'symbols' */
730 c2 = 0;
731 for (i = 0; int_buf[i]; i++) {
732 c = int_buf[i];
733 if (c == '\'' || c == '"') {
734 if (c2 == 0)
735 c2 = c;
736 else {
737 if (c == c2)
738 c2 = 0;
739 else
740 int_buf[i] |= QUOT;
741 }
742 } else if (c2 != 0 && c != '$')
743 int_buf[i] |= QUOT;
744 }
745
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000746 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000747 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
748 for (i = 0; int_buf[i]; i++) {
749 c = int_buf[i];
750 c2 = int_buf[i + 1];
751 j = i ? int_buf[i - 1] : -1;
752 command_mode = 0;
753 if (c == ';' || c == '&' || c == '|') {
754 command_mode = 1 + (c == c2);
755 if (c == '&') {
756 if (j == '>' || j == '<')
757 command_mode = 0;
758 } else if (c == '|' && j == '>')
759 command_mode = 0;
760 }
761 if (command_mode) {
762 collapse_pos(0, i + command_mode);
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200763 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000764 }
765 }
766 /* collapse `command...` */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200767 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000768 if (int_buf[i] == '`') {
769 for (j = i + 1; int_buf[j]; j++)
770 if (int_buf[j] == '`') {
771 collapse_pos(i, j + 1);
772 j = 0;
773 break;
774 }
775 if (j) {
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200776 /* not found closing ` - command mode, collapse all previous */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000777 collapse_pos(0, i + 1);
778 break;
779 } else
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200780 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000781 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200782 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000783
784 /* collapse (command...(command...)...) or {command...{command...}...} */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200785 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000786 c2 = 0;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200787 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000788 if (int_buf[i] == '(' || int_buf[i] == '{') {
789 if (int_buf[i] == '(')
790 c++;
791 else
792 c2++;
793 collapse_pos(0, i + 1);
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200794 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000795 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200796 }
797 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000798 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
799 if (int_buf[i] == ')')
800 c--;
801 else
802 c2--;
803 collapse_pos(0, i + 1);
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200804 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000805 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200806 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000807
808 /* skip first not quote space */
809 for (i = 0; int_buf[i]; i++)
810 if (int_buf[i] != ' ')
811 break;
812 if (i)
813 collapse_pos(0, i);
814
815 /* set find mode for completion */
816 command_mode = FIND_EXE_ONLY;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200817 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000818 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
819 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000820 && matchBuf[pos_buf[0]] == 'c'
821 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000822 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000823 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000824 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000825 command_mode = FIND_FILE_ONLY;
826 break;
827 }
828 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200829 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000830 for (i = 0; int_buf[i]; i++)
831 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000832 /* find last word */
833 for (--i; i >= 0; i--) {
834 c = int_buf[i];
835 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
836 collapse_pos(0, i + 1);
837 break;
838 }
839 }
840 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000841 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
842 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000843 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000844 while ((int_buf[i] & ~QUOT) == '/'
845 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
846 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000847 i++;
848 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000849
850 /* set only match and destroy quotes */
851 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000852 for (c = 0; pos_buf[i] >= 0; i++) {
853 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000854 j = pos_buf[i] + 1;
855 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000856 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000857 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000858 *len_with_quotes = j ? j - pos_buf[0] : 0;
859
860 return command_mode;
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +0200861}
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000862#undef int_buf
863#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000864
Glenn L McGrath4d001292003-01-06 01:11:50 +0000865/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000866 * display by column (original idea from ls applet,
867 * very optimized by me :)
868 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000869static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000870{
871 int ncols, row;
872 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000873 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000874 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000875 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000876
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200877 /* find the longest file name - use that as the column width */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000878 for (row = 0; row < nrows; row++) {
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200879 l = bb_mbstrlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000880 if (column_width < l)
881 column_width = l;
882 }
883 column_width += 2; /* min space for columns */
884 ncols = cmdedit_termw / column_width;
885
886 if (ncols > 1) {
887 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000888 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000889 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000890 } else {
891 ncols = 1;
892 }
893 for (row = 0; row < nrows; row++) {
894 int n = row;
895 int nc;
896
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000897 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000898 printf("%s%-*s", matches[n],
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200899 (int)(column_width - bb_mbstrlen(matches[n])), ""
900 );
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000901 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000902 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000903 }
904}
905
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000906static char *add_quote_for_spec_chars(char *found)
907{
908 int l = 0;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200909 char *s = xzalloc((strlen(found) + 1) * 2);
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000910
911 while (*found) {
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +0200912 if (strchr(" `\"#$%^&*()=+{}[]:;'|\\<>", *found))
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000913 s[l++] = '\\';
914 s[l++] = *found++;
915 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200916 /* s[l] = '\0'; - already is */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000917 return s;
918}
919
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000920/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000921static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000922{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000923 if (!(state->flags & TAB_COMPLETION))
924 return;
925
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000926 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000927 char *tmp, *tmp1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000928 size_t len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000929/* char matchBuf[MAX_LINELEN]; */
930#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000931 int find_type;
932 int recalc_pos;
Denys Vlasenko044b1802009-07-12 02:50:35 +0200933#if ENABLE_FEATURE_ASSUME_UNICODE
934 /* cursor pos in command converted to multibyte form */
935 int cursor_mb;
936#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000937
Eric Andersenc470f442003-07-28 09:56:35 +0000938 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000939
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200940 /* Make a local copy of the string --
941 * up to the position of the cursor */
942 save_string(matchBuf, cursor + 1);
Denys Vlasenko044b1802009-07-12 02:50:35 +0200943#if ENABLE_FEATURE_ASSUME_UNICODE
944 cursor_mb = strlen(matchBuf);
945#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200946 tmp = matchBuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000947
948 find_type = find_match(matchBuf, &recalc_pos);
949
950 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000951 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000952
Denis Vlasenko38f63192007-01-22 09:03:07 +0000953#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000954 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000955 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000956 if (state->flags & USERNAME_COMPLETION)
Denys Vlasenko044b1802009-07-12 02:50:35 +0200957 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == NULL)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000958 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000959#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000960 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000961 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000962 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000963 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000964 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000965 if (matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000966 unsigned i;
967 int n = 0;
Denis Vlasenkofb290382008-03-02 12:51:26 +0000968 qsort_string_vector(matches, num_matches);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000969 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000970 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000971 if (strcmp(matches[i], matches[i+1]) == 0) {
972 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000973 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000974 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000975 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000976 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000977 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000978 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000979 matches[n] = matches[i];
980 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000981 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000982 /* Did we find exactly one match? */
Denys Vlasenko044b1802009-07-12 02:50:35 +0200983 if (!matches || num_matches > 1) { /* no */
Mark Whitley4e338752001-01-26 20:42:23 +0000984 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000985 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000986 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000987 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000988 tmp1 = xstrdup(matches[0]);
Denys Vlasenko044b1802009-07-12 02:50:35 +0200989 for (tmp = tmp1; *tmp; tmp++) {
990 for (len_found = 1; len_found < num_matches; len_found++) {
991 if (matches[len_found][tmp - tmp1] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000992 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000993 break;
994 }
Denys Vlasenko044b1802009-07-12 02:50:35 +0200995 }
996 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000997 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000998 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000999 return;
1000 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001001 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001002 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +00001003 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001004 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001005 /* for next completion current found */
1006 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001007
1008 len_found = strlen(tmp);
1009 if (tmp[len_found-1] != '/') {
1010 tmp[len_found] = ' ';
1011 tmp[len_found+1] = '\0';
1012 }
Mark Whitley4e338752001-01-26 20:42:23 +00001013 }
Denys Vlasenko044b1802009-07-12 02:50:35 +02001014
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001015 len_found = strlen(tmp);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001016#if !ENABLE_FEATURE_ASSUME_UNICODE
Denys Vlasenko044b1802009-07-12 02:50:35 +02001017 /* have space to place the match? */
1018 /* The result consists of three parts with these lengths: */
1019 /* (cursor - recalc_pos) + len_found + (command_len - cursor) */
1020 /* it simplifies into: */
1021 if ((int)(len_found + command_len - recalc_pos) < S.maxsize) {
1022 /* save tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001023 strcpy(matchBuf, command_ps + cursor);
Denys Vlasenko044b1802009-07-12 02:50:35 +02001024 /* add match and tail */
1025 sprintf(&command_ps[cursor - recalc_pos], "%s%s", tmp, matchBuf);
Denis Vlasenko253ce002007-01-22 08:34:44 +00001026 command_len = strlen(command_ps);
Denys Vlasenko044b1802009-07-12 02:50:35 +02001027 /* new pos */
1028 recalc_pos = cursor - recalc_pos + len_found;
1029 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +00001030 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001031 }
Denys Vlasenko044b1802009-07-12 02:50:35 +02001032#else
1033 {
1034 char command[MAX_LINELEN];
1035 int len = save_string(command, sizeof(command));
1036 /* have space to place the match? */
1037 /* (cursor_mb - recalc_pos) + len_found + (len - cursor_mb) */
1038 if ((int)(len_found + len - recalc_pos) < MAX_LINELEN) {
1039 /* save tail */
1040 strcpy(matchBuf, command + cursor_mb);
1041 /* where do we want to have cursor after all? */
1042 strcpy(&command[cursor_mb - recalc_pos], tmp);
1043 len = load_string(command, S.maxsize);
1044 /* add match and tail */
1045 sprintf(&command[cursor_mb - recalc_pos], "%s%s", tmp, matchBuf);
1046 command_len = load_string(command, S.maxsize);
1047 /* write out the matched command */
1048 redraw(cmdedit_y, command_len - len);
1049 }
1050 }
1051#endif
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001052 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001053#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +00001054 } else {
1055 /* Ok -- the last char was a TAB. Since they
1056 * just hit TAB again, print a list of all the
1057 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001058 if (matches && num_matches > 0) {
Denys Vlasenko044b1802009-07-12 02:50:35 +02001059 /* changed by goto_new_line() */
1060 int sav_cursor = cursor;
Erik Andersenf0657d32000-04-12 17:49:52 +00001061
1062 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001063 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001064 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001065 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001066 }
1067 }
1068}
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001069
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001070#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001071
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001072
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001073line_input_t* FAST_FUNC new_line_input_t(int flags)
1074{
1075 line_input_t *n = xzalloc(sizeof(*n));
1076 n->flags = flags;
1077 return n;
1078}
1079
1080
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001081#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001082
Denis Vlasenko682ad302008-09-27 01:28:56 +00001083static void save_command_ps_at_cur_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001084{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001085 if (command_ps[0] != BB_NUL) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001086 int cur = state->cur_history;
1087 free(state->history[cur]);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001088
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001089# if ENABLE_FEATURE_ASSUME_UNICODE
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001090 {
1091 char tbuf[MAX_LINELEN];
1092 save_string(tbuf, sizeof(tbuf));
1093 state->history[cur] = xstrdup(tbuf);
1094 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001095# else
Denis Vlasenko682ad302008-09-27 01:28:56 +00001096 state->history[cur] = xstrdup(command_ps);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001097# endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001098 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001099}
1100
1101/* state->flags is already checked to be nonzero */
1102static int get_previous_history(void)
1103{
1104 if ((state->flags & DO_HISTORY) && state->cur_history) {
1105 save_command_ps_at_cur_history();
1106 state->cur_history--;
1107 return 1;
1108 }
1109 beep();
1110 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001111}
1112
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001113static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001114{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001115 if (state->flags & DO_HISTORY) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001116 if (state->cur_history < state->cnt_history) {
1117 save_command_ps_at_cur_history(); /* save the current history line */
1118 return ++state->cur_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001119 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001120 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001121 beep();
1122 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001123}
Robert Griebl350d26b2002-12-03 22:45:46 +00001124
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001125# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001126/* We try to ensure that concurrent additions to the history
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001127 * do not overwrite each other.
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001128 * Otherwise shell users get unhappy.
1129 *
1130 * History file is trimmed lazily, when it grows several times longer
1131 * than configured MAX_HISTORY lines.
1132 */
1133
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001134static void free_line_input_t(line_input_t *n)
1135{
1136 int i = n->cnt_history;
1137 while (i > 0)
1138 free(n->history[--i]);
1139 free(n);
1140}
1141
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001142/* state->flags is already checked to be nonzero */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001143static void load_history(line_input_t *st_parm)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001144{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001145 char *temp_h[MAX_HISTORY];
1146 char *line;
Robert Griebl350d26b2002-12-03 22:45:46 +00001147 FILE *fp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001148 unsigned idx, i, line_len;
Robert Griebl350d26b2002-12-03 22:45:46 +00001149
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001150 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +00001151
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001152 fp = fopen_for_read(st_parm->hist_file);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001153 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001154 /* clean up old history */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001155 for (idx = st_parm->cnt_history; idx > 0;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001156 idx--;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001157 free(st_parm->history[idx]);
1158 st_parm->history[idx] = NULL;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001159 }
1160
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001161 /* fill temp_h[], retaining only last MAX_HISTORY lines */
1162 memset(temp_h, 0, sizeof(temp_h));
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001163 st_parm->cnt_history_in_file = idx = 0;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001164 while ((line = xmalloc_fgetline(fp)) != NULL) {
1165 if (line[0] == '\0') {
1166 free(line);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001167 continue;
1168 }
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001169 free(temp_h[idx]);
1170 temp_h[idx] = line;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001171 st_parm->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001172 idx++;
1173 if (idx == MAX_HISTORY)
1174 idx = 0;
Robert Griebl350d26b2002-12-03 22:45:46 +00001175 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001176 fclose(fp);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001177
1178 /* find first non-NULL temp_h[], if any */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001179 if (st_parm->cnt_history_in_file) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001180 while (temp_h[idx] == NULL) {
1181 idx++;
1182 if (idx == MAX_HISTORY)
1183 idx = 0;
1184 }
1185 }
1186
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001187 /* copy temp_h[] to st_parm->history[] */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001188 for (i = 0; i < MAX_HISTORY;) {
1189 line = temp_h[idx];
1190 if (!line)
1191 break;
1192 idx++;
1193 if (idx == MAX_HISTORY)
1194 idx = 0;
1195 line_len = strlen(line);
1196 if (line_len >= MAX_LINELEN)
1197 line[MAX_LINELEN-1] = '\0';
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001198 st_parm->history[i++] = line;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001199 }
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001200 st_parm->cnt_history = i;
Robert Griebl350d26b2002-12-03 22:45:46 +00001201 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001202}
1203
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001204/* state->flags is already checked to be nonzero */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001205static void save_history(char *str)
Robert Griebl350d26b2002-12-03 22:45:46 +00001206{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001207 int fd;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001208 int len, len2;
Eric Andersenc470f442003-07-28 09:56:35 +00001209
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001210 fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0666);
1211 if (fd < 0)
1212 return;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001213 xlseek(fd, 0, SEEK_END); /* paranoia */
1214 len = strlen(str);
1215 str[len] = '\n'; /* we (try to) do atomic write */
1216 len2 = full_write(fd, str, len + 1);
1217 str[len] = '\0';
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001218 close(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001219 if (len2 != len + 1)
1220 return; /* "wtf?" */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001221
1222 /* did we write so much that history file needs trimming? */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001223 state->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001224 if (state->cnt_history_in_file > MAX_HISTORY * 4) {
1225 FILE *fp;
1226 char *new_name;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001227 line_input_t *st_temp;
1228 int i;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001229
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001230 /* we may have concurrently written entries from others.
1231 * load them */
1232 st_temp = new_line_input_t(state->flags);
1233 st_temp->hist_file = state->hist_file;
1234 load_history(st_temp);
1235
1236 /* write out temp file and replace hist_file atomically */
1237 new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001238 fp = fopen_for_write(new_name);
1239 if (fp) {
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001240 for (i = 0; i < st_temp->cnt_history; i++)
1241 fprintf(fp, "%s\n", st_temp->history[i]);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001242 fclose(fp);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001243 if (rename(new_name, state->hist_file) == 0)
1244 state->cnt_history_in_file = st_temp->cnt_history;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001245 }
1246 free(new_name);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001247 free_line_input_t(st_temp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001248 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001249}
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001250# else
1251# define load_history(a) ((void)0)
1252# define save_history(a) ((void)0)
1253# endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001254
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001255static void remember_in_history(char *str)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001256{
1257 int i;
1258
1259 if (!(state->flags & DO_HISTORY))
1260 return;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001261 if (str[0] == '\0')
1262 return;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001263 i = state->cnt_history;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001264 /* Don't save dupes */
1265 if (i && strcmp(state->history[i-1], str) == 0)
1266 return;
1267
1268 free(state->history[MAX_HISTORY]); /* redundant, paranoia */
1269 state->history[MAX_HISTORY] = NULL; /* redundant, paranoia */
1270
1271 /* If history[] is full, remove the oldest command */
1272 /* we need to keep history[MAX_HISTORY] empty, hence >=, not > */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001273 if (i >= MAX_HISTORY) {
1274 free(state->history[0]);
1275 for (i = 0; i < MAX_HISTORY-1; i++)
1276 state->history[i] = state->history[i+1];
Denis Vlasenko682ad302008-09-27 01:28:56 +00001277 /* i == MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001278 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001279 /* i <= MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001280 state->history[i++] = xstrdup(str);
Denis Vlasenko682ad302008-09-27 01:28:56 +00001281 /* i <= MAX_HISTORY */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001282 state->cur_history = i;
1283 state->cnt_history = i;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001284# if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001285 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001286 save_history(str);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001287# endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001288 IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001289}
1290
1291#else /* MAX_HISTORY == 0 */
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001292# define remember_in_history(a) ((void)0)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001293#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001294
1295
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001296#if ENABLE_FEATURE_EDITING_VI
Erik Andersen6273f652000-03-17 01:12:41 +00001297/*
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001298 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001299 */
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001300static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001301vi_Word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001302{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001303 CHAR_T *command = command_ps;
1304
1305 while (cursor < command_len && !BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001306 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001307 if (eat) while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001308 input_forward();
1309}
1310
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001311static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001312vi_word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001313{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001314 CHAR_T *command = command_ps;
1315
1316 if (BB_isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001317 while (cursor < command_len
Denys Vlasenko13028922009-07-12 00:51:15 +02001318 && (BB_isalnum(command[cursor+1]) || command[cursor+1] == '_')
1319 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001320 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001321 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001322 } else if (BB_ispunct(command[cursor])) {
1323 while (cursor < command_len && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001324 input_forward();
1325 }
1326
Denis Vlasenko253ce002007-01-22 08:34:44 +00001327 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001328 input_forward();
1329
Denys Vlasenko13028922009-07-12 00:51:15 +02001330 if (eat) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001331 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001332 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001333 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001334}
1335
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001336static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001337vi_End_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001338{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001339 CHAR_T *command = command_ps;
1340
Paul Fox3f11b1b2005-08-04 19:04:46 +00001341 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001342 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001343 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001344 while (cursor < command_len-1 && !BB_isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001345 input_forward();
1346}
1347
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001348static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001349vi_end_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001350{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001351 CHAR_T *command = command_ps;
1352
Denis Vlasenko253ce002007-01-22 08:34:44 +00001353 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001354 return;
1355 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001356 while (cursor < command_len-1 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001357 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001358 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001359 return;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001360 if (BB_isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001361 while (cursor < command_len-1
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001362 && (BB_isalnum(command[cursor+1]) || command[cursor+1] == '_')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001363 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001364 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001365 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001366 } else if (BB_ispunct(command[cursor])) {
1367 while (cursor < command_len-1 && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001368 input_forward();
1369 }
1370}
1371
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001372static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001373vi_Back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001374{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001375 CHAR_T *command = command_ps;
1376
1377 while (cursor > 0 && BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001378 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001379 while (cursor > 0 && !BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001380 input_backward(1);
1381}
1382
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001383static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001384vi_back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001385{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001386 CHAR_T *command = command_ps;
1387
Paul Fox3f11b1b2005-08-04 19:04:46 +00001388 if (cursor <= 0)
1389 return;
1390 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001391 while (cursor > 0 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001392 input_backward(1);
1393 if (cursor <= 0)
1394 return;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001395 if (BB_isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001396 while (cursor > 0
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001397 && (BB_isalnum(command[cursor-1]) || command[cursor-1] == '_')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001398 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001399 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001400 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001401 } else if (BB_ispunct(command[cursor])) {
1402 while (cursor > 0 && BB_ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001403 input_backward(1);
1404 }
1405}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001406#endif
1407
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001408/* Modelled after bash 4.0 behavior of Ctrl-<arrow> */
1409static void ctrl_left(void)
1410{
1411 CHAR_T *command = command_ps;
1412
1413 while (1) {
1414 CHAR_T c;
1415
1416 input_backward(1);
1417 if (cursor == 0)
1418 break;
1419 c = command[cursor];
1420 if (c != ' ' && !BB_ispunct(c)) {
1421 /* we reached a "word" delimited by spaces/punct.
1422 * go to its beginning */
1423 while (1) {
1424 c = command[cursor - 1];
1425 if (c == ' ' || BB_ispunct(c))
1426 break;
1427 input_backward(1);
1428 if (cursor == 0)
1429 break;
1430 }
1431 break;
1432 }
1433 }
1434}
1435static void ctrl_right(void)
1436{
1437 CHAR_T *command = command_ps;
1438
1439 while (1) {
1440 CHAR_T c;
1441
1442 c = command[cursor];
1443 if (c == BB_NUL)
1444 break;
1445 if (c != ' ' && !BB_ispunct(c)) {
1446 /* we reached a "word" delimited by spaces/punct.
1447 * go to its end + 1 */
1448 while (1) {
1449 input_forward();
1450 c = command[cursor];
1451 if (c == BB_NUL || c == ' ' || BB_ispunct(c))
1452 break;
1453 }
1454 break;
1455 }
1456 input_forward();
1457 }
1458}
1459
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001460
1461/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001462 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001463 */
1464
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001465#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
1466static void ask_terminal(void)
1467{
1468 /* Ask terminal where is the cursor now.
1469 * lineedit_read_key handles response and corrects
1470 * our idea of current cursor position.
1471 * Testcase: run "echo -n long_line_long_line_long_line",
1472 * then type in a long, wrapping command and try to
1473 * delete it using backspace key.
1474 * Note: we print it _after_ prompt, because
1475 * prompt may contain CR. Example: PS1='\[\r\n\]\w '
1476 */
1477 /* Problem: if there is buffered input on stdin,
1478 * the response will be delivered later,
1479 * possibly to an unsuspecting application.
1480 * Testcase: "sleep 1; busybox ash" + press and hold [Enter].
1481 * Result:
1482 * ~/srcdevel/bbox/fix/busybox.t4 #
1483 * ~/srcdevel/bbox/fix/busybox.t4 #
1484 * ^[[59;34~/srcdevel/bbox/fix/busybox.t4 # <-- garbage
1485 * ~/srcdevel/bbox/fix/busybox.t4 #
1486 *
1487 * Checking for input with poll only makes the race narrower,
1488 * I still can trigger it. Strace:
1489 *
1490 * write(1, "~/srcdevel/bbox/fix/busybox.t4 # ", 33) = 33
1491 * poll([{fd=0, events=POLLIN}], 1, 0) = 0 (Timeout) <-- no input exists
1492 * write(1, "\33[6n", 4) = 4 <-- send the ESC sequence, quick!
1493 * poll([{fd=0, events=POLLIN}], 1, 4294967295) = 1 ([{fd=0, revents=POLLIN}])
1494 * read(0, "\n", 1) = 1 <-- oh crap, user's input got in first
1495 */
1496 struct pollfd pfd;
1497
1498 pfd.fd = STDIN_FILENO;
1499 pfd.events = POLLIN;
1500 if (safe_poll(&pfd, 1, 0) == 0) {
1501 S.sent_ESC_br6n = 1;
1502 out1str("\033" "[6n");
1503 fflush_all(); /* make terminal see it ASAP! */
1504 }
1505}
1506#else
1507#define ask_terminal() ((void)0)
1508#endif
1509
Denis Vlasenko38f63192007-01-22 09:03:07 +00001510#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001511static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001512{
1513 cmdedit_prompt = prmt_ptr;
1514 cmdedit_prmt_len = strlen(prmt_ptr);
1515 put_prompt();
1516}
1517#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001518static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001519{
1520 int prmt_len = 0;
1521 size_t cur_prmt_len = 0;
1522 char flg_not_length = '[';
1523 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001524 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1525 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001526 char c;
1527 char *pbuf;
1528
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001529 cmdedit_prmt_len = 0;
1530
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001531 if (!cwd_buf) {
1532 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001533 }
1534
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001535 cbuf[1] = '\0'; /* never changes */
1536
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001537 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001538 char *free_me = NULL;
1539
1540 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001541 c = *prmt_ptr++;
1542 if (c == '\\') {
1543 const char *cp = prmt_ptr;
1544 int l;
1545
1546 c = bb_process_escape_sequence(&prmt_ptr);
1547 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001548 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001549 break;
1550 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001551
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001552 switch (c) {
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001553# if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001554 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001555 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001556 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001557# endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001558 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001559 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001560 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001561 break;
1562 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001563 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001564 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001565# if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001566 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001567 /* /home/user[/something] -> ~[/something] */
1568 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001569 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001570 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001571 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1572 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1573 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001574 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001575 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001576 }
1577 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001578# endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001579 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001580 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001581 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001582 if (cp != NULL && cp != pbuf)
1583 pbuf += (cp-pbuf) + 1;
1584 break;
1585 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001586 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001587 break;
1588 case 'e': case 'E': /* \e \E = \033 */
1589 c = '\033';
1590 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001591 case 'x': case 'X': {
1592 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001593 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001594 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001595 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001596 buf2[l] = '\0';
1597 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001598 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001599 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001600 break;
1601 }
1602 prmt_ptr++;
1603 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001604 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001605 if (c == 0)
1606 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001607 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001608 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001609 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001610 case '[': case ']':
1611 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001612 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001613 continue;
1614 }
1615 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001616 } /* switch */
1617 } /* if */
1618 } /* if */
1619 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001620 cur_prmt_len = strlen(pbuf);
1621 prmt_len += cur_prmt_len;
1622 if (flg_not_length != ']')
1623 cmdedit_prmt_len += cur_prmt_len;
1624 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001625 free(free_me);
1626 } /* while */
1627
1628 if (cwd_buf != (char *)bb_msg_unknown)
1629 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001630 cmdedit_prompt = prmt_mem_ptr;
1631 put_prompt();
1632}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001633#endif
1634
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001635static void cmdedit_setwidth(unsigned w, int redraw_flg)
1636{
1637 cmdedit_termw = w;
1638 if (redraw_flg) {
1639 /* new y for current cursor */
1640 int new_y = (cursor + cmdedit_prmt_len) / w;
1641 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001642 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001643 fflush_all();
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001644 }
1645}
1646
1647static void win_changed(int nsig)
1648{
Denis Vlasenko55995022008-05-18 22:28:26 +00001649 unsigned width;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001650 get_terminal_width_height(0, &width, NULL);
1651 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1652 if (nsig == SIGWINCH)
1653 signal(SIGWINCH, win_changed); /* rearm ourself */
1654}
1655
Denys Vlasenko020f4062009-05-17 16:44:54 +02001656static int lineedit_read_key(char *read_key_buffer)
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001657{
Denys Vlasenko020f4062009-05-17 16:44:54 +02001658 int64_t ic;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001659 struct pollfd pfd;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001660 int delay = -1;
1661#if ENABLE_FEATURE_ASSUME_UNICODE
1662 char unicode_buf[MB_CUR_MAX + 1];
1663 int unicode_idx = 0;
1664#endif
Denys Vlasenko020f4062009-05-17 16:44:54 +02001665
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001666 pfd.fd = STDIN_FILENO;
1667 pfd.events = POLLIN;
1668 do {
Denys Vlasenko1f6d2302009-10-29 03:45:26 +01001669#if ENABLE_FEATURE_EDITING_ASK_TERMINAL || ENABLE_FEATURE_ASSUME_UNICODE
1670 poll_again:
1671#endif
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +02001672 if (read_key_buffer[0] == 0) {
1673 /* Wait for input. Can't just call read_key,
1674 * it returns at once if stdin
1675 * is in non-blocking mode. */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001676 safe_poll(&pfd, 1, delay);
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +02001677 }
1678 /* Note: read_key sets errno to 0 on success: */
Denys Vlasenko020f4062009-05-17 16:44:54 +02001679 ic = read_key(STDIN_FILENO, read_key_buffer);
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02001680
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01001681#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
1682 if ((int32_t)ic == KEYCODE_CURSOR_POS
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01001683 && S.sent_ESC_br6n
Denys Vlasenko020f4062009-05-17 16:44:54 +02001684 ) {
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01001685 S.sent_ESC_br6n = 0;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01001686 if (cursor == 0) { /* otherwise it may be bogus */
1687 int col = ((ic >> 32) & 0x7fff) - 1;
1688 if (col > cmdedit_prmt_len) {
1689 cmdedit_x += (col - cmdedit_prmt_len);
1690 while (cmdedit_x >= cmdedit_termw) {
1691 cmdedit_x -= cmdedit_termw;
1692 cmdedit_y++;
1693 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02001694 }
1695 }
Denys Vlasenko1f6d2302009-10-29 03:45:26 +01001696 goto poll_again;
Denys Vlasenko020f4062009-05-17 16:44:54 +02001697 }
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01001698#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001699
1700#if ENABLE_FEATURE_ASSUME_UNICODE
1701 {
1702 wchar_t wc;
1703
1704 if ((int32_t)ic < 0) /* KEYCODE_xxx */
1705 return ic;
1706 unicode_buf[unicode_idx++] = ic;
1707 unicode_buf[unicode_idx] = '\0';
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02001708 if (mbstowcs(&wc, unicode_buf, 1) != 1 && unicode_idx < MB_CUR_MAX) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001709 delay = 50;
Denys Vlasenko1f6d2302009-10-29 03:45:26 +01001710 goto poll_again;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001711 }
1712 ic = wc;
1713 }
1714#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001715 } while (errno == EAGAIN);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001716
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001717 return ic;
1718}
1719
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001720/* leave out the "vi-mode"-only case labels if vi editing isn't
1721 * configured. */
Denys Vlasenko13028922009-07-12 00:51:15 +02001722#define vi_case(caselabel) IF_FEATURE_EDITING_VI(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001723
1724/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001725#undef CTRL
1726#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001727
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02001728/* maxsize must be >= 2.
1729 * Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001730 * -1 on read errors or EOF, or on bare Ctrl-D,
1731 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001732 * >0 length of input string, including terminating '\n'
1733 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001734int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001735{
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00001736 int len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001737#if ENABLE_FEATURE_TAB_COMPLETION
1738 smallint lastWasTab = FALSE;
1739#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001740 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001741#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001742 smallint vi_cmdmode = 0;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001743#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001744 struct termios initial_settings;
1745 struct termios new_settings;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001746 char read_key_buffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001747
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001748 INIT_S();
1749
1750 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1751 || !(initial_settings.c_lflag & ECHO)
1752 ) {
1753 /* Happens when e.g. stty -echo was run before */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001754 parse_and_put_prompt(prompt);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001755 /* fflush_all(); - done by parse_and_put_prompt */
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001756 if (fgets(command, maxsize, stdin) == NULL)
1757 len = -1; /* EOF or error */
1758 else
1759 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001760 DEINIT_S();
1761 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001762 }
1763
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02001764 check_unicode_in_env();
1765
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001766// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001767 if (maxsize > MAX_LINELEN)
1768 maxsize = MAX_LINELEN;
Denys Vlasenko044b1802009-07-12 02:50:35 +02001769 S.maxsize = maxsize;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001770
1771 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001772 state = st ? st : (line_input_t*) &const_int_0;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001773#if MAX_HISTORY > 0
1774# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001775 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001776 if (state->cnt_history == 0)
1777 load_history(state);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001778# endif
Denis Vlasenko3c385cd2008-11-02 00:41:05 +00001779 if (state->flags & DO_HISTORY)
1780 state->cur_history = state->cnt_history;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001781#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001782
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001783 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001784 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001785 command_len = 0;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001786#if ENABLE_FEATURE_ASSUME_UNICODE
1787 command_ps = xzalloc(maxsize * sizeof(command_ps[0]));
1788#else
Mark Whitley4e338752001-01-26 20:42:23 +00001789 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001790 command[0] = '\0';
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001791#endif
1792#define command command_must_not_be_used
Mark Whitley4e338752001-01-26 20:42:23 +00001793
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001794 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001795 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1796 /* Turn off echoing and CTRL-C, so we can trap it */
1797 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001798 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001799 new_settings.c_cc[VMIN] = 1;
1800 new_settings.c_cc[VTIME] = 0;
1801 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001802#ifndef _POSIX_VDISABLE
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001803# define _POSIX_VDISABLE '\0'
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001804#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001805 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko202ac502008-11-05 13:20:58 +00001806 tcsetattr_stdin_TCSANOW(&new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001807
Eric Andersen6faae7d2001-02-16 20:09:17 +00001808 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001809 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1810 win_changed(0); /* do initial resizing */
1811#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1812 {
1813 struct passwd *entry;
1814
1815 entry = getpwuid(geteuid());
1816 if (entry) {
1817 user_buf = xstrdup(entry->pw_name);
1818 home_pwd_buf = xstrdup(entry->pw_dir);
1819 }
1820 }
1821#endif
Denis Vlasenko682ad302008-09-27 01:28:56 +00001822
1823#if 0
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001824 for (i = 0; i <= MAX_HISTORY; i++)
1825 bb_error_msg("history[%d]:'%s'", i, state->history[i]);
Denis Vlasenko682ad302008-09-27 01:28:56 +00001826 bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
1827#endif
1828
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001829 /* Print out the command prompt, optionally ask where cursor is */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001830 parse_and_put_prompt(prompt);
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001831 ask_terminal();
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001832
Denys Vlasenkoc396fe62009-05-17 19:28:14 +02001833 read_key_buffer[0] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001834 while (1) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001835 /*
1836 * The emacs and vi modes share much of the code in the big
1837 * command loop. Commands entered when in vi's command mode
1838 * (aka "escape mode") get an extra bit added to distinguish
1839 * them - this keeps them from being self-inserted. This
1840 * clutters the big switch a bit, but keeps all the code
1841 * in one place.
1842 */
1843 enum {
1844 VI_CMDMODE_BIT = 0x40000000,
1845 /* 0x80000000 bit flags KEYCODE_xxx */
1846 };
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02001847 int32_t ic, ic_raw;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001848
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001849 fflush_all();
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02001850 ic = ic_raw = lineedit_read_key(read_key_buffer);
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001851
Denis Vlasenko38f63192007-01-22 09:03:07 +00001852#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001853 newdelflag = 1;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001854 if (vi_cmdmode) {
1855 /* btw, since KEYCODE_xxx are all < 0, this doesn't
1856 * change ic if it contains one of them: */
1857 ic |= VI_CMDMODE_BIT;
1858 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001859#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001860
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001861 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001862 case '\n':
1863 case '\r':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001864 vi_case('\n'|VI_CMDMODE_BIT:)
1865 vi_case('\r'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001866 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001867 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001868 break_out = 1;
1869 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001870 case CTRL('A'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001871 vi_case('0'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001872 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001873 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001874 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001875 case CTRL('B'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001876 vi_case('h'|VI_CMDMODE_BIT:)
1877 vi_case('\b'|VI_CMDMODE_BIT:)
1878 vi_case('\x7f'|VI_CMDMODE_BIT:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001879 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001880 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001881 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001882 case CTRL('E'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001883 vi_case('$'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001884 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001885 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001886 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001887 case CTRL('F'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001888 vi_case('l'|VI_CMDMODE_BIT:)
1889 vi_case(' '|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001890 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001891 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001892 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001893 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001894 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001895 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001896 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001897 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001898#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001899 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001900 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001901 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001902#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001903 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001904 /* Control-k -- clear to end of line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001905 command_ps[cursor] = BB_NUL;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001906 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001907 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001908 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001909 case CTRL('L'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001910 vi_case(CTRL('L')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001911 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001912 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001913 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001914 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001915#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001916 case CTRL('N'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001917 vi_case(CTRL('N')|VI_CMDMODE_BIT:)
1918 vi_case('j'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001919 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001920 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001921 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001922 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001923 case CTRL('P'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001924 vi_case(CTRL('P')|VI_CMDMODE_BIT:)
1925 vi_case('k'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001926 /* Control-p -- Get previous command from history */
Denis Vlasenko682ad302008-09-27 01:28:56 +00001927 if (get_previous_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001928 goto rewrite_line;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001929 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001930#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001931 case CTRL('U'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001932 vi_case(CTRL('U')|VI_CMDMODE_BIT:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001933 /* Control-U -- Clear line before cursor */
1934 if (cursor) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001935 command_len -= cursor;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001936 memmove(command_ps, command_ps + cursor,
1937 (command_len + 1) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001938 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001939 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001940 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001941 case CTRL('W'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001942 vi_case(CTRL('W')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001943 /* Control-W -- Remove the last word */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001944 while (cursor > 0 && BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001945 input_backspace();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001946 while (cursor > 0 && !BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001947 input_backspace();
1948 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001949
Denis Vlasenko38f63192007-01-22 09:03:07 +00001950#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001951 case 'i'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001952 vi_cmdmode = 0;
1953 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001954 case 'I'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001955 input_backward(cursor);
1956 vi_cmdmode = 0;
1957 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001958 case 'a'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001959 input_forward();
1960 vi_cmdmode = 0;
1961 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001962 case 'A'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001963 input_end();
1964 vi_cmdmode = 0;
1965 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001966 case 'x'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001967 input_delete(1);
1968 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001969 case 'X'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001970 if (cursor > 0) {
1971 input_backward(1);
1972 input_delete(1);
1973 }
1974 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001975 case 'W'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001976 vi_Word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00001977 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001978 case 'w'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001979 vi_word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00001980 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001981 case 'E'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001982 vi_End_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00001983 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001984 case 'e'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001985 vi_end_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00001986 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001987 case 'B'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001988 vi_Back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00001989 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001990 case 'b'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001991 vi_back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00001992 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001993 case 'C'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001994 vi_cmdmode = 0;
1995 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001996 case 'D'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001997 goto clear_to_eol;
1998
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001999 case 'c'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002000 vi_cmdmode = 0;
2001 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002002 case 'd'|VI_CMDMODE_BIT: {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002003 int nc, sc;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002004
Denys Vlasenko020f4062009-05-17 16:44:54 +02002005 ic = lineedit_read_key(read_key_buffer);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002006 if (errno) /* error */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002007 goto prepare_to_die;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002008 if (ic == ic_raw) { /* "cc", "dd" */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002009 input_backward(cursor);
2010 goto clear_to_eol;
2011 break;
2012 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002013
2014 sc = cursor;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002015 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002016 case 'w':
2017 case 'W':
2018 case 'e':
2019 case 'E':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002020 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002021 case 'w': /* "dw", "cw" */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002022 vi_word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002023 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002024 case 'W': /* 'dW', 'cW' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002025 vi_Word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002026 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002027 case 'e': /* 'de', 'ce' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002028 vi_end_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002029 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002030 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002031 case 'E': /* 'dE', 'cE' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002032 vi_End_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002033 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002034 break;
2035 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002036 nc = cursor;
2037 input_backward(cursor - sc);
2038 while (nc-- > cursor)
2039 input_delete(1);
2040 break;
2041 case 'b': /* "db", "cb" */
2042 case 'B': /* implemented as B */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002043 if (ic == 'b')
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002044 vi_back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002045 else
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002046 vi_Back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002047 while (sc-- > cursor)
2048 input_delete(1);
2049 break;
2050 case ' ': /* "d ", "c " */
2051 input_delete(1);
2052 break;
2053 case '$': /* "d$", "c$" */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002054 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002055 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002056 input_delete(1);
2057 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002058 }
2059 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002060 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002061 case 'p'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002062 input_forward();
2063 /* fallthrough */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002064 case 'P'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002065 put();
2066 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002067 case 'r'|VI_CMDMODE_BIT:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002068//FIXME: unicode case?
Denys Vlasenko020f4062009-05-17 16:44:54 +02002069 ic = lineedit_read_key(read_key_buffer);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002070 if (errno) /* error */
Paul Fox3f11b1b2005-08-04 19:04:46 +00002071 goto prepare_to_die;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002072 if (ic < ' ' || ic > 255) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00002073 beep();
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002074 } else {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002075 command_ps[cursor] = ic;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002076 bb_putchar(ic);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002077 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00002078 }
2079 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002080 case '\x1b': /* ESC */
2081 if (state->flags & VI_MODE) {
2082 /* insert mode --> command mode */
2083 vi_cmdmode = 1;
2084 input_backward(1);
2085 }
2086 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002087#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002088
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002089#if MAX_HISTORY > 0
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002090 case KEYCODE_UP:
2091 if (get_previous_history())
2092 goto rewrite_line;
2093 beep();
2094 break;
2095 case KEYCODE_DOWN:
2096 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002097 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002098 rewrite_line:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002099 /* Rewrite the line with the selected history item */
2100 /* change command */
Denys Vlasenko90a99042009-09-06 02:36:23 +02002101 command_len = load_string(state->history[state->cur_history] ?
2102 state->history[state->cur_history] : "", maxsize);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002103 /* redraw and go to eol (bol, in vi) */
2104 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
2105 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002106#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002107 case KEYCODE_RIGHT:
2108 input_forward();
2109 break;
2110 case KEYCODE_LEFT:
2111 input_backward(1);
2112 break;
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002113 case KEYCODE_CTRL_LEFT:
2114 ctrl_left();
2115 break;
2116 case KEYCODE_CTRL_RIGHT:
2117 ctrl_right();
2118 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002119 case KEYCODE_DELETE:
2120 input_delete(0);
2121 break;
2122 case KEYCODE_HOME:
2123 input_backward(cursor);
2124 break;
2125 case KEYCODE_END:
2126 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002127 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002128
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002129 default:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002130 if (initial_settings.c_cc[VINTR] != 0
2131 && ic_raw == initial_settings.c_cc[VINTR]
2132 ) {
2133 /* Ctrl-C (usually) - stop gathering input */
2134 goto_new_line();
2135 command_len = 0;
2136 break_out = -1; /* "do not append '\n'" */
2137 break;
2138 }
2139 if (initial_settings.c_cc[VEOF] != 0
2140 && ic_raw == initial_settings.c_cc[VEOF]
2141 ) {
2142 /* Ctrl-D (usually) - delete one character,
2143 * or exit if len=0 and no chars to delete */
2144 if (command_len == 0) {
2145 errno = 0;
2146#if ENABLE_FEATURE_EDITING_VI
2147 prepare_to_die:
2148#endif
2149 break_out = command_len = -1;
2150 break;
2151 }
2152 input_delete(0);
2153 break;
2154 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002155// /* Control-V -- force insert of next char */
2156// if (c == CTRL('V')) {
2157// if (safe_read(STDIN_FILENO, &c, 1) < 1)
2158// goto prepare_to_die;
2159// if (c == 0) {
2160// beep();
2161// break;
2162// }
2163// }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002164 if (ic < ' '
2165 || (!ENABLE_FEATURE_ASSUME_UNICODE && ic >= 256)
2166 || (ENABLE_FEATURE_ASSUME_UNICODE && ic >= VI_CMDMODE_BIT)
2167 ) {
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002168 /* If VI_CMDMODE_BIT is set, ic is >= 256
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002169 * and vi mode ignores unexpected chars.
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002170 * Otherwise, we are here if ic is a
2171 * control char or an unhandled ESC sequence,
2172 * which is also ignored.
2173 */
2174 break;
2175 }
2176 if ((int)command_len >= (maxsize - 2)) {
2177 /* Not enough space for the char and EOL */
2178 break;
Paul Fox84bbac52008-01-11 16:50:08 +00002179 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002180
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002181 command_len++;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002182 if (cursor == (command_len - 1)) {
2183 /* We are at the end, append */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002184 command_ps[cursor] = ic;
2185 command_ps[cursor + 1] = BB_NUL;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002186 cmdedit_set_out_char(' ');
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002187 } else {
2188 /* In the middle, insert */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002189 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002190
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002191 memmove(command_ps + sc + 1, command_ps + sc,
2192 (command_len - sc) * sizeof(command_ps[0]));
2193 command_ps[sc] = ic;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002194 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00002195 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002196 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00002197 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002198 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002199 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00002200 break;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002201 } /* switch (ic) */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002202
2203 if (break_out)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002204 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002205
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002206#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002207 if (ic_raw != '\t')
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002208 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002209#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002210 } /* while (1) */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002211
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002212#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002213 if (S.sent_ESC_br6n) {
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002214 /* "sleep 1; busybox ash" + hold [Enter] to trigger.
2215 * We sent "ESC [ 6 n", but got '\n' first, and
2216 * KEYCODE_CURSOR_POS response is now buffered from terminal.
2217 * It's bad already and not much can be done with it
2218 * (it _will_ be visible for the next process to read stdin),
2219 * but without this delay it even shows up on the screen
2220 * as garbage because we restore echo settings with tcsetattr
2221 * before it comes in. UGLY!
2222 */
2223 usleep(20*1000);
2224 }
2225#endif
2226
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002227/* Stop bug catching using "command_must_not_be_used" trick */
2228#undef command
2229
2230#if ENABLE_FEATURE_ASSUME_UNICODE
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +02002231 command[0] = '\0';
2232 if (command_len > 0)
2233 command_len = save_string(command, maxsize - 1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002234 free(command_ps);
2235#endif
2236
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002237 if (command_len > 0)
2238 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002239
Eric Andersen27bb7902003-12-23 20:24:51 +00002240 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002241 command[command_len++] = '\n';
2242 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00002243 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002244
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002245#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002246 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002247#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002248
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002249 /* restore initial_settings */
Denis Vlasenko202ac502008-11-05 13:20:58 +00002250 tcsetattr_stdin_TCSANOW(&initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002251 /* restore SIGWINCH handler */
2252 signal(SIGWINCH, previous_SIGWINCH_handler);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002253 fflush_all();
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002254
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002255 len = command_len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002256 DEINIT_S();
2257
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002258 return len; /* can't return command_len, DEINIT_S() destroys it */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002259}
2260
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002261#else
2262
2263#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00002264int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002265{
2266 fputs(prompt, stdout);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002267 fflush_all();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002268 fgets(command, maxsize, stdin);
2269 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00002270}
2271
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002272#endif /* FEATURE_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00002273
2274
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002275/*
2276 * Testing
2277 */
2278
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002279#ifdef TEST
2280
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002281#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002282
2283const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002284
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002285int main(int argc, char **argv)
2286{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00002287 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002288 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00002289#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002290 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
2291 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
2292 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002293#else
2294 "% ";
2295#endif
2296
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002297#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002298 setlocale(LC_ALL, "");
2299#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002300 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00002301 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002302 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002303 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00002304 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002305 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002306 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00002307 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002308 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002309 return 0;
2310}
2311
Eric Andersenc470f442003-07-28 09:56:35 +00002312#endif /* TEST */