blob: e0ab732502c51b4638a78ea94edf756ff49ad44d [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:
19 * Terminal key codes are not extensive, and more will probably
20 * need to be added. This version was created on Debian GNU/Linux 2.x.
21 * 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 *
25 * lineedit does not know that the terminal escape sequences do not
26 * take up space on the screen. The redisplay code assumes, unless
27 * told otherwise, that each character in the prompt is a printable
28 * character that takes up one character position on the screen.
29 * You need to tell lineedit that some sequences of characters
30 * in the prompt take up no screen space. Compatibly with readline,
31 * use the \[ escape to begin a sequence of non-printing characters,
32 * and the \] escape to signal the end of such a sequence. Example:
33 *
34 * PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '
Erik Andersen13456d12000-03-16 08:09:57 +000035 */
36
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000037#include "libbb.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000038
Glenn L McGrath475820c2004-01-22 12:42:23 +000039
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000040/* FIXME: obsolete CONFIG item? */
41#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
42
43
Glenn L McGrath3b251852004-01-03 12:07:32 +000044#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000045
Denis Vlasenko38f63192007-01-22 09:03:07 +000046#define ENABLE_FEATURE_EDITING 0
47#define ENABLE_FEATURE_TAB_COMPLETION 0
48#define ENABLE_FEATURE_USERNAME_COMPLETION 0
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000049#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +000050
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000051#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000052
Eric Andersen5165fbe2001-02-20 06:42:29 +000053
Denis Vlasenko82b39e82007-01-21 19:18:19 +000054/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko38f63192007-01-22 09:03:07 +000055#if ENABLE_FEATURE_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000056
Denis Vlasenko82b39e82007-01-21 19:18:19 +000057#if ENABLE_LOCALE_SUPPORT
58#define Isprint(c) isprint(c)
59#else
60#define Isprint(c) ((c) >= ' ' && (c) != ((unsigned char)'\233'))
61#endif
62
Denis Vlasenko7e46cf72006-12-23 01:21:55 +000063#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000064 (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000065#define IF_FEATURE_GETUSERNAME_AND_HOMEDIR(...)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000066#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000067#undef IF_FEATURE_GETUSERNAME_AND_HOMEDIR
68#define IF_FEATURE_GETUSERNAME_AND_HOMEDIR(...) __VA_ARGS__
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000069#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000070
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000071enum {
72 /* We use int16_t for positions, need to limit line len */
73 MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN < 0x7ff0
Denis Vlasenko6b404432008-01-07 16:13:14 +000074 ? CONFIG_FEATURE_EDITING_MAX_LEN
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000075 : 0x7ff0
76};
Robert Griebl350d26b2002-12-03 22:45:46 +000077
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000078#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
79static const char null_str[] ALIGN1 = "";
80#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +000081
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000082/* We try to minimize both static and stack usage. */
Denis Vlasenko5d89fba2008-04-22 00:08:27 +000083struct lineedit_statics {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000084 line_input_t *state;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000085
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000086 volatile unsigned cmdedit_termw; /* = 80; */ /* actual terminal width */
87 sighandler_t previous_SIGWINCH_handler;
Mark Whitley4e338752001-01-26 20:42:23 +000088
Denis Vlasenkob267ed92008-05-25 21:52:03 +000089 unsigned cmdedit_x; /* real x terminal position */
90 unsigned cmdedit_y; /* pseudoreal y terminal position */
91 unsigned cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000092
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000093 unsigned cursor;
94 unsigned command_len;
95 char *command_ps;
96
97 const char *cmdedit_prompt;
Denis Vlasenko38f63192007-01-22 09:03:07 +000098#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000099 int num_ok_lines; /* = 1; */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000100#endif
101
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000102#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000103 char *user_buf;
104 char *home_pwd_buf; /* = (char*)null_str; */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000105#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000106
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000107#if ENABLE_FEATURE_TAB_COMPLETION
108 char **matches;
109 unsigned num_matches;
110#endif
111
112#if ENABLE_FEATURE_EDITING_VI
113#define DELBUFSIZ 128
114 char *delptr;
115 smallint newdelflag; /* whether delbuf should be reused yet */
116 char delbuf[DELBUFSIZ]; /* a place to store deleted characters */
117#endif
118
119 /* Formerly these were big buffers on stack: */
120#if ENABLE_FEATURE_TAB_COMPLETION
121 char exe_n_cwd_tab_completion__dirbuf[MAX_LINELEN];
122 char input_tab__matchBuf[MAX_LINELEN];
123 int16_t find_match__int_buf[MAX_LINELEN + 1]; /* need to have 9 bits at least */
124 int16_t find_match__pos_buf[MAX_LINELEN + 1];
125#endif
126};
127
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000128/* See lineedit_ptr_hack.c */
129extern struct lineedit_statics *const lineedit_ptr_to_statics;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000130
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000131#define S (*lineedit_ptr_to_statics)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000132#define state (S.state )
133#define cmdedit_termw (S.cmdedit_termw )
134#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
135#define cmdedit_x (S.cmdedit_x )
136#define cmdedit_y (S.cmdedit_y )
137#define cmdedit_prmt_len (S.cmdedit_prmt_len)
138#define cursor (S.cursor )
139#define command_len (S.command_len )
140#define command_ps (S.command_ps )
141#define cmdedit_prompt (S.cmdedit_prompt )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000142#define num_ok_lines (S.num_ok_lines )
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000143#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000144#define home_pwd_buf (S.home_pwd_buf )
145#define matches (S.matches )
146#define num_matches (S.num_matches )
147#define delptr (S.delptr )
148#define newdelflag (S.newdelflag )
149#define delbuf (S.delbuf )
150
151#define INIT_S() do { \
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000152 (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000153 barrier(); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000154 cmdedit_termw = 80; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000155 IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
156 IF_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000157} while (0)
158static void deinit_S(void)
159{
160#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000161 /* This one is allocated only if FANCY_PROMPT is on
162 * (otherwise it points to verbatim prompt (NOT malloced) */
163 free((char*)cmdedit_prompt);
164#endif
165#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
166 free(user_buf);
167 if (home_pwd_buf != null_str)
168 free(home_pwd_buf);
169#endif
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000170 free(lineedit_ptr_to_statics);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000171}
172#define DEINIT_S() deinit_S()
173
Denis Vlasenko2c844952008-04-25 18:44:35 +0000174
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000175/* Put 'command_ps[cursor]', cursor++.
176 * Advance cursor on screen. If we reached right margin, scroll text up
177 * and remove terminal margin effect by printing 'next_char' */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000178#define HACK_FOR_WRONG_WIDTH 1
179#if HACK_FOR_WRONG_WIDTH
180static void cmdedit_set_out_char(void)
181#define cmdedit_set_out_char(next_char) cmdedit_set_out_char()
182#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000183static void cmdedit_set_out_char(int next_char)
Denis Vlasenko2c844952008-04-25 18:44:35 +0000184#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000185{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000186 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000187
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000188 if (c == '\0') {
189 /* erase character after end of input string */
190 c = ' ';
191 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000192#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000193 /* Display non-printable characters in reverse */
194 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000195 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000196 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000197 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000198 c += '@';
199 if (c == 127)
200 c = '?';
201 printf("\033[7m%c\033[0m", c);
202 } else
203#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000204 {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000205 bb_putchar(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000206 }
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000207 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000208 /* terminal is scrolled down */
209 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000210 cmdedit_x = 0;
Denis Vlasenko2c844952008-04-25 18:44:35 +0000211#if HACK_FOR_WRONG_WIDTH
212 /* This works better if our idea of term width is wrong
213 * and it is actually wider (often happens on serial lines).
214 * Printing CR,LF *forces* cursor to next line.
215 * OTOH if terminal width is correct AND terminal does NOT
216 * have automargin (IOW: it is moving cursor to next line
217 * by itself (which is wrong for VT-10x terminals)),
218 * this will break things: there will be one extra empty line */
219 puts("\r"); /* + implicit '\n' */
220#else
221 /* Works ok only if cmdedit_termw is correct */
Mark Whitley4e338752001-01-26 20:42:23 +0000222 /* destroy "(auto)margin" */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000223 bb_putchar(next_char);
224 bb_putchar('\b');
Denis Vlasenko2c844952008-04-25 18:44:35 +0000225#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000226 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000227// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000228 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000229}
230
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000231/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000232static void input_end(void)
233{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000234 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000235 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000236}
237
238/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000239static void goto_new_line(void)
240{
Mark Whitley4e338752001-01-26 20:42:23 +0000241 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000242 if (cmdedit_x)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000243 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000244}
245
246
Rob Landley88621d72006-08-29 19:41:06 +0000247static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000248{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000249 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000250 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000251}
Eric Andersen81fe1232003-07-29 06:38:40 +0000252
Rob Landley88621d72006-08-29 19:41:06 +0000253static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000254{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000255 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000256}
257
Eric Andersenaff114c2004-04-14 17:51:38 +0000258/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000259/* (optimized for slow terminals) */
260static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000261{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000262 int count_y;
263
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000264 if (num > cursor)
265 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000266 if (!num)
267 return;
268 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000269
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000270 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000271 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000272 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000273 /* This is longer by 5 bytes on x86.
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000274 * Also gets miscompiled for ARM users
275 * (busybox.net/bugs/view.php?id=2274).
Denis Vlasenko6f043912008-02-18 22:28:03 +0000276 * printf(("\b\b\b\b" + 4) - num);
277 * return;
278 */
279 do {
280 bb_putchar('\b');
281 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000282 return;
283 }
284 printf("\033[%uD", num);
285 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000286 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000287
288 /* Need to go one or more lines up */
289 num -= cmdedit_x;
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000290 {
291 unsigned w = cmdedit_termw; /* volatile var */
292 count_y = 1 + (num / w);
293 cmdedit_y -= count_y;
294 cmdedit_x = w * count_y - num;
295 }
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000296 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000297 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000298}
299
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000300static void put_prompt(void)
301{
302 out1str(cmdedit_prompt);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000303 cursor = 0;
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000304 {
305 unsigned w = cmdedit_termw; /* volatile var */
306 cmdedit_y = cmdedit_prmt_len / w; /* new quasireal y */
307 cmdedit_x = cmdedit_prmt_len % w;
308 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000309}
310
Eric Andersenaff114c2004-04-14 17:51:38 +0000311/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000312static void redraw(int y, int back_cursor)
313{
Eric Andersenc470f442003-07-28 09:56:35 +0000314 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000315 printf("\033[%dA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000316 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000317 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000318 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000319 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000320 input_backward(back_cursor);
321}
322
Paul Fox3f11b1b2005-08-04 19:04:46 +0000323/* Delete the char in front of the cursor, optionally saving it
324 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000325#if !ENABLE_FEATURE_EDITING_VI
326static void input_delete(void)
327#define input_delete(save) input_delete()
328#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000329static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000330#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000331{
Mark Whitley4e338752001-01-26 20:42:23 +0000332 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000333
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000334 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000335 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000336
Denis Vlasenko38f63192007-01-22 09:03:07 +0000337#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000338 if (save) {
339 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000340 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000341 newdelflag = 0;
342 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000343 if ((delptr - delbuf) < DELBUFSIZ)
344 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000345 }
346#endif
347
Denis Vlasenko41660c52008-07-22 20:25:24 +0000348 overlapping_strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000349 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000350 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000351 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000352 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000353}
354
Denis Vlasenko38f63192007-01-22 09:03:07 +0000355#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000356static void put(void)
357{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000358 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000359 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000360
Paul Fox3f11b1b2005-08-04 19:04:46 +0000361 if (j == 0)
362 return;
363 ocursor = cursor;
364 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000365 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000366 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000367 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000368 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000369 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000370}
371#endif
372
Mark Whitley4e338752001-01-26 20:42:23 +0000373/* Delete the char in back of the cursor */
374static void input_backspace(void)
375{
376 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000377 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000378 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000379 }
380}
381
Eric Andersenaff114c2004-04-14 17:51:38 +0000382/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000383static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000384{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000385 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000386 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000387}
388
Denis Vlasenko38f63192007-01-22 09:03:07 +0000389#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000390
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000391static void free_tab_completion_data(void)
392{
393 if (matches) {
394 while (num_matches)
395 free(matches[--num_matches]);
396 free(matches);
397 matches = NULL;
398 }
399}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000400
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000401static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000402{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000403 matches = xrealloc_vector(matches, 4, num_matches);
404 matches[num_matches] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000405 num_matches++;
406}
407
Denis Vlasenko38f63192007-01-22 09:03:07 +0000408#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000409static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000410{
411 struct passwd *entry;
412 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000413
Eric Andersenc470f442003-07-28 09:56:35 +0000414 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000415 userlen = strlen(ud);
416
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000417 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000418 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000419 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000420
Eric Andersenc470f442003-07-28 09:56:35 +0000421 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000422 home = home_pwd_buf;
423 } else {
424 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000425 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000426 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000427 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000428 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000429 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430 ud = temp;
431 if (entry)
432 home = entry->pw_dir;
433 }
434 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000435 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000436 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000437 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000438 }
439 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000440 } else {
441 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000442 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000443 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000444 struct passwd pwd;
445 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000446
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000447 setpwent();
448 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000449 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000450 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
451 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000452 }
453 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000454 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000455 }
456}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000457#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000458
459enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000460 FIND_EXE_ONLY = 0,
461 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000462 FIND_FILE_ONLY = 2,
463};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000464
Mark Whitley4e338752001-01-26 20:42:23 +0000465static int path_parse(char ***p, int flags)
466{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000467 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000468 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000469 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000470 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000471
Mark Whitley4e338752001-01-26 20:42:23 +0000472 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000473 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000474 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000475
476 if (state->flags & WITH_PATH_LOOKUP)
477 pth = state->path_lookup;
478 else
479 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000480 /* PATH=<empty> or PATH=:<empty> */
481 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
482 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000483
Denis Vlasenko253ce002007-01-22 08:34:44 +0000484 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000485 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000486 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000487 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000488 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000489 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000490 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000491 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000492 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000493 }
494
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000495 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000496 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000497 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000498 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000499 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000500 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000501 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000502 *tmp++ = '\0'; /* ':' -> '\0' */
503 if (*tmp == '\0')
504 break; /* :<empty> */
505 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000506 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000507 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000508 return npth;
509}
510
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000511static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000512{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000513 DIR *dir;
514 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000515 struct stat st;
516 char *path1[1];
517 char **paths = path1;
518 int npaths;
519 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000520 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000521 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000522/* char dirbuf[MAX_LINELEN]; */
523#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000524
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000525 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000526 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000527
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000528 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000529 /* no dir, if flags==EXE_ONLY - get paths, else "." */
530 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000531 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000532 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000533 /* dirbuf = ".../.../.../" */
534 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000535#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000536 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000537 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000538#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000539 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000540 /* point to 'l' in "..../last_component" */
541 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000542 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000543
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000544 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000545 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000546 if (!dir)
547 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000548
549 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000550 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000551 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000552
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000553 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000554 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000555 continue;
556 /* not see .name without .match */
Denis Vlasenkob5202712008-04-24 04:42:52 +0000557 if (*str_found == '.' && *pfind == '\0') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000558 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000559 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000560 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000561 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000562 found = concat_path_file(paths[i], str_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000563 /* hmm, remove in progress? */
564 /* NB: stat() first so that we see is it a directory;
565 * but if that fails, use lstat() so that
566 * we still match dangling links */
567 if (stat(found, &st) && lstat(found, &st))
Eric Andersene5dfced2001-04-09 22:48:12 +0000568 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000569 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000570 if (paths[i] != dirbuf)
Denis Vlasenkob5202712008-04-24 04:42:52 +0000571 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000572
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000573 len1 = strlen(found);
574 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000575 found[len1] = '\0';
576 found[len1+1] = '\0';
577
Mark Whitley4e338752001-01-26 20:42:23 +0000578 if (S_ISDIR(st.st_mode)) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000579 /* name is a directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000580 if (found[len1-1] != '/') {
581 found[len1] = '/';
582 }
Mark Whitley4e338752001-01-26 20:42:23 +0000583 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000584 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000585 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000586 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000587 }
Mark Whitley4e338752001-01-26 20:42:23 +0000588 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000589 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000590 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000591 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000592 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000593 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000594 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000595 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000596 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000597 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000598 free(paths);
599 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000600#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000601}
Erik Andersenf0657d32000-04-12 17:49:52 +0000602
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000603#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000604
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000605#define collapse_pos(is, in) do { \
606 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
607 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
608} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000609
610static int find_match(char *matchBuf, int *len_with_quotes)
611{
612 int i, j;
613 int command_mode;
614 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000615/* int16_t int_buf[MAX_LINELEN + 1]; */
616/* int16_t pos_buf[MAX_LINELEN + 1]; */
617#define int_buf (S.find_match__int_buf)
618#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000619
620 /* set to integer dimension characters and own positions */
621 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000622 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000623 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000624 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000625 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000626 }
627 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000628 }
629
630 /* mask \+symbol and convert '\t' to ' ' */
631 for (i = j = 0; matchBuf[i]; i++, j++)
632 if (matchBuf[i] == '\\') {
633 collapse_pos(j, j + 1);
634 int_buf[j] |= QUOT;
635 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000636#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000637 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000638 int_buf[j] = ' ' | QUOT;
639#endif
640 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000641#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000642 else if (matchBuf[i] == '\t')
643 int_buf[j] = ' ';
644#endif
645
646 /* mask "symbols" or 'symbols' */
647 c2 = 0;
648 for (i = 0; int_buf[i]; i++) {
649 c = int_buf[i];
650 if (c == '\'' || c == '"') {
651 if (c2 == 0)
652 c2 = c;
653 else {
654 if (c == c2)
655 c2 = 0;
656 else
657 int_buf[i] |= QUOT;
658 }
659 } else if (c2 != 0 && c != '$')
660 int_buf[i] |= QUOT;
661 }
662
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000663 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000664 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
665 for (i = 0; int_buf[i]; i++) {
666 c = int_buf[i];
667 c2 = int_buf[i + 1];
668 j = i ? int_buf[i - 1] : -1;
669 command_mode = 0;
670 if (c == ';' || c == '&' || c == '|') {
671 command_mode = 1 + (c == c2);
672 if (c == '&') {
673 if (j == '>' || j == '<')
674 command_mode = 0;
675 } else if (c == '|' && j == '>')
676 command_mode = 0;
677 }
678 if (command_mode) {
679 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000680 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000681 }
682 }
683 /* collapse `command...` */
684 for (i = 0; int_buf[i]; i++)
685 if (int_buf[i] == '`') {
686 for (j = i + 1; int_buf[j]; j++)
687 if (int_buf[j] == '`') {
688 collapse_pos(i, j + 1);
689 j = 0;
690 break;
691 }
692 if (j) {
693 /* not found close ` - command mode, collapse all previous */
694 collapse_pos(0, i + 1);
695 break;
696 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000697 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000698 }
699
700 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000701 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000702 c2 = 0;
703 for (i = 0; int_buf[i]; i++)
704 if (int_buf[i] == '(' || int_buf[i] == '{') {
705 if (int_buf[i] == '(')
706 c++;
707 else
708 c2++;
709 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000710 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000711 }
712 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
713 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
714 if (int_buf[i] == ')')
715 c--;
716 else
717 c2--;
718 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000719 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000720 }
721
722 /* skip first not quote space */
723 for (i = 0; int_buf[i]; i++)
724 if (int_buf[i] != ' ')
725 break;
726 if (i)
727 collapse_pos(0, i);
728
729 /* set find mode for completion */
730 command_mode = FIND_EXE_ONLY;
731 for (i = 0; int_buf[i]; i++)
732 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
733 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000734 && matchBuf[pos_buf[0]] == 'c'
735 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000736 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000737 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000738 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000739 command_mode = FIND_FILE_ONLY;
740 break;
741 }
742 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000743 for (i = 0; int_buf[i]; i++)
744 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000745 /* find last word */
746 for (--i; i >= 0; i--) {
747 c = int_buf[i];
748 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
749 collapse_pos(0, i + 1);
750 break;
751 }
752 }
753 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000754 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
755 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000756 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000757 while ((int_buf[i] & ~QUOT) == '/'
758 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
759 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000760 i++;
761 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000762
763 /* set only match and destroy quotes */
764 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000765 for (c = 0; pos_buf[i] >= 0; i++) {
766 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000767 j = pos_buf[i] + 1;
768 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000769 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000770 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000771 *len_with_quotes = j ? j - pos_buf[0] : 0;
772
773 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000774#undef int_buf
775#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000776}
777
Glenn L McGrath4d001292003-01-06 01:11:50 +0000778/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000779 * display by column (original idea from ls applet,
780 * very optimized by me :)
781 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000782static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000783{
784 int ncols, row;
785 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000786 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000787 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000788 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000789
790 /* find the longest file name- use that as the column width */
791 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000792 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000793 if (column_width < l)
794 column_width = l;
795 }
796 column_width += 2; /* min space for columns */
797 ncols = cmdedit_termw / column_width;
798
799 if (ncols > 1) {
800 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000801 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000802 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000803 } else {
804 ncols = 1;
805 }
806 for (row = 0; row < nrows; row++) {
807 int n = row;
808 int nc;
809
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000810 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000811 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000812 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000813 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000814 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000815 }
816}
817
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000818static char *add_quote_for_spec_chars(char *found)
819{
820 int l = 0;
821 char *s = xmalloc((strlen(found) + 1) * 2);
822
823 while (*found) {
824 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
825 s[l++] = '\\';
826 s[l++] = *found++;
827 }
828 s[l] = 0;
829 return s;
830}
831
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000832/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000833static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000834{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000835 if (!(state->flags & TAB_COMPLETION))
836 return;
837
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000838 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000839 char *tmp, *tmp1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000840 size_t len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000841/* char matchBuf[MAX_LINELEN]; */
842#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000843 int find_type;
844 int recalc_pos;
845
Eric Andersenc470f442003-07-28 09:56:35 +0000846 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000847
848 /* Make a local copy of the string -- up
849 * to the position of the cursor */
850 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000851 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000852
853 find_type = find_match(matchBuf, &recalc_pos);
854
855 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000856 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000857
Denis Vlasenko38f63192007-01-22 09:03:07 +0000858#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000859 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000860 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000861 if (state->flags & USERNAME_COMPLETION)
862 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
863 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000864#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000865 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000866 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000867 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000868 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000869 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000870 if (matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000871 unsigned i;
872 int n = 0;
Denis Vlasenkofb290382008-03-02 12:51:26 +0000873 qsort_string_vector(matches, num_matches);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000874 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000875 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000876 if (strcmp(matches[i], matches[i+1]) == 0) {
877 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000878 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000879 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000880 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000881 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000882 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000883 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000884 matches[n] = matches[i];
885 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000886 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000887 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000888 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000889 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000890 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000891 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000892 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000893 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000894 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000895 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000896 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000897 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000898 break;
899 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000900 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000901 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000902 return;
903 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000904 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000905 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000906 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000907 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000908 /* for next completion current found */
909 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000910
911 len_found = strlen(tmp);
912 if (tmp[len_found-1] != '/') {
913 tmp[len_found] = ' ';
914 tmp[len_found+1] = '\0';
915 }
Mark Whitley4e338752001-01-26 20:42:23 +0000916 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000917 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000918 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000919 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000920 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000921 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000922 /* save tail line */
923 strcpy(matchBuf, command_ps + cursor);
924 /* add match */
925 strcat(command_ps, tmp);
926 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000927 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000928 /* back to begin word for match */
929 input_backward(recalc_pos);
930 /* new pos */
931 recalc_pos = cursor + len_found;
932 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000933 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000934 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000935 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000936 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000937 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000938#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000939 } else {
940 /* Ok -- the last char was a TAB. Since they
941 * just hit TAB again, print a list of all the
942 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000943 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000944 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000945
946 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000947 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000948 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000949 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000950 }
951 }
952}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000953
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000954#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000955
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000956
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +0000957line_input_t* FAST_FUNC new_line_input_t(int flags)
958{
959 line_input_t *n = xzalloc(sizeof(*n));
960 n->flags = flags;
961 return n;
962}
963
964
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000965#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000966
Denis Vlasenko682ad302008-09-27 01:28:56 +0000967static void save_command_ps_at_cur_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000968{
Denis Vlasenko682ad302008-09-27 01:28:56 +0000969 if (command_ps[0] != '\0') {
970 int cur = state->cur_history;
971 free(state->history[cur]);
972 state->history[cur] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000973 }
Denis Vlasenko682ad302008-09-27 01:28:56 +0000974}
975
976/* state->flags is already checked to be nonzero */
977static int get_previous_history(void)
978{
979 if ((state->flags & DO_HISTORY) && state->cur_history) {
980 save_command_ps_at_cur_history();
981 state->cur_history--;
982 return 1;
983 }
984 beep();
985 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000986}
987
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000988static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000989{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000990 if (state->flags & DO_HISTORY) {
Denis Vlasenko682ad302008-09-27 01:28:56 +0000991 if (state->cur_history < state->cnt_history) {
992 save_command_ps_at_cur_history(); /* save the current history line */
993 return ++state->cur_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000994 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000995 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000996 beep();
997 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000998}
Robert Griebl350d26b2002-12-03 22:45:46 +0000999
Denis Vlasenko38f63192007-01-22 09:03:07 +00001000#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001001/* We try to ensure that concurrent additions to the history
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001002 * do not overwrite each other.
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001003 * Otherwise shell users get unhappy.
1004 *
1005 * History file is trimmed lazily, when it grows several times longer
1006 * than configured MAX_HISTORY lines.
1007 */
1008
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001009static void free_line_input_t(line_input_t *n)
1010{
1011 int i = n->cnt_history;
1012 while (i > 0)
1013 free(n->history[--i]);
1014 free(n);
1015}
1016
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001017/* state->flags is already checked to be nonzero */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001018static void load_history(line_input_t *st_parm)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001019{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001020 char *temp_h[MAX_HISTORY];
1021 char *line;
Robert Griebl350d26b2002-12-03 22:45:46 +00001022 FILE *fp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001023 unsigned idx, i, line_len;
Robert Griebl350d26b2002-12-03 22:45:46 +00001024
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001025 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +00001026
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001027 fp = fopen_for_read(st_parm->hist_file);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001028 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001029 /* clean up old history */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001030 for (idx = st_parm->cnt_history; idx > 0;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001031 idx--;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001032 free(st_parm->history[idx]);
1033 st_parm->history[idx] = NULL;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001034 }
1035
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001036 /* fill temp_h[], retaining only last MAX_HISTORY lines */
1037 memset(temp_h, 0, sizeof(temp_h));
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001038 st_parm->cnt_history_in_file = idx = 0;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001039 while ((line = xmalloc_fgetline(fp)) != NULL) {
1040 if (line[0] == '\0') {
1041 free(line);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001042 continue;
1043 }
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001044 free(temp_h[idx]);
1045 temp_h[idx] = line;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001046 st_parm->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001047 idx++;
1048 if (idx == MAX_HISTORY)
1049 idx = 0;
Robert Griebl350d26b2002-12-03 22:45:46 +00001050 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001051 fclose(fp);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001052
1053 /* find first non-NULL temp_h[], if any */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001054 if (st_parm->cnt_history_in_file) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001055 while (temp_h[idx] == NULL) {
1056 idx++;
1057 if (idx == MAX_HISTORY)
1058 idx = 0;
1059 }
1060 }
1061
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001062 /* copy temp_h[] to st_parm->history[] */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001063 for (i = 0; i < MAX_HISTORY;) {
1064 line = temp_h[idx];
1065 if (!line)
1066 break;
1067 idx++;
1068 if (idx == MAX_HISTORY)
1069 idx = 0;
1070 line_len = strlen(line);
1071 if (line_len >= MAX_LINELEN)
1072 line[MAX_LINELEN-1] = '\0';
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001073 st_parm->history[i++] = line;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001074 }
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001075 st_parm->cnt_history = i;
Robert Griebl350d26b2002-12-03 22:45:46 +00001076 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001077}
1078
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001079/* state->flags is already checked to be nonzero */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001080static void save_history(char *str)
Robert Griebl350d26b2002-12-03 22:45:46 +00001081{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001082 int fd;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001083 int len, len2;
Eric Andersenc470f442003-07-28 09:56:35 +00001084
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001085 fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0666);
1086 if (fd < 0)
1087 return;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001088 xlseek(fd, 0, SEEK_END); /* paranoia */
1089 len = strlen(str);
1090 str[len] = '\n'; /* we (try to) do atomic write */
1091 len2 = full_write(fd, str, len + 1);
1092 str[len] = '\0';
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001093 close(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001094 if (len2 != len + 1)
1095 return; /* "wtf?" */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001096
1097 /* did we write so much that history file needs trimming? */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001098 state->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001099 if (state->cnt_history_in_file > MAX_HISTORY * 4) {
1100 FILE *fp;
1101 char *new_name;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001102 line_input_t *st_temp;
1103 int i;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001104
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001105 /* we may have concurrently written entries from others.
1106 * load them */
1107 st_temp = new_line_input_t(state->flags);
1108 st_temp->hist_file = state->hist_file;
1109 load_history(st_temp);
1110
1111 /* write out temp file and replace hist_file atomically */
1112 new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001113 fp = fopen_for_write(new_name);
1114 if (fp) {
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001115 for (i = 0; i < st_temp->cnt_history; i++)
1116 fprintf(fp, "%s\n", st_temp->history[i]);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001117 fclose(fp);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001118 if (rename(new_name, state->hist_file) == 0)
1119 state->cnt_history_in_file = st_temp->cnt_history;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001120 }
1121 free(new_name);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001122 free_line_input_t(st_temp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001123 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001124}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001125#else
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001126#define load_history(a) ((void)0)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001127#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001128#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001129
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001130static void remember_in_history(char *str)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001131{
1132 int i;
1133
1134 if (!(state->flags & DO_HISTORY))
1135 return;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001136 if (str[0] == '\0')
1137 return;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001138 i = state->cnt_history;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001139 /* Don't save dupes */
1140 if (i && strcmp(state->history[i-1], str) == 0)
1141 return;
1142
1143 free(state->history[MAX_HISTORY]); /* redundant, paranoia */
1144 state->history[MAX_HISTORY] = NULL; /* redundant, paranoia */
1145
1146 /* If history[] is full, remove the oldest command */
1147 /* we need to keep history[MAX_HISTORY] empty, hence >=, not > */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001148 if (i >= MAX_HISTORY) {
1149 free(state->history[0]);
1150 for (i = 0; i < MAX_HISTORY-1; i++)
1151 state->history[i] = state->history[i+1];
Denis Vlasenko682ad302008-09-27 01:28:56 +00001152 /* i == MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001153 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001154 /* i <= MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001155 state->history[i++] = xstrdup(str);
Denis Vlasenko682ad302008-09-27 01:28:56 +00001156 /* i <= MAX_HISTORY */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001157 state->cur_history = i;
1158 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001159#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001160 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001161 save_history(str);
Denis Vlasenko09221922007-04-15 13:21:01 +00001162#endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001163 IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001164}
1165
1166#else /* MAX_HISTORY == 0 */
1167#define remember_in_history(a) ((void)0)
1168#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001169
1170
Erik Andersen6273f652000-03-17 01:12:41 +00001171/*
1172 * This function is used to grab a character buffer
1173 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001174 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001175 * a mini readline).
1176 *
1177 * The following standard commands are not implemented:
1178 * ESC-b -- Move back one word
1179 * ESC-f -- Move forward one word
1180 * ESC-d -- Delete back one word
1181 * ESC-h -- Delete forward one word
1182 * CTL-t -- Transpose two characters
1183 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001184 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001185 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001186 */
Eric Andersenc470f442003-07-28 09:56:35 +00001187
Denis Vlasenko38f63192007-01-22 09:03:07 +00001188#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001189static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001190vi_Word_motion(char *command, int eat)
1191{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001192 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001193 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001194 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001195 input_forward();
1196}
1197
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001198static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001199vi_word_motion(char *command, int eat)
1200{
1201 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001202 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001203 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001204 input_forward();
1205 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001206 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001207 input_forward();
1208 }
1209
Denis Vlasenko253ce002007-01-22 08:34:44 +00001210 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001211 input_forward();
1212
Denis Vlasenko253ce002007-01-22 08:34:44 +00001213 if (eat && cursor < command_len && isspace(command[cursor]))
1214 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001215 input_forward();
1216}
1217
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001218static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001219vi_End_motion(char *command)
1220{
1221 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001222 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001223 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001224 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001225 input_forward();
1226}
1227
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001228static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001229vi_end_motion(char *command)
1230{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001231 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001232 return;
1233 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001234 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001235 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001236 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001237 return;
1238 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001239 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001240 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1241 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001242 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001243 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001244 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001245 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001246 input_forward();
1247 }
1248}
1249
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001250static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001251vi_Back_motion(char *command)
1252{
1253 while (cursor > 0 && isspace(command[cursor-1]))
1254 input_backward(1);
1255 while (cursor > 0 && !isspace(command[cursor-1]))
1256 input_backward(1);
1257}
1258
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001259static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001260vi_back_motion(char *command)
1261{
1262 if (cursor <= 0)
1263 return;
1264 input_backward(1);
1265 while (cursor > 0 && isspace(command[cursor]))
1266 input_backward(1);
1267 if (cursor <= 0)
1268 return;
1269 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001270 while (cursor > 0
1271 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1272 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001273 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001274 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001275 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001276 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001277 input_backward(1);
1278 }
1279}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001280#endif
1281
1282
1283/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001284 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001285 */
1286
Denis Vlasenko38f63192007-01-22 09:03:07 +00001287#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001288static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001289{
1290 cmdedit_prompt = prmt_ptr;
1291 cmdedit_prmt_len = strlen(prmt_ptr);
1292 put_prompt();
1293}
1294#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001295static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001296{
1297 int prmt_len = 0;
1298 size_t cur_prmt_len = 0;
1299 char flg_not_length = '[';
1300 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001301 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1302 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001303 char c;
1304 char *pbuf;
1305
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001306 cmdedit_prmt_len = 0;
1307
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001308 if (!cwd_buf) {
1309 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001310 }
1311
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001312 cbuf[1] = '\0'; /* never changes */
1313
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001314 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001315 char *free_me = NULL;
1316
1317 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001318 c = *prmt_ptr++;
1319 if (c == '\\') {
1320 const char *cp = prmt_ptr;
1321 int l;
1322
1323 c = bb_process_escape_sequence(&prmt_ptr);
1324 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001325 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001326 break;
1327 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001328
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001329 switch (c) {
1330#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1331 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001332 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001333 break;
1334#endif
1335 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001336 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001337 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001338 break;
1339 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001340 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001341 break;
1342#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1343 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001344 /* /home/user[/something] -> ~[/something] */
1345 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001346 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001347 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001348 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1349 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1350 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001351 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001352 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001353 }
1354 break;
1355#endif
1356 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001357 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001358 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001359 if (cp != NULL && cp != pbuf)
1360 pbuf += (cp-pbuf) + 1;
1361 break;
1362 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001363 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001364 break;
1365 case 'e': case 'E': /* \e \E = \033 */
1366 c = '\033';
1367 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001368 case 'x': case 'X': {
1369 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001370 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001371 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001372 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001373 buf2[l] = '\0';
1374 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001375 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001376 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001377 break;
1378 }
1379 prmt_ptr++;
1380 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001381 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001382 if (c == 0)
1383 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001384 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001385 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001386 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001387 case '[': case ']':
1388 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001389 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001390 continue;
1391 }
1392 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001393 } /* switch */
1394 } /* if */
1395 } /* if */
1396 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001397 cur_prmt_len = strlen(pbuf);
1398 prmt_len += cur_prmt_len;
1399 if (flg_not_length != ']')
1400 cmdedit_prmt_len += cur_prmt_len;
1401 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001402 free(free_me);
1403 } /* while */
1404
1405 if (cwd_buf != (char *)bb_msg_unknown)
1406 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001407 cmdedit_prompt = prmt_mem_ptr;
1408 put_prompt();
1409}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001410#endif
1411
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001412static void cmdedit_setwidth(unsigned w, int redraw_flg)
1413{
1414 cmdedit_termw = w;
1415 if (redraw_flg) {
1416 /* new y for current cursor */
1417 int new_y = (cursor + cmdedit_prmt_len) / w;
1418 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001419 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001420 fflush(stdout);
1421 }
1422}
1423
1424static void win_changed(int nsig)
1425{
Denis Vlasenko55995022008-05-18 22:28:26 +00001426 unsigned width;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001427 get_terminal_width_height(0, &width, NULL);
1428 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1429 if (nsig == SIGWINCH)
1430 signal(SIGWINCH, win_changed); /* rearm ourself */
1431}
1432
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001433/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001434 * The emacs and vi modes share much of the code in the big
1435 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001436 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001437 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001438 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001439 */
1440
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001441#define vbit 0x100
1442
1443/* leave out the "vi-mode"-only case labels if vi editing isn't
1444 * configured. */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001445#define vi_case(caselabel) IF_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001446
1447/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001448#undef CTRL
1449#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001450
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001451/* Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001452 * -1 on read errors or EOF, or on bare Ctrl-D,
1453 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001454 * >0 length of input string, including terminating '\n'
1455 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001456int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001457{
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00001458 int len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001459#if ENABLE_FEATURE_TAB_COMPLETION
1460 smallint lastWasTab = FALSE;
1461#endif
Denis Vlasenko55995022008-05-18 22:28:26 +00001462 unsigned ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001463 unsigned char c;
1464 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001465#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001466 smallint vi_cmdmode = 0;
1467 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001468#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001469 struct termios initial_settings;
1470 struct termios new_settings;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001471
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001472 INIT_S();
1473
1474 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1475 || !(initial_settings.c_lflag & ECHO)
1476 ) {
1477 /* Happens when e.g. stty -echo was run before */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001478 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001479 fflush(stdout);
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001480 if (fgets(command, maxsize, stdin) == NULL)
1481 len = -1; /* EOF or error */
1482 else
1483 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001484 DEINIT_S();
1485 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001486 }
1487
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001488// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001489 if (maxsize > MAX_LINELEN)
1490 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001491
1492 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001493 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001494#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001495 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001496 if (state->cnt_history == 0)
1497 load_history(state);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001498#endif
Denis Vlasenko3c385cd2008-11-02 00:41:05 +00001499 if (state->flags & DO_HISTORY)
1500 state->cur_history = state->cnt_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001501
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001502 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001503 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001504 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001505 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001506 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001507
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001508 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001509 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1510 /* Turn off echoing and CTRL-C, so we can trap it */
1511 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001512 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001513 new_settings.c_cc[VMIN] = 1;
1514 new_settings.c_cc[VTIME] = 0;
1515 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001516#ifndef _POSIX_VDISABLE
1517#define _POSIX_VDISABLE '\0'
1518#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001519 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko202ac502008-11-05 13:20:58 +00001520 tcsetattr_stdin_TCSANOW(&new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001521
Eric Andersen6faae7d2001-02-16 20:09:17 +00001522 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001523 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1524 win_changed(0); /* do initial resizing */
1525#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1526 {
1527 struct passwd *entry;
1528
1529 entry = getpwuid(geteuid());
1530 if (entry) {
1531 user_buf = xstrdup(entry->pw_name);
1532 home_pwd_buf = xstrdup(entry->pw_dir);
1533 }
1534 }
1535#endif
Denis Vlasenko682ad302008-09-27 01:28:56 +00001536
1537#if 0
1538 for (ic = 0; ic <= MAX_HISTORY; ic++)
1539 bb_error_msg("history[%d]:'%s'", ic, state->history[ic]);
1540 bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
1541#endif
1542
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001543 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001544 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001545
Erik Andersenc7c634b2000-03-19 05:28:55 +00001546 while (1) {
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001547 fflush(NULL);
Mark Whitley4e338752001-01-26 20:42:23 +00001548
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001549 if (nonblock_safe_read(STDIN_FILENO, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001550 /* if we can't read input then exit */
1551 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001552 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001553
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001554 ic = c;
1555
Denis Vlasenko38f63192007-01-22 09:03:07 +00001556#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001557 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001558 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001559 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001560#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001561 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001562 case '\n':
1563 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001564 vi_case('\n'|vbit:)
1565 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001566 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001567 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001568 break_out = 1;
1569 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001570 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001571 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001572 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001573 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001574 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001575 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001576 vi_case('h'|vbit:)
1577 vi_case('\b'|vbit:)
1578 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001579 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001580 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001581 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001582 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001583 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001584 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001585 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001586 command_len = 0;
1587 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001588 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001589 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001590 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001591 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001592 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001593 errno = 0;
1594 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001595 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001596 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001597 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001598 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001599 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001600 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001601
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001602 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001603 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001604 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001605 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001606 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001607 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001608 vi_case('l'|vbit:)
1609 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001610 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001611 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001612 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001613
Erik Andersenf0657d32000-04-12 17:49:52 +00001614 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001615 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001616 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001617 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001618 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001619
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001620#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001621 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001622 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001623 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001624#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001625
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001626 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001627 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001628 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001629 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001630 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001631 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001632 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001633 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001634 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001635 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001636 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001637 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001638
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001639#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001640 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001641 vi_case(CTRL('N')|vbit:)
1642 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001643 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001644 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001645 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001646 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001647 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001648 vi_case(CTRL('P')|vbit:)
1649 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001650 /* Control-p -- Get previous command from history */
Denis Vlasenko682ad302008-09-27 01:28:56 +00001651 if (get_previous_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001652 goto rewrite_line;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001653 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001654#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001655
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001656 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001657 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001658 /* Control-U -- Clear line before cursor */
1659 if (cursor) {
Denis Vlasenko0f293b92008-07-22 20:16:55 +00001660 overlapping_strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001661 command_len -= cursor;
1662 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001663 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001664 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001665 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001666 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001667 /* Control-W -- Remove the last word */
1668 while (cursor > 0 && isspace(command[cursor-1]))
1669 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001670 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001671 input_backspace();
1672 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001673
Denis Vlasenko38f63192007-01-22 09:03:07 +00001674#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001675 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001676 vi_cmdmode = 0;
1677 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001678 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001679 input_backward(cursor);
1680 vi_cmdmode = 0;
1681 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001682 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001683 input_forward();
1684 vi_cmdmode = 0;
1685 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001686 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001687 input_end();
1688 vi_cmdmode = 0;
1689 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001690 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001691 input_delete(1);
1692 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001693 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001694 if (cursor > 0) {
1695 input_backward(1);
1696 input_delete(1);
1697 }
1698 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001699 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001700 vi_Word_motion(command, 1);
1701 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001702 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001703 vi_word_motion(command, 1);
1704 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001705 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001706 vi_End_motion(command);
1707 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001708 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001709 vi_end_motion(command);
1710 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001711 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001712 vi_Back_motion(command);
1713 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001714 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001715 vi_back_motion(command);
1716 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001717 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001718 vi_cmdmode = 0;
1719 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001720 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001721 goto clear_to_eol;
1722
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001723 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001724 vi_cmdmode = 0;
1725 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001726 case 'd'|vbit: {
1727 int nc, sc;
1728 sc = cursor;
1729 prevc = ic;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001730 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001731 goto prepare_to_die;
1732 if (c == (prevc & 0xff)) {
1733 /* "cc", "dd" */
1734 input_backward(cursor);
1735 goto clear_to_eol;
1736 break;
1737 }
1738 switch (c) {
1739 case 'w':
1740 case 'W':
1741 case 'e':
1742 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001743 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001744 case 'w': /* "dw", "cw" */
1745 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001746 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001747 case 'W': /* 'dW', 'cW' */
1748 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001749 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001750 case 'e': /* 'de', 'ce' */
1751 vi_end_motion(command);
1752 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001753 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001754 case 'E': /* 'dE', 'cE' */
1755 vi_End_motion(command);
1756 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001757 break;
1758 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001759 nc = cursor;
1760 input_backward(cursor - sc);
1761 while (nc-- > cursor)
1762 input_delete(1);
1763 break;
1764 case 'b': /* "db", "cb" */
1765 case 'B': /* implemented as B */
1766 if (c == 'b')
1767 vi_back_motion(command);
1768 else
1769 vi_Back_motion(command);
1770 while (sc-- > cursor)
1771 input_delete(1);
1772 break;
1773 case ' ': /* "d ", "c " */
1774 input_delete(1);
1775 break;
1776 case '$': /* "d$", "c$" */
1777 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001778 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001779 input_delete(1);
1780 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001781 }
1782 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001783 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001784 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001785 input_forward();
1786 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001787 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001788 put();
1789 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001790 case 'r'|vbit:
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001791 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001792 goto prepare_to_die;
1793 if (c == 0)
1794 beep();
1795 else {
1796 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001797 bb_putchar(c);
1798 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001799 }
1800 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001801#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001802
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001803 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001804
Denis Vlasenko38f63192007-01-22 09:03:07 +00001805#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001806 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001807 /* ESC: insert mode --> command mode */
1808 vi_cmdmode = 1;
1809 input_backward(1);
1810 break;
1811 }
1812#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001813 /* escape sequence follows */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001814 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001815 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001816 /* different vt100 emulations */
1817 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001818 vi_case('['|vbit:)
1819 vi_case('O'|vbit:)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001820 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001821 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001822 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001823 if (c >= '1' && c <= '9') {
1824 unsigned char dummy;
1825
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001826 if (safe_read(STDIN_FILENO, &dummy, 1) < 1)
Glenn L McGrath475820c2004-01-22 12:42:23 +00001827 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001828 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001829 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001830 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001831
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001832 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001833#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001834 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001835 input_tab(&lastWasTab);
1836 break;
1837#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001838#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001839 case 'A':
1840 /* Up Arrow -- Get previous command from history */
Denis Vlasenko682ad302008-09-27 01:28:56 +00001841 if (get_previous_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001842 goto rewrite_line;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001843 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001844 break;
1845 case 'B':
1846 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001847 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001848 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001849 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001850 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001851 /* change command */
Denis Vlasenko682ad302008-09-27 01:28:56 +00001852 command_len = strlen(strcpy(command, state->history[state->cur_history] ? : ""));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001853 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001854 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001855 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001856#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001857 case 'C':
1858 /* Right Arrow -- Move forward one character */
1859 input_forward();
1860 break;
1861 case 'D':
1862 /* Left Arrow -- Move back one character */
1863 input_backward(1);
1864 break;
1865 case '3':
1866 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001867 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001868 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001869 case '1': // vt100? linux vt? or what?
1870 case '7': // vt100? linux vt? or what?
1871 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001872 input_backward(cursor);
1873 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001874 case '4': // vt100? linux vt? or what?
1875 case '8': // vt100? linux vt? or what?
1876 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001877 input_end();
1878 break;
1879 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001880 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001881 beep();
1882 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001883 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001884
Eric Andersenc470f442003-07-28 09:56:35 +00001885 default: /* If it's regular input, do the normal thing */
Paul Fox84bbac52008-01-11 16:50:08 +00001886
1887 /* Control-V -- force insert of next char */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001888 if (c == CTRL('V')) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001889 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001890 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001891 if (c == 0) {
1892 beep();
1893 break;
1894 }
Paul Fox84bbac52008-01-11 16:50:08 +00001895 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001896
Denis Vlasenko38f63192007-01-22 09:03:07 +00001897#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001898 if (vi_cmdmode) /* Don't self-insert */
1899 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001900#endif
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00001901 if ((int)command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001902 break;
1903
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001904 command_len++;
1905 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001906 command[cursor] = c;
1907 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001908 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001909 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001910 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001911
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001912 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001913 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001914 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001915 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001916 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001917 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001918 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001919 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001920 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001921 }
Eric Andersenc470f442003-07-28 09:56:35 +00001922 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001923 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001924
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001925#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001926 if (c != '\t')
1927 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001928#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001929 }
1930
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001931 if (command_len > 0)
1932 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001933
Eric Andersen27bb7902003-12-23 20:24:51 +00001934 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001935 command[command_len++] = '\n';
1936 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001937 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001938
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001939#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001940 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001941#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001942
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001943 /* restore initial_settings */
Denis Vlasenko202ac502008-11-05 13:20:58 +00001944 tcsetattr_stdin_TCSANOW(&initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001945 /* restore SIGWINCH handler */
1946 signal(SIGWINCH, previous_SIGWINCH_handler);
1947 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001948
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00001949 len = command_len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001950 DEINIT_S();
1951
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00001952 return len; /* can't return command_len, DEINIT_S() destroys it */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001953}
1954
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001955#else
1956
1957#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001958int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001959{
1960 fputs(prompt, stdout);
1961 fflush(stdout);
1962 fgets(command, maxsize, stdin);
1963 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001964}
1965
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001966#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001967
1968
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001969/*
1970 * Testing
1971 */
1972
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001973#ifdef TEST
1974
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001975#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001976
1977const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001978
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001979int main(int argc, char **argv)
1980{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001981 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001982 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001983#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001984 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1985 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1986 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001987#else
1988 "% ";
1989#endif
1990
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001991#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001992 setlocale(LC_ALL, "");
1993#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001994 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001995 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001996 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001997 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001998 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001999 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002000 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00002001 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002002 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002003 return 0;
2004}
2005
Eric Andersenc470f442003-07-28 09:56:35 +00002006#endif /* TEST */