blob: 1397409ccb9bba16d514689ecec803311b4becc3 [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 Vlasenko7f1dc212006-12-19 01:10:25 +000018 Usage and known bugs:
Erik Andersen13456d12000-03-16 08:09:57 +000019 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.
Mark Whitley4e338752001-01-26 20:42:23 +000023 Ctrl-E also works as End.
Erik Andersen13456d12000-03-16 08:09:57 +000024
Eric Andersen5f2c79d2001-02-16 18:36:04 +000025 Small bugs (simple effect):
26 - not true viewing if terminal size (x*y symbols) less
Denis Vlasenko00cdbd82007-01-21 19:21:21 +000027 size (prompt + editor's line + 2 symbols)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000028 - not true viewing if length prompt less terminal width
Erik Andersen13456d12000-03-16 08:09:57 +000029 */
30
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000031#include "libbb.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000032
Glenn L McGrath475820c2004-01-22 12:42:23 +000033
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000034/* FIXME: obsolete CONFIG item? */
35#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
36
37
Glenn L McGrath3b251852004-01-03 12:07:32 +000038#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000039
Denis Vlasenko38f63192007-01-22 09:03:07 +000040#define ENABLE_FEATURE_EDITING 0
41#define ENABLE_FEATURE_TAB_COMPLETION 0
42#define ENABLE_FEATURE_USERNAME_COMPLETION 0
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000043#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +000044
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000045#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000046
Eric Andersen5165fbe2001-02-20 06:42:29 +000047
Denis Vlasenko82b39e82007-01-21 19:18:19 +000048/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko38f63192007-01-22 09:03:07 +000049#if ENABLE_FEATURE_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000050
Denis Vlasenko82b39e82007-01-21 19:18:19 +000051#if ENABLE_LOCALE_SUPPORT
52#define Isprint(c) isprint(c)
53#else
54#define Isprint(c) ((c) >= ' ' && (c) != ((unsigned char)'\233'))
55#endif
56
Denis Vlasenko7e46cf72006-12-23 01:21:55 +000057#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000058 (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
59#define USE_FEATURE_GETUSERNAME_AND_HOMEDIR(...)
60#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
61#undef USE_FEATURE_GETUSERNAME_AND_HOMEDIR
62#define USE_FEATURE_GETUSERNAME_AND_HOMEDIR(...) __VA_ARGS__
63#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000064
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000065enum {
66 /* We use int16_t for positions, need to limit line len */
67 MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN < 0x7ff0
68 ? CONFIG_FEATURE_EDITING_MAX_LEN
69 : 0x7ff0
70};
Robert Griebl350d26b2002-12-03 22:45:46 +000071
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000072#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
73static const char null_str[] ALIGN1 = "";
74#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +000075
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000076/* We try to minimize both static and stack usage. */
77struct statics {
78 line_input_t *state;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000079
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000080 volatile unsigned cmdedit_termw; /* = 80; */ /* actual terminal width */
81 sighandler_t previous_SIGWINCH_handler;
Mark Whitley4e338752001-01-26 20:42:23 +000082
Eric Andersen86349772000-12-18 20:25:50 +000083
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000084 int cmdedit_x; /* real x terminal position */
85 int cmdedit_y; /* pseudoreal y terminal position */
86 int cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000087
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000088 unsigned cursor;
89 unsigned command_len;
90 char *command_ps;
91
92 const char *cmdedit_prompt;
Denis Vlasenko38f63192007-01-22 09:03:07 +000093#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000094 int num_ok_lines; /* = 1; */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000095#endif
96
Denis Vlasenko82b39e82007-01-21 19:18:19 +000097#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000098 char *user_buf;
99 char *home_pwd_buf; /* = (char*)null_str; */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000100#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000101
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000102#if ENABLE_FEATURE_TAB_COMPLETION
103 char **matches;
104 unsigned num_matches;
105#endif
106
107#if ENABLE_FEATURE_EDITING_VI
108#define DELBUFSIZ 128
109 char *delptr;
110 smallint newdelflag; /* whether delbuf should be reused yet */
111 char delbuf[DELBUFSIZ]; /* a place to store deleted characters */
112#endif
113
114 /* Formerly these were big buffers on stack: */
115#if ENABLE_FEATURE_TAB_COMPLETION
116 char exe_n_cwd_tab_completion__dirbuf[MAX_LINELEN];
117 char input_tab__matchBuf[MAX_LINELEN];
118 int16_t find_match__int_buf[MAX_LINELEN + 1]; /* need to have 9 bits at least */
119 int16_t find_match__pos_buf[MAX_LINELEN + 1];
120#endif
121};
122
Denis Vlasenko6672c8e2007-11-30 07:29:05 +0000123/* Make it reside in writable memory, yet make compiler understand
124 * that it is not going to change. */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000125static struct statics *const ptr_to_statics __attribute__ ((section (".data")));
126
127#define S (*ptr_to_statics)
128#define state (S.state )
129#define cmdedit_termw (S.cmdedit_termw )
130#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
131#define cmdedit_x (S.cmdedit_x )
132#define cmdedit_y (S.cmdedit_y )
133#define cmdedit_prmt_len (S.cmdedit_prmt_len)
134#define cursor (S.cursor )
135#define command_len (S.command_len )
136#define command_ps (S.command_ps )
137#define cmdedit_prompt (S.cmdedit_prompt )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000138#define num_ok_lines (S.num_ok_lines )
139#define user_buf (S.user_buf )
140#define home_pwd_buf (S.home_pwd_buf )
141#define matches (S.matches )
142#define num_matches (S.num_matches )
143#define delptr (S.delptr )
144#define newdelflag (S.newdelflag )
145#define delbuf (S.delbuf )
146
147#define INIT_S() do { \
148 (*(struct statics**)&ptr_to_statics) = xzalloc(sizeof(S)); \
149 cmdedit_termw = 80; \
150 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
151 USE_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \
152} while (0)
153static void deinit_S(void)
154{
155#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000156 /* This one is allocated only if FANCY_PROMPT is on
157 * (otherwise it points to verbatim prompt (NOT malloced) */
158 free((char*)cmdedit_prompt);
159#endif
160#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
161 free(user_buf);
162 if (home_pwd_buf != null_str)
163 free(home_pwd_buf);
164#endif
165 free(ptr_to_statics);
166}
167#define DEINIT_S() deinit_S()
168
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000169/* Put 'command_ps[cursor]', cursor++.
170 * Advance cursor on screen. If we reached right margin, scroll text up
171 * and remove terminal margin effect by printing 'next_char' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000172static void cmdedit_set_out_char(int next_char)
173{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000174 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000175
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000176 if (c == '\0') {
177 /* erase character after end of input string */
178 c = ' ';
179 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000180#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000181 /* Display non-printable characters in reverse */
182 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000183 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000184 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000185 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000186 c += '@';
187 if (c == 127)
188 c = '?';
189 printf("\033[7m%c\033[0m", c);
190 } else
191#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000192 {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000193 bb_putchar(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000194 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000195 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000196 /* terminal is scrolled down */
197 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000198 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000199 /* destroy "(auto)margin" */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000200 bb_putchar(next_char);
201 bb_putchar('\b');
Mark Whitley4e338752001-01-26 20:42:23 +0000202 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000203// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000204 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000205}
206
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000207/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000208static void input_end(void)
209{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000210 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000211 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000212}
213
214/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000215static void goto_new_line(void)
216{
Mark Whitley4e338752001-01-26 20:42:23 +0000217 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000218 if (cmdedit_x)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000219 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000220}
221
222
Rob Landley88621d72006-08-29 19:41:06 +0000223static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000224{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000225 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000226 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000227}
Eric Andersen81fe1232003-07-29 06:38:40 +0000228
Rob Landley88621d72006-08-29 19:41:06 +0000229static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000230{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000231 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000232}
233
Eric Andersenaff114c2004-04-14 17:51:38 +0000234/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000235/* (optimized for slow terminals) */
236static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000237{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000238 int count_y;
239
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000240 if (num > cursor)
241 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000242 if (!num)
243 return;
244 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000245
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000246 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000247 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000248 if (num <= 4) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000249 printf("\b\b\b\b" + (4-num));
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000250 return;
251 }
252 printf("\033[%uD", num);
253 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000254 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000255
256 /* Need to go one or more lines up */
257 num -= cmdedit_x;
258 count_y = 1 + (num / cmdedit_termw);
259 cmdedit_y -= count_y;
260 cmdedit_x = cmdedit_termw * count_y - num;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000261 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000262 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000263}
264
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000265static void put_prompt(void)
266{
267 out1str(cmdedit_prompt);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000268 cmdedit_x = cmdedit_prmt_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000269 cursor = 0;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000270// Huh? what if cmdedit_prmt_len >= width?
Eric Andersen7467c8d2001-07-12 20:26:32 +0000271 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000272}
273
Eric Andersenaff114c2004-04-14 17:51:38 +0000274/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000275static void redraw(int y, int back_cursor)
276{
Eric Andersenc470f442003-07-28 09:56:35 +0000277 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000278 printf("\033[%dA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000279 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000280 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000281 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000282 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000283 input_backward(back_cursor);
284}
285
Paul Fox3f11b1b2005-08-04 19:04:46 +0000286/* Delete the char in front of the cursor, optionally saving it
287 * for later putback */
288static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000289{
Mark Whitley4e338752001-01-26 20:42:23 +0000290 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000291
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000292 if (j == command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000293 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000294
Denis Vlasenko38f63192007-01-22 09:03:07 +0000295#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000296 if (save) {
297 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000298 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000299 newdelflag = 0;
300 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000301 if ((delptr - delbuf) < DELBUFSIZ)
302 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000303 }
304#endif
305
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000306 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000307 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000308 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000309 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000310 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000311}
312
Denis Vlasenko38f63192007-01-22 09:03:07 +0000313#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000314static void put(void)
315{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000316 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000317 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000318
Paul Fox3f11b1b2005-08-04 19:04:46 +0000319 if (j == 0)
320 return;
321 ocursor = cursor;
322 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000323 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000324 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000325 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000326 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000327 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000328}
329#endif
330
Mark Whitley4e338752001-01-26 20:42:23 +0000331/* Delete the char in back of the cursor */
332static void input_backspace(void)
333{
334 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000335 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000336 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000337 }
338}
339
Eric Andersenaff114c2004-04-14 17:51:38 +0000340/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000341static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000342{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000343 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000344 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000345}
346
Denis Vlasenko38f63192007-01-22 09:03:07 +0000347#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000348
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000349static void free_tab_completion_data(void)
350{
351 if (matches) {
352 while (num_matches)
353 free(matches[--num_matches]);
354 free(matches);
355 matches = NULL;
356 }
357}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000358
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000359static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000360{
361 int nm = num_matches;
362 int nm1 = nm + 1;
363
364 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000365 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000366 num_matches++;
367}
368
Denis Vlasenko38f63192007-01-22 09:03:07 +0000369#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000370static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000371{
372 struct passwd *entry;
373 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000374
Eric Andersenc470f442003-07-28 09:56:35 +0000375 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000376 userlen = strlen(ud);
377
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000378 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000379 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000380 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000381
Eric Andersenc470f442003-07-28 09:56:35 +0000382 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000383 home = home_pwd_buf;
384 } else {
385 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000386 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000387 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000388 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000389 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000390 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000391 ud = temp;
392 if (entry)
393 home = entry->pw_dir;
394 }
395 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000396 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000397 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000398 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000399 }
400 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000401 } else {
402 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000403 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000404 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000405 struct passwd pwd;
406 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000407
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000408 setpwent();
409 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000410 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000411 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
412 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000413 }
414 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000415 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000416 }
417}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000418#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000419
420enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000421 FIND_EXE_ONLY = 0,
422 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000423 FIND_FILE_ONLY = 2,
424};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000425
Mark Whitley4e338752001-01-26 20:42:23 +0000426static int path_parse(char ***p, int flags)
427{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000428 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000429 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000430 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000431 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000432
Mark Whitley4e338752001-01-26 20:42:23 +0000433 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000434 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000435 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000436
437 if (state->flags & WITH_PATH_LOOKUP)
438 pth = state->path_lookup;
439 else
440 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000441 /* PATH=<empty> or PATH=:<empty> */
442 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
443 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000444
Denis Vlasenko253ce002007-01-22 08:34:44 +0000445 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000446 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000447 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000448 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000449 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000450 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000451 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000452 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000453 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000454 }
455
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000456 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000457 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000458 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000459 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000460 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000461 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000462 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000463 *tmp++ = '\0'; /* ':' -> '\0' */
464 if (*tmp == '\0')
465 break; /* :<empty> */
466 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000467 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000468 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000469 return npth;
470}
471
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000472static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000473{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000474 DIR *dir;
475 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000476 struct stat st;
477 char *path1[1];
478 char **paths = path1;
479 int npaths;
480 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000481 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000482 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000483/* char dirbuf[MAX_LINELEN]; */
484#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000485
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000486 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000487 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000488
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000489 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000490 /* no dir, if flags==EXE_ONLY - get paths, else "." */
491 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000492 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000493 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000494 /* dirbuf = ".../.../.../" */
495 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000496#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000497 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000498 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000499#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000500 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000501 /* point to 'l' in "..../last_component" */
502 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000503 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000504
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000505 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000506 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000507 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000508 continue;
509
510 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000511 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000512 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000513
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000514 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000515 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000516 continue;
517 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000519 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000520 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000521 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000522 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000523 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000524 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000525 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000526 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000527 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000528 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000529 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000530
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000531 len1 = strlen(found);
532 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000533 found[len1] = '\0';
534 found[len1+1] = '\0';
535
Mark Whitley4e338752001-01-26 20:42:23 +0000536 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000537 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000538 if (found[len1-1] != '/') {
539 found[len1] = '/';
540 }
Mark Whitley4e338752001-01-26 20:42:23 +0000541 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000542 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000543 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000544 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000545 }
Mark Whitley4e338752001-01-26 20:42:23 +0000546 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000547 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000548 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000549 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000550 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000551 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000552 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000553 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000554 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000555 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000556 free(paths);
557 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000558#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000559}
Erik Andersenf0657d32000-04-12 17:49:52 +0000560
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000561#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000562
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000563#define collapse_pos(is, in) do { \
564 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
565 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
566} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000567
568static int find_match(char *matchBuf, int *len_with_quotes)
569{
570 int i, j;
571 int command_mode;
572 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000573/* int16_t int_buf[MAX_LINELEN + 1]; */
574/* int16_t pos_buf[MAX_LINELEN + 1]; */
575#define int_buf (S.find_match__int_buf)
576#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000577
578 /* set to integer dimension characters and own positions */
579 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000580 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000581 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000582 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000583 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000584 }
585 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000586 }
587
588 /* mask \+symbol and convert '\t' to ' ' */
589 for (i = j = 0; matchBuf[i]; i++, j++)
590 if (matchBuf[i] == '\\') {
591 collapse_pos(j, j + 1);
592 int_buf[j] |= QUOT;
593 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000594#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000595 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000596 int_buf[j] = ' ' | QUOT;
597#endif
598 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000599#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000600 else if (matchBuf[i] == '\t')
601 int_buf[j] = ' ';
602#endif
603
604 /* mask "symbols" or 'symbols' */
605 c2 = 0;
606 for (i = 0; int_buf[i]; i++) {
607 c = int_buf[i];
608 if (c == '\'' || c == '"') {
609 if (c2 == 0)
610 c2 = c;
611 else {
612 if (c == c2)
613 c2 = 0;
614 else
615 int_buf[i] |= QUOT;
616 }
617 } else if (c2 != 0 && c != '$')
618 int_buf[i] |= QUOT;
619 }
620
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000621 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000622 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
623 for (i = 0; int_buf[i]; i++) {
624 c = int_buf[i];
625 c2 = int_buf[i + 1];
626 j = i ? int_buf[i - 1] : -1;
627 command_mode = 0;
628 if (c == ';' || c == '&' || c == '|') {
629 command_mode = 1 + (c == c2);
630 if (c == '&') {
631 if (j == '>' || j == '<')
632 command_mode = 0;
633 } else if (c == '|' && j == '>')
634 command_mode = 0;
635 }
636 if (command_mode) {
637 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000638 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000639 }
640 }
641 /* collapse `command...` */
642 for (i = 0; int_buf[i]; i++)
643 if (int_buf[i] == '`') {
644 for (j = i + 1; int_buf[j]; j++)
645 if (int_buf[j] == '`') {
646 collapse_pos(i, j + 1);
647 j = 0;
648 break;
649 }
650 if (j) {
651 /* not found close ` - command mode, collapse all previous */
652 collapse_pos(0, i + 1);
653 break;
654 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000655 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000656 }
657
658 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000659 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000660 c2 = 0;
661 for (i = 0; int_buf[i]; i++)
662 if (int_buf[i] == '(' || int_buf[i] == '{') {
663 if (int_buf[i] == '(')
664 c++;
665 else
666 c2++;
667 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000668 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000669 }
670 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
671 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
672 if (int_buf[i] == ')')
673 c--;
674 else
675 c2--;
676 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000677 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000678 }
679
680 /* skip first not quote space */
681 for (i = 0; int_buf[i]; i++)
682 if (int_buf[i] != ' ')
683 break;
684 if (i)
685 collapse_pos(0, i);
686
687 /* set find mode for completion */
688 command_mode = FIND_EXE_ONLY;
689 for (i = 0; int_buf[i]; i++)
690 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
691 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000692 && matchBuf[pos_buf[0]] == 'c'
693 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000694 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000695 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000696 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000697 command_mode = FIND_FILE_ONLY;
698 break;
699 }
700 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000701 for (i = 0; int_buf[i]; i++)
702 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000703 /* find last word */
704 for (--i; i >= 0; i--) {
705 c = int_buf[i];
706 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
707 collapse_pos(0, i + 1);
708 break;
709 }
710 }
711 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000712 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
713 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000714 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000715 while ((int_buf[i] & ~QUOT) == '/'
716 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
717 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000718 i++;
719 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000720
721 /* set only match and destroy quotes */
722 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000723 for (c = 0; pos_buf[i] >= 0; i++) {
724 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000725 j = pos_buf[i] + 1;
726 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000727 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000728 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000729 *len_with_quotes = j ? j - pos_buf[0] : 0;
730
731 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000732#undef int_buf
733#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000734}
735
Glenn L McGrath4d001292003-01-06 01:11:50 +0000736/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000737 * display by column (original idea from ls applet,
738 * very optimized by me :)
739 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000740static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000741{
742 int ncols, row;
743 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000744 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000745 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000746 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000747
748 /* find the longest file name- use that as the column width */
749 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000750 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000751 if (column_width < l)
752 column_width = l;
753 }
754 column_width += 2; /* min space for columns */
755 ncols = cmdedit_termw / column_width;
756
757 if (ncols > 1) {
758 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000759 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000760 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000761 } else {
762 ncols = 1;
763 }
764 for (row = 0; row < nrows; row++) {
765 int n = row;
766 int nc;
767
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000768 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000769 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000770 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000771 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000772 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000773 }
774}
775
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000776static char *add_quote_for_spec_chars(char *found)
777{
778 int l = 0;
779 char *s = xmalloc((strlen(found) + 1) * 2);
780
781 while (*found) {
782 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
783 s[l++] = '\\';
784 s[l++] = *found++;
785 }
786 s[l] = 0;
787 return s;
788}
789
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000790static int match_compare(const void *a, const void *b)
791{
792 return strcmp(*(char**)a, *(char**)b);
793}
794
795/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000796static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000797{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000798 if (!(state->flags & TAB_COMPLETION))
799 return;
800
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000801 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000802 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000803 int len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000804/* char matchBuf[MAX_LINELEN]; */
805#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000806 int find_type;
807 int recalc_pos;
808
Eric Andersenc470f442003-07-28 09:56:35 +0000809 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000810
811 /* Make a local copy of the string -- up
812 * to the position of the cursor */
813 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000814 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000815
816 find_type = find_match(matchBuf, &recalc_pos);
817
818 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000819 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000820
Denis Vlasenko38f63192007-01-22 09:03:07 +0000821#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000822 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000823 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000824 if (state->flags & USERNAME_COMPLETION)
825 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
826 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000827#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000828 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000829 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000830 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000831 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000832 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000833 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000834 int i, n = 0;
835 qsort(matches, num_matches, sizeof(char*), match_compare);
836 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000837 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000838 if (strcmp(matches[i], matches[i+1]) == 0) {
839 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000840 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000841 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000842 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000843 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000844 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000845 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000846 matches[n] = matches[i];
847 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000848 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000849 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000850 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000851 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000852 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000853 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000854 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000855 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000856 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000857 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000858 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000859 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000860 break;
861 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000862 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000863 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000864 return;
865 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000866 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000867 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000868 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000869 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000870 /* for next completion current found */
871 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000872
873 len_found = strlen(tmp);
874 if (tmp[len_found-1] != '/') {
875 tmp[len_found] = ' ';
876 tmp[len_found+1] = '\0';
877 }
Mark Whitley4e338752001-01-26 20:42:23 +0000878 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000879 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000880 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000881 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000882 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000883 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000884 /* save tail line */
885 strcpy(matchBuf, command_ps + cursor);
886 /* add match */
887 strcat(command_ps, tmp);
888 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000889 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000890 /* back to begin word for match */
891 input_backward(recalc_pos);
892 /* new pos */
893 recalc_pos = cursor + len_found;
894 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000895 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000896 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000897 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000898 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000899 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000900#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000901 } else {
902 /* Ok -- the last char was a TAB. Since they
903 * just hit TAB again, print a list of all the
904 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000905 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000906 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000907
908 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000909 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000910 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000911 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000912 }
913 }
914}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000915
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000916#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000917
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000918
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000919#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000920
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000921/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000922static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000923{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000924 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
925 free(state->history[state->cur_history]);
926 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000927 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000928 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000929}
930
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000931static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000932{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000933 if (state->flags & DO_HISTORY) {
934 int ch = state->cur_history;
935 if (ch < state->cnt_history) {
936 get_previous_history(); /* save the current history line */
937 state->cur_history = ch + 1;
938 return state->cur_history;
939 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000940 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000941 beep();
942 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000943}
Robert Griebl350d26b2002-12-03 22:45:46 +0000944
Denis Vlasenko38f63192007-01-22 09:03:07 +0000945#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000946/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000947static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000948{
Robert Griebl350d26b2002-12-03 22:45:46 +0000949 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000950 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000951
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000952 /* cleanup old */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000953 for (hi = state->cnt_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000954 hi--;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000955 free(state->history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000956 }
957
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000958 fp = fopen(fromfile, "r");
959 if (fp) {
960 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000961 char *hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000962 int l;
963
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000964 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000965 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000966 l = strlen(hl);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000967 if (l >= MAX_LINELEN)
968 hl[MAX_LINELEN-1] = '\0';
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000969 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000970 free(hl);
971 continue;
972 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000973 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000974 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000975 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000976 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000977 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000978}
979
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000980/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000981static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000982{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000983 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +0000984
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000985 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000986 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000987 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000988
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000989 for (i = 0; i < state->cnt_history; i++) {
990 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000991 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000992 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000993 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000994}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000995#else
996#define load_history(a) ((void)0)
997#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000998#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +0000999
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001000static void remember_in_history(const char *str)
1001{
1002 int i;
1003
1004 if (!(state->flags & DO_HISTORY))
1005 return;
1006
1007 i = state->cnt_history;
1008 free(state->history[MAX_HISTORY]);
1009 state->history[MAX_HISTORY] = NULL;
1010 /* After max history, remove the oldest command */
1011 if (i >= MAX_HISTORY) {
1012 free(state->history[0]);
1013 for (i = 0; i < MAX_HISTORY-1; i++)
1014 state->history[i] = state->history[i+1];
1015 }
1016// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
1017// (i.e. do not save dups?)
1018 state->history[i++] = xstrdup(str);
1019 state->cur_history = i;
1020 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001021#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001022 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001023 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +00001024#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00001025 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001026}
1027
1028#else /* MAX_HISTORY == 0 */
1029#define remember_in_history(a) ((void)0)
1030#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001031
1032
Erik Andersen6273f652000-03-17 01:12:41 +00001033/*
1034 * This function is used to grab a character buffer
1035 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001036 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001037 * a mini readline).
1038 *
1039 * The following standard commands are not implemented:
1040 * ESC-b -- Move back one word
1041 * ESC-f -- Move forward one word
1042 * ESC-d -- Delete back one word
1043 * ESC-h -- Delete forward one word
1044 * CTL-t -- Transpose two characters
1045 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001046 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001047 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001048 */
Eric Andersenc470f442003-07-28 09:56:35 +00001049
Denis Vlasenko38f63192007-01-22 09:03:07 +00001050#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001051static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001052vi_Word_motion(char *command, int eat)
1053{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001054 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001055 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001056 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001057 input_forward();
1058}
1059
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001060static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001061vi_word_motion(char *command, int eat)
1062{
1063 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001064 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001065 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001066 input_forward();
1067 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001068 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001069 input_forward();
1070 }
1071
Denis Vlasenko253ce002007-01-22 08:34:44 +00001072 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001073 input_forward();
1074
Denis Vlasenko253ce002007-01-22 08:34:44 +00001075 if (eat && cursor < command_len && isspace(command[cursor]))
1076 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001077 input_forward();
1078}
1079
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001080static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001081vi_End_motion(char *command)
1082{
1083 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001084 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001085 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001086 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001087 input_forward();
1088}
1089
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001090static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001091vi_end_motion(char *command)
1092{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001093 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001094 return;
1095 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001096 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001097 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001098 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001099 return;
1100 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001101 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001102 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1103 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001104 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001105 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001106 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001107 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001108 input_forward();
1109 }
1110}
1111
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001112static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001113vi_Back_motion(char *command)
1114{
1115 while (cursor > 0 && isspace(command[cursor-1]))
1116 input_backward(1);
1117 while (cursor > 0 && !isspace(command[cursor-1]))
1118 input_backward(1);
1119}
1120
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001121static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001122vi_back_motion(char *command)
1123{
1124 if (cursor <= 0)
1125 return;
1126 input_backward(1);
1127 while (cursor > 0 && isspace(command[cursor]))
1128 input_backward(1);
1129 if (cursor <= 0)
1130 return;
1131 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001132 while (cursor > 0
1133 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1134 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001135 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001136 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001137 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001138 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001139 input_backward(1);
1140 }
1141}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001142#endif
1143
1144
1145/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001146 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001147 */
1148
Denis Vlasenko38f63192007-01-22 09:03:07 +00001149#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001150static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001151{
1152 cmdedit_prompt = prmt_ptr;
1153 cmdedit_prmt_len = strlen(prmt_ptr);
1154 put_prompt();
1155}
1156#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001157static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001158{
1159 int prmt_len = 0;
1160 size_t cur_prmt_len = 0;
1161 char flg_not_length = '[';
1162 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001163 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1164 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001165 char c;
1166 char *pbuf;
1167
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001168 cmdedit_prmt_len = 0;
1169
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001170 if (!cwd_buf) {
1171 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001172 }
1173
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001174 cbuf[1] = '\0'; /* never changes */
1175
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001176 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001177 char *free_me = NULL;
1178
1179 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001180 c = *prmt_ptr++;
1181 if (c == '\\') {
1182 const char *cp = prmt_ptr;
1183 int l;
1184
1185 c = bb_process_escape_sequence(&prmt_ptr);
1186 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001187 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001188 break;
1189 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001190
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001191 switch (c) {
1192#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1193 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001194 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001195 break;
1196#endif
1197 case 'h':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001198 pbuf = free_me = xzalloc(256);
1199 if (gethostname(pbuf, 255) < 0) {
1200 pbuf[0] = '?';
1201 pbuf[1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001202 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001203 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001204 break;
1205 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001206 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001207 break;
1208#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1209 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001210 /* /home/user[/something] -> ~[/something] */
1211 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001212 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001213 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001214 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1215 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1216 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001217 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001218 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001219 }
1220 break;
1221#endif
1222 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001223 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001224 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001225 if (cp != NULL && cp != pbuf)
1226 pbuf += (cp-pbuf) + 1;
1227 break;
1228 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001229 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001230 break;
1231 case 'e': case 'E': /* \e \E = \033 */
1232 c = '\033';
1233 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001234 case 'x': case 'X': {
1235 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001236 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001237 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001238 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001239 buf2[l] = '\0';
1240 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001241 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001242 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001243 break;
1244 }
1245 prmt_ptr++;
1246 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001247 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001248 if (c == 0)
1249 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001250 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001251 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001252 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001253 case '[': case ']':
1254 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001255 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001256 continue;
1257 }
1258 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001259 } /* switch */
1260 } /* if */
1261 } /* if */
1262 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001263 cur_prmt_len = strlen(pbuf);
1264 prmt_len += cur_prmt_len;
1265 if (flg_not_length != ']')
1266 cmdedit_prmt_len += cur_prmt_len;
1267 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001268 free(free_me);
1269 } /* while */
1270
1271 if (cwd_buf != (char *)bb_msg_unknown)
1272 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001273 cmdedit_prompt = prmt_mem_ptr;
1274 put_prompt();
1275}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001276#endif
1277
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001278static void cmdedit_setwidth(unsigned w, int redraw_flg)
1279{
1280 cmdedit_termw = w;
1281 if (redraw_flg) {
1282 /* new y for current cursor */
1283 int new_y = (cursor + cmdedit_prmt_len) / w;
1284 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001285 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001286 fflush(stdout);
1287 }
1288}
1289
1290static void win_changed(int nsig)
1291{
1292 int width;
1293 get_terminal_width_height(0, &width, NULL);
1294 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1295 if (nsig == SIGWINCH)
1296 signal(SIGWINCH, win_changed); /* rearm ourself */
1297}
1298
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001299/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001300 * The emacs and vi modes share much of the code in the big
1301 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001302 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001303 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001304 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001305 */
1306
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001307#define vbit 0x100
1308
1309/* leave out the "vi-mode"-only case labels if vi editing isn't
1310 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001311#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001312
1313/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001314#undef CTRL
1315#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001316
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001317/* Returns:
1318 * -1 on read errors or EOF, or on bare Ctrl-D.
1319 * 0 on ctrl-C,
1320 * >0 length of input string, including terminating '\n'
1321 */
Denis Vlasenko037576d2007-10-20 18:30:38 +00001322int read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001323{
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001324#if ENABLE_FEATURE_TAB_COMPLETION
1325 smallint lastWasTab = FALSE;
1326#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001327 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001328 unsigned char c;
1329 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001330#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001331 smallint vi_cmdmode = 0;
1332 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001333#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001334 struct termios initial_settings;
1335 struct termios new_settings;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001336
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001337 INIT_S();
1338
1339 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1340 || !(initial_settings.c_lflag & ECHO)
1341 ) {
1342 /* Happens when e.g. stty -echo was run before */
1343 int len;
1344 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001345 fflush(stdout);
1346 fgets(command, maxsize, stdin);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001347 len = strlen(command);
1348 DEINIT_S();
1349 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001350 }
1351
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001352// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001353 if (maxsize > MAX_LINELEN)
1354 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001355
1356 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001357 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001358#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001359 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001360 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001361#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001362
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001363 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001364 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001365 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001366 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001367 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001368
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001369 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001370 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1371 /* Turn off echoing and CTRL-C, so we can trap it */
1372 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001373 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001374 new_settings.c_cc[VMIN] = 1;
1375 new_settings.c_cc[VTIME] = 0;
1376 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001377#ifndef _POSIX_VDISABLE
1378#define _POSIX_VDISABLE '\0'
1379#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001380 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001381 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001382
Eric Andersen6faae7d2001-02-16 20:09:17 +00001383 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001384 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1385 win_changed(0); /* do initial resizing */
1386#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1387 {
1388 struct passwd *entry;
1389
1390 entry = getpwuid(geteuid());
1391 if (entry) {
1392 user_buf = xstrdup(entry->pw_name);
1393 home_pwd_buf = xstrdup(entry->pw_dir);
1394 }
1395 }
1396#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001397 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001398 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001399
Erik Andersenc7c634b2000-03-19 05:28:55 +00001400 while (1) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001401 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +00001402
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001403 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001404 /* if we can't read input then exit */
1405 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001406 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001407
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001408 ic = c;
1409
Denis Vlasenko38f63192007-01-22 09:03:07 +00001410#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001411 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001412 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001413 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001414#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001415 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001416 case '\n':
1417 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001418 vi_case('\n'|vbit:)
1419 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001420 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001421 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001422 break_out = 1;
1423 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001424#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001425 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001426 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001427 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001428 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001429 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001430 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001431 vi_case('h'|vbit:)
1432 vi_case('\b'|vbit:)
1433 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001434 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001435 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001436 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001437#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001438 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001439 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001440 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001441 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001442 command_len = 0;
1443 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001444 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001445 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001446 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001447 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001448 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001449 errno = 0;
1450 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001451 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001452 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001453 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001454 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001455 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001456 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001457
1458#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001459 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001460 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001461 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001462 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001463 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001464 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001465 vi_case('l'|vbit:)
1466 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001467 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001468 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001469 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001470#endif
1471
Erik Andersenf0657d32000-04-12 17:49:52 +00001472 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001473 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001474 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001475 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001476 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001477
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001478#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001479 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001480 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001481 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001482#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001483
1484#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001485 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001486 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001487 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001488 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001489 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001490 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001491 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001492 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001493 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001494 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001495 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001496 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001497#endif
1498
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001499#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001500 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001501 vi_case(CTRL('N')|vbit:)
1502 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001503 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001504 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001505 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001506 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001507 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001508 vi_case(CTRL('P')|vbit:)
1509 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001510 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001511 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001512 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001513 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001514 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001515 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001516 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001517#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001518
1519#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001520 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001521 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001522 /* Control-U -- Clear line before cursor */
1523 if (cursor) {
1524 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001525 command_len -= cursor;
1526 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001527 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001528 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001529#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001530 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001531 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001532 /* Control-W -- Remove the last word */
1533 while (cursor > 0 && isspace(command[cursor-1]))
1534 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001535 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001536 input_backspace();
1537 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001538
Denis Vlasenko38f63192007-01-22 09:03:07 +00001539#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001540 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001541 vi_cmdmode = 0;
1542 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001543 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001544 input_backward(cursor);
1545 vi_cmdmode = 0;
1546 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001547 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001548 input_forward();
1549 vi_cmdmode = 0;
1550 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001551 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001552 input_end();
1553 vi_cmdmode = 0;
1554 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001555 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001556 input_delete(1);
1557 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001558 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001559 if (cursor > 0) {
1560 input_backward(1);
1561 input_delete(1);
1562 }
1563 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001564 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001565 vi_Word_motion(command, 1);
1566 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001567 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001568 vi_word_motion(command, 1);
1569 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001570 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001571 vi_End_motion(command);
1572 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001573 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001574 vi_end_motion(command);
1575 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001576 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001577 vi_Back_motion(command);
1578 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001579 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001580 vi_back_motion(command);
1581 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001582 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001583 vi_cmdmode = 0;
1584 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001585 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001586 goto clear_to_eol;
1587
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001588 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001589 vi_cmdmode = 0;
1590 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001591 case 'd'|vbit: {
1592 int nc, sc;
1593 sc = cursor;
1594 prevc = ic;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001595 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001596 goto prepare_to_die;
1597 if (c == (prevc & 0xff)) {
1598 /* "cc", "dd" */
1599 input_backward(cursor);
1600 goto clear_to_eol;
1601 break;
1602 }
1603 switch (c) {
1604 case 'w':
1605 case 'W':
1606 case 'e':
1607 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001608 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001609 case 'w': /* "dw", "cw" */
1610 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001611 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001612 case 'W': /* 'dW', 'cW' */
1613 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001614 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001615 case 'e': /* 'de', 'ce' */
1616 vi_end_motion(command);
1617 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001618 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001619 case 'E': /* 'dE', 'cE' */
1620 vi_End_motion(command);
1621 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001622 break;
1623 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001624 nc = cursor;
1625 input_backward(cursor - sc);
1626 while (nc-- > cursor)
1627 input_delete(1);
1628 break;
1629 case 'b': /* "db", "cb" */
1630 case 'B': /* implemented as B */
1631 if (c == 'b')
1632 vi_back_motion(command);
1633 else
1634 vi_Back_motion(command);
1635 while (sc-- > cursor)
1636 input_delete(1);
1637 break;
1638 case ' ': /* "d ", "c " */
1639 input_delete(1);
1640 break;
1641 case '$': /* "d$", "c$" */
1642 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001643 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001644 input_delete(1);
1645 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001646 }
1647 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001648 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001649 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001650 input_forward();
1651 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001652 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001653 put();
1654 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001655 case 'r'|vbit:
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001656 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001657 goto prepare_to_die;
1658 if (c == 0)
1659 beep();
1660 else {
1661 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001662 bb_putchar(c);
1663 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001664 }
1665 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001666#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001667
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001668 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001669
Denis Vlasenko38f63192007-01-22 09:03:07 +00001670#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001671 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001672 /* ESC: insert mode --> command mode */
1673 vi_cmdmode = 1;
1674 input_backward(1);
1675 break;
1676 }
1677#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001678 /* escape sequence follows */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001679 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001680 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001681 /* different vt100 emulations */
1682 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001683 vi_case('['|vbit:)
1684 vi_case('O'|vbit:)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001685 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001686 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001687 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001688 if (c >= '1' && c <= '9') {
1689 unsigned char dummy;
1690
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001691 if (safe_read(STDIN_FILENO, &dummy, 1) < 1)
Glenn L McGrath475820c2004-01-22 12:42:23 +00001692 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001693 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001694 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001695 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001696
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001697 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001698#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001699 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001700 input_tab(&lastWasTab);
1701 break;
1702#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001703#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001704 case 'A':
1705 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001706 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001707 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001708 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001709 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001710 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001711 break;
1712 case 'B':
1713 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001714 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001715 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001716 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001717 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001718 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001719 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001720 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001721 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001722 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001723#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001724 case 'C':
1725 /* Right Arrow -- Move forward one character */
1726 input_forward();
1727 break;
1728 case 'D':
1729 /* Left Arrow -- Move back one character */
1730 input_backward(1);
1731 break;
1732 case '3':
1733 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001734 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001735 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001736 case '1': // vt100? linux vt? or what?
1737 case '7': // vt100? linux vt? or what?
1738 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001739 input_backward(cursor);
1740 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001741 case '4': // vt100? linux vt? or what?
1742 case '8': // vt100? linux vt? or what?
1743 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001744 input_end();
1745 break;
1746 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001747 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001748 beep();
1749 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001750 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001751
Eric Andersenc470f442003-07-28 09:56:35 +00001752 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001753#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001754 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001755 if (c == CTRL('V')) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001756 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001757 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001758 if (c == 0) {
1759 beep();
1760 break;
1761 }
1762 } else
1763#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001764
Denis Vlasenko38f63192007-01-22 09:03:07 +00001765#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001766 if (vi_cmdmode) /* Don't self-insert */
1767 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001768#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001769 if (!Isprint(c)) /* Skip non-printable characters */
1770 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001771
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001772 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001773 break;
1774
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001775 command_len++;
1776 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001777 command[cursor] = c;
1778 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001779 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001780 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001781 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001782
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001783 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001784 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001785 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001786 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001787 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001788 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001789 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001790 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001791 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001792 }
Eric Andersenc470f442003-07-28 09:56:35 +00001793 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001794 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001795
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001796#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001797 if (c != '\t')
1798 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001799#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001800 }
1801
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001802 if (command_len > 0)
1803 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001804
Eric Andersen27bb7902003-12-23 20:24:51 +00001805 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001806 command[command_len++] = '\n';
1807 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001808 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001809
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001810#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001811 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001812#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001813
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001814 /* restore initial_settings */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001815 tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001816 /* restore SIGWINCH handler */
1817 signal(SIGWINCH, previous_SIGWINCH_handler);
1818 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001819
1820 DEINIT_S();
1821
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001822 return command_len;
1823}
1824
1825line_input_t *new_line_input_t(int flags)
1826{
1827 line_input_t *n = xzalloc(sizeof(*n));
1828 n->flags = flags;
1829 return n;
1830}
1831
1832#else
1833
1834#undef read_line_input
1835int read_line_input(const char* prompt, char* command, int maxsize)
1836{
1837 fputs(prompt, stdout);
1838 fflush(stdout);
1839 fgets(command, maxsize, stdin);
1840 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001841}
1842
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001843#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001844
1845
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001846/*
1847 * Testing
1848 */
1849
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001850#ifdef TEST
1851
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001852#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001853
1854const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001855
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001856int main(int argc, char **argv)
1857{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001858 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001859 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001860#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001861 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1862 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1863 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001864#else
1865 "% ";
1866#endif
1867
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001868#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001869 setlocale(LC_ALL, "");
1870#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001871 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001872 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001873 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001874 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001875 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001876 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001877 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001878 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001879 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001880 return 0;
1881}
1882
Eric Andersenc470f442003-07-28 09:56:35 +00001883#endif /* TEST */