blob: 2e1bc5d2ef0a133d9f8a8a48a18b44faf58661e1 [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
143
144 /* Formerly these were big buffers on stack: */
145#if ENABLE_FEATURE_TAB_COMPLETION
146 char exe_n_cwd_tab_completion__dirbuf[MAX_LINELEN];
147 char input_tab__matchBuf[MAX_LINELEN];
148 int16_t find_match__int_buf[MAX_LINELEN + 1]; /* need to have 9 bits at least */
149 int16_t find_match__pos_buf[MAX_LINELEN + 1];
150#endif
151};
152
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000153/* See lineedit_ptr_hack.c */
154extern struct lineedit_statics *const lineedit_ptr_to_statics;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000155
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000156#define S (*lineedit_ptr_to_statics)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000157#define state (S.state )
158#define cmdedit_termw (S.cmdedit_termw )
159#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
160#define cmdedit_x (S.cmdedit_x )
161#define cmdedit_y (S.cmdedit_y )
162#define cmdedit_prmt_len (S.cmdedit_prmt_len)
163#define cursor (S.cursor )
164#define command_len (S.command_len )
165#define command_ps (S.command_ps )
166#define cmdedit_prompt (S.cmdedit_prompt )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000167#define num_ok_lines (S.num_ok_lines )
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000168#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000169#define home_pwd_buf (S.home_pwd_buf )
170#define matches (S.matches )
171#define num_matches (S.num_matches )
172#define delptr (S.delptr )
173#define newdelflag (S.newdelflag )
174#define delbuf (S.delbuf )
175
176#define INIT_S() do { \
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000177 (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000178 barrier(); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000179 cmdedit_termw = 80; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000180 IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
181 IF_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000182} while (0)
183static void deinit_S(void)
184{
185#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000186 /* This one is allocated only if FANCY_PROMPT is on
187 * (otherwise it points to verbatim prompt (NOT malloced) */
188 free((char*)cmdedit_prompt);
189#endif
190#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
191 free(user_buf);
192 if (home_pwd_buf != null_str)
193 free(home_pwd_buf);
194#endif
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000195 free(lineedit_ptr_to_statics);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000196}
197#define DEINIT_S() deinit_S()
198
Denis Vlasenko2c844952008-04-25 18:44:35 +0000199
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200200#if ENABLE_FEATURE_ASSUME_UNICODE
201static size_t load_string(const char *src, int maxsize)
202{
203 ssize_t len = mbstowcs(command_ps, src, maxsize - 1);
204 if (len < 0)
205 len = 0;
206 command_ps[len] = L'\0';
207 return len;
208}
209static size_t save_string(char *dst, int maxsize)
210{
211 ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
212 if (len < 0)
213 len = 0;
214 dst[len] = '\0';
215 return len;
216}
217/* I thought just fputwc(c, stdout) would work. But no... */
218static void BB_PUTCHAR(wchar_t c)
219{
220 char buf[MB_CUR_MAX + 1];
221 mbstate_t mbst = { 0 };
222 ssize_t len = wcrtomb(buf, c, &mbst);
223
224 if (len > 0) {
225 buf[len] = '\0';
226 fputs(buf, stdout);
227 }
228}
229#else
230static size_t load_string(const char *src, int maxsize)
231{
232 safe_strncpy(command_ps, src, maxsize);
233 return strlen(command_ps);
234}
Denys Vlasenko9038d6f2009-07-15 20:02:19 +0200235# if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200236static void save_string(char *dst, int maxsize)
237{
238 safe_strncpy(dst, command_ps, maxsize);
239}
Denys Vlasenko7dd0ce42009-07-15 18:27:47 +0200240# endif
241# define BB_PUTCHAR(c) bb_putchar(c)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200242#endif
243
244
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000245/* Put 'command_ps[cursor]', cursor++.
246 * Advance cursor on screen. If we reached right margin, scroll text up
247 * and remove terminal margin effect by printing 'next_char' */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000248#define HACK_FOR_WRONG_WIDTH 1
249#if HACK_FOR_WRONG_WIDTH
250static void cmdedit_set_out_char(void)
251#define cmdedit_set_out_char(next_char) cmdedit_set_out_char()
252#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000253static void cmdedit_set_out_char(int next_char)
Denis Vlasenko2c844952008-04-25 18:44:35 +0000254#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000255{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200256 CHAR_T c = command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000257
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200258 if (c == BB_NUL) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000259 /* erase character after end of input string */
260 c = ' ';
261 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000262#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000263 /* Display non-printable characters in reverse */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200264 if (!BB_isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000265 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000266 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000267 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000268 c += '@';
269 if (c == 127)
270 c = '?';
271 printf("\033[7m%c\033[0m", c);
272 } else
273#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000274 {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200275 BB_PUTCHAR(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000276 }
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000277 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000278 /* terminal is scrolled down */
279 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000280 cmdedit_x = 0;
Denis Vlasenko2c844952008-04-25 18:44:35 +0000281#if HACK_FOR_WRONG_WIDTH
282 /* This works better if our idea of term width is wrong
283 * and it is actually wider (often happens on serial lines).
284 * Printing CR,LF *forces* cursor to next line.
285 * OTOH if terminal width is correct AND terminal does NOT
286 * have automargin (IOW: it is moving cursor to next line
287 * by itself (which is wrong for VT-10x terminals)),
288 * this will break things: there will be one extra empty line */
289 puts("\r"); /* + implicit '\n' */
290#else
291 /* Works ok only if cmdedit_termw is correct */
Mark Whitley4e338752001-01-26 20:42:23 +0000292 /* destroy "(auto)margin" */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000293 bb_putchar(next_char);
294 bb_putchar('\b');
Denis Vlasenko2c844952008-04-25 18:44:35 +0000295#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000296 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200297// Huh? What if command_ps[cursor] == BB_NUL (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000298 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000299}
300
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000301/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000302static void input_end(void)
303{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000304 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000305 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000306}
307
308/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000309static void goto_new_line(void)
310{
Mark Whitley4e338752001-01-26 20:42:23 +0000311 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000312 if (cmdedit_x)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000313 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000314}
315
316
Rob Landley88621d72006-08-29 19:41:06 +0000317static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000318{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000319 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000320 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000321}
Eric Andersen81fe1232003-07-29 06:38:40 +0000322
Rob Landley88621d72006-08-29 19:41:06 +0000323static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000324{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000325 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000326}
327
Eric Andersenaff114c2004-04-14 17:51:38 +0000328/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000329/* (optimized for slow terminals) */
330static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000331{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000332 int count_y;
333
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000334 if (num > cursor)
335 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000336 if (!num)
337 return;
338 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000339
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000340 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000341 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000342 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000343 /* This is longer by 5 bytes on x86.
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000344 * Also gets miscompiled for ARM users
345 * (busybox.net/bugs/view.php?id=2274).
Denis Vlasenko6f043912008-02-18 22:28:03 +0000346 * printf(("\b\b\b\b" + 4) - num);
347 * return;
348 */
349 do {
350 bb_putchar('\b');
351 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000352 return;
353 }
354 printf("\033[%uD", num);
355 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000356 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000357
358 /* Need to go one or more lines up */
359 num -= cmdedit_x;
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000360 {
361 unsigned w = cmdedit_termw; /* volatile var */
362 count_y = 1 + (num / w);
363 cmdedit_y -= count_y;
364 cmdedit_x = w * count_y - num;
365 }
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000366 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000367 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000368}
369
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000370static void put_prompt(void)
371{
372 out1str(cmdedit_prompt);
Denys Vlasenko727e1b52009-10-26 15:23:32 +0100373 fflush(NULL);
Denys Vlasenkoc396fe62009-05-17 19:28:14 +0200374 if (ENABLE_FEATURE_EDITING_ASK_TERMINAL) {
375 /* Ask terminal where is the cursor now.
376 * lineedit_read_key handles response and corrects
377 * our idea of current cursor position.
378 * Testcase: run "echo -n long_line_long_line_long_line",
379 * then type in a long, wrapping command and try to
380 * delete it using backspace key.
381 * Note: we print it _after_ prompt, because
382 * prompt may contain CR. Example: PS1='\[\r\n\]\w '
383 */
Denys Vlasenko727e1b52009-10-26 15:23:32 +0100384 /* Problem: if there is buffered input on stdin,
385 * the response will be delivered later,
386 * possibly to an unsuspecting application.
387 * Testcase: "sleep 1; busybox ash" + press and hold [Enter].
388 * Result:
389 * ~/srcdevel/bbox/fix/busybox.t4 #
390 * ~/srcdevel/bbox/fix/busybox.t4 #
391 * ^[[59;34~/srcdevel/bbox/fix/busybox.t4 # <-- garbage
392 * ~/srcdevel/bbox/fix/busybox.t4 #
393 *
394 * Checking for input with poll only makes the race narrower,
395 * I still can trigger it. Strace:
396 *
397 * write(1, "~/srcdevel/bbox/fix/busybox.t4 # ", 33) = 33
398 * poll([{fd=0, events=POLLIN}], 1, 0) = 0 (Timeout) <-- no input exists
399 * write(1, "\33[6n", 4) = 4 <-- send the ESC sequence, quick!
400 * poll([{fd=0, events=POLLIN}], 1, 4294967295) = 1 ([{fd=0, revents=POLLIN}])
401 * read(0, "\n", 1) = 1 <-- oh crap, user's input got in first
402 */
403 struct pollfd pfd;
404
405 pfd.fd = STDIN_FILENO;
406 pfd.events = POLLIN;
407 if (safe_poll(&pfd, 1, 0) == 0) {
408 out1str("\033" "[6n");
409 fflush(NULL); /* make terminal see it ASAP! */
410 }
Denys Vlasenkoc396fe62009-05-17 19:28:14 +0200411 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000412 cursor = 0;
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000413 {
414 unsigned w = cmdedit_termw; /* volatile var */
415 cmdedit_y = cmdedit_prmt_len / w; /* new quasireal y */
416 cmdedit_x = cmdedit_prmt_len % w;
417 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000418}
419
Eric Andersenaff114c2004-04-14 17:51:38 +0000420/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000421static void redraw(int y, int back_cursor)
422{
Denys Vlasenko044b1802009-07-12 02:50:35 +0200423 if (y > 0) /* up to start y */
424 printf("\033[%uA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000425 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000426 put_prompt();
Denys Vlasenko044b1802009-07-12 02:50:35 +0200427 input_end(); /* rewrite */
428 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000429 input_backward(back_cursor);
430}
431
Paul Fox3f11b1b2005-08-04 19:04:46 +0000432/* Delete the char in front of the cursor, optionally saving it
433 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000434#if !ENABLE_FEATURE_EDITING_VI
435static void input_delete(void)
436#define input_delete(save) input_delete()
437#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000438static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000439#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000440{
Mark Whitley4e338752001-01-26 20:42:23 +0000441 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000442
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000443 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000444 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000445
Denis Vlasenko38f63192007-01-22 09:03:07 +0000446#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000447 if (save) {
448 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000449 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000450 newdelflag = 0;
451 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000452 if ((delptr - delbuf) < DELBUFSIZ)
453 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000454 }
455#endif
456
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200457 memmove(command_ps + j, command_ps + j + 1,
Denys Vlasenko9531f7d2009-07-16 14:33:16 +0200458 /* (command_len + 1 [because of NUL]) - (j + 1)
459 * simplified into (command_len - j) */
460 (command_len - j) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000461 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000462 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000463 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000464 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000465}
466
Denis Vlasenko38f63192007-01-22 09:03:07 +0000467#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000468static void put(void)
469{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000470 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000471 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000472
Paul Fox3f11b1b2005-08-04 19:04:46 +0000473 if (j == 0)
474 return;
475 ocursor = cursor;
476 /* open hole and then fill it */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200477 memmove(command_ps + cursor + j, command_ps + cursor,
478 (command_len - cursor + 1) * sizeof(command_ps[0]));
479 memcpy(command_ps + cursor, delbuf, j * sizeof(command_ps[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000480 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000481 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000482 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000483}
484#endif
485
Mark Whitley4e338752001-01-26 20:42:23 +0000486/* Delete the char in back of the cursor */
487static void input_backspace(void)
488{
489 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000490 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000491 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000492 }
493}
494
Eric Andersenaff114c2004-04-14 17:51:38 +0000495/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000496static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000497{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000498 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000499 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000500}
501
Denis Vlasenko38f63192007-01-22 09:03:07 +0000502#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000503
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000504static void free_tab_completion_data(void)
505{
506 if (matches) {
507 while (num_matches)
508 free(matches[--num_matches]);
509 free(matches);
510 matches = NULL;
511 }
512}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000513
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000514static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000515{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000516 matches = xrealloc_vector(matches, 4, num_matches);
517 matches[num_matches] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000518 num_matches++;
519}
520
Denis Vlasenko38f63192007-01-22 09:03:07 +0000521#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000522static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000523{
524 struct passwd *entry;
525 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000526
Eric Andersenc470f442003-07-28 09:56:35 +0000527 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000528 userlen = strlen(ud);
529
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000530 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000531 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000532 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000533
Eric Andersenc470f442003-07-28 09:56:35 +0000534 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000535 home = home_pwd_buf;
536 } else {
537 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000538 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000539 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000540 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000541 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000542 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000543 ud = temp;
544 if (entry)
545 home = entry->pw_dir;
546 }
547 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000548 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000549 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000550 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000551 }
552 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000553 } else {
554 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000555 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000556 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000557 struct passwd pwd;
558 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000559
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000560 setpwent();
561 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000562 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000563 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
564 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000565 }
566 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000567 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000568 }
569}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000570#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000571
572enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000573 FIND_EXE_ONLY = 0,
574 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000575 FIND_FILE_ONLY = 2,
576};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000577
Mark Whitley4e338752001-01-26 20:42:23 +0000578static int path_parse(char ***p, int flags)
579{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000580 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000581 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000582 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000583 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000584
Mark Whitley4e338752001-01-26 20:42:23 +0000585 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000586 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000587 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000588
589 if (state->flags & WITH_PATH_LOOKUP)
590 pth = state->path_lookup;
591 else
592 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000593 /* PATH=<empty> or PATH=:<empty> */
594 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
595 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000596
Denis Vlasenko253ce002007-01-22 08:34:44 +0000597 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000598 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000599 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000600 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000601 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000602 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000603 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000604 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000605 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000606 }
607
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000608 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000609 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000610 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000611 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000612 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000613 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000614 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000615 *tmp++ = '\0'; /* ':' -> '\0' */
616 if (*tmp == '\0')
617 break; /* :<empty> */
618 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000619 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000620 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000621 return npth;
622}
623
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000624static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000625{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000626 DIR *dir;
627 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000628 struct stat st;
629 char *path1[1];
630 char **paths = path1;
631 int npaths;
632 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000633 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000634 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000635/* char dirbuf[MAX_LINELEN]; */
636#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000637
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000638 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000639 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000640
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000641 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000642 /* no dir, if flags==EXE_ONLY - get paths, else "." */
643 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000644 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000645 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000646 /* dirbuf = ".../.../.../" */
647 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000648#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000649 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000650 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000651#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000652 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000653 /* point to 'l' in "..../last_component" */
654 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000655 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000656
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000657 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000658 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000659 if (!dir)
660 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000661
662 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000663 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000664 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000665
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000666 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000667 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000668 continue;
669 /* not see .name without .match */
Denis Vlasenkob5202712008-04-24 04:42:52 +0000670 if (*str_found == '.' && *pfind == '\0') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000671 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000672 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000673 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000674 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000675 found = concat_path_file(paths[i], str_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000676 /* hmm, remove in progress? */
677 /* NB: stat() first so that we see is it a directory;
678 * but if that fails, use lstat() so that
679 * we still match dangling links */
680 if (stat(found, &st) && lstat(found, &st))
Eric Andersene5dfced2001-04-09 22:48:12 +0000681 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000682 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000683 if (paths[i] != dirbuf)
Denis Vlasenkob5202712008-04-24 04:42:52 +0000684 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000685
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000686 len1 = strlen(found);
687 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000688 found[len1] = '\0';
689 found[len1+1] = '\0';
690
Mark Whitley4e338752001-01-26 20:42:23 +0000691 if (S_ISDIR(st.st_mode)) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000692 /* name is a directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000693 if (found[len1-1] != '/') {
694 found[len1] = '/';
695 }
Mark Whitley4e338752001-01-26 20:42:23 +0000696 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000697 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000698 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000699 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000700 }
Mark Whitley4e338752001-01-26 20:42:23 +0000701 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000702 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000703 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000704 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000705 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000706 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000707 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000708 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000709 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000710 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000711 free(paths);
712 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000713#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000714}
Erik Andersenf0657d32000-04-12 17:49:52 +0000715
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200716/* QUOT is used on elements of int_buf[], which are bytes,
717 * not Unicode chars. Therefore it works correctly even in Unicode mode.
718 */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000719#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000720
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200721#define int_buf (S.find_match__int_buf)
722#define pos_buf (S.find_match__pos_buf)
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +0200723/* is must be <= in */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200724static void collapse_pos(int is, int in)
725{
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +0200726 memmove(int_buf+is, int_buf+in, (MAX_LINELEN+1-in)*sizeof(int_buf[0]));
727 memmove(pos_buf+is, pos_buf+in, (MAX_LINELEN+1-in)*sizeof(pos_buf[0]));
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200728}
Denys Vlasenko044b1802009-07-12 02:50:35 +0200729static NOINLINE int find_match(char *matchBuf, int *len_with_quotes)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000730{
731 int i, j;
732 int command_mode;
733 int c, c2;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200734/* Were local, but it uses too much stack */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000735/* int16_t int_buf[MAX_LINELEN + 1]; */
736/* int16_t pos_buf[MAX_LINELEN + 1]; */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000737
738 /* set to integer dimension characters and own positions */
739 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000740 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000741 if (int_buf[i] == 0) {
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200742 pos_buf[i] = -1; /* end-fo-line indicator */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000743 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000744 }
745 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000746 }
747
748 /* mask \+symbol and convert '\t' to ' ' */
749 for (i = j = 0; matchBuf[i]; i++, j++)
750 if (matchBuf[i] == '\\') {
751 collapse_pos(j, j + 1);
752 int_buf[j] |= QUOT;
753 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000754#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200755 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000756 int_buf[j] = ' ' | QUOT;
757#endif
758 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000759#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000760 else if (matchBuf[i] == '\t')
761 int_buf[j] = ' ';
762#endif
763
764 /* mask "symbols" or 'symbols' */
765 c2 = 0;
766 for (i = 0; int_buf[i]; i++) {
767 c = int_buf[i];
768 if (c == '\'' || c == '"') {
769 if (c2 == 0)
770 c2 = c;
771 else {
772 if (c == c2)
773 c2 = 0;
774 else
775 int_buf[i] |= QUOT;
776 }
777 } else if (c2 != 0 && c != '$')
778 int_buf[i] |= QUOT;
779 }
780
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000781 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000782 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
783 for (i = 0; int_buf[i]; i++) {
784 c = int_buf[i];
785 c2 = int_buf[i + 1];
786 j = i ? int_buf[i - 1] : -1;
787 command_mode = 0;
788 if (c == ';' || c == '&' || c == '|') {
789 command_mode = 1 + (c == c2);
790 if (c == '&') {
791 if (j == '>' || j == '<')
792 command_mode = 0;
793 } else if (c == '|' && j == '>')
794 command_mode = 0;
795 }
796 if (command_mode) {
797 collapse_pos(0, i + command_mode);
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200798 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000799 }
800 }
801 /* collapse `command...` */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200802 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000803 if (int_buf[i] == '`') {
804 for (j = i + 1; int_buf[j]; j++)
805 if (int_buf[j] == '`') {
806 collapse_pos(i, j + 1);
807 j = 0;
808 break;
809 }
810 if (j) {
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200811 /* not found closing ` - command mode, collapse all previous */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000812 collapse_pos(0, i + 1);
813 break;
814 } else
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200815 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000816 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200817 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000818
819 /* collapse (command...(command...)...) or {command...{command...}...} */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200820 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000821 c2 = 0;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200822 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000823 if (int_buf[i] == '(' || int_buf[i] == '{') {
824 if (int_buf[i] == '(')
825 c++;
826 else
827 c2++;
828 collapse_pos(0, i + 1);
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200829 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000830 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200831 }
832 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000833 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
834 if (int_buf[i] == ')')
835 c--;
836 else
837 c2--;
838 collapse_pos(0, i + 1);
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200839 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000840 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200841 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000842
843 /* skip first not quote space */
844 for (i = 0; int_buf[i]; i++)
845 if (int_buf[i] != ' ')
846 break;
847 if (i)
848 collapse_pos(0, i);
849
850 /* set find mode for completion */
851 command_mode = FIND_EXE_ONLY;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200852 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000853 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
854 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000855 && matchBuf[pos_buf[0]] == 'c'
856 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000857 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000858 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000859 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000860 command_mode = FIND_FILE_ONLY;
861 break;
862 }
863 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200864 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000865 for (i = 0; int_buf[i]; i++)
866 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000867 /* find last word */
868 for (--i; i >= 0; i--) {
869 c = int_buf[i];
870 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
871 collapse_pos(0, i + 1);
872 break;
873 }
874 }
875 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000876 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
877 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000878 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000879 while ((int_buf[i] & ~QUOT) == '/'
880 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
881 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000882 i++;
883 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000884
885 /* set only match and destroy quotes */
886 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000887 for (c = 0; pos_buf[i] >= 0; i++) {
888 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000889 j = pos_buf[i] + 1;
890 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000891 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000892 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000893 *len_with_quotes = j ? j - pos_buf[0] : 0;
894
895 return command_mode;
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +0200896}
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000897#undef int_buf
898#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000899
Glenn L McGrath4d001292003-01-06 01:11:50 +0000900/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000901 * display by column (original idea from ls applet,
902 * very optimized by me :)
903 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000904static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000905{
906 int ncols, row;
907 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000908 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000909 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000910 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000911
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200912 /* find the longest file name - use that as the column width */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000913 for (row = 0; row < nrows; row++) {
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200914 l = bb_mbstrlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000915 if (column_width < l)
916 column_width = l;
917 }
918 column_width += 2; /* min space for columns */
919 ncols = cmdedit_termw / column_width;
920
921 if (ncols > 1) {
922 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000923 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000924 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000925 } else {
926 ncols = 1;
927 }
928 for (row = 0; row < nrows; row++) {
929 int n = row;
930 int nc;
931
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000932 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000933 printf("%s%-*s", matches[n],
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200934 (int)(column_width - bb_mbstrlen(matches[n])), ""
935 );
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000936 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000937 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000938 }
939}
940
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000941static char *add_quote_for_spec_chars(char *found)
942{
943 int l = 0;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200944 char *s = xzalloc((strlen(found) + 1) * 2);
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000945
946 while (*found) {
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +0200947 if (strchr(" `\"#$%^&*()=+{}[]:;'|\\<>", *found))
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000948 s[l++] = '\\';
949 s[l++] = *found++;
950 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200951 /* s[l] = '\0'; - already is */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000952 return s;
953}
954
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000955/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000956static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000957{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000958 if (!(state->flags & TAB_COMPLETION))
959 return;
960
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000961 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000962 char *tmp, *tmp1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000963 size_t len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000964/* char matchBuf[MAX_LINELEN]; */
965#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000966 int find_type;
967 int recalc_pos;
Denys Vlasenko044b1802009-07-12 02:50:35 +0200968#if ENABLE_FEATURE_ASSUME_UNICODE
969 /* cursor pos in command converted to multibyte form */
970 int cursor_mb;
971#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000972
Eric Andersenc470f442003-07-28 09:56:35 +0000973 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000974
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200975 /* Make a local copy of the string --
976 * up to the position of the cursor */
977 save_string(matchBuf, cursor + 1);
Denys Vlasenko044b1802009-07-12 02:50:35 +0200978#if ENABLE_FEATURE_ASSUME_UNICODE
979 cursor_mb = strlen(matchBuf);
980#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200981 tmp = matchBuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000982
983 find_type = find_match(matchBuf, &recalc_pos);
984
985 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000986 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000987
Denis Vlasenko38f63192007-01-22 09:03:07 +0000988#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000989 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000990 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000991 if (state->flags & USERNAME_COMPLETION)
Denys Vlasenko044b1802009-07-12 02:50:35 +0200992 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == NULL)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000993 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000994#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000995 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000996 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000997 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000998 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000999 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001000 if (matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001001 unsigned i;
1002 int n = 0;
Denis Vlasenkofb290382008-03-02 12:51:26 +00001003 qsort_string_vector(matches, num_matches);
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001004 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001005 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001006 if (strcmp(matches[i], matches[i+1]) == 0) {
1007 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001008 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001009 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001010 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +00001011 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001012 }
Denis Vlasenko92758142006-10-03 19:56:34 +00001013 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001014 matches[n] = matches[i];
1015 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001016 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001017 /* Did we find exactly one match? */
Denys Vlasenko044b1802009-07-12 02:50:35 +02001018 if (!matches || num_matches > 1) { /* no */
Mark Whitley4e338752001-01-26 20:42:23 +00001019 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001020 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +00001021 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001022 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001023 tmp1 = xstrdup(matches[0]);
Denys Vlasenko044b1802009-07-12 02:50:35 +02001024 for (tmp = tmp1; *tmp; tmp++) {
1025 for (len_found = 1; len_found < num_matches; len_found++) {
1026 if (matches[len_found][tmp - tmp1] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001027 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001028 break;
1029 }
Denys Vlasenko044b1802009-07-12 02:50:35 +02001030 }
1031 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001032 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001033 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001034 return;
1035 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001036 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001037 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +00001038 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001039 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001040 /* for next completion current found */
1041 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001042
1043 len_found = strlen(tmp);
1044 if (tmp[len_found-1] != '/') {
1045 tmp[len_found] = ' ';
1046 tmp[len_found+1] = '\0';
1047 }
Mark Whitley4e338752001-01-26 20:42:23 +00001048 }
Denys Vlasenko044b1802009-07-12 02:50:35 +02001049
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001050 len_found = strlen(tmp);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001051#if !ENABLE_FEATURE_ASSUME_UNICODE
Denys Vlasenko044b1802009-07-12 02:50:35 +02001052 /* have space to place the match? */
1053 /* The result consists of three parts with these lengths: */
1054 /* (cursor - recalc_pos) + len_found + (command_len - cursor) */
1055 /* it simplifies into: */
1056 if ((int)(len_found + command_len - recalc_pos) < S.maxsize) {
1057 /* save tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001058 strcpy(matchBuf, command_ps + cursor);
Denys Vlasenko044b1802009-07-12 02:50:35 +02001059 /* add match and tail */
1060 sprintf(&command_ps[cursor - recalc_pos], "%s%s", tmp, matchBuf);
Denis Vlasenko253ce002007-01-22 08:34:44 +00001061 command_len = strlen(command_ps);
Denys Vlasenko044b1802009-07-12 02:50:35 +02001062 /* new pos */
1063 recalc_pos = cursor - recalc_pos + len_found;
1064 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +00001065 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001066 }
Denys Vlasenko044b1802009-07-12 02:50:35 +02001067#else
1068 {
1069 char command[MAX_LINELEN];
1070 int len = save_string(command, sizeof(command));
1071 /* have space to place the match? */
1072 /* (cursor_mb - recalc_pos) + len_found + (len - cursor_mb) */
1073 if ((int)(len_found + len - recalc_pos) < MAX_LINELEN) {
1074 /* save tail */
1075 strcpy(matchBuf, command + cursor_mb);
1076 /* where do we want to have cursor after all? */
1077 strcpy(&command[cursor_mb - recalc_pos], tmp);
1078 len = load_string(command, S.maxsize);
1079 /* add match and tail */
1080 sprintf(&command[cursor_mb - recalc_pos], "%s%s", tmp, matchBuf);
1081 command_len = load_string(command, S.maxsize);
1082 /* write out the matched command */
1083 redraw(cmdedit_y, command_len - len);
1084 }
1085 }
1086#endif
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001087 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001088#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +00001089 } else {
1090 /* Ok -- the last char was a TAB. Since they
1091 * just hit TAB again, print a list of all the
1092 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001093 if (matches && num_matches > 0) {
Denys Vlasenko044b1802009-07-12 02:50:35 +02001094 /* changed by goto_new_line() */
1095 int sav_cursor = cursor;
Erik Andersenf0657d32000-04-12 17:49:52 +00001096
1097 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001098 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001099 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001100 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001101 }
1102 }
1103}
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001104
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001105#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001106
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001107
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001108line_input_t* FAST_FUNC new_line_input_t(int flags)
1109{
1110 line_input_t *n = xzalloc(sizeof(*n));
1111 n->flags = flags;
1112 return n;
1113}
1114
1115
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001116#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001117
Denis Vlasenko682ad302008-09-27 01:28:56 +00001118static void save_command_ps_at_cur_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001119{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001120 if (command_ps[0] != BB_NUL) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001121 int cur = state->cur_history;
1122 free(state->history[cur]);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001123
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001124# if ENABLE_FEATURE_ASSUME_UNICODE
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001125 {
1126 char tbuf[MAX_LINELEN];
1127 save_string(tbuf, sizeof(tbuf));
1128 state->history[cur] = xstrdup(tbuf);
1129 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001130# else
Denis Vlasenko682ad302008-09-27 01:28:56 +00001131 state->history[cur] = xstrdup(command_ps);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001132# endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001133 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001134}
1135
1136/* state->flags is already checked to be nonzero */
1137static int get_previous_history(void)
1138{
1139 if ((state->flags & DO_HISTORY) && state->cur_history) {
1140 save_command_ps_at_cur_history();
1141 state->cur_history--;
1142 return 1;
1143 }
1144 beep();
1145 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001146}
1147
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001148static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001149{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001150 if (state->flags & DO_HISTORY) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001151 if (state->cur_history < state->cnt_history) {
1152 save_command_ps_at_cur_history(); /* save the current history line */
1153 return ++state->cur_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001154 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001155 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001156 beep();
1157 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001158}
Robert Griebl350d26b2002-12-03 22:45:46 +00001159
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001160# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001161/* We try to ensure that concurrent additions to the history
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001162 * do not overwrite each other.
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001163 * Otherwise shell users get unhappy.
1164 *
1165 * History file is trimmed lazily, when it grows several times longer
1166 * than configured MAX_HISTORY lines.
1167 */
1168
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001169static void free_line_input_t(line_input_t *n)
1170{
1171 int i = n->cnt_history;
1172 while (i > 0)
1173 free(n->history[--i]);
1174 free(n);
1175}
1176
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001177/* state->flags is already checked to be nonzero */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001178static void load_history(line_input_t *st_parm)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001179{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001180 char *temp_h[MAX_HISTORY];
1181 char *line;
Robert Griebl350d26b2002-12-03 22:45:46 +00001182 FILE *fp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001183 unsigned idx, i, line_len;
Robert Griebl350d26b2002-12-03 22:45:46 +00001184
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001185 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +00001186
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001187 fp = fopen_for_read(st_parm->hist_file);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001188 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001189 /* clean up old history */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001190 for (idx = st_parm->cnt_history; idx > 0;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001191 idx--;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001192 free(st_parm->history[idx]);
1193 st_parm->history[idx] = NULL;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001194 }
1195
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001196 /* fill temp_h[], retaining only last MAX_HISTORY lines */
1197 memset(temp_h, 0, sizeof(temp_h));
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001198 st_parm->cnt_history_in_file = idx = 0;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001199 while ((line = xmalloc_fgetline(fp)) != NULL) {
1200 if (line[0] == '\0') {
1201 free(line);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001202 continue;
1203 }
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001204 free(temp_h[idx]);
1205 temp_h[idx] = line;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001206 st_parm->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001207 idx++;
1208 if (idx == MAX_HISTORY)
1209 idx = 0;
Robert Griebl350d26b2002-12-03 22:45:46 +00001210 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001211 fclose(fp);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001212
1213 /* find first non-NULL temp_h[], if any */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001214 if (st_parm->cnt_history_in_file) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001215 while (temp_h[idx] == NULL) {
1216 idx++;
1217 if (idx == MAX_HISTORY)
1218 idx = 0;
1219 }
1220 }
1221
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001222 /* copy temp_h[] to st_parm->history[] */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001223 for (i = 0; i < MAX_HISTORY;) {
1224 line = temp_h[idx];
1225 if (!line)
1226 break;
1227 idx++;
1228 if (idx == MAX_HISTORY)
1229 idx = 0;
1230 line_len = strlen(line);
1231 if (line_len >= MAX_LINELEN)
1232 line[MAX_LINELEN-1] = '\0';
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001233 st_parm->history[i++] = line;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001234 }
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001235 st_parm->cnt_history = i;
Robert Griebl350d26b2002-12-03 22:45:46 +00001236 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001237}
1238
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001239/* state->flags is already checked to be nonzero */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001240static void save_history(char *str)
Robert Griebl350d26b2002-12-03 22:45:46 +00001241{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001242 int fd;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001243 int len, len2;
Eric Andersenc470f442003-07-28 09:56:35 +00001244
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001245 fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0666);
1246 if (fd < 0)
1247 return;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001248 xlseek(fd, 0, SEEK_END); /* paranoia */
1249 len = strlen(str);
1250 str[len] = '\n'; /* we (try to) do atomic write */
1251 len2 = full_write(fd, str, len + 1);
1252 str[len] = '\0';
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001253 close(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001254 if (len2 != len + 1)
1255 return; /* "wtf?" */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001256
1257 /* did we write so much that history file needs trimming? */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001258 state->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001259 if (state->cnt_history_in_file > MAX_HISTORY * 4) {
1260 FILE *fp;
1261 char *new_name;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001262 line_input_t *st_temp;
1263 int i;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001264
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001265 /* we may have concurrently written entries from others.
1266 * load them */
1267 st_temp = new_line_input_t(state->flags);
1268 st_temp->hist_file = state->hist_file;
1269 load_history(st_temp);
1270
1271 /* write out temp file and replace hist_file atomically */
1272 new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001273 fp = fopen_for_write(new_name);
1274 if (fp) {
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001275 for (i = 0; i < st_temp->cnt_history; i++)
1276 fprintf(fp, "%s\n", st_temp->history[i]);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001277 fclose(fp);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001278 if (rename(new_name, state->hist_file) == 0)
1279 state->cnt_history_in_file = st_temp->cnt_history;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001280 }
1281 free(new_name);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001282 free_line_input_t(st_temp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001283 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001284}
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001285# else
1286# define load_history(a) ((void)0)
1287# define save_history(a) ((void)0)
1288# endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001289
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001290static void remember_in_history(char *str)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001291{
1292 int i;
1293
1294 if (!(state->flags & DO_HISTORY))
1295 return;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001296 if (str[0] == '\0')
1297 return;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001298 i = state->cnt_history;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001299 /* Don't save dupes */
1300 if (i && strcmp(state->history[i-1], str) == 0)
1301 return;
1302
1303 free(state->history[MAX_HISTORY]); /* redundant, paranoia */
1304 state->history[MAX_HISTORY] = NULL; /* redundant, paranoia */
1305
1306 /* If history[] is full, remove the oldest command */
1307 /* we need to keep history[MAX_HISTORY] empty, hence >=, not > */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001308 if (i >= MAX_HISTORY) {
1309 free(state->history[0]);
1310 for (i = 0; i < MAX_HISTORY-1; i++)
1311 state->history[i] = state->history[i+1];
Denis Vlasenko682ad302008-09-27 01:28:56 +00001312 /* i == MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001313 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001314 /* i <= MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001315 state->history[i++] = xstrdup(str);
Denis Vlasenko682ad302008-09-27 01:28:56 +00001316 /* i <= MAX_HISTORY */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001317 state->cur_history = i;
1318 state->cnt_history = i;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001319# if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001320 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001321 save_history(str);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001322# endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001323 IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001324}
1325
1326#else /* MAX_HISTORY == 0 */
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001327# define remember_in_history(a) ((void)0)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001328#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001329
1330
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001331#if ENABLE_FEATURE_EDITING_VI
Erik Andersen6273f652000-03-17 01:12:41 +00001332/*
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001333 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001334 */
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001335static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001336vi_Word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001337{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001338 CHAR_T *command = command_ps;
1339
1340 while (cursor < command_len && !BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001341 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001342 if (eat) while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001343 input_forward();
1344}
1345
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001346static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001347vi_word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001348{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001349 CHAR_T *command = command_ps;
1350
1351 if (BB_isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001352 while (cursor < command_len
Denys Vlasenko13028922009-07-12 00:51:15 +02001353 && (BB_isalnum(command[cursor+1]) || command[cursor+1] == '_')
1354 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001355 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001356 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001357 } else if (BB_ispunct(command[cursor])) {
1358 while (cursor < command_len && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001359 input_forward();
1360 }
1361
Denis Vlasenko253ce002007-01-22 08:34:44 +00001362 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001363 input_forward();
1364
Denys Vlasenko13028922009-07-12 00:51:15 +02001365 if (eat) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001366 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001367 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001368 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001369}
1370
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001371static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001372vi_End_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001373{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001374 CHAR_T *command = command_ps;
1375
Paul Fox3f11b1b2005-08-04 19:04:46 +00001376 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001377 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001378 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001379 while (cursor < command_len-1 && !BB_isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001380 input_forward();
1381}
1382
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001383static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001384vi_end_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001385{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001386 CHAR_T *command = command_ps;
1387
Denis Vlasenko253ce002007-01-22 08:34:44 +00001388 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001389 return;
1390 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001391 while (cursor < command_len-1 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001392 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001393 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001394 return;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001395 if (BB_isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001396 while (cursor < command_len-1
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_forward();
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 < command_len-1 && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001403 input_forward();
1404 }
1405}
1406
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001407static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001408vi_Back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001409{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001410 CHAR_T *command = command_ps;
1411
1412 while (cursor > 0 && BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001413 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001414 while (cursor > 0 && !BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001415 input_backward(1);
1416}
1417
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001418static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001419vi_back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001420{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001421 CHAR_T *command = command_ps;
1422
Paul Fox3f11b1b2005-08-04 19:04:46 +00001423 if (cursor <= 0)
1424 return;
1425 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001426 while (cursor > 0 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001427 input_backward(1);
1428 if (cursor <= 0)
1429 return;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001430 if (BB_isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001431 while (cursor > 0
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001432 && (BB_isalnum(command[cursor-1]) || command[cursor-1] == '_')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001433 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001434 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001435 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001436 } else if (BB_ispunct(command[cursor])) {
1437 while (cursor > 0 && BB_ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001438 input_backward(1);
1439 }
1440}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001441#endif
1442
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001443/* Modelled after bash 4.0 behavior of Ctrl-<arrow> */
1444static void ctrl_left(void)
1445{
1446 CHAR_T *command = command_ps;
1447
1448 while (1) {
1449 CHAR_T c;
1450
1451 input_backward(1);
1452 if (cursor == 0)
1453 break;
1454 c = command[cursor];
1455 if (c != ' ' && !BB_ispunct(c)) {
1456 /* we reached a "word" delimited by spaces/punct.
1457 * go to its beginning */
1458 while (1) {
1459 c = command[cursor - 1];
1460 if (c == ' ' || BB_ispunct(c))
1461 break;
1462 input_backward(1);
1463 if (cursor == 0)
1464 break;
1465 }
1466 break;
1467 }
1468 }
1469}
1470static void ctrl_right(void)
1471{
1472 CHAR_T *command = command_ps;
1473
1474 while (1) {
1475 CHAR_T c;
1476
1477 c = command[cursor];
1478 if (c == BB_NUL)
1479 break;
1480 if (c != ' ' && !BB_ispunct(c)) {
1481 /* we reached a "word" delimited by spaces/punct.
1482 * go to its end + 1 */
1483 while (1) {
1484 input_forward();
1485 c = command[cursor];
1486 if (c == BB_NUL || c == ' ' || BB_ispunct(c))
1487 break;
1488 }
1489 break;
1490 }
1491 input_forward();
1492 }
1493}
1494
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001495
1496/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001497 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001498 */
1499
Denis Vlasenko38f63192007-01-22 09:03:07 +00001500#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001501static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001502{
1503 cmdedit_prompt = prmt_ptr;
1504 cmdedit_prmt_len = strlen(prmt_ptr);
1505 put_prompt();
1506}
1507#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001508static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001509{
1510 int prmt_len = 0;
1511 size_t cur_prmt_len = 0;
1512 char flg_not_length = '[';
1513 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001514 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1515 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001516 char c;
1517 char *pbuf;
1518
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001519 cmdedit_prmt_len = 0;
1520
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001521 if (!cwd_buf) {
1522 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001523 }
1524
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001525 cbuf[1] = '\0'; /* never changes */
1526
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001527 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001528 char *free_me = NULL;
1529
1530 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001531 c = *prmt_ptr++;
1532 if (c == '\\') {
1533 const char *cp = prmt_ptr;
1534 int l;
1535
1536 c = bb_process_escape_sequence(&prmt_ptr);
1537 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001538 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001539 break;
1540 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001541
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001542 switch (c) {
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001543# if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001544 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001545 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001546 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001547# endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001548 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001549 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001550 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001551 break;
1552 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001553 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001554 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001555# if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001556 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001557 /* /home/user[/something] -> ~[/something] */
1558 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001559 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001560 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001561 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1562 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1563 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001564 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001565 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001566 }
1567 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001568# endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001569 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001570 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001571 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001572 if (cp != NULL && cp != pbuf)
1573 pbuf += (cp-pbuf) + 1;
1574 break;
1575 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001576 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001577 break;
1578 case 'e': case 'E': /* \e \E = \033 */
1579 c = '\033';
1580 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001581 case 'x': case 'X': {
1582 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001583 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001584 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001585 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001586 buf2[l] = '\0';
1587 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001588 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001589 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001590 break;
1591 }
1592 prmt_ptr++;
1593 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001594 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001595 if (c == 0)
1596 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001597 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001598 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001599 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001600 case '[': case ']':
1601 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001602 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001603 continue;
1604 }
1605 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001606 } /* switch */
1607 } /* if */
1608 } /* if */
1609 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001610 cur_prmt_len = strlen(pbuf);
1611 prmt_len += cur_prmt_len;
1612 if (flg_not_length != ']')
1613 cmdedit_prmt_len += cur_prmt_len;
1614 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001615 free(free_me);
1616 } /* while */
1617
1618 if (cwd_buf != (char *)bb_msg_unknown)
1619 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001620 cmdedit_prompt = prmt_mem_ptr;
1621 put_prompt();
1622}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001623#endif
1624
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001625static void cmdedit_setwidth(unsigned w, int redraw_flg)
1626{
1627 cmdedit_termw = w;
1628 if (redraw_flg) {
1629 /* new y for current cursor */
1630 int new_y = (cursor + cmdedit_prmt_len) / w;
1631 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001632 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denys Vlasenko727e1b52009-10-26 15:23:32 +01001633 fflush(NULL);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001634 }
1635}
1636
1637static void win_changed(int nsig)
1638{
Denis Vlasenko55995022008-05-18 22:28:26 +00001639 unsigned width;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001640 get_terminal_width_height(0, &width, NULL);
1641 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1642 if (nsig == SIGWINCH)
1643 signal(SIGWINCH, win_changed); /* rearm ourself */
1644}
1645
Denys Vlasenko020f4062009-05-17 16:44:54 +02001646static int lineedit_read_key(char *read_key_buffer)
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001647{
Denys Vlasenko020f4062009-05-17 16:44:54 +02001648 int64_t ic;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001649 struct pollfd pfd;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001650 int delay = -1;
1651#if ENABLE_FEATURE_ASSUME_UNICODE
1652 char unicode_buf[MB_CUR_MAX + 1];
1653 int unicode_idx = 0;
1654#endif
Denys Vlasenko020f4062009-05-17 16:44:54 +02001655
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001656 pfd.fd = STDIN_FILENO;
1657 pfd.events = POLLIN;
1658 do {
Denys Vlasenko020f4062009-05-17 16:44:54 +02001659 poll_again:
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +02001660 if (read_key_buffer[0] == 0) {
1661 /* Wait for input. Can't just call read_key,
1662 * it returns at once if stdin
1663 * is in non-blocking mode. */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001664 safe_poll(&pfd, 1, delay);
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +02001665 }
1666 /* Note: read_key sets errno to 0 on success: */
Denys Vlasenko020f4062009-05-17 16:44:54 +02001667 ic = read_key(STDIN_FILENO, read_key_buffer);
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02001668
Denys Vlasenko020f4062009-05-17 16:44:54 +02001669 if (ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenko727e1b52009-10-26 15:23:32 +01001670 && cursor == 0 /* otherwise it may be bogus */
Denys Vlasenko020f4062009-05-17 16:44:54 +02001671 && (int32_t)ic == KEYCODE_CURSOR_POS
1672 ) {
1673 int col = ((ic >> 32) & 0x7fff) - 1;
Denys Vlasenkoc396fe62009-05-17 19:28:14 +02001674 if (col > cmdedit_prmt_len) {
1675 cmdedit_x += (col - cmdedit_prmt_len);
Denys Vlasenko020f4062009-05-17 16:44:54 +02001676 while (cmdedit_x >= cmdedit_termw) {
1677 cmdedit_x -= cmdedit_termw;
1678 cmdedit_y++;
1679 }
1680 }
1681 goto poll_again;
1682 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001683
1684#if ENABLE_FEATURE_ASSUME_UNICODE
1685 {
1686 wchar_t wc;
1687
1688 if ((int32_t)ic < 0) /* KEYCODE_xxx */
1689 return ic;
1690 unicode_buf[unicode_idx++] = ic;
1691 unicode_buf[unicode_idx] = '\0';
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02001692 if (mbstowcs(&wc, unicode_buf, 1) != 1 && unicode_idx < MB_CUR_MAX) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001693 delay = 50;
1694 goto poll_again;
1695 }
1696 ic = wc;
1697 }
1698#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001699 } while (errno == EAGAIN);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001700
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001701 return ic;
1702}
1703
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001704/* leave out the "vi-mode"-only case labels if vi editing isn't
1705 * configured. */
Denys Vlasenko13028922009-07-12 00:51:15 +02001706#define vi_case(caselabel) IF_FEATURE_EDITING_VI(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001707
1708/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001709#undef CTRL
1710#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001711
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02001712/* maxsize must be >= 2.
1713 * Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001714 * -1 on read errors or EOF, or on bare Ctrl-D,
1715 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001716 * >0 length of input string, including terminating '\n'
1717 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001718int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001719{
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00001720 int len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001721#if ENABLE_FEATURE_TAB_COMPLETION
1722 smallint lastWasTab = FALSE;
1723#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001724 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001725#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001726 smallint vi_cmdmode = 0;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001727#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001728 struct termios initial_settings;
1729 struct termios new_settings;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001730 char read_key_buffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001731
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001732 INIT_S();
1733
1734 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1735 || !(initial_settings.c_lflag & ECHO)
1736 ) {
1737 /* Happens when e.g. stty -echo was run before */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001738 parse_and_put_prompt(prompt);
Denys Vlasenko727e1b52009-10-26 15:23:32 +01001739 /* fflush(stdout); - done by parse_and_put_prompt */
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001740 if (fgets(command, maxsize, stdin) == NULL)
1741 len = -1; /* EOF or error */
1742 else
1743 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001744 DEINIT_S();
1745 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001746 }
1747
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02001748 check_unicode_in_env();
1749
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001750// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001751 if (maxsize > MAX_LINELEN)
1752 maxsize = MAX_LINELEN;
Denys Vlasenko044b1802009-07-12 02:50:35 +02001753 S.maxsize = maxsize;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001754
1755 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001756 state = st ? st : (line_input_t*) &const_int_0;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001757#if MAX_HISTORY > 0
1758# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001759 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001760 if (state->cnt_history == 0)
1761 load_history(state);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001762# endif
Denis Vlasenko3c385cd2008-11-02 00:41:05 +00001763 if (state->flags & DO_HISTORY)
1764 state->cur_history = state->cnt_history;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001765#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001766
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001767 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001768 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001769 command_len = 0;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001770#if ENABLE_FEATURE_ASSUME_UNICODE
1771 command_ps = xzalloc(maxsize * sizeof(command_ps[0]));
1772#else
Mark Whitley4e338752001-01-26 20:42:23 +00001773 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001774 command[0] = '\0';
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001775#endif
1776#define command command_must_not_be_used
Mark Whitley4e338752001-01-26 20:42:23 +00001777
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001778 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001779 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1780 /* Turn off echoing and CTRL-C, so we can trap it */
1781 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001782 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001783 new_settings.c_cc[VMIN] = 1;
1784 new_settings.c_cc[VTIME] = 0;
1785 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001786#ifndef _POSIX_VDISABLE
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001787# define _POSIX_VDISABLE '\0'
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001788#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001789 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko202ac502008-11-05 13:20:58 +00001790 tcsetattr_stdin_TCSANOW(&new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001791
Eric Andersen6faae7d2001-02-16 20:09:17 +00001792 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001793 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1794 win_changed(0); /* do initial resizing */
1795#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1796 {
1797 struct passwd *entry;
1798
1799 entry = getpwuid(geteuid());
1800 if (entry) {
1801 user_buf = xstrdup(entry->pw_name);
1802 home_pwd_buf = xstrdup(entry->pw_dir);
1803 }
1804 }
1805#endif
Denis Vlasenko682ad302008-09-27 01:28:56 +00001806
1807#if 0
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001808 for (i = 0; i <= MAX_HISTORY; i++)
1809 bb_error_msg("history[%d]:'%s'", i, state->history[i]);
Denis Vlasenko682ad302008-09-27 01:28:56 +00001810 bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
1811#endif
1812
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001813 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001814 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001815
Denys Vlasenkoc396fe62009-05-17 19:28:14 +02001816 read_key_buffer[0] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001817 while (1) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001818 /*
1819 * The emacs and vi modes share much of the code in the big
1820 * command loop. Commands entered when in vi's command mode
1821 * (aka "escape mode") get an extra bit added to distinguish
1822 * them - this keeps them from being self-inserted. This
1823 * clutters the big switch a bit, but keeps all the code
1824 * in one place.
1825 */
1826 enum {
1827 VI_CMDMODE_BIT = 0x40000000,
1828 /* 0x80000000 bit flags KEYCODE_xxx */
1829 };
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02001830 int32_t ic, ic_raw;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001831
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001832 fflush(NULL);
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02001833 ic = ic_raw = lineedit_read_key(read_key_buffer);
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001834
Denis Vlasenko38f63192007-01-22 09:03:07 +00001835#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001836 newdelflag = 1;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001837 if (vi_cmdmode) {
1838 /* btw, since KEYCODE_xxx are all < 0, this doesn't
1839 * change ic if it contains one of them: */
1840 ic |= VI_CMDMODE_BIT;
1841 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001842#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001843
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001844 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001845 case '\n':
1846 case '\r':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001847 vi_case('\n'|VI_CMDMODE_BIT:)
1848 vi_case('\r'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001849 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001850 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001851 break_out = 1;
1852 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001853 case CTRL('A'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001854 vi_case('0'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001855 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001856 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001857 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001858 case CTRL('B'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001859 vi_case('h'|VI_CMDMODE_BIT:)
1860 vi_case('\b'|VI_CMDMODE_BIT:)
1861 vi_case('\x7f'|VI_CMDMODE_BIT:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001862 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001863 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001864 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001865 case CTRL('E'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001866 vi_case('$'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001867 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001868 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001869 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001870 case CTRL('F'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001871 vi_case('l'|VI_CMDMODE_BIT:)
1872 vi_case(' '|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001873 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001874 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001875 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001876 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001877 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001878 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001879 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001880 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001881#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001882 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001883 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001884 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001885#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001886 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001887 /* Control-k -- clear to end of line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001888 command_ps[cursor] = BB_NUL;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001889 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001890 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001891 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001892 case CTRL('L'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001893 vi_case(CTRL('L')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001894 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001895 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001896 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001897 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001898#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001899 case CTRL('N'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001900 vi_case(CTRL('N')|VI_CMDMODE_BIT:)
1901 vi_case('j'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001902 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001903 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001904 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001905 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001906 case CTRL('P'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001907 vi_case(CTRL('P')|VI_CMDMODE_BIT:)
1908 vi_case('k'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001909 /* Control-p -- Get previous command from history */
Denis Vlasenko682ad302008-09-27 01:28:56 +00001910 if (get_previous_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001911 goto rewrite_line;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001912 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001913#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001914 case CTRL('U'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001915 vi_case(CTRL('U')|VI_CMDMODE_BIT:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001916 /* Control-U -- Clear line before cursor */
1917 if (cursor) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001918 command_len -= cursor;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001919 memmove(command_ps, command_ps + cursor,
1920 (command_len + 1) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001921 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001922 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001923 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001924 case CTRL('W'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001925 vi_case(CTRL('W')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001926 /* Control-W -- Remove the last word */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001927 while (cursor > 0 && BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001928 input_backspace();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001929 while (cursor > 0 && !BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001930 input_backspace();
1931 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001932
Denis Vlasenko38f63192007-01-22 09:03:07 +00001933#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001934 case 'i'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001935 vi_cmdmode = 0;
1936 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001937 case 'I'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001938 input_backward(cursor);
1939 vi_cmdmode = 0;
1940 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001941 case 'a'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001942 input_forward();
1943 vi_cmdmode = 0;
1944 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001945 case 'A'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001946 input_end();
1947 vi_cmdmode = 0;
1948 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001949 case 'x'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001950 input_delete(1);
1951 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001952 case 'X'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001953 if (cursor > 0) {
1954 input_backward(1);
1955 input_delete(1);
1956 }
1957 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001958 case 'W'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001959 vi_Word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00001960 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001961 case 'w'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001962 vi_word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00001963 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001964 case 'E'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001965 vi_End_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00001966 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001967 case 'e'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001968 vi_end_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00001969 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001970 case 'B'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001971 vi_Back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00001972 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001973 case 'b'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001974 vi_back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00001975 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001976 case 'C'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001977 vi_cmdmode = 0;
1978 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001979 case 'D'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001980 goto clear_to_eol;
1981
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001982 case 'c'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001983 vi_cmdmode = 0;
1984 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001985 case 'd'|VI_CMDMODE_BIT: {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001986 int nc, sc;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001987
Denys Vlasenko020f4062009-05-17 16:44:54 +02001988 ic = lineedit_read_key(read_key_buffer);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001989 if (errno) /* error */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001990 goto prepare_to_die;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02001991 if (ic == ic_raw) { /* "cc", "dd" */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001992 input_backward(cursor);
1993 goto clear_to_eol;
1994 break;
1995 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02001996
1997 sc = cursor;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001998 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001999 case 'w':
2000 case 'W':
2001 case 'e':
2002 case 'E':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002003 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002004 case 'w': /* "dw", "cw" */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002005 vi_word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002006 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002007 case 'W': /* 'dW', 'cW' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002008 vi_Word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002009 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002010 case 'e': /* 'de', 'ce' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002011 vi_end_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002012 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002013 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002014 case 'E': /* 'dE', 'cE' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002015 vi_End_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002016 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002017 break;
2018 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002019 nc = cursor;
2020 input_backward(cursor - sc);
2021 while (nc-- > cursor)
2022 input_delete(1);
2023 break;
2024 case 'b': /* "db", "cb" */
2025 case 'B': /* implemented as B */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002026 if (ic == 'b')
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002027 vi_back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002028 else
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002029 vi_Back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002030 while (sc-- > cursor)
2031 input_delete(1);
2032 break;
2033 case ' ': /* "d ", "c " */
2034 input_delete(1);
2035 break;
2036 case '$': /* "d$", "c$" */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002037 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002038 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002039 input_delete(1);
2040 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002041 }
2042 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002043 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002044 case 'p'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002045 input_forward();
2046 /* fallthrough */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002047 case 'P'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002048 put();
2049 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002050 case 'r'|VI_CMDMODE_BIT:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002051//FIXME: unicode case?
Denys Vlasenko020f4062009-05-17 16:44:54 +02002052 ic = lineedit_read_key(read_key_buffer);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002053 if (errno) /* error */
Paul Fox3f11b1b2005-08-04 19:04:46 +00002054 goto prepare_to_die;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002055 if (ic < ' ' || ic > 255) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00002056 beep();
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002057 } else {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002058 command_ps[cursor] = ic;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002059 bb_putchar(ic);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002060 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00002061 }
2062 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002063 case '\x1b': /* ESC */
2064 if (state->flags & VI_MODE) {
2065 /* insert mode --> command mode */
2066 vi_cmdmode = 1;
2067 input_backward(1);
2068 }
2069 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002070#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002071
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002072#if MAX_HISTORY > 0
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002073 case KEYCODE_UP:
2074 if (get_previous_history())
2075 goto rewrite_line;
2076 beep();
2077 break;
2078 case KEYCODE_DOWN:
2079 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002080 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002081 rewrite_line:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002082 /* Rewrite the line with the selected history item */
2083 /* change command */
Denys Vlasenko90a99042009-09-06 02:36:23 +02002084 command_len = load_string(state->history[state->cur_history] ?
2085 state->history[state->cur_history] : "", maxsize);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002086 /* redraw and go to eol (bol, in vi) */
2087 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
2088 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002089#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002090 case KEYCODE_RIGHT:
2091 input_forward();
2092 break;
2093 case KEYCODE_LEFT:
2094 input_backward(1);
2095 break;
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002096 case KEYCODE_CTRL_LEFT:
2097 ctrl_left();
2098 break;
2099 case KEYCODE_CTRL_RIGHT:
2100 ctrl_right();
2101 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002102 case KEYCODE_DELETE:
2103 input_delete(0);
2104 break;
2105 case KEYCODE_HOME:
2106 input_backward(cursor);
2107 break;
2108 case KEYCODE_END:
2109 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002110 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002111
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002112 default:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002113 if (initial_settings.c_cc[VINTR] != 0
2114 && ic_raw == initial_settings.c_cc[VINTR]
2115 ) {
2116 /* Ctrl-C (usually) - stop gathering input */
2117 goto_new_line();
2118 command_len = 0;
2119 break_out = -1; /* "do not append '\n'" */
2120 break;
2121 }
2122 if (initial_settings.c_cc[VEOF] != 0
2123 && ic_raw == initial_settings.c_cc[VEOF]
2124 ) {
2125 /* Ctrl-D (usually) - delete one character,
2126 * or exit if len=0 and no chars to delete */
2127 if (command_len == 0) {
2128 errno = 0;
2129#if ENABLE_FEATURE_EDITING_VI
2130 prepare_to_die:
2131#endif
2132 break_out = command_len = -1;
2133 break;
2134 }
2135 input_delete(0);
2136 break;
2137 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002138// /* Control-V -- force insert of next char */
2139// if (c == CTRL('V')) {
2140// if (safe_read(STDIN_FILENO, &c, 1) < 1)
2141// goto prepare_to_die;
2142// if (c == 0) {
2143// beep();
2144// break;
2145// }
2146// }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002147 if (ic < ' '
2148 || (!ENABLE_FEATURE_ASSUME_UNICODE && ic >= 256)
2149 || (ENABLE_FEATURE_ASSUME_UNICODE && ic >= VI_CMDMODE_BIT)
2150 ) {
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002151 /* If VI_CMDMODE_BIT is set, ic is >= 256
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002152 * and vi mode ignores unexpected chars.
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002153 * Otherwise, we are here if ic is a
2154 * control char or an unhandled ESC sequence,
2155 * which is also ignored.
2156 */
2157 break;
2158 }
2159 if ((int)command_len >= (maxsize - 2)) {
2160 /* Not enough space for the char and EOL */
2161 break;
Paul Fox84bbac52008-01-11 16:50:08 +00002162 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002163
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002164 command_len++;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002165 if (cursor == (command_len - 1)) {
2166 /* We are at the end, append */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002167 command_ps[cursor] = ic;
2168 command_ps[cursor + 1] = BB_NUL;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002169 cmdedit_set_out_char(' ');
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002170 } else {
2171 /* In the middle, insert */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002172 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002173
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002174 memmove(command_ps + sc + 1, command_ps + sc,
2175 (command_len - sc) * sizeof(command_ps[0]));
2176 command_ps[sc] = ic;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002177 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00002178 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002179 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00002180 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002181 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002182 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00002183 break;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002184 } /* switch (ic) */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002185
2186 if (break_out)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002187 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002188
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002189#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002190 if (ic_raw != '\t')
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002191 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002192#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002193 } /* while (1) */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002194
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002195/* Stop bug catching using "command_must_not_be_used" trick */
2196#undef command
2197
2198#if ENABLE_FEATURE_ASSUME_UNICODE
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +02002199 command[0] = '\0';
2200 if (command_len > 0)
2201 command_len = save_string(command, maxsize - 1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002202 free(command_ps);
2203#endif
2204
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002205 if (command_len > 0)
2206 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002207
Eric Andersen27bb7902003-12-23 20:24:51 +00002208 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002209 command[command_len++] = '\n';
2210 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00002211 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002212
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002213#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002214 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002215#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002216
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002217 /* restore initial_settings */
Denis Vlasenko202ac502008-11-05 13:20:58 +00002218 tcsetattr_stdin_TCSANOW(&initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002219 /* restore SIGWINCH handler */
2220 signal(SIGWINCH, previous_SIGWINCH_handler);
Denys Vlasenko727e1b52009-10-26 15:23:32 +01002221 fflush(NULL);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002222
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002223 len = command_len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002224 DEINIT_S();
2225
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002226 return len; /* can't return command_len, DEINIT_S() destroys it */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002227}
2228
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002229#else
2230
2231#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00002232int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002233{
2234 fputs(prompt, stdout);
Denys Vlasenko727e1b52009-10-26 15:23:32 +01002235 fflush(NULL);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002236 fgets(command, maxsize, stdin);
2237 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00002238}
2239
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002240#endif /* FEATURE_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00002241
2242
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002243/*
2244 * Testing
2245 */
2246
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002247#ifdef TEST
2248
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002249#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002250
2251const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002252
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002253int main(int argc, char **argv)
2254{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00002255 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002256 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00002257#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002258 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
2259 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
2260 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002261#else
2262 "% ";
2263#endif
2264
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002265#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002266 setlocale(LC_ALL, "");
2267#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002268 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00002269 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002270 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002271 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00002272 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002273 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002274 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00002275 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002276 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002277 return 0;
2278}
2279
Eric Andersenc470f442003-07-28 09:56:35 +00002280#endif /* TEST */