blob: 7fffe7ba23d4b671564ce9e53f5e613b9c831f9a [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
Denys Vlasenko94043e82010-05-11 14:49:13 +020069#define SEQ_CLEAR_TILL_END_OF_SCREEN "\033[J"
70//#define SEQ_CLEAR_TILL_END_OF_LINE "\033[K"
71
72
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020073#undef CHAR_T
Denys Vlasenko19158a82010-03-26 14:06:56 +010074#if ENABLE_UNICODE_SUPPORT
Tomas Heinricha659b812010-04-29 13:43:39 +020075# define BB_NUL ((wchar_t)0)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020076# define CHAR_T wchar_t
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010077static bool BB_isspace(CHAR_T c) { return ((unsigned)c < 256 && isspace(c)); }
Denys Vlasenko31e2e7b2009-12-12 02:42:35 +010078# if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010079static bool BB_isalnum(CHAR_T c) { return ((unsigned)c < 256 && isalnum(c)); }
Denys Vlasenko31e2e7b2009-12-12 02:42:35 +010080# endif
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010081static bool BB_ispunct(CHAR_T c) { return ((unsigned)c < 256 && ispunct(c)); }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020082# undef isspace
83# undef isalnum
84# undef ispunct
85# undef isprint
86# define isspace isspace_must_not_be_used
87# define isalnum isalnum_must_not_be_used
88# define ispunct ispunct_must_not_be_used
89# define isprint isprint_must_not_be_used
90#else
91# define BB_NUL '\0'
92# define CHAR_T char
93# define BB_isspace(c) isspace(c)
94# define BB_isalnum(c) isalnum(c)
95# define BB_ispunct(c) ispunct(c)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +020096#endif
97
98
Tomas Heinricha659b812010-04-29 13:43:39 +020099# if ENABLE_UNICODE_PRESERVE_BROKEN
100# define unicode_mark_inv_wchar(wc) ((wc) | 0x20000000)
101# define unicode_is_inv_wchar(wc) ((wc) & 0x20000000)
102# else
103# define unicode_is_inv_wchar(wc) 0
104# endif
105
106
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000107enum {
108 /* We use int16_t for positions, need to limit line len */
109 MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN < 0x7ff0
Denis Vlasenko6b404432008-01-07 16:13:14 +0000110 ? CONFIG_FEATURE_EDITING_MAX_LEN
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000111 : 0x7ff0
112};
Robert Griebl350d26b2002-12-03 22:45:46 +0000113
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000114#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
115static const char null_str[] ALIGN1 = "";
116#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +0000117
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000118/* We try to minimize both static and stack usage. */
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000119struct lineedit_statics {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000120 line_input_t *state;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000121
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000122 volatile unsigned cmdedit_termw; /* = 80; */ /* actual terminal width */
123 sighandler_t previous_SIGWINCH_handler;
Mark Whitley4e338752001-01-26 20:42:23 +0000124
Denys Vlasenko020f4062009-05-17 16:44:54 +0200125 unsigned cmdedit_x; /* real x (col) terminal position */
126 unsigned cmdedit_y; /* pseudoreal y (row) terminal position */
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000127 unsigned cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000128
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000129 unsigned cursor;
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +0200130 int command_len; /* must be signed */
131 /* signed maxsize: we want x in "if (x > S.maxsize)"
Denys Vlasenko044b1802009-07-12 02:50:35 +0200132 * to _not_ be promoted to unsigned */
133 int maxsize;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200134 CHAR_T *command_ps;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000135
136 const char *cmdedit_prompt;
Denis Vlasenko38f63192007-01-22 09:03:07 +0000137#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000138 int num_ok_lines; /* = 1; */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000139#endif
140
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000141#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000142 char *user_buf;
143 char *home_pwd_buf; /* = (char*)null_str; */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000144#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000145
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000146#if ENABLE_FEATURE_TAB_COMPLETION
147 char **matches;
148 unsigned num_matches;
149#endif
150
151#if ENABLE_FEATURE_EDITING_VI
152#define DELBUFSIZ 128
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200153 CHAR_T *delptr;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000154 smallint newdelflag; /* whether delbuf should be reused yet */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200155 CHAR_T delbuf[DELBUFSIZ]; /* a place to store deleted characters */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000156#endif
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100157#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +0100158 smallint sent_ESC_br6n;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +0100159#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000160
161 /* Formerly these were big buffers on stack: */
162#if ENABLE_FEATURE_TAB_COMPLETION
163 char exe_n_cwd_tab_completion__dirbuf[MAX_LINELEN];
164 char input_tab__matchBuf[MAX_LINELEN];
165 int16_t find_match__int_buf[MAX_LINELEN + 1]; /* need to have 9 bits at least */
166 int16_t find_match__pos_buf[MAX_LINELEN + 1];
167#endif
168};
169
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000170/* See lineedit_ptr_hack.c */
171extern struct lineedit_statics *const lineedit_ptr_to_statics;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000172
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000173#define S (*lineedit_ptr_to_statics)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000174#define state (S.state )
175#define cmdedit_termw (S.cmdedit_termw )
176#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
177#define cmdedit_x (S.cmdedit_x )
178#define cmdedit_y (S.cmdedit_y )
179#define cmdedit_prmt_len (S.cmdedit_prmt_len)
180#define cursor (S.cursor )
181#define command_len (S.command_len )
182#define command_ps (S.command_ps )
183#define cmdedit_prompt (S.cmdedit_prompt )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000184#define num_ok_lines (S.num_ok_lines )
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000185#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000186#define home_pwd_buf (S.home_pwd_buf )
187#define matches (S.matches )
188#define num_matches (S.num_matches )
189#define delptr (S.delptr )
190#define newdelflag (S.newdelflag )
191#define delbuf (S.delbuf )
192
193#define INIT_S() do { \
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000194 (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000195 barrier(); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000196 cmdedit_termw = 80; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000197 IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
198 IF_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000199} while (0)
200static void deinit_S(void)
201{
202#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000203 /* This one is allocated only if FANCY_PROMPT is on
204 * (otherwise it points to verbatim prompt (NOT malloced) */
205 free((char*)cmdedit_prompt);
206#endif
207#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
208 free(user_buf);
209 if (home_pwd_buf != null_str)
210 free(home_pwd_buf);
211#endif
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000212 free(lineedit_ptr_to_statics);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000213}
214#define DEINIT_S() deinit_S()
215
Denis Vlasenko2c844952008-04-25 18:44:35 +0000216
Denys Vlasenko19158a82010-03-26 14:06:56 +0100217#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200218static size_t load_string(const char *src, int maxsize)
219{
220 ssize_t len = mbstowcs(command_ps, src, maxsize - 1);
221 if (len < 0)
222 len = 0;
Tomas Heinricha659b812010-04-29 13:43:39 +0200223 command_ps[len] = 0;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200224 return len;
225}
Tomas Heinricha659b812010-04-29 13:43:39 +0200226static unsigned save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200227{
Denys Vlasenko94043e82010-05-11 14:49:13 +0200228# if !ENABLE_UNICODE_PRESERVE_BROKEN
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200229 ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
230 if (len < 0)
231 len = 0;
232 dst[len] = '\0';
233 return len;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200234# else
Tomas Heinricha659b812010-04-29 13:43:39 +0200235 unsigned dstpos = 0;
236 unsigned srcpos = 0;
237
238 maxsize--;
239 while (dstpos < maxsize) {
240 wchar_t wc;
241 int n = srcpos;
242 while ((wc = command_ps[srcpos]) != 0
243 && !unicode_is_inv_wchar(wc)
244 ) {
245 srcpos++;
246 }
247 command_ps[srcpos] = 0;
248 n = wcstombs(dst + dstpos, command_ps + n, maxsize - dstpos);
249 if (n < 0) /* should not happen */
250 break;
251 dstpos += n;
252 if (wc == 0) /* usually is */
253 break;
254 /* We do have invalid byte here! */
255 command_ps[srcpos] = wc; /* restore it */
256 srcpos++;
257 if (dstpos == maxsize)
258 break;
259 dst[dstpos++] = (char) wc;
260 }
261 dst[dstpos] = '\0';
262 return dstpos;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200263# endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200264}
265/* I thought just fputwc(c, stdout) would work. But no... */
266static void BB_PUTCHAR(wchar_t c)
267{
268 char buf[MB_CUR_MAX + 1];
269 mbstate_t mbst = { 0 };
Tomas Heinricha659b812010-04-29 13:43:39 +0200270 ssize_t len;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200271
Tomas Heinricha659b812010-04-29 13:43:39 +0200272 if (unicode_is_inv_wchar(c))
273 c = CONFIG_SUBST_WCHAR;
274 len = wcrtomb(buf, c, &mbst);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200275 if (len > 0) {
276 buf[len] = '\0';
277 fputs(buf, stdout);
278 }
279}
280#else
281static size_t load_string(const char *src, int maxsize)
282{
283 safe_strncpy(command_ps, src, maxsize);
284 return strlen(command_ps);
285}
Denys Vlasenko9038d6f2009-07-15 20:02:19 +0200286# if ENABLE_FEATURE_TAB_COMPLETION
Tomas Heinricha659b812010-04-29 13:43:39 +0200287static void save_string(char *dst, unsigned maxsize)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200288{
289 safe_strncpy(dst, command_ps, maxsize);
290}
Denys Vlasenko7dd0ce42009-07-15 18:27:47 +0200291# endif
292# define BB_PUTCHAR(c) bb_putchar(c)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200293#endif
294
295
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000296/* Put 'command_ps[cursor]', cursor++.
297 * Advance cursor on screen. If we reached right margin, scroll text up
298 * and remove terminal margin effect by printing 'next_char' */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000299#define HACK_FOR_WRONG_WIDTH 1
Denys Vlasenko94043e82010-05-11 14:49:13 +0200300static void put_cur_glyph_and_inc_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000301{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200302 CHAR_T c = command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000303
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200304 if (c == BB_NUL) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000305 /* erase character after end of input string */
306 c = ' ';
Denys Vlasenko94043e82010-05-11 14:49:13 +0200307 } else {
308 /* advance cursor only if we aren't at the end yet */
309 cursor++;
310 cmdedit_x++;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000311 }
Denys Vlasenko94043e82010-05-11 14:49:13 +0200312
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000313#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000314 /* Display non-printable characters in reverse */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200315 if (!BB_isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000316 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000317 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000318 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000319 c += '@';
320 if (c == 127)
321 c = '?';
322 printf("\033[7m%c\033[0m", c);
323 } else
324#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000325 {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200326 BB_PUTCHAR(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000327 }
Denys Vlasenko94043e82010-05-11 14:49:13 +0200328 if (cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000329 /* terminal is scrolled down */
330 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000331 cmdedit_x = 0;
Denis Vlasenko2c844952008-04-25 18:44:35 +0000332#if HACK_FOR_WRONG_WIDTH
333 /* This works better if our idea of term width is wrong
334 * and it is actually wider (often happens on serial lines).
335 * Printing CR,LF *forces* cursor to next line.
336 * OTOH if terminal width is correct AND terminal does NOT
337 * have automargin (IOW: it is moving cursor to next line
338 * by itself (which is wrong for VT-10x terminals)),
339 * this will break things: there will be one extra empty line */
340 puts("\r"); /* + implicit '\n' */
341#else
Denys Vlasenko94043e82010-05-11 14:49:13 +0200342 /* VT-10x terminals don't wrap cursor to next line when last char
343 * on the line is printed - cursor stays "over" this char.
344 * Need to print _next_ char too (first one to appear on next line)
345 * to make cursor move down to next line.
346 */
347 /* Works ok only if cmdedit_termw is correct. */
348 c = command_ps[cursor];
349 if (c == BB_NUL)
350 c = ' ';
351 BB_PUTCHAR(c);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000352 bb_putchar('\b');
Denis Vlasenko2c844952008-04-25 18:44:35 +0000353#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000354 }
Erik Andersen13456d12000-03-16 08:09:57 +0000355}
356
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000357/* Move to end of line (by printing all chars till the end) */
Denys Vlasenko94043e82010-05-11 14:49:13 +0200358static void put_till_end_and_adv_cursor(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000359{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000360 while (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200361 put_cur_glyph_and_inc_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +0000362}
363
364/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000365static void goto_new_line(void)
366{
Denys Vlasenko94043e82010-05-11 14:49:13 +0200367 put_till_end_and_adv_cursor();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000368 if (cmdedit_x)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000369 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000370}
371
372
Rob Landley88621d72006-08-29 19:41:06 +0000373static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000374{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000375 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000376 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000377}
Eric Andersen81fe1232003-07-29 06:38:40 +0000378
Rob Landley88621d72006-08-29 19:41:06 +0000379static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000380{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000381 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000382}
383
Eric Andersenaff114c2004-04-14 17:51:38 +0000384/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000385/* (optimized for slow terminals) */
386static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000387{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000388 int count_y;
389
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000390 if (num > cursor)
391 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000392 if (!num)
393 return;
394 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000395
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000396 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000397 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000398 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000399 /* This is longer by 5 bytes on x86.
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000400 * Also gets miscompiled for ARM users
401 * (busybox.net/bugs/view.php?id=2274).
Denis Vlasenko6f043912008-02-18 22:28:03 +0000402 * printf(("\b\b\b\b" + 4) - num);
403 * return;
404 */
405 do {
406 bb_putchar('\b');
407 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000408 return;
409 }
410 printf("\033[%uD", num);
411 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000412 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000413
414 /* Need to go one or more lines up */
415 num -= cmdedit_x;
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000416 {
417 unsigned w = cmdedit_termw; /* volatile var */
418 count_y = 1 + (num / w);
419 cmdedit_y -= count_y;
420 cmdedit_x = w * count_y - num;
421 }
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000422 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000423 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000424}
425
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000426static void put_prompt(void)
427{
Denys Vlasenko13ad9062009-11-11 03:19:30 +0100428 unsigned w;
429
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430 out1str(cmdedit_prompt);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100431 fflush_all();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000432 cursor = 0;
Denys Vlasenko13ad9062009-11-11 03:19:30 +0100433 w = cmdedit_termw; /* read volatile var once */
434 cmdedit_y = cmdedit_prmt_len / w; /* new quasireal y */
435 cmdedit_x = cmdedit_prmt_len % w;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000436}
437
Eric Andersenaff114c2004-04-14 17:51:38 +0000438/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000439static void redraw(int y, int back_cursor)
440{
Denys Vlasenko044b1802009-07-12 02:50:35 +0200441 if (y > 0) /* up to start y */
442 printf("\033[%uA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000443 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000444 put_prompt();
Denys Vlasenko94043e82010-05-11 14:49:13 +0200445 put_till_end_and_adv_cursor();
446 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000447 input_backward(back_cursor);
448}
449
Paul Fox3f11b1b2005-08-04 19:04:46 +0000450/* Delete the char in front of the cursor, optionally saving it
451 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000452#if !ENABLE_FEATURE_EDITING_VI
453static void input_delete(void)
454#define input_delete(save) input_delete()
455#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000456static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000457#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000458{
Mark Whitley4e338752001-01-26 20:42:23 +0000459 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000460
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000461 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000462 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000463
Denis Vlasenko38f63192007-01-22 09:03:07 +0000464#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000465 if (save) {
466 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000467 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000468 newdelflag = 0;
469 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000470 if ((delptr - delbuf) < DELBUFSIZ)
471 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000472 }
473#endif
474
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200475 memmove(command_ps + j, command_ps + j + 1,
Denys Vlasenko9531f7d2009-07-16 14:33:16 +0200476 /* (command_len + 1 [because of NUL]) - (j + 1)
477 * simplified into (command_len - j) */
478 (command_len - j) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000479 command_len--;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200480 put_till_end_and_adv_cursor();
481 /* Last char is still visible, erase it (and more) */
482 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersenc470f442003-07-28 09:56:35 +0000483 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000484}
485
Denis Vlasenko38f63192007-01-22 09:03:07 +0000486#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000487static void put(void)
488{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000489 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000490 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000491
Paul Fox3f11b1b2005-08-04 19:04:46 +0000492 if (j == 0)
493 return;
494 ocursor = cursor;
495 /* open hole and then fill it */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200496 memmove(command_ps + cursor + j, command_ps + cursor,
497 (command_len - cursor + 1) * sizeof(command_ps[0]));
498 memcpy(command_ps + cursor, delbuf, j * sizeof(command_ps[0]));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000499 command_len += j;
Denys Vlasenko94043e82010-05-11 14:49:13 +0200500 put_till_end_and_adv_cursor();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000501 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000502}
503#endif
504
Mark Whitley4e338752001-01-26 20:42:23 +0000505/* Delete the char in back of the cursor */
506static void input_backspace(void)
507{
508 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000509 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000510 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000511 }
512}
513
Eric Andersenaff114c2004-04-14 17:51:38 +0000514/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000515static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000516{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000517 if (cursor < command_len)
Denys Vlasenko94043e82010-05-11 14:49:13 +0200518 put_cur_glyph_and_inc_cursor();
Erik Andersenf0657d32000-04-12 17:49:52 +0000519}
520
Denis Vlasenko38f63192007-01-22 09:03:07 +0000521#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000522
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000523static void free_tab_completion_data(void)
524{
525 if (matches) {
526 while (num_matches)
527 free(matches[--num_matches]);
528 free(matches);
529 matches = NULL;
530 }
531}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000532
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000533static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000534{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000535 matches = xrealloc_vector(matches, 4, num_matches);
536 matches[num_matches] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000537 num_matches++;
538}
539
Denis Vlasenko38f63192007-01-22 09:03:07 +0000540#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000541static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000542{
543 struct passwd *entry;
544 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000545
Eric Andersenc470f442003-07-28 09:56:35 +0000546 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000547 userlen = strlen(ud);
548
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000549 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000550 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000551 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000552
Eric Andersenc470f442003-07-28 09:56:35 +0000553 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000554 home = home_pwd_buf;
555 } else {
556 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000557 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000558 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000559 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000560 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000561 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000562 ud = temp;
563 if (entry)
564 home = entry->pw_dir;
565 }
566 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000567 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000568 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000569 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000570 }
571 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000572 } else {
573 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000574 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000575 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000576 struct passwd pwd;
577 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000578
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000579 setpwent();
580 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000581 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000582 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
583 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000584 }
585 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000586 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000587 }
588}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000589#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000590
591enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000592 FIND_EXE_ONLY = 0,
593 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000594 FIND_FILE_ONLY = 2,
595};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000596
Mark Whitley4e338752001-01-26 20:42:23 +0000597static int path_parse(char ***p, int flags)
598{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000599 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000600 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000601 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000602 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000603
Mark Whitley4e338752001-01-26 20:42:23 +0000604 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000605 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000606 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000607
608 if (state->flags & WITH_PATH_LOOKUP)
609 pth = state->path_lookup;
610 else
611 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000612 /* PATH=<empty> or PATH=:<empty> */
613 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
614 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000615
Denis Vlasenko253ce002007-01-22 08:34:44 +0000616 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000617 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000618 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000619 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000620 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000621 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000622 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000623 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000624 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000625 }
626
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000627 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000628 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000629 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000630 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000631 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000632 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000633 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000634 *tmp++ = '\0'; /* ':' -> '\0' */
635 if (*tmp == '\0')
636 break; /* :<empty> */
637 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000638 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000639 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000640 return npth;
641}
642
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000643static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000644{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000645 DIR *dir;
646 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000647 struct stat st;
648 char *path1[1];
649 char **paths = path1;
650 int npaths;
651 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000652 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000653 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000654/* char dirbuf[MAX_LINELEN]; */
655#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000656
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000657 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000658 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000659
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000660 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000661 /* no dir, if flags==EXE_ONLY - get paths, else "." */
662 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000663 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000664 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000665 /* dirbuf = ".../.../.../" */
666 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000667#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000668 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000669 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000670#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000671 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000672 /* point to 'l' in "..../last_component" */
673 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000674 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000675
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000676 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000677 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000678 if (!dir)
679 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000680
681 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000682 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000683 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000684
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000685 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000686 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000687 continue;
688 /* not see .name without .match */
Denis Vlasenkob5202712008-04-24 04:42:52 +0000689 if (*str_found == '.' && *pfind == '\0') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000690 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000691 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000692 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000693 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000694 found = concat_path_file(paths[i], str_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000695 /* hmm, remove in progress? */
696 /* NB: stat() first so that we see is it a directory;
697 * but if that fails, use lstat() so that
698 * we still match dangling links */
699 if (stat(found, &st) && lstat(found, &st))
Eric Andersene5dfced2001-04-09 22:48:12 +0000700 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000701 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000702 if (paths[i] != dirbuf)
Denis Vlasenkob5202712008-04-24 04:42:52 +0000703 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000704
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000705 len1 = strlen(found);
706 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000707 found[len1] = '\0';
708 found[len1+1] = '\0';
709
Mark Whitley4e338752001-01-26 20:42:23 +0000710 if (S_ISDIR(st.st_mode)) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000711 /* name is a directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000712 if (found[len1-1] != '/') {
713 found[len1] = '/';
714 }
Mark Whitley4e338752001-01-26 20:42:23 +0000715 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000716 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000717 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000718 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000719 }
Mark Whitley4e338752001-01-26 20:42:23 +0000720 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000721 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000722 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000723 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000724 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000725 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000726 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000727 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000728 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000729 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000730 free(paths);
731 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000732#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000733}
Erik Andersenf0657d32000-04-12 17:49:52 +0000734
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200735/* QUOT is used on elements of int_buf[], which are bytes,
736 * not Unicode chars. Therefore it works correctly even in Unicode mode.
737 */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000738#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000739
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200740#define int_buf (S.find_match__int_buf)
741#define pos_buf (S.find_match__pos_buf)
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +0200742/* is must be <= in */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200743static void collapse_pos(int is, int in)
744{
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +0200745 memmove(int_buf+is, int_buf+in, (MAX_LINELEN+1-in)*sizeof(int_buf[0]));
746 memmove(pos_buf+is, pos_buf+in, (MAX_LINELEN+1-in)*sizeof(pos_buf[0]));
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200747}
Denys Vlasenko044b1802009-07-12 02:50:35 +0200748static NOINLINE int find_match(char *matchBuf, int *len_with_quotes)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000749{
750 int i, j;
751 int command_mode;
752 int c, c2;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200753/* Were local, but it uses too much stack */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000754/* int16_t int_buf[MAX_LINELEN + 1]; */
755/* int16_t pos_buf[MAX_LINELEN + 1]; */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000756
757 /* set to integer dimension characters and own positions */
758 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000759 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000760 if (int_buf[i] == 0) {
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200761 pos_buf[i] = -1; /* end-fo-line indicator */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000762 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000763 }
764 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000765 }
766
767 /* mask \+symbol and convert '\t' to ' ' */
768 for (i = j = 0; matchBuf[i]; i++, j++)
769 if (matchBuf[i] == '\\') {
770 collapse_pos(j, j + 1);
771 int_buf[j] |= QUOT;
772 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000773#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200774 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000775 int_buf[j] = ' ' | QUOT;
776#endif
777 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000778#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000779 else if (matchBuf[i] == '\t')
780 int_buf[j] = ' ';
781#endif
782
783 /* mask "symbols" or 'symbols' */
784 c2 = 0;
785 for (i = 0; int_buf[i]; i++) {
786 c = int_buf[i];
787 if (c == '\'' || c == '"') {
788 if (c2 == 0)
789 c2 = c;
790 else {
791 if (c == c2)
792 c2 = 0;
793 else
794 int_buf[i] |= QUOT;
795 }
796 } else if (c2 != 0 && c != '$')
797 int_buf[i] |= QUOT;
798 }
799
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000800 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000801 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
802 for (i = 0; int_buf[i]; i++) {
803 c = int_buf[i];
804 c2 = int_buf[i + 1];
805 j = i ? int_buf[i - 1] : -1;
806 command_mode = 0;
807 if (c == ';' || c == '&' || c == '|') {
808 command_mode = 1 + (c == c2);
809 if (c == '&') {
810 if (j == '>' || j == '<')
811 command_mode = 0;
812 } else if (c == '|' && j == '>')
813 command_mode = 0;
814 }
815 if (command_mode) {
816 collapse_pos(0, i + command_mode);
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200817 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000818 }
819 }
820 /* collapse `command...` */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200821 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000822 if (int_buf[i] == '`') {
823 for (j = i + 1; int_buf[j]; j++)
824 if (int_buf[j] == '`') {
825 collapse_pos(i, j + 1);
826 j = 0;
827 break;
828 }
829 if (j) {
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200830 /* not found closing ` - command mode, collapse all previous */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000831 collapse_pos(0, i + 1);
832 break;
833 } else
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200834 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000835 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200836 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000837
838 /* collapse (command...(command...)...) or {command...{command...}...} */
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200839 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000840 c2 = 0;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200841 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000842 if (int_buf[i] == '(' || int_buf[i] == '{') {
843 if (int_buf[i] == '(')
844 c++;
845 else
846 c2++;
847 collapse_pos(0, i + 1);
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200848 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000849 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200850 }
851 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000852 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
853 if (int_buf[i] == ')')
854 c--;
855 else
856 c2--;
857 collapse_pos(0, i + 1);
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200858 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000859 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200860 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000861
862 /* skip first not quote space */
863 for (i = 0; int_buf[i]; i++)
864 if (int_buf[i] != ' ')
865 break;
866 if (i)
867 collapse_pos(0, i);
868
869 /* set find mode for completion */
870 command_mode = FIND_EXE_ONLY;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200871 for (i = 0; int_buf[i]; i++) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000872 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
873 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000874 && matchBuf[pos_buf[0]] == 'c'
875 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000876 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000877 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000878 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000879 command_mode = FIND_FILE_ONLY;
880 break;
881 }
882 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200883 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000884 for (i = 0; int_buf[i]; i++)
885 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000886 /* find last word */
887 for (--i; i >= 0; i--) {
888 c = int_buf[i];
889 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
890 collapse_pos(0, i + 1);
891 break;
892 }
893 }
894 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000895 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
896 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000897 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000898 while ((int_buf[i] & ~QUOT) == '/'
899 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
900 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000901 i++;
902 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000903
904 /* set only match and destroy quotes */
905 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000906 for (c = 0; pos_buf[i] >= 0; i++) {
907 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000908 j = pos_buf[i] + 1;
909 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000910 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000911 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000912 *len_with_quotes = j ? j - pos_buf[0] : 0;
913
914 return command_mode;
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +0200915}
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000916#undef int_buf
917#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000918
Glenn L McGrath4d001292003-01-06 01:11:50 +0000919/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000920 * display by column (original idea from ls applet,
921 * very optimized by me :)
922 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000923static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000924{
925 int ncols, row;
926 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000927 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000928 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000929 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000930
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200931 /* find the longest file name - use that as the column width */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000932 for (row = 0; row < nrows; row++) {
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100933 l = unicode_strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000934 if (column_width < l)
935 column_width = l;
936 }
937 column_width += 2; /* min space for columns */
938 ncols = cmdedit_termw / column_width;
939
940 if (ncols > 1) {
941 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000942 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000943 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000944 } else {
945 ncols = 1;
946 }
947 for (row = 0; row < nrows; row++) {
948 int n = row;
949 int nc;
950
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000951 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000952 printf("%s%-*s", matches[n],
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100953 (int)(column_width - unicode_strlen(matches[n])), ""
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200954 );
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000955 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000956 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000957 }
958}
959
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000960static char *add_quote_for_spec_chars(char *found)
961{
962 int l = 0;
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200963 char *s = xzalloc((strlen(found) + 1) * 2);
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000964
965 while (*found) {
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +0200966 if (strchr(" `\"#$%^&*()=+{}[]:;'|\\<>", *found))
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000967 s[l++] = '\\';
968 s[l++] = *found++;
969 }
Denys Vlasenko53fd1bf2009-07-16 02:19:39 +0200970 /* s[l] = '\0'; - already is */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000971 return s;
972}
973
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000974/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000975static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000976{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000977 if (!(state->flags & TAB_COMPLETION))
978 return;
979
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000980 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000981 char *tmp, *tmp1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000982 size_t len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000983/* char matchBuf[MAX_LINELEN]; */
984#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000985 int find_type;
986 int recalc_pos;
Denys Vlasenko19158a82010-03-26 14:06:56 +0100987#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko044b1802009-07-12 02:50:35 +0200988 /* cursor pos in command converted to multibyte form */
989 int cursor_mb;
990#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000991
Eric Andersenc470f442003-07-28 09:56:35 +0000992 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000993
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +0200994 /* Make a local copy of the string --
995 * up to the position of the cursor */
996 save_string(matchBuf, cursor + 1);
Denys Vlasenko19158a82010-03-26 14:06:56 +0100997#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko044b1802009-07-12 02:50:35 +0200998 cursor_mb = strlen(matchBuf);
999#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001000 tmp = matchBuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001001
1002 find_type = find_match(matchBuf, &recalc_pos);
1003
1004 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001005 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +00001006
Denis Vlasenko38f63192007-01-22 09:03:07 +00001007#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001008 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001009 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001010 if (state->flags & USERNAME_COMPLETION)
Denys Vlasenko044b1802009-07-12 02:50:35 +02001011 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == NULL)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001012 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +00001013#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001014 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001015 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001016 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001017 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001018 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001019 if (matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001020 unsigned i;
1021 int n = 0;
Denis Vlasenkofb290382008-03-02 12:51:26 +00001022 qsort_string_vector(matches, num_matches);
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001023 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001024 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001025 if (strcmp(matches[i], matches[i+1]) == 0) {
1026 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001027 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001028 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001029 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +00001030 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001031 }
Denis Vlasenko92758142006-10-03 19:56:34 +00001032 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001033 matches[n] = matches[i];
1034 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001035 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001036 /* Did we find exactly one match? */
Denys Vlasenko044b1802009-07-12 02:50:35 +02001037 if (!matches || num_matches > 1) { /* no */
Mark Whitley4e338752001-01-26 20:42:23 +00001038 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001039 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +00001040 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001041 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001042 tmp1 = xstrdup(matches[0]);
Denys Vlasenko044b1802009-07-12 02:50:35 +02001043 for (tmp = tmp1; *tmp; tmp++) {
1044 for (len_found = 1; len_found < num_matches; len_found++) {
1045 if (matches[len_found][tmp - tmp1] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001046 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001047 break;
1048 }
Denys Vlasenko044b1802009-07-12 02:50:35 +02001049 }
1050 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001051 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001052 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001053 return;
1054 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001055 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001056 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +00001057 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001058 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001059 /* for next completion current found */
1060 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001061
1062 len_found = strlen(tmp);
1063 if (tmp[len_found-1] != '/') {
1064 tmp[len_found] = ' ';
1065 tmp[len_found+1] = '\0';
1066 }
Mark Whitley4e338752001-01-26 20:42:23 +00001067 }
Denys Vlasenko044b1802009-07-12 02:50:35 +02001068
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001069 len_found = strlen(tmp);
Denys Vlasenko19158a82010-03-26 14:06:56 +01001070#if !ENABLE_UNICODE_SUPPORT
Denys Vlasenko044b1802009-07-12 02:50:35 +02001071 /* have space to place the match? */
1072 /* The result consists of three parts with these lengths: */
1073 /* (cursor - recalc_pos) + len_found + (command_len - cursor) */
1074 /* it simplifies into: */
1075 if ((int)(len_found + command_len - recalc_pos) < S.maxsize) {
1076 /* save tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001077 strcpy(matchBuf, command_ps + cursor);
Denys Vlasenko044b1802009-07-12 02:50:35 +02001078 /* add match and tail */
1079 sprintf(&command_ps[cursor - recalc_pos], "%s%s", tmp, matchBuf);
Denis Vlasenko253ce002007-01-22 08:34:44 +00001080 command_len = strlen(command_ps);
Denys Vlasenko044b1802009-07-12 02:50:35 +02001081 /* new pos */
1082 recalc_pos = cursor - recalc_pos + len_found;
1083 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +00001084 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001085 }
Denys Vlasenko044b1802009-07-12 02:50:35 +02001086#else
1087 {
1088 char command[MAX_LINELEN];
1089 int len = save_string(command, sizeof(command));
1090 /* have space to place the match? */
1091 /* (cursor_mb - recalc_pos) + len_found + (len - cursor_mb) */
1092 if ((int)(len_found + len - recalc_pos) < MAX_LINELEN) {
1093 /* save tail */
1094 strcpy(matchBuf, command + cursor_mb);
1095 /* where do we want to have cursor after all? */
1096 strcpy(&command[cursor_mb - recalc_pos], tmp);
1097 len = load_string(command, S.maxsize);
1098 /* add match and tail */
1099 sprintf(&command[cursor_mb - recalc_pos], "%s%s", tmp, matchBuf);
1100 command_len = load_string(command, S.maxsize);
1101 /* write out the matched command */
1102 redraw(cmdedit_y, command_len - len);
1103 }
1104 }
1105#endif
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001106 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001107#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +00001108 } else {
1109 /* Ok -- the last char was a TAB. Since they
1110 * just hit TAB again, print a list of all the
1111 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001112 if (matches && num_matches > 0) {
Denys Vlasenko044b1802009-07-12 02:50:35 +02001113 /* changed by goto_new_line() */
1114 int sav_cursor = cursor;
Erik Andersenf0657d32000-04-12 17:49:52 +00001115
1116 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001117 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001118 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001119 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001120 }
1121 }
1122}
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001123
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001124#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001125
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001126
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001127line_input_t* FAST_FUNC new_line_input_t(int flags)
1128{
1129 line_input_t *n = xzalloc(sizeof(*n));
1130 n->flags = flags;
1131 return n;
1132}
1133
1134
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001135#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001136
Denis Vlasenko682ad302008-09-27 01:28:56 +00001137static void save_command_ps_at_cur_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001138{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001139 if (command_ps[0] != BB_NUL) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001140 int cur = state->cur_history;
1141 free(state->history[cur]);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001142
Denys Vlasenko19158a82010-03-26 14:06:56 +01001143# if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001144 {
1145 char tbuf[MAX_LINELEN];
1146 save_string(tbuf, sizeof(tbuf));
1147 state->history[cur] = xstrdup(tbuf);
1148 }
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001149# else
Denis Vlasenko682ad302008-09-27 01:28:56 +00001150 state->history[cur] = xstrdup(command_ps);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001151# endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001152 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001153}
1154
1155/* state->flags is already checked to be nonzero */
1156static int get_previous_history(void)
1157{
1158 if ((state->flags & DO_HISTORY) && state->cur_history) {
1159 save_command_ps_at_cur_history();
1160 state->cur_history--;
1161 return 1;
1162 }
1163 beep();
1164 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001165}
1166
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001167static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001168{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001169 if (state->flags & DO_HISTORY) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001170 if (state->cur_history < state->cnt_history) {
1171 save_command_ps_at_cur_history(); /* save the current history line */
1172 return ++state->cur_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001173 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001174 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001175 beep();
1176 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001177}
Robert Griebl350d26b2002-12-03 22:45:46 +00001178
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001179# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001180/* We try to ensure that concurrent additions to the history
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001181 * do not overwrite each other.
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001182 * Otherwise shell users get unhappy.
1183 *
1184 * History file is trimmed lazily, when it grows several times longer
1185 * than configured MAX_HISTORY lines.
1186 */
1187
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001188static void free_line_input_t(line_input_t *n)
1189{
1190 int i = n->cnt_history;
1191 while (i > 0)
1192 free(n->history[--i]);
1193 free(n);
1194}
1195
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001196/* state->flags is already checked to be nonzero */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001197static void load_history(line_input_t *st_parm)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001198{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001199 char *temp_h[MAX_HISTORY];
1200 char *line;
Robert Griebl350d26b2002-12-03 22:45:46 +00001201 FILE *fp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001202 unsigned idx, i, line_len;
Robert Griebl350d26b2002-12-03 22:45:46 +00001203
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001204 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +00001205
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001206 fp = fopen_for_read(st_parm->hist_file);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001207 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001208 /* clean up old history */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001209 for (idx = st_parm->cnt_history; idx > 0;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001210 idx--;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001211 free(st_parm->history[idx]);
1212 st_parm->history[idx] = NULL;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001213 }
1214
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001215 /* fill temp_h[], retaining only last MAX_HISTORY lines */
1216 memset(temp_h, 0, sizeof(temp_h));
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001217 st_parm->cnt_history_in_file = idx = 0;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001218 while ((line = xmalloc_fgetline(fp)) != NULL) {
1219 if (line[0] == '\0') {
1220 free(line);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001221 continue;
1222 }
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001223 free(temp_h[idx]);
1224 temp_h[idx] = line;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001225 st_parm->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001226 idx++;
1227 if (idx == MAX_HISTORY)
1228 idx = 0;
Robert Griebl350d26b2002-12-03 22:45:46 +00001229 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001230 fclose(fp);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001231
1232 /* find first non-NULL temp_h[], if any */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001233 if (st_parm->cnt_history_in_file) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001234 while (temp_h[idx] == NULL) {
1235 idx++;
1236 if (idx == MAX_HISTORY)
1237 idx = 0;
1238 }
1239 }
1240
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001241 /* copy temp_h[] to st_parm->history[] */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001242 for (i = 0; i < MAX_HISTORY;) {
1243 line = temp_h[idx];
1244 if (!line)
1245 break;
1246 idx++;
1247 if (idx == MAX_HISTORY)
1248 idx = 0;
1249 line_len = strlen(line);
1250 if (line_len >= MAX_LINELEN)
1251 line[MAX_LINELEN-1] = '\0';
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001252 st_parm->history[i++] = line;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001253 }
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001254 st_parm->cnt_history = i;
Robert Griebl350d26b2002-12-03 22:45:46 +00001255 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001256}
1257
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001258/* state->flags is already checked to be nonzero */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001259static void save_history(char *str)
Robert Griebl350d26b2002-12-03 22:45:46 +00001260{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001261 int fd;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001262 int len, len2;
Eric Andersenc470f442003-07-28 09:56:35 +00001263
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001264 fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0666);
1265 if (fd < 0)
1266 return;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001267 xlseek(fd, 0, SEEK_END); /* paranoia */
1268 len = strlen(str);
1269 str[len] = '\n'; /* we (try to) do atomic write */
1270 len2 = full_write(fd, str, len + 1);
1271 str[len] = '\0';
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001272 close(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001273 if (len2 != len + 1)
1274 return; /* "wtf?" */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001275
1276 /* did we write so much that history file needs trimming? */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001277 state->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001278 if (state->cnt_history_in_file > MAX_HISTORY * 4) {
1279 FILE *fp;
1280 char *new_name;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001281 line_input_t *st_temp;
1282 int i;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001283
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001284 /* we may have concurrently written entries from others.
1285 * load them */
1286 st_temp = new_line_input_t(state->flags);
1287 st_temp->hist_file = state->hist_file;
1288 load_history(st_temp);
1289
1290 /* write out temp file and replace hist_file atomically */
1291 new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001292 fp = fopen_for_write(new_name);
1293 if (fp) {
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001294 for (i = 0; i < st_temp->cnt_history; i++)
1295 fprintf(fp, "%s\n", st_temp->history[i]);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001296 fclose(fp);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001297 if (rename(new_name, state->hist_file) == 0)
1298 state->cnt_history_in_file = st_temp->cnt_history;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001299 }
1300 free(new_name);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001301 free_line_input_t(st_temp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001302 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001303}
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001304# else
1305# define load_history(a) ((void)0)
1306# define save_history(a) ((void)0)
1307# endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001308
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001309static void remember_in_history(char *str)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001310{
1311 int i;
1312
1313 if (!(state->flags & DO_HISTORY))
1314 return;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001315 if (str[0] == '\0')
1316 return;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001317 i = state->cnt_history;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001318 /* Don't save dupes */
1319 if (i && strcmp(state->history[i-1], str) == 0)
1320 return;
1321
1322 free(state->history[MAX_HISTORY]); /* redundant, paranoia */
1323 state->history[MAX_HISTORY] = NULL; /* redundant, paranoia */
1324
1325 /* If history[] is full, remove the oldest command */
1326 /* we need to keep history[MAX_HISTORY] empty, hence >=, not > */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001327 if (i >= MAX_HISTORY) {
1328 free(state->history[0]);
1329 for (i = 0; i < MAX_HISTORY-1; i++)
1330 state->history[i] = state->history[i+1];
Denis Vlasenko682ad302008-09-27 01:28:56 +00001331 /* i == MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001332 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001333 /* i <= MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001334 state->history[i++] = xstrdup(str);
Denis Vlasenko682ad302008-09-27 01:28:56 +00001335 /* i <= MAX_HISTORY */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001336 state->cur_history = i;
1337 state->cnt_history = i;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001338# if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001339 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001340 save_history(str);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001341# endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001342 IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001343}
1344
1345#else /* MAX_HISTORY == 0 */
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001346# define remember_in_history(a) ((void)0)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001347#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001348
1349
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001350#if ENABLE_FEATURE_EDITING_VI
Erik Andersen6273f652000-03-17 01:12:41 +00001351/*
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001352 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001353 */
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001354static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001355vi_Word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001356{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001357 CHAR_T *command = command_ps;
1358
1359 while (cursor < command_len && !BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001360 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001361 if (eat) while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001362 input_forward();
1363}
1364
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001365static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001366vi_word_motion(int eat)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001367{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001368 CHAR_T *command = command_ps;
1369
1370 if (BB_isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001371 while (cursor < command_len
Denys Vlasenko13028922009-07-12 00:51:15 +02001372 && (BB_isalnum(command[cursor+1]) || command[cursor+1] == '_')
1373 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001374 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001375 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001376 } else if (BB_ispunct(command[cursor])) {
1377 while (cursor < command_len && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001378 input_forward();
1379 }
1380
Denis Vlasenko253ce002007-01-22 08:34:44 +00001381 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001382 input_forward();
1383
Denys Vlasenko13028922009-07-12 00:51:15 +02001384 if (eat) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001385 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001386 input_forward();
Denys Vlasenko13028922009-07-12 00:51:15 +02001387 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001388}
1389
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001390static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001391vi_End_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001392{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001393 CHAR_T *command = command_ps;
1394
Paul Fox3f11b1b2005-08-04 19:04:46 +00001395 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001396 while (cursor < command_len && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001397 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001398 while (cursor < command_len-1 && !BB_isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001399 input_forward();
1400}
1401
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001402static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001403vi_end_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001404{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001405 CHAR_T *command = command_ps;
1406
Denis Vlasenko253ce002007-01-22 08:34:44 +00001407 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001408 return;
1409 input_forward();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001410 while (cursor < command_len-1 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001411 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001412 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001413 return;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001414 if (BB_isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001415 while (cursor < command_len-1
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001416 && (BB_isalnum(command[cursor+1]) || command[cursor+1] == '_')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001417 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001418 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001419 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001420 } else if (BB_ispunct(command[cursor])) {
1421 while (cursor < command_len-1 && BB_ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001422 input_forward();
1423 }
1424}
1425
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001426static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001427vi_Back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001428{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001429 CHAR_T *command = command_ps;
1430
1431 while (cursor > 0 && BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001432 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001433 while (cursor > 0 && !BB_isspace(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001434 input_backward(1);
1435}
1436
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001437static void
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001438vi_back_motion(void)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001439{
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001440 CHAR_T *command = command_ps;
1441
Paul Fox3f11b1b2005-08-04 19:04:46 +00001442 if (cursor <= 0)
1443 return;
1444 input_backward(1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001445 while (cursor > 0 && BB_isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001446 input_backward(1);
1447 if (cursor <= 0)
1448 return;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001449 if (BB_isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001450 while (cursor > 0
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001451 && (BB_isalnum(command[cursor-1]) || command[cursor-1] == '_')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001452 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001453 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001454 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001455 } else if (BB_ispunct(command[cursor])) {
1456 while (cursor > 0 && BB_ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001457 input_backward(1);
1458 }
1459}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001460#endif
1461
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01001462/* Modelled after bash 4.0 behavior of Ctrl-<arrow> */
1463static void ctrl_left(void)
1464{
1465 CHAR_T *command = command_ps;
1466
1467 while (1) {
1468 CHAR_T c;
1469
1470 input_backward(1);
1471 if (cursor == 0)
1472 break;
1473 c = command[cursor];
1474 if (c != ' ' && !BB_ispunct(c)) {
1475 /* we reached a "word" delimited by spaces/punct.
1476 * go to its beginning */
1477 while (1) {
1478 c = command[cursor - 1];
1479 if (c == ' ' || BB_ispunct(c))
1480 break;
1481 input_backward(1);
1482 if (cursor == 0)
1483 break;
1484 }
1485 break;
1486 }
1487 }
1488}
1489static void ctrl_right(void)
1490{
1491 CHAR_T *command = command_ps;
1492
1493 while (1) {
1494 CHAR_T c;
1495
1496 c = command[cursor];
1497 if (c == BB_NUL)
1498 break;
1499 if (c != ' ' && !BB_ispunct(c)) {
1500 /* we reached a "word" delimited by spaces/punct.
1501 * go to its end + 1 */
1502 while (1) {
1503 input_forward();
1504 c = command[cursor];
1505 if (c == BB_NUL || c == ' ' || BB_ispunct(c))
1506 break;
1507 }
1508 break;
1509 }
1510 input_forward();
1511 }
1512}
1513
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001514
1515/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001516 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001517 */
1518
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001519#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
1520static void ask_terminal(void)
1521{
1522 /* Ask terminal where is the cursor now.
1523 * lineedit_read_key handles response and corrects
1524 * our idea of current cursor position.
1525 * Testcase: run "echo -n long_line_long_line_long_line",
1526 * then type in a long, wrapping command and try to
1527 * delete it using backspace key.
1528 * Note: we print it _after_ prompt, because
1529 * prompt may contain CR. Example: PS1='\[\r\n\]\w '
1530 */
1531 /* Problem: if there is buffered input on stdin,
1532 * the response will be delivered later,
1533 * possibly to an unsuspecting application.
1534 * Testcase: "sleep 1; busybox ash" + press and hold [Enter].
1535 * Result:
1536 * ~/srcdevel/bbox/fix/busybox.t4 #
1537 * ~/srcdevel/bbox/fix/busybox.t4 #
1538 * ^[[59;34~/srcdevel/bbox/fix/busybox.t4 # <-- garbage
1539 * ~/srcdevel/bbox/fix/busybox.t4 #
1540 *
1541 * Checking for input with poll only makes the race narrower,
1542 * I still can trigger it. Strace:
1543 *
1544 * write(1, "~/srcdevel/bbox/fix/busybox.t4 # ", 33) = 33
1545 * poll([{fd=0, events=POLLIN}], 1, 0) = 0 (Timeout) <-- no input exists
1546 * write(1, "\33[6n", 4) = 4 <-- send the ESC sequence, quick!
1547 * poll([{fd=0, events=POLLIN}], 1, 4294967295) = 1 ([{fd=0, revents=POLLIN}])
1548 * read(0, "\n", 1) = 1 <-- oh crap, user's input got in first
1549 */
1550 struct pollfd pfd;
1551
1552 pfd.fd = STDIN_FILENO;
1553 pfd.events = POLLIN;
1554 if (safe_poll(&pfd, 1, 0) == 0) {
1555 S.sent_ESC_br6n = 1;
1556 out1str("\033" "[6n");
1557 fflush_all(); /* make terminal see it ASAP! */
1558 }
1559}
1560#else
1561#define ask_terminal() ((void)0)
1562#endif
1563
Denis Vlasenko38f63192007-01-22 09:03:07 +00001564#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001565static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001566{
1567 cmdedit_prompt = prmt_ptr;
1568 cmdedit_prmt_len = strlen(prmt_ptr);
1569 put_prompt();
1570}
1571#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001572static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001573{
1574 int prmt_len = 0;
1575 size_t cur_prmt_len = 0;
1576 char flg_not_length = '[';
1577 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001578 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1579 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001580 char c;
1581 char *pbuf;
1582
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001583 cmdedit_prmt_len = 0;
1584
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001585 if (!cwd_buf) {
1586 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001587 }
1588
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001589 cbuf[1] = '\0'; /* never changes */
1590
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001591 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001592 char *free_me = NULL;
1593
1594 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001595 c = *prmt_ptr++;
1596 if (c == '\\') {
1597 const char *cp = prmt_ptr;
1598 int l;
1599
1600 c = bb_process_escape_sequence(&prmt_ptr);
1601 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001602 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001603 break;
1604 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001605
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001606 switch (c) {
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001607# if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001608 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001609 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001610 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001611# endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001612 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001613 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001614 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001615 break;
1616 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001617 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001618 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001619# if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001620 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001621 /* /home/user[/something] -> ~[/something] */
1622 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001623 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001624 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001625 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1626 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1627 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001628 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001629 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001630 }
1631 break;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001632# endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001633 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001634 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001635 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001636 if (cp != NULL && cp != pbuf)
1637 pbuf += (cp-pbuf) + 1;
1638 break;
1639 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001640 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001641 break;
1642 case 'e': case 'E': /* \e \E = \033 */
1643 c = '\033';
1644 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001645 case 'x': case 'X': {
1646 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001647 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001648 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001649 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001650 buf2[l] = '\0';
1651 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001652 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001653 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001654 break;
1655 }
1656 prmt_ptr++;
1657 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001658 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001659 if (c == 0)
1660 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001661 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001662 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001663 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001664 case '[': case ']':
1665 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001666 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001667 continue;
1668 }
1669 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001670 } /* switch */
1671 } /* if */
1672 } /* if */
1673 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001674 cur_prmt_len = strlen(pbuf);
1675 prmt_len += cur_prmt_len;
1676 if (flg_not_length != ']')
1677 cmdedit_prmt_len += cur_prmt_len;
1678 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001679 free(free_me);
1680 } /* while */
1681
1682 if (cwd_buf != (char *)bb_msg_unknown)
1683 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001684 cmdedit_prompt = prmt_mem_ptr;
1685 put_prompt();
1686}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001687#endif
1688
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001689static void cmdedit_setwidth(unsigned w, int redraw_flg)
1690{
1691 cmdedit_termw = w;
1692 if (redraw_flg) {
1693 /* new y for current cursor */
1694 int new_y = (cursor + cmdedit_prmt_len) / w;
1695 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001696 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001697 fflush_all();
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001698 }
1699}
1700
1701static void win_changed(int nsig)
1702{
Denis Vlasenko55995022008-05-18 22:28:26 +00001703 unsigned width;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001704 get_terminal_width_height(0, &width, NULL);
1705 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1706 if (nsig == SIGWINCH)
1707 signal(SIGWINCH, win_changed); /* rearm ourself */
1708}
1709
Denys Vlasenko020f4062009-05-17 16:44:54 +02001710static int lineedit_read_key(char *read_key_buffer)
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001711{
Denys Vlasenko020f4062009-05-17 16:44:54 +02001712 int64_t ic;
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001713 int timeout = -1;
Denys Vlasenko19158a82010-03-26 14:06:56 +01001714#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001715 char unicode_buf[MB_CUR_MAX + 1];
1716 int unicode_idx = 0;
1717#endif
Denys Vlasenko020f4062009-05-17 16:44:54 +02001718
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001719 while (1) {
1720 /* Wait for input. TIMEOUT = -1 makes read_key wait even
1721 * on nonblocking stdin, TIMEOUT = 50 makes sure we won't
1722 * insist on full MB_CUR_MAX buffer to declare input like
1723 * "\xff\n",pause,"ls\n" invalid and thus won't lose "ls".
1724 *
1725 * Note: read_key sets errno to 0 on success.
1726 */
1727 ic = read_key(STDIN_FILENO, read_key_buffer, timeout);
1728 if (errno) {
Denys Vlasenko19158a82010-03-26 14:06:56 +01001729#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001730 if (errno == EAGAIN && unicode_idx != 0)
1731 goto pushback;
Denys Vlasenko1f6d2302009-10-29 03:45:26 +01001732#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001733 break;
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +02001734 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02001735
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01001736#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
1737 if ((int32_t)ic == KEYCODE_CURSOR_POS
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01001738 && S.sent_ESC_br6n
Denys Vlasenko020f4062009-05-17 16:44:54 +02001739 ) {
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01001740 S.sent_ESC_br6n = 0;
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01001741 if (cursor == 0) { /* otherwise it may be bogus */
1742 int col = ((ic >> 32) & 0x7fff) - 1;
1743 if (col > cmdedit_prmt_len) {
1744 cmdedit_x += (col - cmdedit_prmt_len);
1745 while (cmdedit_x >= cmdedit_termw) {
1746 cmdedit_x -= cmdedit_termw;
1747 cmdedit_y++;
1748 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02001749 }
1750 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001751 continue;
Denys Vlasenko020f4062009-05-17 16:44:54 +02001752 }
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01001753#endif
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001754
Denys Vlasenko19158a82010-03-26 14:06:56 +01001755#if ENABLE_UNICODE_SUPPORT
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001756 if (unicode_status == UNICODE_ON) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001757 wchar_t wc;
1758
1759 if ((int32_t)ic < 0) /* KEYCODE_xxx */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001760 break;
1761 // TODO: imagine sequence like: 0xff,<left-arrow>: we are currently losing 0xff...
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001762
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001763 unicode_buf[unicode_idx++] = ic;
1764 unicode_buf[unicode_idx] = '\0';
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001765 if (mbstowcs(&wc, unicode_buf, 1) != 1) {
1766 /* Not (yet?) a valid unicode char */
1767 if (unicode_idx < MB_CUR_MAX) {
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001768 timeout = 50;
1769 continue;
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001770 }
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001771 pushback:
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001772 /* Invalid sequence. Save all "bad bytes" except first */
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001773 read_key_ungets(read_key_buffer, unicode_buf + 1, unicode_idx - 1);
Tomas Heinricha659b812010-04-29 13:43:39 +02001774# if !ENABLE_UNICODE_PRESERVE_BROKEN
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001775 ic = CONFIG_SUBST_WCHAR;
Tomas Heinricha659b812010-04-29 13:43:39 +02001776# else
1777 ic = unicode_mark_inv_wchar(unicode_buf[0]);
1778# endif
Tomas Heinrichd2b04052010-03-09 14:09:24 +01001779 } else {
1780 /* Valid unicode char, return its code */
1781 ic = wc;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001782 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001783 }
1784#endif
Denys Vlasenko58f108e2010-03-11 21:17:55 +01001785 break;
1786 }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001787
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001788 return ic;
1789}
1790
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01001791#if ENABLE_UNICODE_BIDI_SUPPORT
1792static int isrtl_str(void)
1793{
1794 int idx = cursor;
Tomas Heinrichaa167552010-03-26 13:13:24 +01001795
1796 while (idx < command_len && unicode_bidi_is_neutral_wchar(command_ps[idx]))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01001797 idx++;
Tomas Heinrichaa167552010-03-26 13:13:24 +01001798 return unicode_bidi_isrtl(command_ps[idx]);
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01001799}
1800#else
1801# define isrtl_str() 0
1802#endif
1803
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001804/* leave out the "vi-mode"-only case labels if vi editing isn't
1805 * configured. */
Denys Vlasenko13028922009-07-12 00:51:15 +02001806#define vi_case(caselabel) IF_FEATURE_EDITING_VI(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001807
1808/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001809#undef CTRL
1810#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001811
Denys Vlasenko5c2e81b2009-07-16 14:14:34 +02001812/* maxsize must be >= 2.
1813 * Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001814 * -1 on read errors or EOF, or on bare Ctrl-D,
1815 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001816 * >0 length of input string, including terminating '\n'
1817 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001818int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001819{
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00001820 int len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001821#if ENABLE_FEATURE_TAB_COMPLETION
1822 smallint lastWasTab = FALSE;
1823#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001824 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001825#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001826 smallint vi_cmdmode = 0;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001827#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001828 struct termios initial_settings;
1829 struct termios new_settings;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001830 char read_key_buffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001831
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001832 INIT_S();
1833
1834 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1835 || !(initial_settings.c_lflag & ECHO)
1836 ) {
1837 /* Happens when e.g. stty -echo was run before */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001838 parse_and_put_prompt(prompt);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001839 /* fflush_all(); - done by parse_and_put_prompt */
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001840 if (fgets(command, maxsize, stdin) == NULL)
1841 len = -1; /* EOF or error */
1842 else
1843 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001844 DEINIT_S();
1845 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001846 }
1847
Denys Vlasenko28055022010-01-04 20:49:58 +01001848 init_unicode();
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02001849
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001850// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001851 if (maxsize > MAX_LINELEN)
1852 maxsize = MAX_LINELEN;
Denys Vlasenko044b1802009-07-12 02:50:35 +02001853 S.maxsize = maxsize;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001854
1855 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001856 state = st ? st : (line_input_t*) &const_int_0;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001857#if MAX_HISTORY > 0
1858# if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001859 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001860 if (state->cnt_history == 0)
1861 load_history(state);
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001862# endif
Denis Vlasenko3c385cd2008-11-02 00:41:05 +00001863 if (state->flags & DO_HISTORY)
1864 state->cur_history = state->cnt_history;
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001865#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001866
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001867 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001868 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001869 command_len = 0;
Denys Vlasenko19158a82010-03-26 14:06:56 +01001870#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001871 command_ps = xzalloc(maxsize * sizeof(command_ps[0]));
1872#else
Mark Whitley4e338752001-01-26 20:42:23 +00001873 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001874 command[0] = '\0';
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001875#endif
1876#define command command_must_not_be_used
Mark Whitley4e338752001-01-26 20:42:23 +00001877
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001878 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001879 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1880 /* Turn off echoing and CTRL-C, so we can trap it */
1881 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001882 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001883 new_settings.c_cc[VMIN] = 1;
1884 new_settings.c_cc[VTIME] = 0;
1885 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001886#ifndef _POSIX_VDISABLE
Denys Vlasenkodb9c57e2009-09-27 02:48:53 +02001887# define _POSIX_VDISABLE '\0'
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001888#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001889 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko202ac502008-11-05 13:20:58 +00001890 tcsetattr_stdin_TCSANOW(&new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001891
Eric Andersen6faae7d2001-02-16 20:09:17 +00001892 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001893 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1894 win_changed(0); /* do initial resizing */
1895#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1896 {
1897 struct passwd *entry;
1898
1899 entry = getpwuid(geteuid());
1900 if (entry) {
1901 user_buf = xstrdup(entry->pw_name);
1902 home_pwd_buf = xstrdup(entry->pw_dir);
1903 }
1904 }
1905#endif
Denis Vlasenko682ad302008-09-27 01:28:56 +00001906
1907#if 0
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001908 for (i = 0; i <= MAX_HISTORY; i++)
1909 bb_error_msg("history[%d]:'%s'", i, state->history[i]);
Denis Vlasenko682ad302008-09-27 01:28:56 +00001910 bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
1911#endif
1912
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001913 /* Print out the command prompt, optionally ask where cursor is */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001914 parse_and_put_prompt(prompt);
Denys Vlasenko13ad9062009-11-11 03:19:30 +01001915 ask_terminal();
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001916
Denys Vlasenkoc396fe62009-05-17 19:28:14 +02001917 read_key_buffer[0] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001918 while (1) {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001919 /*
1920 * The emacs and vi modes share much of the code in the big
1921 * command loop. Commands entered when in vi's command mode
1922 * (aka "escape mode") get an extra bit added to distinguish
1923 * them - this keeps them from being self-inserted. This
1924 * clutters the big switch a bit, but keeps all the code
1925 * in one place.
1926 */
1927 enum {
1928 VI_CMDMODE_BIT = 0x40000000,
1929 /* 0x80000000 bit flags KEYCODE_xxx */
1930 };
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02001931 int32_t ic, ic_raw;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001932
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001933 fflush_all();
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02001934 ic = ic_raw = lineedit_read_key(read_key_buffer);
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001935
Denis Vlasenko38f63192007-01-22 09:03:07 +00001936#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001937 newdelflag = 1;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001938 if (vi_cmdmode) {
1939 /* btw, since KEYCODE_xxx are all < 0, this doesn't
1940 * change ic if it contains one of them: */
1941 ic |= VI_CMDMODE_BIT;
1942 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001943#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001944
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001945 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001946 case '\n':
1947 case '\r':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001948 vi_case('\n'|VI_CMDMODE_BIT:)
1949 vi_case('\r'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001950 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001951 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001952 break_out = 1;
1953 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001954 case CTRL('A'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001955 vi_case('0'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001956 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001957 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001958 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001959 case CTRL('B'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001960 vi_case('h'|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01001961 vi_case('\b'|VI_CMDMODE_BIT:) /* ^H */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001962 vi_case('\x7f'|VI_CMDMODE_BIT:) /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01001963 input_backward(1); /* Move back one character */
Erik Andersenf0657d32000-04-12 17:49:52 +00001964 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001965 case CTRL('E'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001966 vi_case('$'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001967 /* Control-e -- End of line */
Denys Vlasenko94043e82010-05-11 14:49:13 +02001968 put_till_end_and_adv_cursor();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001969 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001970 case CTRL('F'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001971 vi_case('l'|VI_CMDMODE_BIT:)
1972 vi_case(' '|VI_CMDMODE_BIT:)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01001973 input_forward(); /* Move forward one character */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001974 break;
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01001975 case '\b': /* ^H */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001976 case '\x7f': /* DEL */
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01001977 if (!isrtl_str())
1978 input_backspace();
1979 else
1980 input_delete(0);
1981 break;
1982 case KEYCODE_DELETE:
1983 if (!isrtl_str())
1984 input_delete(0);
1985 else
1986 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001987 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001988#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001989 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001990 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001991 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001992#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001993 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001994 /* Control-k -- clear to end of line */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02001995 command_ps[cursor] = BB_NUL;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001996 command_len = cursor;
Denys Vlasenko94043e82010-05-11 14:49:13 +02001997 printf(SEQ_CLEAR_TILL_END_OF_SCREEN);
Eric Andersen65a07302002-04-13 13:26:49 +00001998 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001999 case CTRL('L'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002000 vi_case(CTRL('L')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002001 /* Control-l -- clear screen */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002002 printf("\033[H"); /* cursor to top,left */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002003 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00002004 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002005#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002006 case CTRL('N'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002007 vi_case(CTRL('N')|VI_CMDMODE_BIT:)
2008 vi_case('j'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002009 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002010 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002011 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00002012 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002013 case CTRL('P'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002014 vi_case(CTRL('P')|VI_CMDMODE_BIT:)
2015 vi_case('k'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00002016 /* Control-p -- Get previous command from history */
Denis Vlasenko682ad302008-09-27 01:28:56 +00002017 if (get_previous_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00002018 goto rewrite_line;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002019 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002020#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002021 case CTRL('U'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002022 vi_case(CTRL('U')|VI_CMDMODE_BIT:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002023 /* Control-U -- Clear line before cursor */
2024 if (cursor) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002025 command_len -= cursor;
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002026 memmove(command_ps, command_ps + cursor,
2027 (command_len + 1) * sizeof(command_ps[0]));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002028 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002029 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002030 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002031 case CTRL('W'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002032 vi_case(CTRL('W')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00002033 /* Control-W -- Remove the last word */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002034 while (cursor > 0 && BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002035 input_backspace();
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002036 while (cursor > 0 && !BB_isspace(command_ps[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00002037 input_backspace();
2038 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002039
Denis Vlasenko38f63192007-01-22 09:03:07 +00002040#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002041 case 'i'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002042 vi_cmdmode = 0;
2043 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002044 case 'I'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002045 input_backward(cursor);
2046 vi_cmdmode = 0;
2047 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002048 case 'a'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002049 input_forward();
2050 vi_cmdmode = 0;
2051 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002052 case 'A'|VI_CMDMODE_BIT:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002053 put_till_end_and_adv_cursor();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002054 vi_cmdmode = 0;
2055 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002056 case 'x'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002057 input_delete(1);
2058 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002059 case 'X'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002060 if (cursor > 0) {
2061 input_backward(1);
2062 input_delete(1);
2063 }
2064 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002065 case 'W'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002066 vi_Word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002067 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002068 case 'w'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002069 vi_word_motion(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +00002070 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002071 case 'E'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002072 vi_End_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002073 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002074 case 'e'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002075 vi_end_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002076 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002077 case 'B'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002078 vi_Back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002079 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002080 case 'b'|VI_CMDMODE_BIT:
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002081 vi_back_motion();
Paul Fox3f11b1b2005-08-04 19:04:46 +00002082 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002083 case 'C'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002084 vi_cmdmode = 0;
2085 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002086 case 'D'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002087 goto clear_to_eol;
2088
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002089 case 'c'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002090 vi_cmdmode = 0;
2091 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002092 case 'd'|VI_CMDMODE_BIT: {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002093 int nc, sc;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002094
Denys Vlasenko020f4062009-05-17 16:44:54 +02002095 ic = lineedit_read_key(read_key_buffer);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002096 if (errno) /* error */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002097 goto prepare_to_die;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002098 if (ic == ic_raw) { /* "cc", "dd" */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002099 input_backward(cursor);
2100 goto clear_to_eol;
2101 break;
2102 }
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002103
2104 sc = cursor;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002105 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002106 case 'w':
2107 case 'W':
2108 case 'e':
2109 case 'E':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002110 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002111 case 'w': /* "dw", "cw" */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002112 vi_word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002113 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002114 case 'W': /* 'dW', 'cW' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002115 vi_Word_motion(vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00002116 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002117 case 'e': /* 'de', 'ce' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002118 vi_end_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002119 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002120 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002121 case 'E': /* 'dE', 'cE' */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002122 vi_End_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002123 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00002124 break;
2125 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002126 nc = cursor;
2127 input_backward(cursor - sc);
2128 while (nc-- > cursor)
2129 input_delete(1);
2130 break;
2131 case 'b': /* "db", "cb" */
2132 case 'B': /* implemented as B */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002133 if (ic == 'b')
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002134 vi_back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002135 else
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002136 vi_Back_motion();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002137 while (sc-- > cursor)
2138 input_delete(1);
2139 break;
2140 case ' ': /* "d ", "c " */
2141 input_delete(1);
2142 break;
2143 case '$': /* "d$", "c$" */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002144 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002145 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002146 input_delete(1);
2147 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00002148 }
2149 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002150 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002151 case 'p'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002152 input_forward();
2153 /* fallthrough */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002154 case 'P'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00002155 put();
2156 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002157 case 'r'|VI_CMDMODE_BIT:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002158//FIXME: unicode case?
Denys Vlasenko020f4062009-05-17 16:44:54 +02002159 ic = lineedit_read_key(read_key_buffer);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002160 if (errno) /* error */
Paul Fox3f11b1b2005-08-04 19:04:46 +00002161 goto prepare_to_die;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002162 if (ic < ' ' || ic > 255) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00002163 beep();
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002164 } else {
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002165 command_ps[cursor] = ic;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002166 bb_putchar(ic);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002167 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00002168 }
2169 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002170 case '\x1b': /* ESC */
2171 if (state->flags & VI_MODE) {
2172 /* insert mode --> command mode */
2173 vi_cmdmode = 1;
2174 input_backward(1);
2175 }
2176 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002177#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00002178
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00002179#if MAX_HISTORY > 0
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002180 case KEYCODE_UP:
2181 if (get_previous_history())
2182 goto rewrite_line;
2183 beep();
2184 break;
2185 case KEYCODE_DOWN:
2186 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002187 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002188 rewrite_line:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002189 /* Rewrite the line with the selected history item */
2190 /* change command */
Denys Vlasenko90a99042009-09-06 02:36:23 +02002191 command_len = load_string(state->history[state->cur_history] ?
2192 state->history[state->cur_history] : "", maxsize);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002193 /* redraw and go to eol (bol, in vi) */
2194 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
2195 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00002196#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002197 case KEYCODE_RIGHT:
2198 input_forward();
2199 break;
2200 case KEYCODE_LEFT:
2201 input_backward(1);
2202 break;
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +01002203 case KEYCODE_CTRL_LEFT:
2204 ctrl_left();
2205 break;
2206 case KEYCODE_CTRL_RIGHT:
2207 ctrl_right();
2208 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002209 case KEYCODE_HOME:
2210 input_backward(cursor);
2211 break;
2212 case KEYCODE_END:
Denys Vlasenko94043e82010-05-11 14:49:13 +02002213 put_till_end_and_adv_cursor();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002214 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002215
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002216 default:
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002217 if (initial_settings.c_cc[VINTR] != 0
2218 && ic_raw == initial_settings.c_cc[VINTR]
2219 ) {
2220 /* Ctrl-C (usually) - stop gathering input */
2221 goto_new_line();
2222 command_len = 0;
2223 break_out = -1; /* "do not append '\n'" */
2224 break;
2225 }
2226 if (initial_settings.c_cc[VEOF] != 0
2227 && ic_raw == initial_settings.c_cc[VEOF]
2228 ) {
2229 /* Ctrl-D (usually) - delete one character,
2230 * or exit if len=0 and no chars to delete */
2231 if (command_len == 0) {
2232 errno = 0;
2233#if ENABLE_FEATURE_EDITING_VI
2234 prepare_to_die:
2235#endif
2236 break_out = command_len = -1;
2237 break;
2238 }
2239 input_delete(0);
2240 break;
2241 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002242// /* Control-V -- force insert of next char */
2243// if (c == CTRL('V')) {
2244// if (safe_read(STDIN_FILENO, &c, 1) < 1)
2245// goto prepare_to_die;
2246// if (c == 0) {
2247// beep();
2248// break;
2249// }
2250// }
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002251 if (ic < ' '
Denys Vlasenko19158a82010-03-26 14:06:56 +01002252 || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2253 || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002254 ) {
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002255 /* If VI_CMDMODE_BIT is set, ic is >= 256
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002256 * and vi mode ignores unexpected chars.
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002257 * Otherwise, we are here if ic is a
2258 * control char or an unhandled ESC sequence,
2259 * which is also ignored.
2260 */
2261 break;
2262 }
2263 if ((int)command_len >= (maxsize - 2)) {
2264 /* Not enough space for the char and EOL */
2265 break;
Paul Fox84bbac52008-01-11 16:50:08 +00002266 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00002267
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002268 command_len++;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002269 if (cursor == (command_len - 1)) {
2270 /* We are at the end, append */
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002271 command_ps[cursor] = ic;
2272 command_ps[cursor + 1] = BB_NUL;
Denys Vlasenko94043e82010-05-11 14:49:13 +02002273 put_cur_glyph_and_inc_cursor();
Tomas Heinrichaa167552010-03-26 13:13:24 +01002274 if (unicode_bidi_isrtl(ic))
Tomas Heinrichc5c006c2010-03-18 18:35:37 +01002275 input_backward(1);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002276 } else {
2277 /* In the middle, insert */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002278 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00002279
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002280 memmove(command_ps + sc + 1, command_ps + sc,
2281 (command_len - sc) * sizeof(command_ps[0]));
2282 command_ps[sc] = ic;
Tomas Heinrichaa167552010-03-26 13:13:24 +01002283 /* is right-to-left char, or neutral one (e.g. comma) was just added to rtl text? */
2284 if (!isrtl_str())
2285 sc++; /* no */
Denys Vlasenko94043e82010-05-11 14:49:13 +02002286 put_till_end_and_adv_cursor();
Mark Whitley4e338752001-01-26 20:42:23 +00002287 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002288 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00002289 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00002290 break;
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002291 } /* switch (ic) */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002292
2293 if (break_out)
Erik Andersenc7c634b2000-03-19 05:28:55 +00002294 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002295
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002296#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenko04bb6b62009-10-14 12:53:04 +02002297 if (ic_raw != '\t')
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002298 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002299#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02002300 } /* while (1) */
Erik Andersenc7c634b2000-03-19 05:28:55 +00002301
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002302#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
Denys Vlasenkod83bbf42009-10-27 10:47:49 +01002303 if (S.sent_ESC_br6n) {
Denys Vlasenkoeb62d7c2009-10-27 10:34:06 +01002304 /* "sleep 1; busybox ash" + hold [Enter] to trigger.
2305 * We sent "ESC [ 6 n", but got '\n' first, and
2306 * KEYCODE_CURSOR_POS response is now buffered from terminal.
2307 * It's bad already and not much can be done with it
2308 * (it _will_ be visible for the next process to read stdin),
2309 * but without this delay it even shows up on the screen
2310 * as garbage because we restore echo settings with tcsetattr
2311 * before it comes in. UGLY!
2312 */
2313 usleep(20*1000);
2314 }
2315#endif
2316
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002317/* Stop bug catching using "command_must_not_be_used" trick */
2318#undef command
2319
Denys Vlasenko19158a82010-03-26 14:06:56 +01002320#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko2f3f09c2009-09-29 00:00:12 +02002321 command[0] = '\0';
2322 if (command_len > 0)
2323 command_len = save_string(command, maxsize - 1);
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002324 free(command_ps);
2325#endif
2326
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002327 if (command_len > 0)
2328 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002329
Eric Andersen27bb7902003-12-23 20:24:51 +00002330 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002331 command[command_len++] = '\n';
2332 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00002333 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002334
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002335#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00002336 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002337#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002338
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002339 /* restore initial_settings */
Denis Vlasenko202ac502008-11-05 13:20:58 +00002340 tcsetattr_stdin_TCSANOW(&initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00002341 /* restore SIGWINCH handler */
2342 signal(SIGWINCH, previous_SIGWINCH_handler);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002343 fflush_all();
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002344
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002345 len = command_len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00002346 DEINIT_S();
2347
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00002348 return len; /* can't return command_len, DEINIT_S() destroys it */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002349}
2350
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002351#else
2352
2353#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00002354int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002355{
2356 fputs(prompt, stdout);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002357 fflush_all();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002358 fgets(command, maxsize, stdin);
2359 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00002360}
2361
Denys Vlasenko2e6d4ef2009-07-10 18:40:49 +02002362#endif /* FEATURE_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00002363
2364
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002365/*
2366 * Testing
2367 */
2368
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002369#ifdef TEST
2370
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002371#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00002372
2373const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002374
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002375int main(int argc, char **argv)
2376{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00002377 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002378 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00002379#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002380 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
2381 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
2382 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002383#else
2384 "% ";
2385#endif
2386
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002387#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002388 setlocale(LC_ALL, "");
2389#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002390 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00002391 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002392 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002393 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00002394 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002395 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002396 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00002397 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002398 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002399 return 0;
2400}
2401
Eric Andersenc470f442003-07-28 09:56:35 +00002402#endif /* TEST */