blob: 4d33834cea696fa4afe2980d870caf009fb403b8 [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
124static struct statics *const ptr_to_statics __attribute__ ((section (".data")));
125
126#define S (*ptr_to_statics)
127#define state (S.state )
128#define cmdedit_termw (S.cmdedit_termw )
129#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
130#define cmdedit_x (S.cmdedit_x )
131#define cmdedit_y (S.cmdedit_y )
132#define cmdedit_prmt_len (S.cmdedit_prmt_len)
133#define cursor (S.cursor )
134#define command_len (S.command_len )
135#define command_ps (S.command_ps )
136#define cmdedit_prompt (S.cmdedit_prompt )
137#define hostname_buf (S.hostname_buf )
138#define num_ok_lines (S.num_ok_lines )
139#define user_buf (S.user_buf )
140#define home_pwd_buf (S.home_pwd_buf )
141#define matches (S.matches )
142#define num_matches (S.num_matches )
143#define delptr (S.delptr )
144#define newdelflag (S.newdelflag )
145#define delbuf (S.delbuf )
146
147#define INIT_S() do { \
148 (*(struct statics**)&ptr_to_statics) = xzalloc(sizeof(S)); \
149 cmdedit_termw = 80; \
150 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
151 USE_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \
152} while (0)
153static void deinit_S(void)
154{
155#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
156 free(hostname_buf);
157 /* This one is allocated only if FANCY_PROMPT is on
158 * (otherwise it points to verbatim prompt (NOT malloced) */
159 free((char*)cmdedit_prompt);
160#endif
161#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
162 free(user_buf);
163 if (home_pwd_buf != null_str)
164 free(home_pwd_buf);
165#endif
166 free(ptr_to_statics);
167}
168#define DEINIT_S() deinit_S()
169
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000170/* Put 'command_ps[cursor]', cursor++.
171 * Advance cursor on screen. If we reached right margin, scroll text up
172 * and remove terminal margin effect by printing 'next_char' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000173static void cmdedit_set_out_char(int next_char)
174{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000175 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000176
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000177 if (c == '\0') {
178 /* erase character after end of input string */
179 c = ' ';
180 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000181#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000182 /* Display non-printable characters in reverse */
183 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000184 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000185 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000186 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000187 c += '@';
188 if (c == 127)
189 c = '?';
190 printf("\033[7m%c\033[0m", c);
191 } else
192#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000193 {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000194 bb_putchar(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000195 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000196 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000197 /* terminal is scrolled down */
198 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000199 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000200 /* destroy "(auto)margin" */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000201 bb_putchar(next_char);
202 bb_putchar('\b');
Mark Whitley4e338752001-01-26 20:42:23 +0000203 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000204// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000205 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000206}
207
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000208/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000209static void input_end(void)
210{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000211 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000212 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000213}
214
215/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000216static void goto_new_line(void)
217{
Mark Whitley4e338752001-01-26 20:42:23 +0000218 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000219 if (cmdedit_x)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000220 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000221}
222
223
Rob Landley88621d72006-08-29 19:41:06 +0000224static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000225{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000226 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000227 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000228}
Eric Andersen81fe1232003-07-29 06:38:40 +0000229
Rob Landley88621d72006-08-29 19:41:06 +0000230static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000231{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000232 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000233}
234
Eric Andersenaff114c2004-04-14 17:51:38 +0000235/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000236/* (optimized for slow terminals) */
237static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000238{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000239 int count_y;
240
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000241 if (num > cursor)
242 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000243 if (!num)
244 return;
245 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000246
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000247 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000248 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000249 if (num <= 4) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000250 printf("\b\b\b\b" + (4-num));
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000251 return;
252 }
253 printf("\033[%uD", num);
254 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000255 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000256
257 /* Need to go one or more lines up */
258 num -= cmdedit_x;
259 count_y = 1 + (num / cmdedit_termw);
260 cmdedit_y -= count_y;
261 cmdedit_x = cmdedit_termw * count_y - num;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000262 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000263 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000264}
265
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000266static void put_prompt(void)
267{
268 out1str(cmdedit_prompt);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000269 cmdedit_x = cmdedit_prmt_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000270 cursor = 0;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000271// Huh? what if cmdedit_prmt_len >= width?
Eric Andersen7467c8d2001-07-12 20:26:32 +0000272 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000273}
274
Eric Andersenaff114c2004-04-14 17:51:38 +0000275/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000276static void redraw(int y, int back_cursor)
277{
Eric Andersenc470f442003-07-28 09:56:35 +0000278 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000279 printf("\033[%dA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000280 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000281 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000282 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000283 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000284 input_backward(back_cursor);
285}
286
Paul Fox3f11b1b2005-08-04 19:04:46 +0000287/* Delete the char in front of the cursor, optionally saving it
288 * for later putback */
289static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000290{
Mark Whitley4e338752001-01-26 20:42:23 +0000291 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000292
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000293 if (j == command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000294 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000295
Denis Vlasenko38f63192007-01-22 09:03:07 +0000296#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000297 if (save) {
298 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000299 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000300 newdelflag = 0;
301 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000302 if ((delptr - delbuf) < DELBUFSIZ)
303 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000304 }
305#endif
306
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000307 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000308 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000309 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000310 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000311 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000312}
313
Denis Vlasenko38f63192007-01-22 09:03:07 +0000314#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000315static void put(void)
316{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000317 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000318 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000319
Paul Fox3f11b1b2005-08-04 19:04:46 +0000320 if (j == 0)
321 return;
322 ocursor = cursor;
323 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000324 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000325 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000326 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000327 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000328 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000329}
330#endif
331
Mark Whitley4e338752001-01-26 20:42:23 +0000332/* Delete the char in back of the cursor */
333static void input_backspace(void)
334{
335 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000336 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000337 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000338 }
339}
340
Eric Andersenaff114c2004-04-14 17:51:38 +0000341/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000342static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000343{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000344 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000345 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000346}
347
Denis Vlasenko38f63192007-01-22 09:03:07 +0000348#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000349
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000350static void free_tab_completion_data(void)
351{
352 if (matches) {
353 while (num_matches)
354 free(matches[--num_matches]);
355 free(matches);
356 matches = NULL;
357 }
358}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000359
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000360static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000361{
362 int nm = num_matches;
363 int nm1 = nm + 1;
364
365 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000366 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000367 num_matches++;
368}
369
Denis Vlasenko38f63192007-01-22 09:03:07 +0000370#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000371static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000372{
373 struct passwd *entry;
374 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000375
Eric Andersenc470f442003-07-28 09:56:35 +0000376 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000377 userlen = strlen(ud);
378
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000379 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000380 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000381 char *home = NULL;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000382 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000383
Eric Andersenc470f442003-07-28 09:56:35 +0000384 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000385 home = home_pwd_buf;
386 } else {
387 /* "~user/..." */
388 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000389 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000390 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000391 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000392 ud = temp;
393 if (entry)
394 home = entry->pw_dir;
395 }
396 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000397 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000398 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000399 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000400 }
401 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000402 } else {
403 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000404 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000405 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000406 struct passwd pwd;
407 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000408
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000409 setpwent();
410 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000411 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000412 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
413 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000414 }
415 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000416 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000417 }
418}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000419#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000420
421enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000422 FIND_EXE_ONLY = 0,
423 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000424 FIND_FILE_ONLY = 2,
425};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000426
Mark Whitley4e338752001-01-26 20:42:23 +0000427static int path_parse(char ***p, int flags)
428{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000429 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000430 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000431 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000432 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000433
Mark Whitley4e338752001-01-26 20:42:23 +0000434 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000435 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000436 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000437
438 if (state->flags & WITH_PATH_LOOKUP)
439 pth = state->path_lookup;
440 else
441 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000442 /* PATH=<empty> or PATH=:<empty> */
443 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
444 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000445
Denis Vlasenko253ce002007-01-22 08:34:44 +0000446 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000447 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000448 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000449 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000450 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000451 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000452 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000453 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000454 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000455 }
456
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000457 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000458 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000459 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000460 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000461 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000462 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000463 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000464 *tmp++ = '\0'; /* ':' -> '\0' */
465 if (*tmp == '\0')
466 break; /* :<empty> */
467 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000468 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000469 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000470 return npth;
471}
472
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000473static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000474{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000475 DIR *dir;
476 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000477 struct stat st;
478 char *path1[1];
479 char **paths = path1;
480 int npaths;
481 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000482 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000483 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000484/* char dirbuf[MAX_LINELEN]; */
485#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000486
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000487 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000488 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000489
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000490 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000491 /* no dir, if flags==EXE_ONLY - get paths, else "." */
492 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000493 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000494 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000495 /* dirbuf = ".../.../.../" */
496 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000497#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000498 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000499 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000500#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000501 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000502 /* point to 'l' in "..../last_component" */
503 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000504 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000505
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000506 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000507 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000508 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000509 continue;
510
511 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000512 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000513 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000514
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000515 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000516 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000517 continue;
518 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000519 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000520 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000521 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000522 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000523 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000524 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000525 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000526 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000527 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000528 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000529 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000530 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000531
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000532 len1 = strlen(found);
533 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000534 found[len1] = '\0';
535 found[len1+1] = '\0';
536
Mark Whitley4e338752001-01-26 20:42:23 +0000537 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000538 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000539 if (found[len1-1] != '/') {
540 found[len1] = '/';
541 }
Mark Whitley4e338752001-01-26 20:42:23 +0000542 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000543 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000544 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000545 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000546 }
Mark Whitley4e338752001-01-26 20:42:23 +0000547 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000548 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000549 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000550 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000551 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000552 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000553 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000554 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000555 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000556 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000557 free(paths);
558 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000559#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000560}
Erik Andersenf0657d32000-04-12 17:49:52 +0000561
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000562#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000563
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000564#define collapse_pos(is, in) do { \
565 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
566 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
567} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000568
569static int find_match(char *matchBuf, int *len_with_quotes)
570{
571 int i, j;
572 int command_mode;
573 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000574/* int16_t int_buf[MAX_LINELEN + 1]; */
575/* int16_t pos_buf[MAX_LINELEN + 1]; */
576#define int_buf (S.find_match__int_buf)
577#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000578
579 /* set to integer dimension characters and own positions */
580 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000581 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000582 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000583 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000584 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000585 }
586 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000587 }
588
589 /* mask \+symbol and convert '\t' to ' ' */
590 for (i = j = 0; matchBuf[i]; i++, j++)
591 if (matchBuf[i] == '\\') {
592 collapse_pos(j, j + 1);
593 int_buf[j] |= QUOT;
594 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000595#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000596 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000597 int_buf[j] = ' ' | QUOT;
598#endif
599 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000600#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000601 else if (matchBuf[i] == '\t')
602 int_buf[j] = ' ';
603#endif
604
605 /* mask "symbols" or 'symbols' */
606 c2 = 0;
607 for (i = 0; int_buf[i]; i++) {
608 c = int_buf[i];
609 if (c == '\'' || c == '"') {
610 if (c2 == 0)
611 c2 = c;
612 else {
613 if (c == c2)
614 c2 = 0;
615 else
616 int_buf[i] |= QUOT;
617 }
618 } else if (c2 != 0 && c != '$')
619 int_buf[i] |= QUOT;
620 }
621
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000622 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000623 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
624 for (i = 0; int_buf[i]; i++) {
625 c = int_buf[i];
626 c2 = int_buf[i + 1];
627 j = i ? int_buf[i - 1] : -1;
628 command_mode = 0;
629 if (c == ';' || c == '&' || c == '|') {
630 command_mode = 1 + (c == c2);
631 if (c == '&') {
632 if (j == '>' || j == '<')
633 command_mode = 0;
634 } else if (c == '|' && j == '>')
635 command_mode = 0;
636 }
637 if (command_mode) {
638 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000639 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000640 }
641 }
642 /* collapse `command...` */
643 for (i = 0; int_buf[i]; i++)
644 if (int_buf[i] == '`') {
645 for (j = i + 1; int_buf[j]; j++)
646 if (int_buf[j] == '`') {
647 collapse_pos(i, j + 1);
648 j = 0;
649 break;
650 }
651 if (j) {
652 /* not found close ` - command mode, collapse all previous */
653 collapse_pos(0, i + 1);
654 break;
655 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000656 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000657 }
658
659 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000660 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000661 c2 = 0;
662 for (i = 0; int_buf[i]; i++)
663 if (int_buf[i] == '(' || int_buf[i] == '{') {
664 if (int_buf[i] == '(')
665 c++;
666 else
667 c2++;
668 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000669 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000670 }
671 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
672 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
673 if (int_buf[i] == ')')
674 c--;
675 else
676 c2--;
677 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000678 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000679 }
680
681 /* skip first not quote space */
682 for (i = 0; int_buf[i]; i++)
683 if (int_buf[i] != ' ')
684 break;
685 if (i)
686 collapse_pos(0, i);
687
688 /* set find mode for completion */
689 command_mode = FIND_EXE_ONLY;
690 for (i = 0; int_buf[i]; i++)
691 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
692 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000693 && matchBuf[pos_buf[0]] == 'c'
694 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000695 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000696 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000697 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000698 command_mode = FIND_FILE_ONLY;
699 break;
700 }
701 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000702 for (i = 0; int_buf[i]; i++)
703 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000704 /* find last word */
705 for (--i; i >= 0; i--) {
706 c = int_buf[i];
707 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
708 collapse_pos(0, i + 1);
709 break;
710 }
711 }
712 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000713 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
714 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000715 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000716 while ((int_buf[i] & ~QUOT) == '/'
717 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
718 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000719 i++;
720 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000721
722 /* set only match and destroy quotes */
723 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000724 for (c = 0; pos_buf[i] >= 0; i++) {
725 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000726 j = pos_buf[i] + 1;
727 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000728 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000729 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000730 *len_with_quotes = j ? j - pos_buf[0] : 0;
731
732 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000733#undef int_buf
734#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000735}
736
Glenn L McGrath4d001292003-01-06 01:11:50 +0000737/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000738 * display by column (original idea from ls applet,
739 * very optimized by me :)
740 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000741static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000742{
743 int ncols, row;
744 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000745 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000746 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000747 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000748
749 /* find the longest file name- use that as the column width */
750 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000751 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000752 if (column_width < l)
753 column_width = l;
754 }
755 column_width += 2; /* min space for columns */
756 ncols = cmdedit_termw / column_width;
757
758 if (ncols > 1) {
759 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000760 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000761 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000762 } else {
763 ncols = 1;
764 }
765 for (row = 0; row < nrows; row++) {
766 int n = row;
767 int nc;
768
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000769 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000770 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000771 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000772 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000773 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000774 }
775}
776
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000777static char *add_quote_for_spec_chars(char *found)
778{
779 int l = 0;
780 char *s = xmalloc((strlen(found) + 1) * 2);
781
782 while (*found) {
783 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
784 s[l++] = '\\';
785 s[l++] = *found++;
786 }
787 s[l] = 0;
788 return s;
789}
790
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000791static int match_compare(const void *a, const void *b)
792{
793 return strcmp(*(char**)a, *(char**)b);
794}
795
796/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000797static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000798{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000799 if (!(state->flags & TAB_COMPLETION))
800 return;
801
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000802 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000803 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000804 int len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000805/* char matchBuf[MAX_LINELEN]; */
806#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000807 int find_type;
808 int recalc_pos;
809
Eric Andersenc470f442003-07-28 09:56:35 +0000810 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000811
812 /* Make a local copy of the string -- up
813 * to the position of the cursor */
814 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000815 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000816
817 find_type = find_match(matchBuf, &recalc_pos);
818
819 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000820 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000821
Denis Vlasenko38f63192007-01-22 09:03:07 +0000822#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000823 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000824 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000825 if (state->flags & USERNAME_COMPLETION)
826 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
827 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000828#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000829 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000830 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000831 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000832 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000833 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000834 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000835 int i, n = 0;
836 qsort(matches, num_matches, sizeof(char*), match_compare);
837 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000838 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000839 if (strcmp(matches[i], matches[i+1]) == 0) {
840 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000841 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000842 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000843 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000844 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000845 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000846 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000847 matches[n] = matches[i];
848 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000849 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000850 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000851 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000852 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000853 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000854 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000855 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000856 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000857 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000858 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000859 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000860 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000861 break;
862 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000863 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000864 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000865 return;
866 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000867 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000868 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000869 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000870 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000871 /* for next completion current found */
872 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000873
874 len_found = strlen(tmp);
875 if (tmp[len_found-1] != '/') {
876 tmp[len_found] = ' ';
877 tmp[len_found+1] = '\0';
878 }
Mark Whitley4e338752001-01-26 20:42:23 +0000879 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000880 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000881 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000882 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000883 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000884 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000885 /* save tail line */
886 strcpy(matchBuf, command_ps + cursor);
887 /* add match */
888 strcat(command_ps, tmp);
889 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000890 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000891 /* back to begin word for match */
892 input_backward(recalc_pos);
893 /* new pos */
894 recalc_pos = cursor + len_found;
895 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000896 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000897 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000898 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000899 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000900 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000901#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000902 } else {
903 /* Ok -- the last char was a TAB. Since they
904 * just hit TAB again, print a list of all the
905 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000906 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000907 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000908
909 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000910 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000911 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000912 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000913 }
914 }
915}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000916
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000917#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000918
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000919
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000920#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000921
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000922/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000923static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000924{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000925 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
926 free(state->history[state->cur_history]);
927 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000928 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000929 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000930}
931
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000932static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000933{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000934 if (state->flags & DO_HISTORY) {
935 int ch = state->cur_history;
936 if (ch < state->cnt_history) {
937 get_previous_history(); /* save the current history line */
938 state->cur_history = ch + 1;
939 return state->cur_history;
940 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000941 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000942 beep();
943 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000944}
Robert Griebl350d26b2002-12-03 22:45:46 +0000945
Denis Vlasenko38f63192007-01-22 09:03:07 +0000946#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000947/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000948static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000949{
Robert Griebl350d26b2002-12-03 22:45:46 +0000950 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000951 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000952
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000953 /* cleanup old */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000954 for (hi = state->cnt_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000955 hi--;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000956 free(state->history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000957 }
958
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000959 fp = fopen(fromfile, "r");
960 if (fp) {
961 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000962 char *hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000963 int l;
964
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000965 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000966 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000967 l = strlen(hl);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000968 if (l >= MAX_LINELEN)
969 hl[MAX_LINELEN-1] = '\0';
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000970 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000971 free(hl);
972 continue;
973 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000974 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000975 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000976 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000977 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000978 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000979}
980
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000981/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000982static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000983{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000984 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +0000985
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000986 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000987 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000988 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000989
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000990 for (i = 0; i < state->cnt_history; i++) {
991 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000992 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000993 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000994 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000995}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000996#else
997#define load_history(a) ((void)0)
998#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000999#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001000
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001001static void remember_in_history(const char *str)
1002{
1003 int i;
1004
1005 if (!(state->flags & DO_HISTORY))
1006 return;
1007
1008 i = state->cnt_history;
1009 free(state->history[MAX_HISTORY]);
1010 state->history[MAX_HISTORY] = NULL;
1011 /* After max history, remove the oldest command */
1012 if (i >= MAX_HISTORY) {
1013 free(state->history[0]);
1014 for (i = 0; i < MAX_HISTORY-1; i++)
1015 state->history[i] = state->history[i+1];
1016 }
1017// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
1018// (i.e. do not save dups?)
1019 state->history[i++] = xstrdup(str);
1020 state->cur_history = i;
1021 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001022#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001023 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001024 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +00001025#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00001026 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001027}
1028
1029#else /* MAX_HISTORY == 0 */
1030#define remember_in_history(a) ((void)0)
1031#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001032
1033
Erik Andersen6273f652000-03-17 01:12:41 +00001034/*
1035 * This function is used to grab a character buffer
1036 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001037 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001038 * a mini readline).
1039 *
1040 * The following standard commands are not implemented:
1041 * ESC-b -- Move back one word
1042 * ESC-f -- Move forward one word
1043 * ESC-d -- Delete back one word
1044 * ESC-h -- Delete forward one word
1045 * CTL-t -- Transpose two characters
1046 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001047 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001048 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001049 */
Eric Andersenc470f442003-07-28 09:56:35 +00001050
Denis Vlasenko38f63192007-01-22 09:03:07 +00001051#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001052static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001053vi_Word_motion(char *command, int eat)
1054{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001055 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001056 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001057 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001058 input_forward();
1059}
1060
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001061static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001062vi_word_motion(char *command, int eat)
1063{
1064 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001065 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001066 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001067 input_forward();
1068 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001069 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001070 input_forward();
1071 }
1072
Denis Vlasenko253ce002007-01-22 08:34:44 +00001073 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001074 input_forward();
1075
Denis Vlasenko253ce002007-01-22 08:34:44 +00001076 if (eat && cursor < command_len && isspace(command[cursor]))
1077 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001078 input_forward();
1079}
1080
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001081static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001082vi_End_motion(char *command)
1083{
1084 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001085 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001086 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001087 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001088 input_forward();
1089}
1090
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001091static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001092vi_end_motion(char *command)
1093{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001094 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001095 return;
1096 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001097 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001098 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001099 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001100 return;
1101 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001102 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001103 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1104 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001105 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001106 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001107 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001108 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001109 input_forward();
1110 }
1111}
1112
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001113static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001114vi_Back_motion(char *command)
1115{
1116 while (cursor > 0 && isspace(command[cursor-1]))
1117 input_backward(1);
1118 while (cursor > 0 && !isspace(command[cursor-1]))
1119 input_backward(1);
1120}
1121
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001122static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001123vi_back_motion(char *command)
1124{
1125 if (cursor <= 0)
1126 return;
1127 input_backward(1);
1128 while (cursor > 0 && isspace(command[cursor]))
1129 input_backward(1);
1130 if (cursor <= 0)
1131 return;
1132 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001133 while (cursor > 0
1134 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1135 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001136 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001137 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001138 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001139 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001140 input_backward(1);
1141 }
1142}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001143#endif
1144
1145
1146/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001147 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001148 */
1149
Denis Vlasenko38f63192007-01-22 09:03:07 +00001150#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001151static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001152{
1153 cmdedit_prompt = prmt_ptr;
1154 cmdedit_prmt_len = strlen(prmt_ptr);
1155 put_prompt();
1156}
1157#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001158static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001159{
1160 int prmt_len = 0;
1161 size_t cur_prmt_len = 0;
1162 char flg_not_length = '[';
1163 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko6ca04442007-02-11 16:19:28 +00001164 char *pwd_buf = xrealloc_getcwd_or_warn(NULL);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001165 char buf2[PATH_MAX + 1];
1166 char buf[2];
1167 char c;
1168 char *pbuf;
1169
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001170 cmdedit_prmt_len = 0;
1171
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001172 if (!pwd_buf) {
1173 pwd_buf = (char *)bb_msg_unknown;
1174 }
1175
1176 while (*prmt_ptr) {
1177 pbuf = buf;
1178 pbuf[1] = 0;
1179 c = *prmt_ptr++;
1180 if (c == '\\') {
1181 const char *cp = prmt_ptr;
1182 int l;
1183
1184 c = bb_process_escape_sequence(&prmt_ptr);
1185 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001186 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001187 break;
1188 c = *prmt_ptr++;
1189 switch (c) {
1190#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1191 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001192 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001193 break;
1194#endif
1195 case 'h':
1196 pbuf = hostname_buf;
1197 if (!pbuf) {
1198 pbuf = xzalloc(256);
1199 if (gethostname(pbuf, 255) < 0) {
1200 strcpy(pbuf, "?");
1201 } else {
1202 char *s = strchr(pbuf, '.');
1203 if (s)
1204 *s = '\0';
1205 }
1206 hostname_buf = pbuf;
1207 }
1208 break;
1209 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001210 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001211 break;
1212#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1213 case 'w':
1214 pbuf = pwd_buf;
1215 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001216 if (l != 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001217 && strncmp(home_pwd_buf, pbuf, l) == 0
1218 && (pbuf[l]=='/' || pbuf[l]=='\0')
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001219 && strlen(pwd_buf+l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001220 ) {
1221 pbuf = buf2;
1222 *pbuf = '~';
1223 strcpy(pbuf+1, pwd_buf+l);
1224 }
1225 break;
1226#endif
1227 case 'W':
1228 pbuf = pwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001229 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001230 if (cp != NULL && cp != pbuf)
1231 pbuf += (cp-pbuf) + 1;
1232 break;
1233 case '!':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001234 pbuf = buf2;
1235 snprintf(buf2, sizeof(buf2), "%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001236 break;
1237 case 'e': case 'E': /* \e \E = \033 */
1238 c = '\033';
1239 break;
1240 case 'x': case 'X':
1241 for (l = 0; l < 3;) {
1242 int h;
1243 buf2[l++] = *prmt_ptr;
1244 buf2[l] = 0;
1245 h = strtol(buf2, &pbuf, 16);
1246 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
1247 l--;
1248 break;
1249 }
1250 prmt_ptr++;
1251 }
1252 buf2[l] = 0;
1253 c = (char)strtol(buf2, NULL, 16);
1254 if (c == 0)
1255 c = '?';
1256 pbuf = buf;
1257 break;
1258 case '[': case ']':
1259 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001260 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001261 continue;
1262 }
1263 break;
1264 }
1265 }
1266 }
1267 if (pbuf == buf)
1268 *pbuf = c;
1269 cur_prmt_len = strlen(pbuf);
1270 prmt_len += cur_prmt_len;
1271 if (flg_not_length != ']')
1272 cmdedit_prmt_len += cur_prmt_len;
1273 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
1274 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001275 if (pwd_buf != (char *)bb_msg_unknown)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001276 free(pwd_buf);
1277 cmdedit_prompt = prmt_mem_ptr;
1278 put_prompt();
1279}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001280#endif
1281
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001282static void cmdedit_setwidth(unsigned w, int redraw_flg)
1283{
1284 cmdedit_termw = w;
1285 if (redraw_flg) {
1286 /* new y for current cursor */
1287 int new_y = (cursor + cmdedit_prmt_len) / w;
1288 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001289 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001290 fflush(stdout);
1291 }
1292}
1293
1294static void win_changed(int nsig)
1295{
1296 int width;
1297 get_terminal_width_height(0, &width, NULL);
1298 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1299 if (nsig == SIGWINCH)
1300 signal(SIGWINCH, win_changed); /* rearm ourself */
1301}
1302
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001303/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001304 * The emacs and vi modes share much of the code in the big
1305 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001306 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001307 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001308 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001309 */
1310
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001311#define vbit 0x100
1312
1313/* leave out the "vi-mode"-only case labels if vi editing isn't
1314 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001315#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001316
1317/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001318#undef CTRL
1319#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001320
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001321/* Returns:
1322 * -1 on read errors or EOF, or on bare Ctrl-D.
1323 * 0 on ctrl-C,
1324 * >0 length of input string, including terminating '\n'
1325 */
Denis Vlasenko037576d2007-10-20 18:30:38 +00001326int read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001327{
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001328#if ENABLE_FEATURE_TAB_COMPLETION
1329 smallint lastWasTab = FALSE;
1330#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001331 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001332 unsigned char c;
1333 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001334#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001335 smallint vi_cmdmode = 0;
1336 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001337#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001338 struct termios initial_settings;
1339 struct termios new_settings;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001340
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001341 INIT_S();
1342
1343 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1344 || !(initial_settings.c_lflag & ECHO)
1345 ) {
1346 /* Happens when e.g. stty -echo was run before */
1347 int len;
1348 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001349 fflush(stdout);
1350 fgets(command, maxsize, stdin);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001351 len = strlen(command);
1352 DEINIT_S();
1353 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001354 }
1355
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001356// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001357 if (maxsize > MAX_LINELEN)
1358 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001359
1360 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001361 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001362#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001363 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001364 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001365#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001366
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001367 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001368 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001369 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001370 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001371 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001372
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001373 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001374 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1375 /* Turn off echoing and CTRL-C, so we can trap it */
1376 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001377 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001378 new_settings.c_cc[VMIN] = 1;
1379 new_settings.c_cc[VTIME] = 0;
1380 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001381#ifndef _POSIX_VDISABLE
1382#define _POSIX_VDISABLE '\0'
1383#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001384 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001385 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001386
Eric Andersen6faae7d2001-02-16 20:09:17 +00001387 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001388 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1389 win_changed(0); /* do initial resizing */
1390#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1391 {
1392 struct passwd *entry;
1393
1394 entry = getpwuid(geteuid());
1395 if (entry) {
1396 user_buf = xstrdup(entry->pw_name);
1397 home_pwd_buf = xstrdup(entry->pw_dir);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001398 /* They are not freed on exit (too small to bother) */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001399 }
1400 }
1401#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001402 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001403 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001404
Erik Andersenc7c634b2000-03-19 05:28:55 +00001405 while (1) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001406 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +00001407
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001408 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001409 /* if we can't read input then exit */
1410 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001411 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001412
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001413 ic = c;
1414
Denis Vlasenko38f63192007-01-22 09:03:07 +00001415#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001416 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001417 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001418 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001419#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001420 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001421 case '\n':
1422 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001423 vi_case('\n'|vbit:)
1424 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001425 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001426 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001427 break_out = 1;
1428 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001429#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001430 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001431 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001432 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001433 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001434 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001435 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001436 vi_case('h'|vbit:)
1437 vi_case('\b'|vbit:)
1438 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001439 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001440 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001441 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001442#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001443 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001444 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001445 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001446 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001447 command_len = 0;
1448 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001449 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001450 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001451 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001452 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001453 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001454 errno = 0;
1455 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001456 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001457 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001458 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001459 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001460 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001461 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001462
1463#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001464 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001465 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001466 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001467 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001468 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001469 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001470 vi_case('l'|vbit:)
1471 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001472 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001473 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001474 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001475#endif
1476
Erik Andersenf0657d32000-04-12 17:49:52 +00001477 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001478 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001479 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001480 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001481 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001482
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001483#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001484 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001485 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001486 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001487#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001488
1489#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001490 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001491 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001492 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001493 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001494 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001495 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001496 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001497 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001498 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001499 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001500 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001501 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001502#endif
1503
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001504#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001505 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001506 vi_case(CTRL('N')|vbit:)
1507 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001508 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001509 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001510 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001511 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001512 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001513 vi_case(CTRL('P')|vbit:)
1514 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001515 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001516 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001517 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001518 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001519 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001520 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001521 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001522#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001523
1524#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001525 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001526 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001527 /* Control-U -- Clear line before cursor */
1528 if (cursor) {
1529 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001530 command_len -= cursor;
1531 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001532 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001533 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001534#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001535 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001536 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001537 /* Control-W -- Remove the last word */
1538 while (cursor > 0 && isspace(command[cursor-1]))
1539 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001540 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001541 input_backspace();
1542 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001543
Denis Vlasenko38f63192007-01-22 09:03:07 +00001544#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001545 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001546 vi_cmdmode = 0;
1547 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001548 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001549 input_backward(cursor);
1550 vi_cmdmode = 0;
1551 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001552 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001553 input_forward();
1554 vi_cmdmode = 0;
1555 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001556 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001557 input_end();
1558 vi_cmdmode = 0;
1559 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001560 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001561 input_delete(1);
1562 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001563 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001564 if (cursor > 0) {
1565 input_backward(1);
1566 input_delete(1);
1567 }
1568 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001569 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001570 vi_Word_motion(command, 1);
1571 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001572 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001573 vi_word_motion(command, 1);
1574 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001575 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001576 vi_End_motion(command);
1577 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001578 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001579 vi_end_motion(command);
1580 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001581 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001582 vi_Back_motion(command);
1583 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001584 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001585 vi_back_motion(command);
1586 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001587 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001588 vi_cmdmode = 0;
1589 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001590 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001591 goto clear_to_eol;
1592
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001593 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001594 vi_cmdmode = 0;
1595 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001596 case 'd'|vbit: {
1597 int nc, sc;
1598 sc = cursor;
1599 prevc = ic;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001600 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001601 goto prepare_to_die;
1602 if (c == (prevc & 0xff)) {
1603 /* "cc", "dd" */
1604 input_backward(cursor);
1605 goto clear_to_eol;
1606 break;
1607 }
1608 switch (c) {
1609 case 'w':
1610 case 'W':
1611 case 'e':
1612 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001613 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001614 case 'w': /* "dw", "cw" */
1615 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001616 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001617 case 'W': /* 'dW', 'cW' */
1618 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001619 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001620 case 'e': /* 'de', 'ce' */
1621 vi_end_motion(command);
1622 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001623 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001624 case 'E': /* 'dE', 'cE' */
1625 vi_End_motion(command);
1626 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001627 break;
1628 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001629 nc = cursor;
1630 input_backward(cursor - sc);
1631 while (nc-- > cursor)
1632 input_delete(1);
1633 break;
1634 case 'b': /* "db", "cb" */
1635 case 'B': /* implemented as B */
1636 if (c == 'b')
1637 vi_back_motion(command);
1638 else
1639 vi_Back_motion(command);
1640 while (sc-- > cursor)
1641 input_delete(1);
1642 break;
1643 case ' ': /* "d ", "c " */
1644 input_delete(1);
1645 break;
1646 case '$': /* "d$", "c$" */
1647 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001648 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001649 input_delete(1);
1650 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001651 }
1652 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001653 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001654 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001655 input_forward();
1656 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001657 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001658 put();
1659 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001660 case 'r'|vbit:
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001661 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001662 goto prepare_to_die;
1663 if (c == 0)
1664 beep();
1665 else {
1666 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001667 bb_putchar(c);
1668 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001669 }
1670 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001671#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001672
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001673 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001674
Denis Vlasenko38f63192007-01-22 09:03:07 +00001675#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001676 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001677 /* ESC: insert mode --> command mode */
1678 vi_cmdmode = 1;
1679 input_backward(1);
1680 break;
1681 }
1682#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001683 /* escape sequence follows */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001684 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001685 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001686 /* different vt100 emulations */
1687 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001688 vi_case('['|vbit:)
1689 vi_case('O'|vbit:)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001690 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001691 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001692 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001693 if (c >= '1' && c <= '9') {
1694 unsigned char dummy;
1695
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001696 if (safe_read(STDIN_FILENO, &dummy, 1) < 1)
Glenn L McGrath475820c2004-01-22 12:42:23 +00001697 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001698 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001699 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001700 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001701
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001702 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001703#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001704 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001705 input_tab(&lastWasTab);
1706 break;
1707#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001708#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001709 case 'A':
1710 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001711 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001712 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001713 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001714 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001715 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001716 break;
1717 case 'B':
1718 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001719 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001720 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001721 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001722 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001723 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001724 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001725 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001726 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001727 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001728#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001729 case 'C':
1730 /* Right Arrow -- Move forward one character */
1731 input_forward();
1732 break;
1733 case 'D':
1734 /* Left Arrow -- Move back one character */
1735 input_backward(1);
1736 break;
1737 case '3':
1738 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001739 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001740 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001741 case '1': // vt100? linux vt? or what?
1742 case '7': // vt100? linux vt? or what?
1743 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001744 input_backward(cursor);
1745 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001746 case '4': // vt100? linux vt? or what?
1747 case '8': // vt100? linux vt? or what?
1748 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001749 input_end();
1750 break;
1751 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001752 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001753 beep();
1754 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001755 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001756
Eric Andersenc470f442003-07-28 09:56:35 +00001757 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001758#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001759 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001760 if (c == CTRL('V')) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001761 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001762 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001763 if (c == 0) {
1764 beep();
1765 break;
1766 }
1767 } else
1768#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001769
Denis Vlasenko38f63192007-01-22 09:03:07 +00001770#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001771 if (vi_cmdmode) /* Don't self-insert */
1772 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001773#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001774 if (!Isprint(c)) /* Skip non-printable characters */
1775 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001776
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001777 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001778 break;
1779
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001780 command_len++;
1781 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001782 command[cursor] = c;
1783 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001784 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001785 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001786 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001787
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001788 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001789 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001790 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001791 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001792 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001793 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001794 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001795 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001796 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001797 }
Eric Andersenc470f442003-07-28 09:56:35 +00001798 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001799 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001800
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001801#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001802 if (c != '\t')
1803 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001804#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001805 }
1806
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001807 if (command_len > 0)
1808 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001809
Eric Andersen27bb7902003-12-23 20:24:51 +00001810 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001811 command[command_len++] = '\n';
1812 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001813 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001814
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001815#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001816 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001817#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001818
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001819 /* restore initial_settings */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001820 tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001821 /* restore SIGWINCH handler */
1822 signal(SIGWINCH, previous_SIGWINCH_handler);
1823 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001824
1825 DEINIT_S();
1826
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001827 return command_len;
1828}
1829
1830line_input_t *new_line_input_t(int flags)
1831{
1832 line_input_t *n = xzalloc(sizeof(*n));
1833 n->flags = flags;
1834 return n;
1835}
1836
1837#else
1838
1839#undef read_line_input
1840int read_line_input(const char* prompt, char* command, int maxsize)
1841{
1842 fputs(prompt, stdout);
1843 fflush(stdout);
1844 fgets(command, maxsize, stdin);
1845 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001846}
1847
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001848#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001849
1850
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001851/*
1852 * Testing
1853 */
1854
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001855#ifdef TEST
1856
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001857#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001858
1859const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001860
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001861int main(int argc, char **argv)
1862{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001863 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001864 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001865#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001866 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1867 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1868 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001869#else
1870 "% ";
1871#endif
1872
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001873#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001874 setlocale(LC_ALL, "");
1875#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001876 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001877 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001878 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001879 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001880 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001881 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001882 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001883 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001884 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001885 return 0;
1886}
1887
Eric Andersenc470f442003-07-28 09:56:35 +00001888#endif /* TEST */