blob: a0b1bcf8e27ae21953d7ed66e4511758196ea3a9 [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
Denys Vlasenko020f4062009-05-17 16:44:54 +020089 unsigned cmdedit_x; /* real x (col) terminal position */
90 unsigned cmdedit_y; /* pseudoreal y (row) terminal position */
Denis Vlasenkob267ed92008-05-25 21:52:03 +000091 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);
Denys Vlasenkoc396fe62009-05-17 19:28:14 +0200303 if (ENABLE_FEATURE_EDITING_ASK_TERMINAL) {
304 /* Ask terminal where is the cursor now.
305 * lineedit_read_key handles response and corrects
306 * our idea of current cursor position.
307 * Testcase: run "echo -n long_line_long_line_long_line",
308 * then type in a long, wrapping command and try to
309 * delete it using backspace key.
310 * Note: we print it _after_ prompt, because
311 * prompt may contain CR. Example: PS1='\[\r\n\]\w '
312 */
313 out1str("\033" "[6n");
314 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000315 cursor = 0;
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000316 {
317 unsigned w = cmdedit_termw; /* volatile var */
318 cmdedit_y = cmdedit_prmt_len / w; /* new quasireal y */
319 cmdedit_x = cmdedit_prmt_len % w;
320 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000321}
322
Eric Andersenaff114c2004-04-14 17:51:38 +0000323/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000324static void redraw(int y, int back_cursor)
325{
Eric Andersenc470f442003-07-28 09:56:35 +0000326 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000327 printf("\033[%dA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000328 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000329 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000330 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000331 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000332 input_backward(back_cursor);
333}
334
Paul Fox3f11b1b2005-08-04 19:04:46 +0000335/* Delete the char in front of the cursor, optionally saving it
336 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000337#if !ENABLE_FEATURE_EDITING_VI
338static void input_delete(void)
339#define input_delete(save) input_delete()
340#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000341static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000342#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000343{
Mark Whitley4e338752001-01-26 20:42:23 +0000344 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000345
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000346 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000347 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000348
Denis Vlasenko38f63192007-01-22 09:03:07 +0000349#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000350 if (save) {
351 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000352 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000353 newdelflag = 0;
354 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000355 if ((delptr - delbuf) < DELBUFSIZ)
356 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000357 }
358#endif
359
Denis Vlasenko41660c52008-07-22 20:25:24 +0000360 overlapping_strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000361 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000362 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000363 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000364 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000365}
366
Denis Vlasenko38f63192007-01-22 09:03:07 +0000367#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000368static void put(void)
369{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000370 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000371 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000372
Paul Fox3f11b1b2005-08-04 19:04:46 +0000373 if (j == 0)
374 return;
375 ocursor = cursor;
376 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000377 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000378 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000379 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000380 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000381 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000382}
383#endif
384
Mark Whitley4e338752001-01-26 20:42:23 +0000385/* Delete the char in back of the cursor */
386static void input_backspace(void)
387{
388 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000389 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000390 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000391 }
392}
393
Eric Andersenaff114c2004-04-14 17:51:38 +0000394/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000395static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000396{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000397 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000398 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000399}
400
Denis Vlasenko38f63192007-01-22 09:03:07 +0000401#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000402
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000403static void free_tab_completion_data(void)
404{
405 if (matches) {
406 while (num_matches)
407 free(matches[--num_matches]);
408 free(matches);
409 matches = NULL;
410 }
411}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000412
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000413static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000414{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000415 matches = xrealloc_vector(matches, 4, num_matches);
416 matches[num_matches] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000417 num_matches++;
418}
419
Denis Vlasenko38f63192007-01-22 09:03:07 +0000420#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000421static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000422{
423 struct passwd *entry;
424 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000425
Eric Andersenc470f442003-07-28 09:56:35 +0000426 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000427 userlen = strlen(ud);
428
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000429 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000431 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000432
Eric Andersenc470f442003-07-28 09:56:35 +0000433 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000434 home = home_pwd_buf;
435 } else {
436 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000437 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000438 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000439 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000440 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000441 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000442 ud = temp;
443 if (entry)
444 home = entry->pw_dir;
445 }
446 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000447 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000448 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000449 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000450 }
451 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000452 } else {
453 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000454 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000455 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000456 struct passwd pwd;
457 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000458
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000459 setpwent();
460 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000461 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000462 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
463 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000464 }
465 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000466 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000467 }
468}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000469#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000470
471enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000472 FIND_EXE_ONLY = 0,
473 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000474 FIND_FILE_ONLY = 2,
475};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000476
Mark Whitley4e338752001-01-26 20:42:23 +0000477static int path_parse(char ***p, int flags)
478{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000479 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000480 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000481 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000482 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000483
Mark Whitley4e338752001-01-26 20:42:23 +0000484 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000485 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000486 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000487
488 if (state->flags & WITH_PATH_LOOKUP)
489 pth = state->path_lookup;
490 else
491 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000492 /* PATH=<empty> or PATH=:<empty> */
493 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
494 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000495
Denis Vlasenko253ce002007-01-22 08:34:44 +0000496 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000497 npth = 1; /* path component count */
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 Vlasenko82b39e82007-01-21 19:18:19 +0000502 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000503 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000504 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000505 }
506
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000507 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000508 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000509 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000510 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000511 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000512 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000513 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000514 *tmp++ = '\0'; /* ':' -> '\0' */
515 if (*tmp == '\0')
516 break; /* :<empty> */
517 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000518 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000519 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000520 return npth;
521}
522
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000523static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000524{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000525 DIR *dir;
526 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000527 struct stat st;
528 char *path1[1];
529 char **paths = path1;
530 int npaths;
531 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000532 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000533 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000534/* char dirbuf[MAX_LINELEN]; */
535#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000536
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000537 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000538 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000539
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000540 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000541 /* no dir, if flags==EXE_ONLY - get paths, else "." */
542 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000543 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000544 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000545 /* dirbuf = ".../.../.../" */
546 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000547#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000548 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000549 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000550#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000551 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000552 /* point to 'l' in "..../last_component" */
553 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000554 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000555
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000556 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000557 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000558 if (!dir)
559 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000560
561 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000562 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000563 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000564
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000565 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000566 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000567 continue;
568 /* not see .name without .match */
Denis Vlasenkob5202712008-04-24 04:42:52 +0000569 if (*str_found == '.' && *pfind == '\0') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000570 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000571 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000572 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000573 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000574 found = concat_path_file(paths[i], str_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000575 /* hmm, remove in progress? */
576 /* NB: stat() first so that we see is it a directory;
577 * but if that fails, use lstat() so that
578 * we still match dangling links */
579 if (stat(found, &st) && lstat(found, &st))
Eric Andersene5dfced2001-04-09 22:48:12 +0000580 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000581 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000582 if (paths[i] != dirbuf)
Denis Vlasenkob5202712008-04-24 04:42:52 +0000583 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000584
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000585 len1 = strlen(found);
586 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000587 found[len1] = '\0';
588 found[len1+1] = '\0';
589
Mark Whitley4e338752001-01-26 20:42:23 +0000590 if (S_ISDIR(st.st_mode)) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000591 /* name is a directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000592 if (found[len1-1] != '/') {
593 found[len1] = '/';
594 }
Mark Whitley4e338752001-01-26 20:42:23 +0000595 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000596 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000597 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000598 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000599 }
Mark Whitley4e338752001-01-26 20:42:23 +0000600 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000601 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000602 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000603 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000604 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000605 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000606 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000607 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000608 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000609 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000610 free(paths);
611 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000612#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000613}
Erik Andersenf0657d32000-04-12 17:49:52 +0000614
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000615#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000616
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000617#define collapse_pos(is, in) do { \
618 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
619 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
620} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000621
622static int find_match(char *matchBuf, int *len_with_quotes)
623{
624 int i, j;
625 int command_mode;
626 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000627/* int16_t int_buf[MAX_LINELEN + 1]; */
628/* int16_t pos_buf[MAX_LINELEN + 1]; */
629#define int_buf (S.find_match__int_buf)
630#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000631
632 /* set to integer dimension characters and own positions */
633 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000634 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000635 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000636 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000637 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000638 }
639 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000640 }
641
642 /* mask \+symbol and convert '\t' to ' ' */
643 for (i = j = 0; matchBuf[i]; i++, j++)
644 if (matchBuf[i] == '\\') {
645 collapse_pos(j, j + 1);
646 int_buf[j] |= QUOT;
647 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000648#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000649 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000650 int_buf[j] = ' ' | QUOT;
651#endif
652 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000653#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000654 else if (matchBuf[i] == '\t')
655 int_buf[j] = ' ';
656#endif
657
658 /* mask "symbols" or 'symbols' */
659 c2 = 0;
660 for (i = 0; int_buf[i]; i++) {
661 c = int_buf[i];
662 if (c == '\'' || c == '"') {
663 if (c2 == 0)
664 c2 = c;
665 else {
666 if (c == c2)
667 c2 = 0;
668 else
669 int_buf[i] |= QUOT;
670 }
671 } else if (c2 != 0 && c != '$')
672 int_buf[i] |= QUOT;
673 }
674
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000675 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000676 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
677 for (i = 0; int_buf[i]; i++) {
678 c = int_buf[i];
679 c2 = int_buf[i + 1];
680 j = i ? int_buf[i - 1] : -1;
681 command_mode = 0;
682 if (c == ';' || c == '&' || c == '|') {
683 command_mode = 1 + (c == c2);
684 if (c == '&') {
685 if (j == '>' || j == '<')
686 command_mode = 0;
687 } else if (c == '|' && j == '>')
688 command_mode = 0;
689 }
690 if (command_mode) {
691 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000692 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000693 }
694 }
695 /* collapse `command...` */
696 for (i = 0; int_buf[i]; i++)
697 if (int_buf[i] == '`') {
698 for (j = i + 1; int_buf[j]; j++)
699 if (int_buf[j] == '`') {
700 collapse_pos(i, j + 1);
701 j = 0;
702 break;
703 }
704 if (j) {
705 /* not found close ` - command mode, collapse all previous */
706 collapse_pos(0, i + 1);
707 break;
708 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000709 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000710 }
711
712 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000713 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000714 c2 = 0;
715 for (i = 0; int_buf[i]; i++)
716 if (int_buf[i] == '(' || int_buf[i] == '{') {
717 if (int_buf[i] == '(')
718 c++;
719 else
720 c2++;
721 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000722 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000723 }
724 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
725 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
726 if (int_buf[i] == ')')
727 c--;
728 else
729 c2--;
730 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000731 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000732 }
733
734 /* skip first not quote space */
735 for (i = 0; int_buf[i]; i++)
736 if (int_buf[i] != ' ')
737 break;
738 if (i)
739 collapse_pos(0, i);
740
741 /* set find mode for completion */
742 command_mode = FIND_EXE_ONLY;
743 for (i = 0; int_buf[i]; i++)
744 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
745 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000746 && matchBuf[pos_buf[0]] == 'c'
747 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000748 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000749 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000750 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000751 command_mode = FIND_FILE_ONLY;
752 break;
753 }
754 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000755 for (i = 0; int_buf[i]; i++)
756 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000757 /* find last word */
758 for (--i; i >= 0; i--) {
759 c = int_buf[i];
760 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
761 collapse_pos(0, i + 1);
762 break;
763 }
764 }
765 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000766 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
767 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000768 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000769 while ((int_buf[i] & ~QUOT) == '/'
770 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
771 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000772 i++;
773 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000774
775 /* set only match and destroy quotes */
776 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000777 for (c = 0; pos_buf[i] >= 0; i++) {
778 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000779 j = pos_buf[i] + 1;
780 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000781 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000782 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000783 *len_with_quotes = j ? j - pos_buf[0] : 0;
784
785 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000786#undef int_buf
787#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000788}
789
Glenn L McGrath4d001292003-01-06 01:11:50 +0000790/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000791 * display by column (original idea from ls applet,
792 * very optimized by me :)
793 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000794static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000795{
796 int ncols, row;
797 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000798 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000799 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000800 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000801
802 /* find the longest file name- use that as the column width */
803 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000804 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000805 if (column_width < l)
806 column_width = l;
807 }
808 column_width += 2; /* min space for columns */
809 ncols = cmdedit_termw / column_width;
810
811 if (ncols > 1) {
812 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000813 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000814 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000815 } else {
816 ncols = 1;
817 }
818 for (row = 0; row < nrows; row++) {
819 int n = row;
820 int nc;
821
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000822 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000823 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000824 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000825 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000826 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000827 }
828}
829
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000830static char *add_quote_for_spec_chars(char *found)
831{
832 int l = 0;
833 char *s = xmalloc((strlen(found) + 1) * 2);
834
835 while (*found) {
836 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
837 s[l++] = '\\';
838 s[l++] = *found++;
839 }
840 s[l] = 0;
841 return s;
842}
843
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000844/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000845static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000846{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000847 if (!(state->flags & TAB_COMPLETION))
848 return;
849
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000850 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000851 char *tmp, *tmp1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000852 size_t len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000853/* char matchBuf[MAX_LINELEN]; */
854#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000855 int find_type;
856 int recalc_pos;
857
Eric Andersenc470f442003-07-28 09:56:35 +0000858 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000859
860 /* Make a local copy of the string -- up
861 * to the position of the cursor */
862 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000863 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000864
865 find_type = find_match(matchBuf, &recalc_pos);
866
867 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000868 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000869
Denis Vlasenko38f63192007-01-22 09:03:07 +0000870#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000871 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000872 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000873 if (state->flags & USERNAME_COMPLETION)
874 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
875 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000876#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000877 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000878 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000879 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000880 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000881 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000882 if (matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000883 unsigned i;
884 int n = 0;
Denis Vlasenkofb290382008-03-02 12:51:26 +0000885 qsort_string_vector(matches, num_matches);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000886 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000887 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000888 if (strcmp(matches[i], matches[i+1]) == 0) {
889 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000890 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000891 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000892 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000893 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000894 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000895 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000896 matches[n] = matches[i];
897 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000898 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000899 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000900 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000901 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000902 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000903 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000904 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000905 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000906 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000907 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000908 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000909 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000910 break;
911 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000912 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000913 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000914 return;
915 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000916 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000917 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000918 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000919 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000920 /* for next completion current found */
921 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000922
923 len_found = strlen(tmp);
924 if (tmp[len_found-1] != '/') {
925 tmp[len_found] = ' ';
926 tmp[len_found+1] = '\0';
927 }
Mark Whitley4e338752001-01-26 20:42:23 +0000928 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000929 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000930 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000931 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000932 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000933 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000934 /* save tail line */
935 strcpy(matchBuf, command_ps + cursor);
936 /* add match */
937 strcat(command_ps, tmp);
938 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000939 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000940 /* back to begin word for match */
941 input_backward(recalc_pos);
942 /* new pos */
943 recalc_pos = cursor + len_found;
944 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000945 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000946 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000947 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000948 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000949 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000950#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000951 } else {
952 /* Ok -- the last char was a TAB. Since they
953 * just hit TAB again, print a list of all the
954 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000955 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000956 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000957
958 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000959 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000960 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000961 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000962 }
963 }
964}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000965
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000966#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000967
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000968
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +0000969line_input_t* FAST_FUNC new_line_input_t(int flags)
970{
971 line_input_t *n = xzalloc(sizeof(*n));
972 n->flags = flags;
973 return n;
974}
975
976
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000977#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000978
Denis Vlasenko682ad302008-09-27 01:28:56 +0000979static void save_command_ps_at_cur_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000980{
Denis Vlasenko682ad302008-09-27 01:28:56 +0000981 if (command_ps[0] != '\0') {
982 int cur = state->cur_history;
983 free(state->history[cur]);
984 state->history[cur] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000985 }
Denis Vlasenko682ad302008-09-27 01:28:56 +0000986}
987
988/* state->flags is already checked to be nonzero */
989static int get_previous_history(void)
990{
991 if ((state->flags & DO_HISTORY) && state->cur_history) {
992 save_command_ps_at_cur_history();
993 state->cur_history--;
994 return 1;
995 }
996 beep();
997 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000998}
999
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001000static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001001{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001002 if (state->flags & DO_HISTORY) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001003 if (state->cur_history < state->cnt_history) {
1004 save_command_ps_at_cur_history(); /* save the current history line */
1005 return ++state->cur_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001006 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001007 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001008 beep();
1009 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001010}
Robert Griebl350d26b2002-12-03 22:45:46 +00001011
Denis Vlasenko38f63192007-01-22 09:03:07 +00001012#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001013/* We try to ensure that concurrent additions to the history
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001014 * do not overwrite each other.
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001015 * Otherwise shell users get unhappy.
1016 *
1017 * History file is trimmed lazily, when it grows several times longer
1018 * than configured MAX_HISTORY lines.
1019 */
1020
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001021static void free_line_input_t(line_input_t *n)
1022{
1023 int i = n->cnt_history;
1024 while (i > 0)
1025 free(n->history[--i]);
1026 free(n);
1027}
1028
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001029/* state->flags is already checked to be nonzero */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001030static void load_history(line_input_t *st_parm)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001031{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001032 char *temp_h[MAX_HISTORY];
1033 char *line;
Robert Griebl350d26b2002-12-03 22:45:46 +00001034 FILE *fp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001035 unsigned idx, i, line_len;
Robert Griebl350d26b2002-12-03 22:45:46 +00001036
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001037 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +00001038
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001039 fp = fopen_for_read(st_parm->hist_file);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001040 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001041 /* clean up old history */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001042 for (idx = st_parm->cnt_history; idx > 0;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001043 idx--;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001044 free(st_parm->history[idx]);
1045 st_parm->history[idx] = NULL;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001046 }
1047
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001048 /* fill temp_h[], retaining only last MAX_HISTORY lines */
1049 memset(temp_h, 0, sizeof(temp_h));
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001050 st_parm->cnt_history_in_file = idx = 0;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001051 while ((line = xmalloc_fgetline(fp)) != NULL) {
1052 if (line[0] == '\0') {
1053 free(line);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001054 continue;
1055 }
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001056 free(temp_h[idx]);
1057 temp_h[idx] = line;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001058 st_parm->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001059 idx++;
1060 if (idx == MAX_HISTORY)
1061 idx = 0;
Robert Griebl350d26b2002-12-03 22:45:46 +00001062 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001063 fclose(fp);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001064
1065 /* find first non-NULL temp_h[], if any */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001066 if (st_parm->cnt_history_in_file) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001067 while (temp_h[idx] == NULL) {
1068 idx++;
1069 if (idx == MAX_HISTORY)
1070 idx = 0;
1071 }
1072 }
1073
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001074 /* copy temp_h[] to st_parm->history[] */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001075 for (i = 0; i < MAX_HISTORY;) {
1076 line = temp_h[idx];
1077 if (!line)
1078 break;
1079 idx++;
1080 if (idx == MAX_HISTORY)
1081 idx = 0;
1082 line_len = strlen(line);
1083 if (line_len >= MAX_LINELEN)
1084 line[MAX_LINELEN-1] = '\0';
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001085 st_parm->history[i++] = line;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001086 }
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001087 st_parm->cnt_history = i;
Robert Griebl350d26b2002-12-03 22:45:46 +00001088 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001089}
1090
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001091/* state->flags is already checked to be nonzero */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001092static void save_history(char *str)
Robert Griebl350d26b2002-12-03 22:45:46 +00001093{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001094 int fd;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001095 int len, len2;
Eric Andersenc470f442003-07-28 09:56:35 +00001096
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001097 fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0666);
1098 if (fd < 0)
1099 return;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001100 xlseek(fd, 0, SEEK_END); /* paranoia */
1101 len = strlen(str);
1102 str[len] = '\n'; /* we (try to) do atomic write */
1103 len2 = full_write(fd, str, len + 1);
1104 str[len] = '\0';
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001105 close(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001106 if (len2 != len + 1)
1107 return; /* "wtf?" */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001108
1109 /* did we write so much that history file needs trimming? */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001110 state->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001111 if (state->cnt_history_in_file > MAX_HISTORY * 4) {
1112 FILE *fp;
1113 char *new_name;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001114 line_input_t *st_temp;
1115 int i;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001116
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001117 /* we may have concurrently written entries from others.
1118 * load them */
1119 st_temp = new_line_input_t(state->flags);
1120 st_temp->hist_file = state->hist_file;
1121 load_history(st_temp);
1122
1123 /* write out temp file and replace hist_file atomically */
1124 new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001125 fp = fopen_for_write(new_name);
1126 if (fp) {
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001127 for (i = 0; i < st_temp->cnt_history; i++)
1128 fprintf(fp, "%s\n", st_temp->history[i]);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001129 fclose(fp);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001130 if (rename(new_name, state->hist_file) == 0)
1131 state->cnt_history_in_file = st_temp->cnt_history;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001132 }
1133 free(new_name);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001134 free_line_input_t(st_temp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001135 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001136}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001137#else
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001138#define load_history(a) ((void)0)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001139#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001140#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001141
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001142static void remember_in_history(char *str)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001143{
1144 int i;
1145
1146 if (!(state->flags & DO_HISTORY))
1147 return;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001148 if (str[0] == '\0')
1149 return;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001150 i = state->cnt_history;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001151 /* Don't save dupes */
1152 if (i && strcmp(state->history[i-1], str) == 0)
1153 return;
1154
1155 free(state->history[MAX_HISTORY]); /* redundant, paranoia */
1156 state->history[MAX_HISTORY] = NULL; /* redundant, paranoia */
1157
1158 /* If history[] is full, remove the oldest command */
1159 /* we need to keep history[MAX_HISTORY] empty, hence >=, not > */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001160 if (i >= MAX_HISTORY) {
1161 free(state->history[0]);
1162 for (i = 0; i < MAX_HISTORY-1; i++)
1163 state->history[i] = state->history[i+1];
Denis Vlasenko682ad302008-09-27 01:28:56 +00001164 /* i == MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001165 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001166 /* i <= MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001167 state->history[i++] = xstrdup(str);
Denis Vlasenko682ad302008-09-27 01:28:56 +00001168 /* i <= MAX_HISTORY */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001169 state->cur_history = i;
1170 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001171#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001172 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001173 save_history(str);
Denis Vlasenko09221922007-04-15 13:21:01 +00001174#endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001175 IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001176}
1177
1178#else /* MAX_HISTORY == 0 */
1179#define remember_in_history(a) ((void)0)
1180#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001181
1182
Erik Andersen6273f652000-03-17 01:12:41 +00001183/*
1184 * This function is used to grab a character buffer
1185 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001186 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001187 * a mini readline).
1188 *
1189 * The following standard commands are not implemented:
1190 * ESC-b -- Move back one word
1191 * ESC-f -- Move forward one word
1192 * ESC-d -- Delete back one word
1193 * ESC-h -- Delete forward one word
1194 * CTL-t -- Transpose two characters
1195 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001196 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001197 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001198 */
Eric Andersenc470f442003-07-28 09:56:35 +00001199
Denis Vlasenko38f63192007-01-22 09:03:07 +00001200#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001201static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001202vi_Word_motion(char *command, int eat)
1203{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001204 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001205 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001206 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001207 input_forward();
1208}
1209
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001210static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001211vi_word_motion(char *command, int eat)
1212{
1213 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001214 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001215 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001216 input_forward();
1217 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001218 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001219 input_forward();
1220 }
1221
Denis Vlasenko253ce002007-01-22 08:34:44 +00001222 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001223 input_forward();
1224
Denis Vlasenko253ce002007-01-22 08:34:44 +00001225 if (eat && cursor < command_len && isspace(command[cursor]))
1226 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001227 input_forward();
1228}
1229
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001230static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001231vi_End_motion(char *command)
1232{
1233 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001234 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001235 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001236 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001237 input_forward();
1238}
1239
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001240static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001241vi_end_motion(char *command)
1242{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001243 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001244 return;
1245 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001246 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001247 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001248 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001249 return;
1250 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001251 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001252 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1253 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001254 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001255 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001256 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001257 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001258 input_forward();
1259 }
1260}
1261
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001262static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001263vi_Back_motion(char *command)
1264{
1265 while (cursor > 0 && isspace(command[cursor-1]))
1266 input_backward(1);
1267 while (cursor > 0 && !isspace(command[cursor-1]))
1268 input_backward(1);
1269}
1270
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001271static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001272vi_back_motion(char *command)
1273{
1274 if (cursor <= 0)
1275 return;
1276 input_backward(1);
1277 while (cursor > 0 && isspace(command[cursor]))
1278 input_backward(1);
1279 if (cursor <= 0)
1280 return;
1281 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001282 while (cursor > 0
1283 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1284 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001285 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001286 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001287 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001288 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001289 input_backward(1);
1290 }
1291}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001292#endif
1293
1294
1295/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001296 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001297 */
1298
Denis Vlasenko38f63192007-01-22 09:03:07 +00001299#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001300static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001301{
1302 cmdedit_prompt = prmt_ptr;
1303 cmdedit_prmt_len = strlen(prmt_ptr);
1304 put_prompt();
1305}
1306#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001307static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001308{
1309 int prmt_len = 0;
1310 size_t cur_prmt_len = 0;
1311 char flg_not_length = '[';
1312 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001313 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1314 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001315 char c;
1316 char *pbuf;
1317
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001318 cmdedit_prmt_len = 0;
1319
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001320 if (!cwd_buf) {
1321 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001322 }
1323
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001324 cbuf[1] = '\0'; /* never changes */
1325
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001326 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001327 char *free_me = NULL;
1328
1329 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001330 c = *prmt_ptr++;
1331 if (c == '\\') {
1332 const char *cp = prmt_ptr;
1333 int l;
1334
1335 c = bb_process_escape_sequence(&prmt_ptr);
1336 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001337 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001338 break;
1339 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001340
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001341 switch (c) {
1342#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1343 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001344 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001345 break;
1346#endif
1347 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001348 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001349 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001350 break;
1351 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001352 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001353 break;
1354#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1355 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001356 /* /home/user[/something] -> ~[/something] */
1357 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001358 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001359 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001360 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1361 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1362 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001363 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001364 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001365 }
1366 break;
1367#endif
1368 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001369 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001370 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001371 if (cp != NULL && cp != pbuf)
1372 pbuf += (cp-pbuf) + 1;
1373 break;
1374 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001375 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001376 break;
1377 case 'e': case 'E': /* \e \E = \033 */
1378 c = '\033';
1379 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001380 case 'x': case 'X': {
1381 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001382 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001383 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001384 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001385 buf2[l] = '\0';
1386 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001387 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001388 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001389 break;
1390 }
1391 prmt_ptr++;
1392 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001393 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001394 if (c == 0)
1395 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001396 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001397 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001398 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001399 case '[': case ']':
1400 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001401 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001402 continue;
1403 }
1404 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001405 } /* switch */
1406 } /* if */
1407 } /* if */
1408 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001409 cur_prmt_len = strlen(pbuf);
1410 prmt_len += cur_prmt_len;
1411 if (flg_not_length != ']')
1412 cmdedit_prmt_len += cur_prmt_len;
1413 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001414 free(free_me);
1415 } /* while */
1416
1417 if (cwd_buf != (char *)bb_msg_unknown)
1418 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001419 cmdedit_prompt = prmt_mem_ptr;
1420 put_prompt();
1421}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001422#endif
1423
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001424static void cmdedit_setwidth(unsigned w, int redraw_flg)
1425{
1426 cmdedit_termw = w;
1427 if (redraw_flg) {
1428 /* new y for current cursor */
1429 int new_y = (cursor + cmdedit_prmt_len) / w;
1430 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001431 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001432 fflush(stdout);
1433 }
1434}
1435
1436static void win_changed(int nsig)
1437{
Denis Vlasenko55995022008-05-18 22:28:26 +00001438 unsigned width;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001439 get_terminal_width_height(0, &width, NULL);
1440 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1441 if (nsig == SIGWINCH)
1442 signal(SIGWINCH, win_changed); /* rearm ourself */
1443}
1444
Denys Vlasenko020f4062009-05-17 16:44:54 +02001445static int lineedit_read_key(char *read_key_buffer)
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001446{
Denys Vlasenko020f4062009-05-17 16:44:54 +02001447 int64_t ic;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001448 struct pollfd pfd;
Denys Vlasenko020f4062009-05-17 16:44:54 +02001449
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001450 pfd.fd = STDIN_FILENO;
1451 pfd.events = POLLIN;
1452 do {
Denys Vlasenko020f4062009-05-17 16:44:54 +02001453 poll_again:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001454 /* Wait for input. Can't just call read_key, it will return
1455 * at once if stdin is in non-blocking mode. */
1456 safe_poll(&pfd, 1, -1);
1457 /* note: read_key sets errno to 0 on success: */
Denys Vlasenko020f4062009-05-17 16:44:54 +02001458 ic = read_key(STDIN_FILENO, read_key_buffer);
1459 if (ENABLE_FEATURE_EDITING_ASK_TERMINAL
1460 && (int32_t)ic == KEYCODE_CURSOR_POS
1461 ) {
1462 int col = ((ic >> 32) & 0x7fff) - 1;
Denys Vlasenkoc396fe62009-05-17 19:28:14 +02001463 if (col > cmdedit_prmt_len) {
1464 cmdedit_x += (col - cmdedit_prmt_len);
Denys Vlasenko020f4062009-05-17 16:44:54 +02001465 while (cmdedit_x >= cmdedit_termw) {
1466 cmdedit_x -= cmdedit_termw;
1467 cmdedit_y++;
1468 }
1469 }
1470 goto poll_again;
1471 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001472 } while (errno == EAGAIN);
1473 return ic;
1474}
1475
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001476/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001477 * The emacs and vi modes share much of the code in the big
1478 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001479 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001480 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001481 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001482 */
1483
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001484#define VI_CMDMODE_BIT 0x100
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001485
1486/* leave out the "vi-mode"-only case labels if vi editing isn't
1487 * configured. */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001488#define vi_case(caselabel) IF_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001489
1490/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001491#undef CTRL
1492#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001493
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001494/* Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001495 * -1 on read errors or EOF, or on bare Ctrl-D,
1496 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001497 * >0 length of input string, including terminating '\n'
1498 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001499int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001500{
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00001501 int len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001502#if ENABLE_FEATURE_TAB_COMPLETION
1503 smallint lastWasTab = FALSE;
1504#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001505 int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001506 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001507#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001508 smallint vi_cmdmode = 0;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001509#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001510 struct termios initial_settings;
1511 struct termios new_settings;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001512 char read_key_buffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001513
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001514 INIT_S();
1515
1516 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1517 || !(initial_settings.c_lflag & ECHO)
1518 ) {
1519 /* Happens when e.g. stty -echo was run before */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001520 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001521 fflush(stdout);
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001522 if (fgets(command, maxsize, stdin) == NULL)
1523 len = -1; /* EOF or error */
1524 else
1525 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001526 DEINIT_S();
1527 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001528 }
1529
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001530// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001531 if (maxsize > MAX_LINELEN)
1532 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001533
1534 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001535 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001536#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001537 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001538 if (state->cnt_history == 0)
1539 load_history(state);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001540#endif
Denis Vlasenko3c385cd2008-11-02 00:41:05 +00001541 if (state->flags & DO_HISTORY)
1542 state->cur_history = state->cnt_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001543
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001544 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001545 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001546 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001547 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001548 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001549
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001550 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001551 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1552 /* Turn off echoing and CTRL-C, so we can trap it */
1553 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001554 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001555 new_settings.c_cc[VMIN] = 1;
1556 new_settings.c_cc[VTIME] = 0;
1557 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001558#ifndef _POSIX_VDISABLE
1559#define _POSIX_VDISABLE '\0'
1560#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001561 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko202ac502008-11-05 13:20:58 +00001562 tcsetattr_stdin_TCSANOW(&new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001563
Eric Andersen6faae7d2001-02-16 20:09:17 +00001564 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001565 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1566 win_changed(0); /* do initial resizing */
1567#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1568 {
1569 struct passwd *entry;
1570
1571 entry = getpwuid(geteuid());
1572 if (entry) {
1573 user_buf = xstrdup(entry->pw_name);
1574 home_pwd_buf = xstrdup(entry->pw_dir);
1575 }
1576 }
1577#endif
Denis Vlasenko682ad302008-09-27 01:28:56 +00001578
1579#if 0
1580 for (ic = 0; ic <= MAX_HISTORY; ic++)
1581 bb_error_msg("history[%d]:'%s'", ic, state->history[ic]);
1582 bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
1583#endif
1584
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001585 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001586 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001587
Denys Vlasenkoc396fe62009-05-17 19:28:14 +02001588 read_key_buffer[0] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001589 while (1) {
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001590 fflush(NULL);
Denys Vlasenko020f4062009-05-17 16:44:54 +02001591 ic = lineedit_read_key(read_key_buffer);
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001592
Denis Vlasenko38f63192007-01-22 09:03:07 +00001593#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001594 newdelflag = 1;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001595 if (vi_cmdmode) {
1596 /* btw, since KEYCODE_xxx are all < 0, this doesn't
1597 * change ic if it contains one of them: */
1598 ic |= VI_CMDMODE_BIT;
1599 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001600#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001601
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001602 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001603 case '\n':
1604 case '\r':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001605 vi_case('\n'|VI_CMDMODE_BIT:)
1606 vi_case('\r'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001607 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001608 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001609 break_out = 1;
1610 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001611 case CTRL('A'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001612 vi_case('0'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001613 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001614 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001615 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001616 case CTRL('B'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001617 vi_case('h'|VI_CMDMODE_BIT:)
1618 vi_case('\b'|VI_CMDMODE_BIT:)
1619 vi_case('\x7f'|VI_CMDMODE_BIT:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001620 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001621 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001622 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001623 case CTRL('C'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001624 vi_case(CTRL('C')|VI_CMDMODE_BIT:)
Eric Andersen86349772000-12-18 20:25:50 +00001625 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001626 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001627 command_len = 0;
1628 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001629 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001630 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001631 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001632 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001633 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001634 errno = 0;
1635 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001636 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001637 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001638 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001639 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001640 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001641 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001642 case CTRL('E'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001643 vi_case('$'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001644 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001645 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001646 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001647 case CTRL('F'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001648 vi_case('l'|VI_CMDMODE_BIT:)
1649 vi_case(' '|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001650 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001651 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001652 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001653 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001654 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001655 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001656 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001657 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001658#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001659 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001660 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001661 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001662#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001663 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001664 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001665 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001666 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001667 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001668 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001669 case CTRL('L'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001670 vi_case(CTRL('L')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001671 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001672 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001673 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001674 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001675#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001676 case CTRL('N'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001677 vi_case(CTRL('N')|VI_CMDMODE_BIT:)
1678 vi_case('j'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001679 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001680 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001681 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001682 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001683 case CTRL('P'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001684 vi_case(CTRL('P')|VI_CMDMODE_BIT:)
1685 vi_case('k'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001686 /* Control-p -- Get previous command from history */
Denis Vlasenko682ad302008-09-27 01:28:56 +00001687 if (get_previous_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001688 goto rewrite_line;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001689 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001690#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001691 case CTRL('U'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001692 vi_case(CTRL('U')|VI_CMDMODE_BIT:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001693 /* Control-U -- Clear line before cursor */
1694 if (cursor) {
Denis Vlasenko0f293b92008-07-22 20:16:55 +00001695 overlapping_strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001696 command_len -= cursor;
1697 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001698 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001699 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001700 case CTRL('W'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001701 vi_case(CTRL('W')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001702 /* Control-W -- Remove the last word */
1703 while (cursor > 0 && isspace(command[cursor-1]))
1704 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001705 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001706 input_backspace();
1707 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001708
Denis Vlasenko38f63192007-01-22 09:03:07 +00001709#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001710 case 'i'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001711 vi_cmdmode = 0;
1712 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001713 case 'I'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001714 input_backward(cursor);
1715 vi_cmdmode = 0;
1716 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001717 case 'a'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001718 input_forward();
1719 vi_cmdmode = 0;
1720 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001721 case 'A'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001722 input_end();
1723 vi_cmdmode = 0;
1724 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001725 case 'x'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001726 input_delete(1);
1727 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001728 case 'X'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001729 if (cursor > 0) {
1730 input_backward(1);
1731 input_delete(1);
1732 }
1733 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001734 case 'W'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001735 vi_Word_motion(command, 1);
1736 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001737 case 'w'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001738 vi_word_motion(command, 1);
1739 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001740 case 'E'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001741 vi_End_motion(command);
1742 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001743 case 'e'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001744 vi_end_motion(command);
1745 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001746 case 'B'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001747 vi_Back_motion(command);
1748 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001749 case 'b'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001750 vi_back_motion(command);
1751 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001752 case 'C'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001753 vi_cmdmode = 0;
1754 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001755 case 'D'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001756 goto clear_to_eol;
1757
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001758 case 'c'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001759 vi_cmdmode = 0;
1760 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001761 case 'd'|VI_CMDMODE_BIT: {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001762 int nc, sc;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001763 int prev_ic;
1764
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001765 sc = cursor;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001766 prev_ic = ic;
1767
Denys Vlasenko020f4062009-05-17 16:44:54 +02001768 ic = lineedit_read_key(read_key_buffer);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001769 if (errno) /* error */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001770 goto prepare_to_die;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001771
1772 if ((ic | VI_CMDMODE_BIT) == prev_ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001773 /* "cc", "dd" */
1774 input_backward(cursor);
1775 goto clear_to_eol;
1776 break;
1777 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001778 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001779 case 'w':
1780 case 'W':
1781 case 'e':
1782 case 'E':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001783 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001784 case 'w': /* "dw", "cw" */
1785 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001786 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001787 case 'W': /* 'dW', 'cW' */
1788 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001789 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001790 case 'e': /* 'de', 'ce' */
1791 vi_end_motion(command);
1792 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001793 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001794 case 'E': /* 'dE', 'cE' */
1795 vi_End_motion(command);
1796 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001797 break;
1798 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001799 nc = cursor;
1800 input_backward(cursor - sc);
1801 while (nc-- > cursor)
1802 input_delete(1);
1803 break;
1804 case 'b': /* "db", "cb" */
1805 case 'B': /* implemented as B */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001806 if (ic == 'b')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001807 vi_back_motion(command);
1808 else
1809 vi_Back_motion(command);
1810 while (sc-- > cursor)
1811 input_delete(1);
1812 break;
1813 case ' ': /* "d ", "c " */
1814 input_delete(1);
1815 break;
1816 case '$': /* "d$", "c$" */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001817 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001818 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001819 input_delete(1);
1820 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001821 }
1822 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001823 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001824 case 'p'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001825 input_forward();
1826 /* fallthrough */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001827 case 'P'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001828 put();
1829 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001830 case 'r'|VI_CMDMODE_BIT:
Denys Vlasenko020f4062009-05-17 16:44:54 +02001831 ic = lineedit_read_key(read_key_buffer);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001832 if (errno) /* error */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001833 goto prepare_to_die;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001834 if (ic < ' ' || ic > 255) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001835 beep();
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001836 } else {
1837 command[cursor] = ic;
1838 bb_putchar(ic);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001839 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001840 }
1841 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001842 case '\x1b': /* ESC */
1843 if (state->flags & VI_MODE) {
1844 /* insert mode --> command mode */
1845 vi_cmdmode = 1;
1846 input_backward(1);
1847 }
1848 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001849#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001850
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001851#if MAX_HISTORY > 0
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001852 case KEYCODE_UP:
1853 if (get_previous_history())
1854 goto rewrite_line;
1855 beep();
1856 break;
1857 case KEYCODE_DOWN:
1858 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001859 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001860 rewrite_line:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001861 /* Rewrite the line with the selected history item */
1862 /* change command */
1863 command_len = strlen(strcpy(command, state->history[state->cur_history] ? : ""));
1864 /* redraw and go to eol (bol, in vi) */
1865 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
1866 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001867#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001868 case KEYCODE_RIGHT:
1869 input_forward();
1870 break;
1871 case KEYCODE_LEFT:
1872 input_backward(1);
1873 break;
1874 case KEYCODE_DELETE:
1875 input_delete(0);
1876 break;
1877 case KEYCODE_HOME:
1878 input_backward(cursor);
1879 break;
1880 case KEYCODE_END:
1881 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001882 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001883
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001884 default:
1885// /* Control-V -- force insert of next char */
1886// if (c == CTRL('V')) {
1887// if (safe_read(STDIN_FILENO, &c, 1) < 1)
1888// goto prepare_to_die;
1889// if (c == 0) {
1890// beep();
1891// break;
1892// }
1893// }
1894 if (ic < ' ' || ic > 255) {
1895 /* If VI_CMDMODE_BIT is set, ic is >= 256
1896 * and command mode ignores unexpected chars.
1897 * Otherwise, we are here if ic is a
1898 * control char or an unhandled ESC sequence,
1899 * which is also ignored.
1900 */
1901 break;
1902 }
1903 if ((int)command_len >= (maxsize - 2)) {
1904 /* Not enough space for the char and EOL */
1905 break;
Paul Fox84bbac52008-01-11 16:50:08 +00001906 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001907
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001908 command_len++;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001909 if (cursor == (command_len - 1)) {
1910 /* We are at the end, append */
1911 command[cursor] = ic;
1912 command[cursor + 1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001913 cmdedit_set_out_char(' ');
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001914 } else {
1915 /* In the middle, insert */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001916 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001917
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001918 memmove(command + sc + 1, command + sc, command_len - sc);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001919 command[sc] = ic;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001920 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001921 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001922 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001923 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001924 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001925 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001926 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001927 } /* switch (input_key) */
1928
1929 if (break_out)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001930 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001931
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001932#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001933 ic &= ~VI_CMDMODE_BIT;
1934 if (ic != '\t')
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001935 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001936#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001937 } /* while (1) */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001938
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001939 if (command_len > 0)
1940 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001941
Eric Andersen27bb7902003-12-23 20:24:51 +00001942 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001943 command[command_len++] = '\n';
1944 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001945 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001946
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001947#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001948 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001949#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001950
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001951 /* restore initial_settings */
Denis Vlasenko202ac502008-11-05 13:20:58 +00001952 tcsetattr_stdin_TCSANOW(&initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001953 /* restore SIGWINCH handler */
1954 signal(SIGWINCH, previous_SIGWINCH_handler);
1955 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001956
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00001957 len = command_len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001958 DEINIT_S();
1959
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00001960 return len; /* can't return command_len, DEINIT_S() destroys it */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001961}
1962
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001963#else
1964
1965#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001966int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001967{
1968 fputs(prompt, stdout);
1969 fflush(stdout);
1970 fgets(command, maxsize, stdin);
1971 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001972}
1973
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001974#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001975
1976
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001977/*
1978 * Testing
1979 */
1980
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001981#ifdef TEST
1982
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001983#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001984
1985const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001986
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001987int main(int argc, char **argv)
1988{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001989 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001990 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001991#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001992 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1993 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1994 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001995#else
1996 "% ";
1997#endif
1998
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001999#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00002000 setlocale(LC_ALL, "");
2001#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00002002 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00002003 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002004 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002005 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00002006 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002007 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002008 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00002009 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002010 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002011 return 0;
2012}
2013
Eric Andersenc470f442003-07-28 09:56:35 +00002014#endif /* TEST */