blob: e1404fb68634002fcc305127f47dc76f2f338279 [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{
Denys Vlasenko020f4062009-05-17 16:44:54 +0200302#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
303 /* Ask terminal where is cursor now.
304 * lineedit_read_key handles response and corrects
305 * our idea of current cursor position.
306 * Testcase: run "echo -n long_line_long_line_long_line",
307 * then type in a long, wrapping command and try to
308 * delete it using backspace key.
309 */
310 out1str("\033" "[6n");
311#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000312 out1str(cmdedit_prompt);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000313 cursor = 0;
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000314 {
315 unsigned w = cmdedit_termw; /* volatile var */
316 cmdedit_y = cmdedit_prmt_len / w; /* new quasireal y */
317 cmdedit_x = cmdedit_prmt_len % w;
318 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000319}
320
Eric Andersenaff114c2004-04-14 17:51:38 +0000321/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000322static void redraw(int y, int back_cursor)
323{
Eric Andersenc470f442003-07-28 09:56:35 +0000324 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000325 printf("\033[%dA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000326 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000327 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000328 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000329 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000330 input_backward(back_cursor);
331}
332
Paul Fox3f11b1b2005-08-04 19:04:46 +0000333/* Delete the char in front of the cursor, optionally saving it
334 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000335#if !ENABLE_FEATURE_EDITING_VI
336static void input_delete(void)
337#define input_delete(save) input_delete()
338#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000339static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000340#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000341{
Mark Whitley4e338752001-01-26 20:42:23 +0000342 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000343
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000344 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000345 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000346
Denis Vlasenko38f63192007-01-22 09:03:07 +0000347#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000348 if (save) {
349 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000350 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000351 newdelflag = 0;
352 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000353 if ((delptr - delbuf) < DELBUFSIZ)
354 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000355 }
356#endif
357
Denis Vlasenko41660c52008-07-22 20:25:24 +0000358 overlapping_strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000359 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000360 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000361 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000362 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000363}
364
Denis Vlasenko38f63192007-01-22 09:03:07 +0000365#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000366static void put(void)
367{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000368 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000369 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000370
Paul Fox3f11b1b2005-08-04 19:04:46 +0000371 if (j == 0)
372 return;
373 ocursor = cursor;
374 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000375 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000376 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000377 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000378 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000379 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000380}
381#endif
382
Mark Whitley4e338752001-01-26 20:42:23 +0000383/* Delete the char in back of the cursor */
384static void input_backspace(void)
385{
386 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000387 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000388 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000389 }
390}
391
Eric Andersenaff114c2004-04-14 17:51:38 +0000392/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000393static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000394{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000395 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000396 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000397}
398
Denis Vlasenko38f63192007-01-22 09:03:07 +0000399#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000400
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000401static void free_tab_completion_data(void)
402{
403 if (matches) {
404 while (num_matches)
405 free(matches[--num_matches]);
406 free(matches);
407 matches = NULL;
408 }
409}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000410
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000411static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000412{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000413 matches = xrealloc_vector(matches, 4, num_matches);
414 matches[num_matches] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000415 num_matches++;
416}
417
Denis Vlasenko38f63192007-01-22 09:03:07 +0000418#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000419static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000420{
421 struct passwd *entry;
422 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000423
Eric Andersenc470f442003-07-28 09:56:35 +0000424 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000425 userlen = strlen(ud);
426
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000427 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000428 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000429 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430
Eric Andersenc470f442003-07-28 09:56:35 +0000431 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000432 home = home_pwd_buf;
433 } else {
434 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000435 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000436 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000437 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000438 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000439 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000440 ud = temp;
441 if (entry)
442 home = entry->pw_dir;
443 }
444 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000445 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000446 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000447 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000448 }
449 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000450 } else {
451 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000452 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000453 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000454 struct passwd pwd;
455 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000456
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000457 setpwent();
458 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000459 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000460 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
461 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000462 }
463 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000464 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000465 }
466}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000467#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000468
469enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000470 FIND_EXE_ONLY = 0,
471 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000472 FIND_FILE_ONLY = 2,
473};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000474
Mark Whitley4e338752001-01-26 20:42:23 +0000475static int path_parse(char ***p, int flags)
476{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000477 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000478 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000479 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000480 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000481
Mark Whitley4e338752001-01-26 20:42:23 +0000482 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000483 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000484 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000485
486 if (state->flags & WITH_PATH_LOOKUP)
487 pth = state->path_lookup;
488 else
489 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000490 /* PATH=<empty> or PATH=:<empty> */
491 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
492 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000493
Denis Vlasenko253ce002007-01-22 08:34:44 +0000494 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000495 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000496 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000497 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000498 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000499 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000500 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000501 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000502 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000503 }
504
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000505 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000506 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000507 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000508 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000509 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000510 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000511 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000512 *tmp++ = '\0'; /* ':' -> '\0' */
513 if (*tmp == '\0')
514 break; /* :<empty> */
515 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000516 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000517 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000518 return npth;
519}
520
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000521static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000522{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000523 DIR *dir;
524 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000525 struct stat st;
526 char *path1[1];
527 char **paths = path1;
528 int npaths;
529 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000530 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000531 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000532/* char dirbuf[MAX_LINELEN]; */
533#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000534
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000535 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000536 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000537
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000538 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000539 /* no dir, if flags==EXE_ONLY - get paths, else "." */
540 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000541 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000542 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000543 /* dirbuf = ".../.../.../" */
544 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000545#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000546 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000547 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000548#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000549 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000550 /* point to 'l' in "..../last_component" */
551 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000552 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000553
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000554 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000555 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000556 if (!dir)
557 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000558
559 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000560 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000561 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000562
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000563 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000564 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000565 continue;
566 /* not see .name without .match */
Denis Vlasenkob5202712008-04-24 04:42:52 +0000567 if (*str_found == '.' && *pfind == '\0') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000568 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000569 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000570 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000571 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000572 found = concat_path_file(paths[i], str_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000573 /* hmm, remove in progress? */
574 /* NB: stat() first so that we see is it a directory;
575 * but if that fails, use lstat() so that
576 * we still match dangling links */
577 if (stat(found, &st) && lstat(found, &st))
Eric Andersene5dfced2001-04-09 22:48:12 +0000578 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000579 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000580 if (paths[i] != dirbuf)
Denis Vlasenkob5202712008-04-24 04:42:52 +0000581 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000582
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000583 len1 = strlen(found);
584 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000585 found[len1] = '\0';
586 found[len1+1] = '\0';
587
Mark Whitley4e338752001-01-26 20:42:23 +0000588 if (S_ISDIR(st.st_mode)) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000589 /* name is a directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000590 if (found[len1-1] != '/') {
591 found[len1] = '/';
592 }
Mark Whitley4e338752001-01-26 20:42:23 +0000593 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000594 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000595 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000596 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000597 }
Mark Whitley4e338752001-01-26 20:42:23 +0000598 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000599 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000600 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000601 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000602 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000603 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000604 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000605 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000606 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000607 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000608 free(paths);
609 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000610#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000611}
Erik Andersenf0657d32000-04-12 17:49:52 +0000612
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000613#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000614
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000615#define collapse_pos(is, in) do { \
616 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
617 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
618} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000619
620static int find_match(char *matchBuf, int *len_with_quotes)
621{
622 int i, j;
623 int command_mode;
624 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000625/* int16_t int_buf[MAX_LINELEN + 1]; */
626/* int16_t pos_buf[MAX_LINELEN + 1]; */
627#define int_buf (S.find_match__int_buf)
628#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000629
630 /* set to integer dimension characters and own positions */
631 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000632 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000633 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000634 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000635 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000636 }
637 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000638 }
639
640 /* mask \+symbol and convert '\t' to ' ' */
641 for (i = j = 0; matchBuf[i]; i++, j++)
642 if (matchBuf[i] == '\\') {
643 collapse_pos(j, j + 1);
644 int_buf[j] |= QUOT;
645 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000646#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000647 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000648 int_buf[j] = ' ' | QUOT;
649#endif
650 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000651#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000652 else if (matchBuf[i] == '\t')
653 int_buf[j] = ' ';
654#endif
655
656 /* mask "symbols" or 'symbols' */
657 c2 = 0;
658 for (i = 0; int_buf[i]; i++) {
659 c = int_buf[i];
660 if (c == '\'' || c == '"') {
661 if (c2 == 0)
662 c2 = c;
663 else {
664 if (c == c2)
665 c2 = 0;
666 else
667 int_buf[i] |= QUOT;
668 }
669 } else if (c2 != 0 && c != '$')
670 int_buf[i] |= QUOT;
671 }
672
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000673 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000674 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
675 for (i = 0; int_buf[i]; i++) {
676 c = int_buf[i];
677 c2 = int_buf[i + 1];
678 j = i ? int_buf[i - 1] : -1;
679 command_mode = 0;
680 if (c == ';' || c == '&' || c == '|') {
681 command_mode = 1 + (c == c2);
682 if (c == '&') {
683 if (j == '>' || j == '<')
684 command_mode = 0;
685 } else if (c == '|' && j == '>')
686 command_mode = 0;
687 }
688 if (command_mode) {
689 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000690 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000691 }
692 }
693 /* collapse `command...` */
694 for (i = 0; int_buf[i]; i++)
695 if (int_buf[i] == '`') {
696 for (j = i + 1; int_buf[j]; j++)
697 if (int_buf[j] == '`') {
698 collapse_pos(i, j + 1);
699 j = 0;
700 break;
701 }
702 if (j) {
703 /* not found close ` - command mode, collapse all previous */
704 collapse_pos(0, i + 1);
705 break;
706 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000707 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000708 }
709
710 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000711 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000712 c2 = 0;
713 for (i = 0; int_buf[i]; i++)
714 if (int_buf[i] == '(' || int_buf[i] == '{') {
715 if (int_buf[i] == '(')
716 c++;
717 else
718 c2++;
719 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000720 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000721 }
722 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
723 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
724 if (int_buf[i] == ')')
725 c--;
726 else
727 c2--;
728 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000729 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000730 }
731
732 /* skip first not quote space */
733 for (i = 0; int_buf[i]; i++)
734 if (int_buf[i] != ' ')
735 break;
736 if (i)
737 collapse_pos(0, i);
738
739 /* set find mode for completion */
740 command_mode = FIND_EXE_ONLY;
741 for (i = 0; int_buf[i]; i++)
742 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
743 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000744 && matchBuf[pos_buf[0]] == 'c'
745 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000746 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000747 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000748 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000749 command_mode = FIND_FILE_ONLY;
750 break;
751 }
752 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000753 for (i = 0; int_buf[i]; i++)
754 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000755 /* find last word */
756 for (--i; i >= 0; i--) {
757 c = int_buf[i];
758 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
759 collapse_pos(0, i + 1);
760 break;
761 }
762 }
763 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000764 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
765 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000766 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000767 while ((int_buf[i] & ~QUOT) == '/'
768 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
769 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000770 i++;
771 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000772
773 /* set only match and destroy quotes */
774 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000775 for (c = 0; pos_buf[i] >= 0; i++) {
776 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000777 j = pos_buf[i] + 1;
778 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000779 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000780 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000781 *len_with_quotes = j ? j - pos_buf[0] : 0;
782
783 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000784#undef int_buf
785#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000786}
787
Glenn L McGrath4d001292003-01-06 01:11:50 +0000788/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000789 * display by column (original idea from ls applet,
790 * very optimized by me :)
791 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000792static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000793{
794 int ncols, row;
795 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000796 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000797 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000798 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000799
800 /* find the longest file name- use that as the column width */
801 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000802 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000803 if (column_width < l)
804 column_width = l;
805 }
806 column_width += 2; /* min space for columns */
807 ncols = cmdedit_termw / column_width;
808
809 if (ncols > 1) {
810 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000811 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000812 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000813 } else {
814 ncols = 1;
815 }
816 for (row = 0; row < nrows; row++) {
817 int n = row;
818 int nc;
819
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000820 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000821 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000822 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000823 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000824 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000825 }
826}
827
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000828static char *add_quote_for_spec_chars(char *found)
829{
830 int l = 0;
831 char *s = xmalloc((strlen(found) + 1) * 2);
832
833 while (*found) {
834 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
835 s[l++] = '\\';
836 s[l++] = *found++;
837 }
838 s[l] = 0;
839 return s;
840}
841
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000842/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000843static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000844{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000845 if (!(state->flags & TAB_COMPLETION))
846 return;
847
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000848 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000849 char *tmp, *tmp1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000850 size_t len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000851/* char matchBuf[MAX_LINELEN]; */
852#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000853 int find_type;
854 int recalc_pos;
855
Eric Andersenc470f442003-07-28 09:56:35 +0000856 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000857
858 /* Make a local copy of the string -- up
859 * to the position of the cursor */
860 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000861 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000862
863 find_type = find_match(matchBuf, &recalc_pos);
864
865 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000866 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000867
Denis Vlasenko38f63192007-01-22 09:03:07 +0000868#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000869 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000870 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000871 if (state->flags & USERNAME_COMPLETION)
872 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
873 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000874#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000875 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000876 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000877 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000878 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000879 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000880 if (matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000881 unsigned i;
882 int n = 0;
Denis Vlasenkofb290382008-03-02 12:51:26 +0000883 qsort_string_vector(matches, num_matches);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000884 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000885 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000886 if (strcmp(matches[i], matches[i+1]) == 0) {
887 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000888 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000889 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000890 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000891 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000892 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000893 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000894 matches[n] = matches[i];
895 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000896 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000897 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000898 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000899 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000900 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000901 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000902 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000903 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000904 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000905 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000906 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000907 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000908 break;
909 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000910 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000911 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000912 return;
913 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000914 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000915 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000916 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000917 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000918 /* for next completion current found */
919 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000920
921 len_found = strlen(tmp);
922 if (tmp[len_found-1] != '/') {
923 tmp[len_found] = ' ';
924 tmp[len_found+1] = '\0';
925 }
Mark Whitley4e338752001-01-26 20:42:23 +0000926 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000927 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000928 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000929 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000930 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000931 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000932 /* save tail line */
933 strcpy(matchBuf, command_ps + cursor);
934 /* add match */
935 strcat(command_ps, tmp);
936 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000937 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000938 /* back to begin word for match */
939 input_backward(recalc_pos);
940 /* new pos */
941 recalc_pos = cursor + len_found;
942 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000943 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000944 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000945 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000946 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000947 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000948#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000949 } else {
950 /* Ok -- the last char was a TAB. Since they
951 * just hit TAB again, print a list of all the
952 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000953 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000954 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000955
956 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000957 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000958 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000959 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000960 }
961 }
962}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000963
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000964#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000965
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000966
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +0000967line_input_t* FAST_FUNC new_line_input_t(int flags)
968{
969 line_input_t *n = xzalloc(sizeof(*n));
970 n->flags = flags;
971 return n;
972}
973
974
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000975#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000976
Denis Vlasenko682ad302008-09-27 01:28:56 +0000977static void save_command_ps_at_cur_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000978{
Denis Vlasenko682ad302008-09-27 01:28:56 +0000979 if (command_ps[0] != '\0') {
980 int cur = state->cur_history;
981 free(state->history[cur]);
982 state->history[cur] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000983 }
Denis Vlasenko682ad302008-09-27 01:28:56 +0000984}
985
986/* state->flags is already checked to be nonzero */
987static int get_previous_history(void)
988{
989 if ((state->flags & DO_HISTORY) && state->cur_history) {
990 save_command_ps_at_cur_history();
991 state->cur_history--;
992 return 1;
993 }
994 beep();
995 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000996}
997
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000998static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000999{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001000 if (state->flags & DO_HISTORY) {
Denis Vlasenko682ad302008-09-27 01:28:56 +00001001 if (state->cur_history < state->cnt_history) {
1002 save_command_ps_at_cur_history(); /* save the current history line */
1003 return ++state->cur_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001004 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001005 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001006 beep();
1007 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +00001008}
Robert Griebl350d26b2002-12-03 22:45:46 +00001009
Denis Vlasenko38f63192007-01-22 09:03:07 +00001010#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001011/* We try to ensure that concurrent additions to the history
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001012 * do not overwrite each other.
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001013 * Otherwise shell users get unhappy.
1014 *
1015 * History file is trimmed lazily, when it grows several times longer
1016 * than configured MAX_HISTORY lines.
1017 */
1018
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001019static void free_line_input_t(line_input_t *n)
1020{
1021 int i = n->cnt_history;
1022 while (i > 0)
1023 free(n->history[--i]);
1024 free(n);
1025}
1026
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001027/* state->flags is already checked to be nonzero */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001028static void load_history(line_input_t *st_parm)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001029{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001030 char *temp_h[MAX_HISTORY];
1031 char *line;
Robert Griebl350d26b2002-12-03 22:45:46 +00001032 FILE *fp;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001033 unsigned idx, i, line_len;
Robert Griebl350d26b2002-12-03 22:45:46 +00001034
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001035 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +00001036
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001037 fp = fopen_for_read(st_parm->hist_file);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001038 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001039 /* clean up old history */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001040 for (idx = st_parm->cnt_history; idx > 0;) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001041 idx--;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001042 free(st_parm->history[idx]);
1043 st_parm->history[idx] = NULL;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001044 }
1045
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001046 /* fill temp_h[], retaining only last MAX_HISTORY lines */
1047 memset(temp_h, 0, sizeof(temp_h));
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001048 st_parm->cnt_history_in_file = idx = 0;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001049 while ((line = xmalloc_fgetline(fp)) != NULL) {
1050 if (line[0] == '\0') {
1051 free(line);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001052 continue;
1053 }
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001054 free(temp_h[idx]);
1055 temp_h[idx] = line;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001056 st_parm->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001057 idx++;
1058 if (idx == MAX_HISTORY)
1059 idx = 0;
Robert Griebl350d26b2002-12-03 22:45:46 +00001060 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001061 fclose(fp);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001062
1063 /* find first non-NULL temp_h[], if any */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001064 if (st_parm->cnt_history_in_file) {
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001065 while (temp_h[idx] == NULL) {
1066 idx++;
1067 if (idx == MAX_HISTORY)
1068 idx = 0;
1069 }
1070 }
1071
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001072 /* copy temp_h[] to st_parm->history[] */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001073 for (i = 0; i < MAX_HISTORY;) {
1074 line = temp_h[idx];
1075 if (!line)
1076 break;
1077 idx++;
1078 if (idx == MAX_HISTORY)
1079 idx = 0;
1080 line_len = strlen(line);
1081 if (line_len >= MAX_LINELEN)
1082 line[MAX_LINELEN-1] = '\0';
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001083 st_parm->history[i++] = line;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001084 }
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001085 st_parm->cnt_history = i;
Robert Griebl350d26b2002-12-03 22:45:46 +00001086 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001087}
1088
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001089/* state->flags is already checked to be nonzero */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001090static void save_history(char *str)
Robert Griebl350d26b2002-12-03 22:45:46 +00001091{
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001092 int fd;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001093 int len, len2;
Eric Andersenc470f442003-07-28 09:56:35 +00001094
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001095 fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0666);
1096 if (fd < 0)
1097 return;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001098 xlseek(fd, 0, SEEK_END); /* paranoia */
1099 len = strlen(str);
1100 str[len] = '\n'; /* we (try to) do atomic write */
1101 len2 = full_write(fd, str, len + 1);
1102 str[len] = '\0';
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001103 close(fd);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001104 if (len2 != len + 1)
1105 return; /* "wtf?" */
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001106
1107 /* did we write so much that history file needs trimming? */
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001108 state->cnt_history_in_file++;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001109 if (state->cnt_history_in_file > MAX_HISTORY * 4) {
1110 FILE *fp;
1111 char *new_name;
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001112 line_input_t *st_temp;
1113 int i;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001114
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001115 /* we may have concurrently written entries from others.
1116 * load them */
1117 st_temp = new_line_input_t(state->flags);
1118 st_temp->hist_file = state->hist_file;
1119 load_history(st_temp);
1120
1121 /* write out temp file and replace hist_file atomically */
1122 new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001123 fp = fopen_for_write(new_name);
1124 if (fp) {
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001125 for (i = 0; i < st_temp->cnt_history; i++)
1126 fprintf(fp, "%s\n", st_temp->history[i]);
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001127 fclose(fp);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001128 if (rename(new_name, state->hist_file) == 0)
1129 state->cnt_history_in_file = st_temp->cnt_history;
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001130 }
1131 free(new_name);
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001132 free_line_input_t(st_temp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001133 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001134}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001135#else
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001136#define load_history(a) ((void)0)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001137#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001138#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001139
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001140static void remember_in_history(char *str)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001141{
1142 int i;
1143
1144 if (!(state->flags & DO_HISTORY))
1145 return;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001146 if (str[0] == '\0')
1147 return;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001148 i = state->cnt_history;
Denis Vlasenko682ad302008-09-27 01:28:56 +00001149 /* Don't save dupes */
1150 if (i && strcmp(state->history[i-1], str) == 0)
1151 return;
1152
1153 free(state->history[MAX_HISTORY]); /* redundant, paranoia */
1154 state->history[MAX_HISTORY] = NULL; /* redundant, paranoia */
1155
1156 /* If history[] is full, remove the oldest command */
1157 /* we need to keep history[MAX_HISTORY] empty, hence >=, not > */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001158 if (i >= MAX_HISTORY) {
1159 free(state->history[0]);
1160 for (i = 0; i < MAX_HISTORY-1; i++)
1161 state->history[i] = state->history[i+1];
Denis Vlasenko682ad302008-09-27 01:28:56 +00001162 /* i == MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001163 }
Denis Vlasenko682ad302008-09-27 01:28:56 +00001164 /* i <= MAX_HISTORY-1 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001165 state->history[i++] = xstrdup(str);
Denis Vlasenko682ad302008-09-27 01:28:56 +00001166 /* i <= MAX_HISTORY */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001167 state->cur_history = i;
1168 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001169#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001170 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko57abf9e2009-03-22 19:00:05 +00001171 save_history(str);
Denis Vlasenko09221922007-04-15 13:21:01 +00001172#endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001173 IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001174}
1175
1176#else /* MAX_HISTORY == 0 */
1177#define remember_in_history(a) ((void)0)
1178#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001179
1180
Erik Andersen6273f652000-03-17 01:12:41 +00001181/*
1182 * This function is used to grab a character buffer
1183 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001184 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001185 * a mini readline).
1186 *
1187 * The following standard commands are not implemented:
1188 * ESC-b -- Move back one word
1189 * ESC-f -- Move forward one word
1190 * ESC-d -- Delete back one word
1191 * ESC-h -- Delete forward one word
1192 * CTL-t -- Transpose two characters
1193 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001194 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001195 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001196 */
Eric Andersenc470f442003-07-28 09:56:35 +00001197
Denis Vlasenko38f63192007-01-22 09:03:07 +00001198#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001199static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001200vi_Word_motion(char *command, int eat)
1201{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001202 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001203 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001204 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001205 input_forward();
1206}
1207
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001208static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001209vi_word_motion(char *command, int eat)
1210{
1211 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001212 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001213 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001214 input_forward();
1215 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001216 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001217 input_forward();
1218 }
1219
Denis Vlasenko253ce002007-01-22 08:34:44 +00001220 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001221 input_forward();
1222
Denis Vlasenko253ce002007-01-22 08:34:44 +00001223 if (eat && cursor < command_len && isspace(command[cursor]))
1224 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001225 input_forward();
1226}
1227
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001228static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001229vi_End_motion(char *command)
1230{
1231 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001232 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001233 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001234 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001235 input_forward();
1236}
1237
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001238static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001239vi_end_motion(char *command)
1240{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001241 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001242 return;
1243 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001244 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001245 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001246 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001247 return;
1248 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001249 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001250 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1251 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001252 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001253 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001254 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001255 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001256 input_forward();
1257 }
1258}
1259
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001260static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001261vi_Back_motion(char *command)
1262{
1263 while (cursor > 0 && isspace(command[cursor-1]))
1264 input_backward(1);
1265 while (cursor > 0 && !isspace(command[cursor-1]))
1266 input_backward(1);
1267}
1268
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001269static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001270vi_back_motion(char *command)
1271{
1272 if (cursor <= 0)
1273 return;
1274 input_backward(1);
1275 while (cursor > 0 && isspace(command[cursor]))
1276 input_backward(1);
1277 if (cursor <= 0)
1278 return;
1279 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001280 while (cursor > 0
1281 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1282 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001283 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001284 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001285 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001286 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001287 input_backward(1);
1288 }
1289}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001290#endif
1291
1292
1293/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001294 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001295 */
1296
Denis Vlasenko38f63192007-01-22 09:03:07 +00001297#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001298static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001299{
1300 cmdedit_prompt = prmt_ptr;
1301 cmdedit_prmt_len = strlen(prmt_ptr);
1302 put_prompt();
1303}
1304#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001305static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001306{
1307 int prmt_len = 0;
1308 size_t cur_prmt_len = 0;
1309 char flg_not_length = '[';
1310 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001311 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1312 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001313 char c;
1314 char *pbuf;
1315
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001316 cmdedit_prmt_len = 0;
1317
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001318 if (!cwd_buf) {
1319 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001320 }
1321
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001322 cbuf[1] = '\0'; /* never changes */
1323
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001324 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001325 char *free_me = NULL;
1326
1327 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001328 c = *prmt_ptr++;
1329 if (c == '\\') {
1330 const char *cp = prmt_ptr;
1331 int l;
1332
1333 c = bb_process_escape_sequence(&prmt_ptr);
1334 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001335 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001336 break;
1337 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001338
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001339 switch (c) {
1340#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1341 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001342 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001343 break;
1344#endif
1345 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001346 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001347 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001348 break;
1349 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001350 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001351 break;
1352#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1353 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001354 /* /home/user[/something] -> ~[/something] */
1355 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001356 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001357 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001358 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1359 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1360 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001361 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001362 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001363 }
1364 break;
1365#endif
1366 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001367 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001368 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001369 if (cp != NULL && cp != pbuf)
1370 pbuf += (cp-pbuf) + 1;
1371 break;
1372 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001373 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001374 break;
1375 case 'e': case 'E': /* \e \E = \033 */
1376 c = '\033';
1377 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001378 case 'x': case 'X': {
1379 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001380 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001381 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001382 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001383 buf2[l] = '\0';
1384 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001385 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001386 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001387 break;
1388 }
1389 prmt_ptr++;
1390 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001391 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001392 if (c == 0)
1393 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001394 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001395 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001396 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001397 case '[': case ']':
1398 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001399 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001400 continue;
1401 }
1402 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001403 } /* switch */
1404 } /* if */
1405 } /* if */
1406 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001407 cur_prmt_len = strlen(pbuf);
1408 prmt_len += cur_prmt_len;
1409 if (flg_not_length != ']')
1410 cmdedit_prmt_len += cur_prmt_len;
1411 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001412 free(free_me);
1413 } /* while */
1414
1415 if (cwd_buf != (char *)bb_msg_unknown)
1416 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001417 cmdedit_prompt = prmt_mem_ptr;
1418 put_prompt();
1419}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001420#endif
1421
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001422static void cmdedit_setwidth(unsigned w, int redraw_flg)
1423{
1424 cmdedit_termw = w;
1425 if (redraw_flg) {
1426 /* new y for current cursor */
1427 int new_y = (cursor + cmdedit_prmt_len) / w;
1428 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001429 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001430 fflush(stdout);
1431 }
1432}
1433
1434static void win_changed(int nsig)
1435{
Denis Vlasenko55995022008-05-18 22:28:26 +00001436 unsigned width;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001437 get_terminal_width_height(0, &width, NULL);
1438 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1439 if (nsig == SIGWINCH)
1440 signal(SIGWINCH, win_changed); /* rearm ourself */
1441}
1442
Denys Vlasenko020f4062009-05-17 16:44:54 +02001443static int lineedit_read_key(char *read_key_buffer)
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001444{
Denys Vlasenko020f4062009-05-17 16:44:54 +02001445 int64_t ic;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001446 struct pollfd pfd;
Denys Vlasenko020f4062009-05-17 16:44:54 +02001447
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001448 pfd.fd = STDIN_FILENO;
1449 pfd.events = POLLIN;
1450 do {
Denys Vlasenko020f4062009-05-17 16:44:54 +02001451 poll_again:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001452 /* Wait for input. Can't just call read_key, it will return
1453 * at once if stdin is in non-blocking mode. */
1454 safe_poll(&pfd, 1, -1);
1455 /* note: read_key sets errno to 0 on success: */
Denys Vlasenko020f4062009-05-17 16:44:54 +02001456 ic = read_key(STDIN_FILENO, read_key_buffer);
1457 if (ENABLE_FEATURE_EDITING_ASK_TERMINAL
1458 && (int32_t)ic == KEYCODE_CURSOR_POS
1459 ) {
1460 int col = ((ic >> 32) & 0x7fff) - 1;
1461 if (col > 0) {
1462 cmdedit_x += col;
1463 while (cmdedit_x >= cmdedit_termw) {
1464 cmdedit_x -= cmdedit_termw;
1465 cmdedit_y++;
1466 }
1467 }
1468 goto poll_again;
1469 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001470 } while (errno == EAGAIN);
1471 return ic;
1472}
1473
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001474/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001475 * The emacs and vi modes share much of the code in the big
1476 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001477 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001478 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001479 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001480 */
1481
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001482#define VI_CMDMODE_BIT 0x100
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001483
1484/* leave out the "vi-mode"-only case labels if vi editing isn't
1485 * configured. */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001486#define vi_case(caselabel) IF_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001487
1488/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001489#undef CTRL
1490#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001491
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001492/* Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001493 * -1 on read errors or EOF, or on bare Ctrl-D,
1494 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001495 * >0 length of input string, including terminating '\n'
1496 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001497int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001498{
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00001499 int len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001500#if ENABLE_FEATURE_TAB_COMPLETION
1501 smallint lastWasTab = FALSE;
1502#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001503 int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001504 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001505#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001506 smallint vi_cmdmode = 0;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001507#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001508 struct termios initial_settings;
1509 struct termios new_settings;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001510 char read_key_buffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001511
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001512 INIT_S();
1513
1514 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1515 || !(initial_settings.c_lflag & ECHO)
1516 ) {
1517 /* Happens when e.g. stty -echo was run before */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001518 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001519 fflush(stdout);
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001520 if (fgets(command, maxsize, stdin) == NULL)
1521 len = -1; /* EOF or error */
1522 else
1523 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001524 DEINIT_S();
1525 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001526 }
1527
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001528// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001529 if (maxsize > MAX_LINELEN)
1530 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001531
1532 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001533 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001534#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001535 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenkoc0ea82a2009-03-23 06:33:37 +00001536 if (state->cnt_history == 0)
1537 load_history(state);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001538#endif
Denis Vlasenko3c385cd2008-11-02 00:41:05 +00001539 if (state->flags & DO_HISTORY)
1540 state->cur_history = state->cnt_history;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001541
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001542 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001543 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001544 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001545 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001546 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001547
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001548 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001549 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1550 /* Turn off echoing and CTRL-C, so we can trap it */
1551 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001552 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001553 new_settings.c_cc[VMIN] = 1;
1554 new_settings.c_cc[VTIME] = 0;
1555 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001556#ifndef _POSIX_VDISABLE
1557#define _POSIX_VDISABLE '\0'
1558#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001559 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko202ac502008-11-05 13:20:58 +00001560 tcsetattr_stdin_TCSANOW(&new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001561
Eric Andersen6faae7d2001-02-16 20:09:17 +00001562 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001563 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1564 win_changed(0); /* do initial resizing */
1565#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1566 {
1567 struct passwd *entry;
1568
1569 entry = getpwuid(geteuid());
1570 if (entry) {
1571 user_buf = xstrdup(entry->pw_name);
1572 home_pwd_buf = xstrdup(entry->pw_dir);
1573 }
1574 }
1575#endif
Denis Vlasenko682ad302008-09-27 01:28:56 +00001576
1577#if 0
1578 for (ic = 0; ic <= MAX_HISTORY; ic++)
1579 bb_error_msg("history[%d]:'%s'", ic, state->history[ic]);
1580 bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
1581#endif
1582
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001583 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001584 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001585
Erik Andersenc7c634b2000-03-19 05:28:55 +00001586 while (1) {
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001587 fflush(NULL);
Denys Vlasenko020f4062009-05-17 16:44:54 +02001588 ic = lineedit_read_key(read_key_buffer);
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001589
Denis Vlasenko38f63192007-01-22 09:03:07 +00001590#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001591 newdelflag = 1;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001592 if (vi_cmdmode) {
1593 /* btw, since KEYCODE_xxx are all < 0, this doesn't
1594 * change ic if it contains one of them: */
1595 ic |= VI_CMDMODE_BIT;
1596 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001597#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001598
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001599 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001600 case '\n':
1601 case '\r':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001602 vi_case('\n'|VI_CMDMODE_BIT:)
1603 vi_case('\r'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001604 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001605 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001606 break_out = 1;
1607 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001608 case CTRL('A'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001609 vi_case('0'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001610 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001611 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001612 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001613 case CTRL('B'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001614 vi_case('h'|VI_CMDMODE_BIT:)
1615 vi_case('\b'|VI_CMDMODE_BIT:)
1616 vi_case('\x7f'|VI_CMDMODE_BIT:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001617 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001618 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001619 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001620 case CTRL('C'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001621 vi_case(CTRL('C')|VI_CMDMODE_BIT:)
Eric Andersen86349772000-12-18 20:25:50 +00001622 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001623 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001624 command_len = 0;
1625 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001626 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001627 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001628 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001629 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001630 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001631 errno = 0;
1632 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001633 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001634 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001635 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001636 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001637 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001638 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001639 case CTRL('E'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001640 vi_case('$'|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001641 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001642 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001643 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001644 case CTRL('F'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001645 vi_case('l'|VI_CMDMODE_BIT:)
1646 vi_case(' '|VI_CMDMODE_BIT:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001647 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001648 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001649 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001650 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001651 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001652 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001653 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001654 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001655#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001656 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001657 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001658 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001659#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001660 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001661 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001662 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001663 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001664 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001665 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001666 case CTRL('L'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001667 vi_case(CTRL('L')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001668 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001669 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001670 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001671 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001672#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001673 case CTRL('N'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001674 vi_case(CTRL('N')|VI_CMDMODE_BIT:)
1675 vi_case('j'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001676 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001677 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001678 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001679 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001680 case CTRL('P'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001681 vi_case(CTRL('P')|VI_CMDMODE_BIT:)
1682 vi_case('k'|VI_CMDMODE_BIT:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001683 /* Control-p -- Get previous command from history */
Denis Vlasenko682ad302008-09-27 01:28:56 +00001684 if (get_previous_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001685 goto rewrite_line;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001686 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001687#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001688 case CTRL('U'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001689 vi_case(CTRL('U')|VI_CMDMODE_BIT:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001690 /* Control-U -- Clear line before cursor */
1691 if (cursor) {
Denis Vlasenko0f293b92008-07-22 20:16:55 +00001692 overlapping_strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001693 command_len -= cursor;
1694 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001695 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001696 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001697 case CTRL('W'):
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001698 vi_case(CTRL('W')|VI_CMDMODE_BIT:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001699 /* Control-W -- Remove the last word */
1700 while (cursor > 0 && isspace(command[cursor-1]))
1701 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001702 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001703 input_backspace();
1704 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001705
Denis Vlasenko38f63192007-01-22 09:03:07 +00001706#if ENABLE_FEATURE_EDITING_VI
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001707 case 'i'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001708 vi_cmdmode = 0;
1709 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001710 case 'I'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001711 input_backward(cursor);
1712 vi_cmdmode = 0;
1713 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001714 case 'a'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001715 input_forward();
1716 vi_cmdmode = 0;
1717 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001718 case 'A'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001719 input_end();
1720 vi_cmdmode = 0;
1721 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001722 case 'x'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001723 input_delete(1);
1724 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001725 case 'X'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001726 if (cursor > 0) {
1727 input_backward(1);
1728 input_delete(1);
1729 }
1730 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001731 case 'W'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001732 vi_Word_motion(command, 1);
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 'E'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001738 vi_End_motion(command);
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 'B'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001744 vi_Back_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 'C'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001750 vi_cmdmode = 0;
1751 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001752 case 'D'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001753 goto clear_to_eol;
1754
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001755 case 'c'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001756 vi_cmdmode = 0;
1757 /* fall through */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001758 case 'd'|VI_CMDMODE_BIT: {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001759 int nc, sc;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001760 int prev_ic;
1761
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001762 sc = cursor;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001763 prev_ic = ic;
1764
Denys Vlasenko020f4062009-05-17 16:44:54 +02001765 ic = lineedit_read_key(read_key_buffer);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001766 if (errno) /* error */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001767 goto prepare_to_die;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001768
1769 if ((ic | VI_CMDMODE_BIT) == prev_ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001770 /* "cc", "dd" */
1771 input_backward(cursor);
1772 goto clear_to_eol;
1773 break;
1774 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001775 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001776 case 'w':
1777 case 'W':
1778 case 'e':
1779 case 'E':
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001780 switch (ic) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001781 case 'w': /* "dw", "cw" */
1782 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001783 break;
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 'e': /* 'de', 'ce' */
1788 vi_end_motion(command);
1789 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001790 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001791 case 'E': /* 'dE', 'cE' */
1792 vi_End_motion(command);
1793 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001794 break;
1795 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001796 nc = cursor;
1797 input_backward(cursor - sc);
1798 while (nc-- > cursor)
1799 input_delete(1);
1800 break;
1801 case 'b': /* "db", "cb" */
1802 case 'B': /* implemented as B */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001803 if (ic == 'b')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001804 vi_back_motion(command);
1805 else
1806 vi_Back_motion(command);
1807 while (sc-- > cursor)
1808 input_delete(1);
1809 break;
1810 case ' ': /* "d ", "c " */
1811 input_delete(1);
1812 break;
1813 case '$': /* "d$", "c$" */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001814 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001815 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001816 input_delete(1);
1817 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001818 }
1819 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001820 }
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001821 case 'p'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001822 input_forward();
1823 /* fallthrough */
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001824 case 'P'|VI_CMDMODE_BIT:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001825 put();
1826 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001827 case 'r'|VI_CMDMODE_BIT:
Denys Vlasenko020f4062009-05-17 16:44:54 +02001828 ic = lineedit_read_key(read_key_buffer);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001829 if (errno) /* error */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001830 goto prepare_to_die;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001831 if (ic < ' ' || ic > 255) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001832 beep();
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001833 } else {
1834 command[cursor] = ic;
1835 bb_putchar(ic);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001836 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001837 }
1838 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001839 case '\x1b': /* ESC */
1840 if (state->flags & VI_MODE) {
1841 /* insert mode --> command mode */
1842 vi_cmdmode = 1;
1843 input_backward(1);
1844 }
1845 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001846#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001847
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001848#if MAX_HISTORY > 0
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001849 case KEYCODE_UP:
1850 if (get_previous_history())
1851 goto rewrite_line;
1852 beep();
1853 break;
1854 case KEYCODE_DOWN:
1855 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001856 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001857 rewrite_line:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001858 /* Rewrite the line with the selected history item */
1859 /* change command */
1860 command_len = strlen(strcpy(command, state->history[state->cur_history] ? : ""));
1861 /* redraw and go to eol (bol, in vi) */
1862 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
1863 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001864#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001865 case KEYCODE_RIGHT:
1866 input_forward();
1867 break;
1868 case KEYCODE_LEFT:
1869 input_backward(1);
1870 break;
1871 case KEYCODE_DELETE:
1872 input_delete(0);
1873 break;
1874 case KEYCODE_HOME:
1875 input_backward(cursor);
1876 break;
1877 case KEYCODE_END:
1878 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001879 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001880
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001881 default:
1882// /* Control-V -- force insert of next char */
1883// if (c == CTRL('V')) {
1884// if (safe_read(STDIN_FILENO, &c, 1) < 1)
1885// goto prepare_to_die;
1886// if (c == 0) {
1887// beep();
1888// break;
1889// }
1890// }
1891 if (ic < ' ' || ic > 255) {
1892 /* If VI_CMDMODE_BIT is set, ic is >= 256
1893 * and command mode ignores unexpected chars.
1894 * Otherwise, we are here if ic is a
1895 * control char or an unhandled ESC sequence,
1896 * which is also ignored.
1897 */
1898 break;
1899 }
1900 if ((int)command_len >= (maxsize - 2)) {
1901 /* Not enough space for the char and EOL */
1902 break;
Paul Fox84bbac52008-01-11 16:50:08 +00001903 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001904
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001905 command_len++;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001906 if (cursor == (command_len - 1)) {
1907 /* We are at the end, append */
1908 command[cursor] = ic;
1909 command[cursor + 1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001910 cmdedit_set_out_char(' ');
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001911 } else {
1912 /* In the middle, insert */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001913 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001914
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001915 memmove(command + sc + 1, command + sc, command_len - sc);
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001916 command[sc] = ic;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001917 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001918 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001919 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001920 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001921 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001922 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001923 break;
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001924 } /* switch (input_key) */
1925
1926 if (break_out)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001927 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001928
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001929#if ENABLE_FEATURE_TAB_COMPLETION
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001930 ic &= ~VI_CMDMODE_BIT;
1931 if (ic != '\t')
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001932 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001933#endif
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +02001934 } /* while (1) */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001935
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001936 if (command_len > 0)
1937 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001938
Eric Andersen27bb7902003-12-23 20:24:51 +00001939 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001940 command[command_len++] = '\n';
1941 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001942 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001943
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001944#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001945 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001946#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001947
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001948 /* restore initial_settings */
Denis Vlasenko202ac502008-11-05 13:20:58 +00001949 tcsetattr_stdin_TCSANOW(&initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001950 /* restore SIGWINCH handler */
1951 signal(SIGWINCH, previous_SIGWINCH_handler);
1952 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001953
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00001954 len = command_len;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001955 DEINIT_S();
1956
Denis Vlasenkof31c3b62008-08-20 00:46:32 +00001957 return len; /* can't return command_len, DEINIT_S() destroys it */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001958}
1959
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001960#else
1961
1962#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001963int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001964{
1965 fputs(prompt, stdout);
1966 fflush(stdout);
1967 fgets(command, maxsize, stdin);
1968 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001969}
1970
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001971#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001972
1973
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001974/*
1975 * Testing
1976 */
1977
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001978#ifdef TEST
1979
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001980#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001981
1982const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001983
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001984int main(int argc, char **argv)
1985{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001986 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001987 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001988#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001989 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1990 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1991 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001992#else
1993 "% ";
1994#endif
1995
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001996#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001997 setlocale(LC_ALL, "");
1998#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001999 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00002000 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002001 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002002 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00002003 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00002004 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002005 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00002006 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002007 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00002008 return 0;
2009}
2010
Eric Andersenc470f442003-07-28 09:56:35 +00002011#endif /* TEST */