blob: 07db6358dec2bf293a2656c1418b575790f0fb1f [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 char *hostname_buf;
95 int num_ok_lines; /* = 1; */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000096#endif
97
Denis Vlasenko82b39e82007-01-21 19:18:19 +000098#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000099 char *user_buf;
100 char *home_pwd_buf; /* = (char*)null_str; */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000101#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000102
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000103#if ENABLE_FEATURE_TAB_COMPLETION
104 char **matches;
105 unsigned num_matches;
106#endif
107
108#if ENABLE_FEATURE_EDITING_VI
109#define DELBUFSIZ 128
110 char *delptr;
111 smallint newdelflag; /* whether delbuf should be reused yet */
112 char delbuf[DELBUFSIZ]; /* a place to store deleted characters */
113#endif
114
115 /* Formerly these were big buffers on stack: */
116#if ENABLE_FEATURE_TAB_COMPLETION
117 char exe_n_cwd_tab_completion__dirbuf[MAX_LINELEN];
118 char input_tab__matchBuf[MAX_LINELEN];
119 int16_t find_match__int_buf[MAX_LINELEN + 1]; /* need to have 9 bits at least */
120 int16_t find_match__pos_buf[MAX_LINELEN + 1];
121#endif
122};
123
Denis Vlasenko6672c8e2007-11-30 07:29:05 +0000124/* Make it reside in writable memory, yet make compiler understand
125 * that it is not going to change. */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000126static struct statics *const ptr_to_statics __attribute__ ((section (".data")));
127
128#define S (*ptr_to_statics)
129#define state (S.state )
130#define cmdedit_termw (S.cmdedit_termw )
131#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
132#define cmdedit_x (S.cmdedit_x )
133#define cmdedit_y (S.cmdedit_y )
134#define cmdedit_prmt_len (S.cmdedit_prmt_len)
135#define cursor (S.cursor )
136#define command_len (S.command_len )
137#define command_ps (S.command_ps )
138#define cmdedit_prompt (S.cmdedit_prompt )
139#define hostname_buf (S.hostname_buf )
140#define num_ok_lines (S.num_ok_lines )
141#define user_buf (S.user_buf )
142#define home_pwd_buf (S.home_pwd_buf )
143#define matches (S.matches )
144#define num_matches (S.num_matches )
145#define delptr (S.delptr )
146#define newdelflag (S.newdelflag )
147#define delbuf (S.delbuf )
148
149#define INIT_S() do { \
150 (*(struct statics**)&ptr_to_statics) = xzalloc(sizeof(S)); \
151 cmdedit_termw = 80; \
152 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
153 USE_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \
154} while (0)
155static void deinit_S(void)
156{
157#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
158 free(hostname_buf);
159 /* This one is allocated only if FANCY_PROMPT is on
160 * (otherwise it points to verbatim prompt (NOT malloced) */
161 free((char*)cmdedit_prompt);
162#endif
163#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
164 free(user_buf);
165 if (home_pwd_buf != null_str)
166 free(home_pwd_buf);
167#endif
168 free(ptr_to_statics);
169}
170#define DEINIT_S() deinit_S()
171
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000172/* Put 'command_ps[cursor]', cursor++.
173 * Advance cursor on screen. If we reached right margin, scroll text up
174 * and remove terminal margin effect by printing 'next_char' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000175static void cmdedit_set_out_char(int next_char)
176{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000177 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000178
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000179 if (c == '\0') {
180 /* erase character after end of input string */
181 c = ' ';
182 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000183#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000184 /* Display non-printable characters in reverse */
185 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000186 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000187 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000188 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000189 c += '@';
190 if (c == 127)
191 c = '?';
192 printf("\033[7m%c\033[0m", c);
193 } else
194#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000195 {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000196 bb_putchar(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000197 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000198 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000199 /* terminal is scrolled down */
200 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000201 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000202 /* destroy "(auto)margin" */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000203 bb_putchar(next_char);
204 bb_putchar('\b');
Mark Whitley4e338752001-01-26 20:42:23 +0000205 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000206// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000207 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000208}
209
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000210/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000211static void input_end(void)
212{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000213 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000214 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000215}
216
217/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000218static void goto_new_line(void)
219{
Mark Whitley4e338752001-01-26 20:42:23 +0000220 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000221 if (cmdedit_x)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000222 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000223}
224
225
Rob Landley88621d72006-08-29 19:41:06 +0000226static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000227{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000228 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000229 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000230}
Eric Andersen81fe1232003-07-29 06:38:40 +0000231
Rob Landley88621d72006-08-29 19:41:06 +0000232static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000233{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000234 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000235}
236
Eric Andersenaff114c2004-04-14 17:51:38 +0000237/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000238/* (optimized for slow terminals) */
239static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000240{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000241 int count_y;
242
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000243 if (num > cursor)
244 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000245 if (!num)
246 return;
247 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000248
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000249 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000250 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000251 if (num <= 4) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000252 printf("\b\b\b\b" + (4-num));
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000253 return;
254 }
255 printf("\033[%uD", num);
256 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000257 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000258
259 /* Need to go one or more lines up */
260 num -= cmdedit_x;
261 count_y = 1 + (num / cmdedit_termw);
262 cmdedit_y -= count_y;
263 cmdedit_x = cmdedit_termw * count_y - num;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000264 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000265 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000266}
267
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000268static void put_prompt(void)
269{
270 out1str(cmdedit_prompt);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000271 cmdedit_x = cmdedit_prmt_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000272 cursor = 0;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000273// Huh? what if cmdedit_prmt_len >= width?
Eric Andersen7467c8d2001-07-12 20:26:32 +0000274 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000275}
276
Eric Andersenaff114c2004-04-14 17:51:38 +0000277/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000278static void redraw(int y, int back_cursor)
279{
Eric Andersenc470f442003-07-28 09:56:35 +0000280 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000281 printf("\033[%dA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000282 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000283 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000284 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000285 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000286 input_backward(back_cursor);
287}
288
Paul Fox3f11b1b2005-08-04 19:04:46 +0000289/* Delete the char in front of the cursor, optionally saving it
290 * for later putback */
291static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000292{
Mark Whitley4e338752001-01-26 20:42:23 +0000293 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000294
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000295 if (j == command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000296 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000297
Denis Vlasenko38f63192007-01-22 09:03:07 +0000298#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000299 if (save) {
300 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000301 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000302 newdelflag = 0;
303 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000304 if ((delptr - delbuf) < DELBUFSIZ)
305 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000306 }
307#endif
308
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000309 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000310 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000311 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000312 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000313 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000314}
315
Denis Vlasenko38f63192007-01-22 09:03:07 +0000316#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000317static void put(void)
318{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000319 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000320 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000321
Paul Fox3f11b1b2005-08-04 19:04:46 +0000322 if (j == 0)
323 return;
324 ocursor = cursor;
325 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000326 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000327 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000328 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000329 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000330 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000331}
332#endif
333
Mark Whitley4e338752001-01-26 20:42:23 +0000334/* Delete the char in back of the cursor */
335static void input_backspace(void)
336{
337 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000338 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000339 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000340 }
341}
342
Eric Andersenaff114c2004-04-14 17:51:38 +0000343/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000344static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000345{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000346 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000347 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000348}
349
Denis Vlasenko38f63192007-01-22 09:03:07 +0000350#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000351
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000352static void free_tab_completion_data(void)
353{
354 if (matches) {
355 while (num_matches)
356 free(matches[--num_matches]);
357 free(matches);
358 matches = NULL;
359 }
360}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000361
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000362static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000363{
364 int nm = num_matches;
365 int nm1 = nm + 1;
366
367 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000368 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000369 num_matches++;
370}
371
Denis Vlasenko38f63192007-01-22 09:03:07 +0000372#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000373static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000374{
375 struct passwd *entry;
376 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000377
Eric Andersenc470f442003-07-28 09:56:35 +0000378 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000379 userlen = strlen(ud);
380
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000381 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000382 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000383 char *home = NULL;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000384 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000385
Eric Andersenc470f442003-07-28 09:56:35 +0000386 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000387 home = home_pwd_buf;
388 } else {
389 /* "~user/..." */
390 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000391 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000392 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000393 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000394 ud = temp;
395 if (entry)
396 home = entry->pw_dir;
397 }
398 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000399 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000400 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000401 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000402 }
403 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000404 } else {
405 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000406 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000407 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000408 struct passwd pwd;
409 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000410
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000411 setpwent();
412 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000413 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000414 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
415 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000416 }
417 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000418 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000419 }
420}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000421#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000422
423enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000424 FIND_EXE_ONLY = 0,
425 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000426 FIND_FILE_ONLY = 2,
427};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000428
Mark Whitley4e338752001-01-26 20:42:23 +0000429static int path_parse(char ***p, int flags)
430{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000431 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000432 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000433 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000434 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000435
Mark Whitley4e338752001-01-26 20:42:23 +0000436 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000437 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000438 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000439
440 if (state->flags & WITH_PATH_LOOKUP)
441 pth = state->path_lookup;
442 else
443 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000444 /* PATH=<empty> or PATH=:<empty> */
445 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
446 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000447
Denis Vlasenko253ce002007-01-22 08:34:44 +0000448 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000449 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000450 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000451 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000452 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000453 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000454 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000455 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000456 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000457 }
458
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000459 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000460 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000461 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000462 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000463 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000464 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000465 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000466 *tmp++ = '\0'; /* ':' -> '\0' */
467 if (*tmp == '\0')
468 break; /* :<empty> */
469 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000470 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000471 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000472 return npth;
473}
474
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000475static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000476{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000477 DIR *dir;
478 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000479 struct stat st;
480 char *path1[1];
481 char **paths = path1;
482 int npaths;
483 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000484 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000485 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000486/* char dirbuf[MAX_LINELEN]; */
487#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000488
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000489 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000490 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000491
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000492 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000493 /* no dir, if flags==EXE_ONLY - get paths, else "." */
494 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000495 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000496 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000497 /* dirbuf = ".../.../.../" */
498 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000499#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000500 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000501 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000502#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000503 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000504 /* point to 'l' in "..../last_component" */
505 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000506 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000507
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000508 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000509 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000510 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000511 continue;
512
513 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000514 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000515 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000516
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000517 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000519 continue;
520 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000521 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000522 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000523 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000524 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000525 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000526 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000527 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000528 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000529 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000530 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000531 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000532 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000533
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000534 len1 = strlen(found);
535 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000536 found[len1] = '\0';
537 found[len1+1] = '\0';
538
Mark Whitley4e338752001-01-26 20:42:23 +0000539 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000540 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000541 if (found[len1-1] != '/') {
542 found[len1] = '/';
543 }
Mark Whitley4e338752001-01-26 20:42:23 +0000544 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000545 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000546 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000547 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000548 }
Mark Whitley4e338752001-01-26 20:42:23 +0000549 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000550 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000551 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000552 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000553 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000554 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000555 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000556 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000557 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000558 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000559 free(paths);
560 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000561#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000562}
Erik Andersenf0657d32000-04-12 17:49:52 +0000563
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000564#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000565
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000566#define collapse_pos(is, in) do { \
567 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
568 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
569} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000570
571static int find_match(char *matchBuf, int *len_with_quotes)
572{
573 int i, j;
574 int command_mode;
575 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000576/* int16_t int_buf[MAX_LINELEN + 1]; */
577/* int16_t pos_buf[MAX_LINELEN + 1]; */
578#define int_buf (S.find_match__int_buf)
579#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000580
581 /* set to integer dimension characters and own positions */
582 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000583 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000584 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000585 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000586 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000587 }
588 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000589 }
590
591 /* mask \+symbol and convert '\t' to ' ' */
592 for (i = j = 0; matchBuf[i]; i++, j++)
593 if (matchBuf[i] == '\\') {
594 collapse_pos(j, j + 1);
595 int_buf[j] |= QUOT;
596 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000597#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000598 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000599 int_buf[j] = ' ' | QUOT;
600#endif
601 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000602#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000603 else if (matchBuf[i] == '\t')
604 int_buf[j] = ' ';
605#endif
606
607 /* mask "symbols" or 'symbols' */
608 c2 = 0;
609 for (i = 0; int_buf[i]; i++) {
610 c = int_buf[i];
611 if (c == '\'' || c == '"') {
612 if (c2 == 0)
613 c2 = c;
614 else {
615 if (c == c2)
616 c2 = 0;
617 else
618 int_buf[i] |= QUOT;
619 }
620 } else if (c2 != 0 && c != '$')
621 int_buf[i] |= QUOT;
622 }
623
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000624 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000625 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
626 for (i = 0; int_buf[i]; i++) {
627 c = int_buf[i];
628 c2 = int_buf[i + 1];
629 j = i ? int_buf[i - 1] : -1;
630 command_mode = 0;
631 if (c == ';' || c == '&' || c == '|') {
632 command_mode = 1 + (c == c2);
633 if (c == '&') {
634 if (j == '>' || j == '<')
635 command_mode = 0;
636 } else if (c == '|' && j == '>')
637 command_mode = 0;
638 }
639 if (command_mode) {
640 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000641 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000642 }
643 }
644 /* collapse `command...` */
645 for (i = 0; int_buf[i]; i++)
646 if (int_buf[i] == '`') {
647 for (j = i + 1; int_buf[j]; j++)
648 if (int_buf[j] == '`') {
649 collapse_pos(i, j + 1);
650 j = 0;
651 break;
652 }
653 if (j) {
654 /* not found close ` - command mode, collapse all previous */
655 collapse_pos(0, i + 1);
656 break;
657 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000658 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000659 }
660
661 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000662 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000663 c2 = 0;
664 for (i = 0; int_buf[i]; i++)
665 if (int_buf[i] == '(' || int_buf[i] == '{') {
666 if (int_buf[i] == '(')
667 c++;
668 else
669 c2++;
670 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000671 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000672 }
673 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
674 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
675 if (int_buf[i] == ')')
676 c--;
677 else
678 c2--;
679 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000680 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000681 }
682
683 /* skip first not quote space */
684 for (i = 0; int_buf[i]; i++)
685 if (int_buf[i] != ' ')
686 break;
687 if (i)
688 collapse_pos(0, i);
689
690 /* set find mode for completion */
691 command_mode = FIND_EXE_ONLY;
692 for (i = 0; int_buf[i]; i++)
693 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
694 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000695 && matchBuf[pos_buf[0]] == 'c'
696 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000697 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000698 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000699 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000700 command_mode = FIND_FILE_ONLY;
701 break;
702 }
703 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000704 for (i = 0; int_buf[i]; i++)
705 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000706 /* find last word */
707 for (--i; i >= 0; i--) {
708 c = int_buf[i];
709 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
710 collapse_pos(0, i + 1);
711 break;
712 }
713 }
714 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000715 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
716 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000717 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000718 while ((int_buf[i] & ~QUOT) == '/'
719 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
720 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000721 i++;
722 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000723
724 /* set only match and destroy quotes */
725 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000726 for (c = 0; pos_buf[i] >= 0; i++) {
727 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000728 j = pos_buf[i] + 1;
729 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000730 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000731 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000732 *len_with_quotes = j ? j - pos_buf[0] : 0;
733
734 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000735#undef int_buf
736#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000737}
738
Glenn L McGrath4d001292003-01-06 01:11:50 +0000739/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000740 * display by column (original idea from ls applet,
741 * very optimized by me :)
742 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000743static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000744{
745 int ncols, row;
746 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000747 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000748 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000749 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000750
751 /* find the longest file name- use that as the column width */
752 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000753 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000754 if (column_width < l)
755 column_width = l;
756 }
757 column_width += 2; /* min space for columns */
758 ncols = cmdedit_termw / column_width;
759
760 if (ncols > 1) {
761 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000762 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000763 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000764 } else {
765 ncols = 1;
766 }
767 for (row = 0; row < nrows; row++) {
768 int n = row;
769 int nc;
770
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000771 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000772 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000773 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000774 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000775 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000776 }
777}
778
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000779static char *add_quote_for_spec_chars(char *found)
780{
781 int l = 0;
782 char *s = xmalloc((strlen(found) + 1) * 2);
783
784 while (*found) {
785 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
786 s[l++] = '\\';
787 s[l++] = *found++;
788 }
789 s[l] = 0;
790 return s;
791}
792
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000793static int match_compare(const void *a, const void *b)
794{
795 return strcmp(*(char**)a, *(char**)b);
796}
797
798/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000799static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000800{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000801 if (!(state->flags & TAB_COMPLETION))
802 return;
803
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000804 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000805 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000806 int len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000807/* char matchBuf[MAX_LINELEN]; */
808#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000809 int find_type;
810 int recalc_pos;
811
Eric Andersenc470f442003-07-28 09:56:35 +0000812 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000813
814 /* Make a local copy of the string -- up
815 * to the position of the cursor */
816 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000817 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000818
819 find_type = find_match(matchBuf, &recalc_pos);
820
821 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000822 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000823
Denis Vlasenko38f63192007-01-22 09:03:07 +0000824#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000825 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000826 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000827 if (state->flags & USERNAME_COMPLETION)
828 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
829 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000830#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000831 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000832 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000833 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000834 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000835 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000836 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000837 int i, n = 0;
838 qsort(matches, num_matches, sizeof(char*), match_compare);
839 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000840 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000841 if (strcmp(matches[i], matches[i+1]) == 0) {
842 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000843 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000844 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000845 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000846 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000847 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000848 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000849 matches[n] = matches[i];
850 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000851 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000852 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000853 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000854 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000855 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000856 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000857 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000858 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000859 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000860 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000861 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000862 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000863 break;
864 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000865 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000866 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000867 return;
868 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000869 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000870 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000871 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000872 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000873 /* for next completion current found */
874 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000875
876 len_found = strlen(tmp);
877 if (tmp[len_found-1] != '/') {
878 tmp[len_found] = ' ';
879 tmp[len_found+1] = '\0';
880 }
Mark Whitley4e338752001-01-26 20:42:23 +0000881 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000882 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000883 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000884 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000885 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000886 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000887 /* save tail line */
888 strcpy(matchBuf, command_ps + cursor);
889 /* add match */
890 strcat(command_ps, tmp);
891 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000892 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000893 /* back to begin word for match */
894 input_backward(recalc_pos);
895 /* new pos */
896 recalc_pos = cursor + len_found;
897 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000898 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000899 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000900 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000901 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000902 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000903#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000904 } else {
905 /* Ok -- the last char was a TAB. Since they
906 * just hit TAB again, print a list of all the
907 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000908 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000909 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000910
911 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000912 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000913 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000914 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000915 }
916 }
917}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000918
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000919#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000920
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000921
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000922#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000923
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000924/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000925static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000926{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000927 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
928 free(state->history[state->cur_history]);
929 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000930 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000931 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000932}
933
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000934static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000935{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000936 if (state->flags & DO_HISTORY) {
937 int ch = state->cur_history;
938 if (ch < state->cnt_history) {
939 get_previous_history(); /* save the current history line */
940 state->cur_history = ch + 1;
941 return state->cur_history;
942 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000943 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000944 beep();
945 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000946}
Robert Griebl350d26b2002-12-03 22:45:46 +0000947
Denis Vlasenko38f63192007-01-22 09:03:07 +0000948#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000949/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000950static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000951{
Robert Griebl350d26b2002-12-03 22:45:46 +0000952 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000953 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000954
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000955 /* cleanup old */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000956 for (hi = state->cnt_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000957 hi--;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000958 free(state->history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000959 }
960
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000961 fp = fopen(fromfile, "r");
962 if (fp) {
963 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000964 char *hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000965 int l;
966
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000967 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000968 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000969 l = strlen(hl);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000970 if (l >= MAX_LINELEN)
971 hl[MAX_LINELEN-1] = '\0';
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000972 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000973 free(hl);
974 continue;
975 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000976 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000977 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000978 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000979 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000980 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000981}
982
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000983/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000984static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000985{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000986 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +0000987
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000988 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000989 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000990 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000991
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000992 for (i = 0; i < state->cnt_history; i++) {
993 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000994 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000995 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000996 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000997}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000998#else
999#define load_history(a) ((void)0)
1000#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001001#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001002
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001003static void remember_in_history(const char *str)
1004{
1005 int i;
1006
1007 if (!(state->flags & DO_HISTORY))
1008 return;
1009
1010 i = state->cnt_history;
1011 free(state->history[MAX_HISTORY]);
1012 state->history[MAX_HISTORY] = NULL;
1013 /* After max history, remove the oldest command */
1014 if (i >= MAX_HISTORY) {
1015 free(state->history[0]);
1016 for (i = 0; i < MAX_HISTORY-1; i++)
1017 state->history[i] = state->history[i+1];
1018 }
1019// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
1020// (i.e. do not save dups?)
1021 state->history[i++] = xstrdup(str);
1022 state->cur_history = i;
1023 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001024#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001025 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001026 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +00001027#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00001028 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001029}
1030
1031#else /* MAX_HISTORY == 0 */
1032#define remember_in_history(a) ((void)0)
1033#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001034
1035
Erik Andersen6273f652000-03-17 01:12:41 +00001036/*
1037 * This function is used to grab a character buffer
1038 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001039 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001040 * a mini readline).
1041 *
1042 * The following standard commands are not implemented:
1043 * ESC-b -- Move back one word
1044 * ESC-f -- Move forward one word
1045 * ESC-d -- Delete back one word
1046 * ESC-h -- Delete forward one word
1047 * CTL-t -- Transpose two characters
1048 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001049 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001050 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001051 */
Eric Andersenc470f442003-07-28 09:56:35 +00001052
Denis Vlasenko38f63192007-01-22 09:03:07 +00001053#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001054static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001055vi_Word_motion(char *command, int eat)
1056{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001057 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001058 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001059 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001060 input_forward();
1061}
1062
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001063static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001064vi_word_motion(char *command, int eat)
1065{
1066 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001067 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001068 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001069 input_forward();
1070 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001071 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001072 input_forward();
1073 }
1074
Denis Vlasenko253ce002007-01-22 08:34:44 +00001075 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001076 input_forward();
1077
Denis Vlasenko253ce002007-01-22 08:34:44 +00001078 if (eat && cursor < command_len && isspace(command[cursor]))
1079 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001080 input_forward();
1081}
1082
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001083static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001084vi_End_motion(char *command)
1085{
1086 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001087 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001088 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001089 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001090 input_forward();
1091}
1092
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001093static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001094vi_end_motion(char *command)
1095{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001096 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001097 return;
1098 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001099 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001100 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001101 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001102 return;
1103 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001104 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001105 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1106 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001107 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001108 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001109 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001110 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001111 input_forward();
1112 }
1113}
1114
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001115static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001116vi_Back_motion(char *command)
1117{
1118 while (cursor > 0 && isspace(command[cursor-1]))
1119 input_backward(1);
1120 while (cursor > 0 && !isspace(command[cursor-1]))
1121 input_backward(1);
1122}
1123
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001124static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001125vi_back_motion(char *command)
1126{
1127 if (cursor <= 0)
1128 return;
1129 input_backward(1);
1130 while (cursor > 0 && isspace(command[cursor]))
1131 input_backward(1);
1132 if (cursor <= 0)
1133 return;
1134 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001135 while (cursor > 0
1136 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1137 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001138 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001139 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001140 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001141 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001142 input_backward(1);
1143 }
1144}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001145#endif
1146
1147
1148/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001149 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001150 */
1151
Denis Vlasenko38f63192007-01-22 09:03:07 +00001152#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001153static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001154{
1155 cmdedit_prompt = prmt_ptr;
1156 cmdedit_prmt_len = strlen(prmt_ptr);
1157 put_prompt();
1158}
1159#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001160static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001161{
1162 int prmt_len = 0;
1163 size_t cur_prmt_len = 0;
1164 char flg_not_length = '[';
1165 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko6ca04442007-02-11 16:19:28 +00001166 char *pwd_buf = xrealloc_getcwd_or_warn(NULL);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001167 char buf2[PATH_MAX + 1];
1168 char buf[2];
1169 char c;
1170 char *pbuf;
1171
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001172 cmdedit_prmt_len = 0;
1173
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001174 if (!pwd_buf) {
1175 pwd_buf = (char *)bb_msg_unknown;
1176 }
1177
1178 while (*prmt_ptr) {
1179 pbuf = buf;
1180 pbuf[1] = 0;
1181 c = *prmt_ptr++;
1182 if (c == '\\') {
1183 const char *cp = prmt_ptr;
1184 int l;
1185
1186 c = bb_process_escape_sequence(&prmt_ptr);
1187 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001188 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001189 break;
1190 c = *prmt_ptr++;
1191 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':
1198 pbuf = hostname_buf;
1199 if (!pbuf) {
1200 pbuf = xzalloc(256);
1201 if (gethostname(pbuf, 255) < 0) {
1202 strcpy(pbuf, "?");
1203 } else {
1204 char *s = strchr(pbuf, '.');
1205 if (s)
1206 *s = '\0';
1207 }
1208 hostname_buf = pbuf;
1209 }
1210 break;
1211 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001212 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001213 break;
1214#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1215 case 'w':
1216 pbuf = pwd_buf;
1217 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001218 if (l != 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001219 && strncmp(home_pwd_buf, pbuf, l) == 0
1220 && (pbuf[l]=='/' || pbuf[l]=='\0')
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001221 && strlen(pwd_buf+l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001222 ) {
1223 pbuf = buf2;
1224 *pbuf = '~';
1225 strcpy(pbuf+1, pwd_buf+l);
1226 }
1227 break;
1228#endif
1229 case 'W':
1230 pbuf = pwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001231 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001232 if (cp != NULL && cp != pbuf)
1233 pbuf += (cp-pbuf) + 1;
1234 break;
1235 case '!':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001236 pbuf = buf2;
1237 snprintf(buf2, sizeof(buf2), "%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001238 break;
1239 case 'e': case 'E': /* \e \E = \033 */
1240 c = '\033';
1241 break;
1242 case 'x': case 'X':
1243 for (l = 0; l < 3;) {
1244 int h;
1245 buf2[l++] = *prmt_ptr;
1246 buf2[l] = 0;
1247 h = strtol(buf2, &pbuf, 16);
1248 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
1249 l--;
1250 break;
1251 }
1252 prmt_ptr++;
1253 }
1254 buf2[l] = 0;
1255 c = (char)strtol(buf2, NULL, 16);
1256 if (c == 0)
1257 c = '?';
1258 pbuf = buf;
1259 break;
1260 case '[': case ']':
1261 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001262 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001263 continue;
1264 }
1265 break;
1266 }
1267 }
1268 }
1269 if (pbuf == buf)
1270 *pbuf = c;
1271 cur_prmt_len = strlen(pbuf);
1272 prmt_len += cur_prmt_len;
1273 if (flg_not_length != ']')
1274 cmdedit_prmt_len += cur_prmt_len;
1275 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
1276 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001277 if (pwd_buf != (char *)bb_msg_unknown)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001278 free(pwd_buf);
1279 cmdedit_prompt = prmt_mem_ptr;
1280 put_prompt();
1281}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001282#endif
1283
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001284static void cmdedit_setwidth(unsigned w, int redraw_flg)
1285{
1286 cmdedit_termw = w;
1287 if (redraw_flg) {
1288 /* new y for current cursor */
1289 int new_y = (cursor + cmdedit_prmt_len) / w;
1290 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001291 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001292 fflush(stdout);
1293 }
1294}
1295
1296static void win_changed(int nsig)
1297{
1298 int width;
1299 get_terminal_width_height(0, &width, NULL);
1300 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1301 if (nsig == SIGWINCH)
1302 signal(SIGWINCH, win_changed); /* rearm ourself */
1303}
1304
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001305/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001306 * The emacs and vi modes share much of the code in the big
1307 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001308 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001309 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001310 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001311 */
1312
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001313#define vbit 0x100
1314
1315/* leave out the "vi-mode"-only case labels if vi editing isn't
1316 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001317#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001318
1319/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001320#undef CTRL
1321#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001322
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001323/* Returns:
1324 * -1 on read errors or EOF, or on bare Ctrl-D.
1325 * 0 on ctrl-C,
1326 * >0 length of input string, including terminating '\n'
1327 */
Denis Vlasenko037576d2007-10-20 18:30:38 +00001328int read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001329{
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001330#if ENABLE_FEATURE_TAB_COMPLETION
1331 smallint lastWasTab = FALSE;
1332#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001333 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001334 unsigned char c;
1335 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001336#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001337 smallint vi_cmdmode = 0;
1338 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001339#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001340 struct termios initial_settings;
1341 struct termios new_settings;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001342
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001343 INIT_S();
1344
1345 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1346 || !(initial_settings.c_lflag & ECHO)
1347 ) {
1348 /* Happens when e.g. stty -echo was run before */
1349 int len;
1350 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001351 fflush(stdout);
1352 fgets(command, maxsize, stdin);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001353 len = strlen(command);
1354 DEINIT_S();
1355 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001356 }
1357
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001358// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001359 if (maxsize > MAX_LINELEN)
1360 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001361
1362 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001363 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001364#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001365 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001366 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001367#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001368
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001369 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001370 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001371 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001372 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001373 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001374
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001375 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001376 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1377 /* Turn off echoing and CTRL-C, so we can trap it */
1378 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001379 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001380 new_settings.c_cc[VMIN] = 1;
1381 new_settings.c_cc[VTIME] = 0;
1382 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001383#ifndef _POSIX_VDISABLE
1384#define _POSIX_VDISABLE '\0'
1385#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001386 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001387 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001388
Eric Andersen6faae7d2001-02-16 20:09:17 +00001389 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001390 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1391 win_changed(0); /* do initial resizing */
1392#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1393 {
1394 struct passwd *entry;
1395
1396 entry = getpwuid(geteuid());
1397 if (entry) {
1398 user_buf = xstrdup(entry->pw_name);
1399 home_pwd_buf = xstrdup(entry->pw_dir);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001400 /* They are not freed on exit (too small to bother) */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001401 }
1402 }
1403#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001404 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001405 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001406
Erik Andersenc7c634b2000-03-19 05:28:55 +00001407 while (1) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001408 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +00001409
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001410 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001411 /* if we can't read input then exit */
1412 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001413 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001414
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001415 ic = c;
1416
Denis Vlasenko38f63192007-01-22 09:03:07 +00001417#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001418 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001419 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001420 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001421#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001422 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001423 case '\n':
1424 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001425 vi_case('\n'|vbit:)
1426 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001427 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001428 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001429 break_out = 1;
1430 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001431#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001432 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001433 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001434 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001435 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001436 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001437 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001438 vi_case('h'|vbit:)
1439 vi_case('\b'|vbit:)
1440 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001441 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001442 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001443 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001444#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001445 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001446 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001447 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001448 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001449 command_len = 0;
1450 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001451 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001452 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001453 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001454 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001455 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001456 errno = 0;
1457 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001458 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001459 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001460 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001461 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001462 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001463 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001464
1465#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001466 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001467 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001468 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001469 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001470 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001471 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001472 vi_case('l'|vbit:)
1473 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001474 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001475 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001476 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001477#endif
1478
Erik Andersenf0657d32000-04-12 17:49:52 +00001479 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001480 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001481 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001482 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001483 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001484
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001485#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001486 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001487 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001488 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001489#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001490
1491#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001492 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001493 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001494 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001495 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001496 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001497 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001498 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001499 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001500 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001501 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001502 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001503 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001504#endif
1505
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001506#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001507 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001508 vi_case(CTRL('N')|vbit:)
1509 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001510 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001511 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001512 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001513 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001514 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001515 vi_case(CTRL('P')|vbit:)
1516 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001517 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001518 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001519 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001520 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001521 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001522 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001523 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001524#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001525
1526#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001527 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001528 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001529 /* Control-U -- Clear line before cursor */
1530 if (cursor) {
1531 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001532 command_len -= cursor;
1533 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001534 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001535 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001536#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001537 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001538 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001539 /* Control-W -- Remove the last word */
1540 while (cursor > 0 && isspace(command[cursor-1]))
1541 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001542 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001543 input_backspace();
1544 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001545
Denis Vlasenko38f63192007-01-22 09:03:07 +00001546#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001547 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001548 vi_cmdmode = 0;
1549 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001550 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001551 input_backward(cursor);
1552 vi_cmdmode = 0;
1553 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001554 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001555 input_forward();
1556 vi_cmdmode = 0;
1557 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001558 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001559 input_end();
1560 vi_cmdmode = 0;
1561 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001562 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001563 input_delete(1);
1564 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001565 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001566 if (cursor > 0) {
1567 input_backward(1);
1568 input_delete(1);
1569 }
1570 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001571 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001572 vi_Word_motion(command, 1);
1573 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001574 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001575 vi_word_motion(command, 1);
1576 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001577 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001578 vi_End_motion(command);
1579 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001580 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001581 vi_end_motion(command);
1582 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001583 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001584 vi_Back_motion(command);
1585 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001586 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001587 vi_back_motion(command);
1588 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001589 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001590 vi_cmdmode = 0;
1591 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001592 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001593 goto clear_to_eol;
1594
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001595 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001596 vi_cmdmode = 0;
1597 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001598 case 'd'|vbit: {
1599 int nc, sc;
1600 sc = cursor;
1601 prevc = ic;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001602 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001603 goto prepare_to_die;
1604 if (c == (prevc & 0xff)) {
1605 /* "cc", "dd" */
1606 input_backward(cursor);
1607 goto clear_to_eol;
1608 break;
1609 }
1610 switch (c) {
1611 case 'w':
1612 case 'W':
1613 case 'e':
1614 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001615 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001616 case 'w': /* "dw", "cw" */
1617 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001618 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001619 case 'W': /* 'dW', 'cW' */
1620 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001621 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001622 case 'e': /* 'de', 'ce' */
1623 vi_end_motion(command);
1624 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001625 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001626 case 'E': /* 'dE', 'cE' */
1627 vi_End_motion(command);
1628 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001629 break;
1630 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001631 nc = cursor;
1632 input_backward(cursor - sc);
1633 while (nc-- > cursor)
1634 input_delete(1);
1635 break;
1636 case 'b': /* "db", "cb" */
1637 case 'B': /* implemented as B */
1638 if (c == 'b')
1639 vi_back_motion(command);
1640 else
1641 vi_Back_motion(command);
1642 while (sc-- > cursor)
1643 input_delete(1);
1644 break;
1645 case ' ': /* "d ", "c " */
1646 input_delete(1);
1647 break;
1648 case '$': /* "d$", "c$" */
1649 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001650 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001651 input_delete(1);
1652 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001653 }
1654 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001655 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001656 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001657 input_forward();
1658 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001659 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001660 put();
1661 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001662 case 'r'|vbit:
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001663 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001664 goto prepare_to_die;
1665 if (c == 0)
1666 beep();
1667 else {
1668 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001669 bb_putchar(c);
1670 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001671 }
1672 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001673#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001674
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001675 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001676
Denis Vlasenko38f63192007-01-22 09:03:07 +00001677#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001678 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001679 /* ESC: insert mode --> command mode */
1680 vi_cmdmode = 1;
1681 input_backward(1);
1682 break;
1683 }
1684#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001685 /* escape sequence follows */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001686 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001687 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001688 /* different vt100 emulations */
1689 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001690 vi_case('['|vbit:)
1691 vi_case('O'|vbit:)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001692 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001693 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001694 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001695 if (c >= '1' && c <= '9') {
1696 unsigned char dummy;
1697
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001698 if (safe_read(STDIN_FILENO, &dummy, 1) < 1)
Glenn L McGrath475820c2004-01-22 12:42:23 +00001699 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001700 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001701 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001702 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001703
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001704 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001705#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001706 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001707 input_tab(&lastWasTab);
1708 break;
1709#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001710#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001711 case 'A':
1712 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001713 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001714 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001715 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001716 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001717 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001718 break;
1719 case 'B':
1720 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001721 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001722 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001723 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001724 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001725 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001726 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001727 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001728 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001729 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001730#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001731 case 'C':
1732 /* Right Arrow -- Move forward one character */
1733 input_forward();
1734 break;
1735 case 'D':
1736 /* Left Arrow -- Move back one character */
1737 input_backward(1);
1738 break;
1739 case '3':
1740 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001741 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001742 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001743 case '1': // vt100? linux vt? or what?
1744 case '7': // vt100? linux vt? or what?
1745 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001746 input_backward(cursor);
1747 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001748 case '4': // vt100? linux vt? or what?
1749 case '8': // vt100? linux vt? or what?
1750 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001751 input_end();
1752 break;
1753 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001754 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001755 beep();
1756 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001757 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001758
Eric Andersenc470f442003-07-28 09:56:35 +00001759 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001760#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001761 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001762 if (c == CTRL('V')) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001763 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001764 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001765 if (c == 0) {
1766 beep();
1767 break;
1768 }
1769 } else
1770#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001771
Denis Vlasenko38f63192007-01-22 09:03:07 +00001772#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001773 if (vi_cmdmode) /* Don't self-insert */
1774 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001775#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001776 if (!Isprint(c)) /* Skip non-printable characters */
1777 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001778
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001779 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001780 break;
1781
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001782 command_len++;
1783 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001784 command[cursor] = c;
1785 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001786 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001787 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001788 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001789
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001790 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001791 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001792 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001793 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001794 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001795 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001796 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001797 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001798 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001799 }
Eric Andersenc470f442003-07-28 09:56:35 +00001800 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001801 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001802
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001803#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001804 if (c != '\t')
1805 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001806#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001807 }
1808
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001809 if (command_len > 0)
1810 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001811
Eric Andersen27bb7902003-12-23 20:24:51 +00001812 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001813 command[command_len++] = '\n';
1814 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001815 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001816
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001817#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001818 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001819#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001820
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001821 /* restore initial_settings */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001822 tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001823 /* restore SIGWINCH handler */
1824 signal(SIGWINCH, previous_SIGWINCH_handler);
1825 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001826
1827 DEINIT_S();
1828
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001829 return command_len;
1830}
1831
1832line_input_t *new_line_input_t(int flags)
1833{
1834 line_input_t *n = xzalloc(sizeof(*n));
1835 n->flags = flags;
1836 return n;
1837}
1838
1839#else
1840
1841#undef read_line_input
1842int read_line_input(const char* prompt, char* command, int maxsize)
1843{
1844 fputs(prompt, stdout);
1845 fflush(stdout);
1846 fgets(command, maxsize, stdin);
1847 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001848}
1849
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001850#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001851
1852
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001853/*
1854 * Testing
1855 */
1856
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001857#ifdef TEST
1858
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001859#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001860
1861const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001862
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001863int main(int argc, char **argv)
1864{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001865 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001866 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001867#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001868 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1869 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1870 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001871#else
1872 "% ";
1873#endif
1874
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001875#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001876 setlocale(LC_ALL, "");
1877#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001878 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001879 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001880 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001881 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001882 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001883 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001884 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001885 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001886 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001887 return 0;
1888}
1889
Eric Andersenc470f442003-07-28 09:56:35 +00001890#endif /* TEST */