blob: b25386bc06c23aad8b7a10dfd0145ce9ddd6f649 [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
Denis Vlasenko6b404432008-01-07 16:13:14 +000068 ? CONFIG_FEATURE_EDITING_MAX_LEN
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000069 : 0x7ff0
70};
Robert Griebl350d26b2002-12-03 22:45:46 +000071
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000072#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
73static const char null_str[] ALIGN1 = "";
74#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +000075
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000076/* We try to minimize both static and stack usage. */
77struct statics {
78 line_input_t *state;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000079
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000080 volatile unsigned cmdedit_termw; /* = 80; */ /* actual terminal width */
81 sighandler_t previous_SIGWINCH_handler;
Mark Whitley4e338752001-01-26 20:42:23 +000082
Eric Andersen86349772000-12-18 20:25:50 +000083
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000084 int cmdedit_x; /* real x terminal position */
85 int cmdedit_y; /* pseudoreal y terminal position */
86 int cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000087
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000088 unsigned cursor;
89 unsigned command_len;
90 char *command_ps;
91
92 const char *cmdedit_prompt;
Denis Vlasenko38f63192007-01-22 09:03:07 +000093#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000094 int num_ok_lines; /* = 1; */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000095#endif
96
Denis Vlasenko82b39e82007-01-21 19:18:19 +000097#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000098 char *user_buf;
99 char *home_pwd_buf; /* = (char*)null_str; */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000100#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000101
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000102#if ENABLE_FEATURE_TAB_COMPLETION
103 char **matches;
104 unsigned num_matches;
105#endif
106
107#if ENABLE_FEATURE_EDITING_VI
108#define DELBUFSIZ 128
109 char *delptr;
110 smallint newdelflag; /* whether delbuf should be reused yet */
111 char delbuf[DELBUFSIZ]; /* a place to store deleted characters */
112#endif
113
114 /* Formerly these were big buffers on stack: */
115#if ENABLE_FEATURE_TAB_COMPLETION
116 char exe_n_cwd_tab_completion__dirbuf[MAX_LINELEN];
117 char input_tab__matchBuf[MAX_LINELEN];
118 int16_t find_match__int_buf[MAX_LINELEN + 1]; /* need to have 9 bits at least */
119 int16_t find_match__pos_buf[MAX_LINELEN + 1];
120#endif
121};
122
Denis Vlasenko6672c8e2007-11-30 07:29:05 +0000123/* Make it reside in writable memory, yet make compiler understand
124 * that it is not going to change. */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000125static struct statics *const ptr_to_statics __attribute__ ((section (".data")));
126
127#define S (*ptr_to_statics)
128#define state (S.state )
129#define cmdedit_termw (S.cmdedit_termw )
130#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
131#define cmdedit_x (S.cmdedit_x )
132#define cmdedit_y (S.cmdedit_y )
133#define cmdedit_prmt_len (S.cmdedit_prmt_len)
134#define cursor (S.cursor )
135#define command_len (S.command_len )
136#define command_ps (S.command_ps )
137#define cmdedit_prompt (S.cmdedit_prompt )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000138#define num_ok_lines (S.num_ok_lines )
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000139#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000140#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)); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000149 barrier(); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000150 cmdedit_termw = 80; \
151 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
152 USE_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \
153} while (0)
154static void deinit_S(void)
155{
156#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000157 /* 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 Vlasenko6f043912008-02-18 22:28:03 +0000250 /* This is longer by 5 bytes on x86.
251 * Also gets mysteriously
252 * miscompiled for some ARM users.
253 * printf(("\b\b\b\b" + 4) - num);
254 * return;
255 */
256 do {
257 bb_putchar('\b');
258 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000259 return;
260 }
261 printf("\033[%uD", num);
262 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000263 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000264
265 /* Need to go one or more lines up */
266 num -= cmdedit_x;
267 count_y = 1 + (num / cmdedit_termw);
268 cmdedit_y -= count_y;
269 cmdedit_x = cmdedit_termw * count_y - num;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000270 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000271 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000272}
273
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000274static void put_prompt(void)
275{
276 out1str(cmdedit_prompt);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000277 cmdedit_x = cmdedit_prmt_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000278 cursor = 0;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000279// Huh? what if cmdedit_prmt_len >= width?
Eric Andersen7467c8d2001-07-12 20:26:32 +0000280 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000281}
282
Eric Andersenaff114c2004-04-14 17:51:38 +0000283/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000284static void redraw(int y, int back_cursor)
285{
Eric Andersenc470f442003-07-28 09:56:35 +0000286 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000287 printf("\033[%dA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000288 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000289 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000290 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000291 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000292 input_backward(back_cursor);
293}
294
Paul Fox3f11b1b2005-08-04 19:04:46 +0000295/* Delete the char in front of the cursor, optionally saving it
296 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000297#if !ENABLE_FEATURE_EDITING_VI
298static void input_delete(void)
299#define input_delete(save) input_delete()
300#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000301static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000302#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000303{
Mark Whitley4e338752001-01-26 20:42:23 +0000304 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000305
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000306 if (j == command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000307 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000308
Denis Vlasenko38f63192007-01-22 09:03:07 +0000309#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000310 if (save) {
311 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000312 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000313 newdelflag = 0;
314 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000315 if ((delptr - delbuf) < DELBUFSIZ)
316 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000317 }
318#endif
319
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000320 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000321 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000322 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000323 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000324 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000325}
326
Denis Vlasenko38f63192007-01-22 09:03:07 +0000327#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000328static void put(void)
329{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000330 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000331 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000332
Paul Fox3f11b1b2005-08-04 19:04:46 +0000333 if (j == 0)
334 return;
335 ocursor = cursor;
336 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000337 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000338 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000339 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000340 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000341 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000342}
343#endif
344
Mark Whitley4e338752001-01-26 20:42:23 +0000345/* Delete the char in back of the cursor */
346static void input_backspace(void)
347{
348 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000349 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000350 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000351 }
352}
353
Eric Andersenaff114c2004-04-14 17:51:38 +0000354/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000355static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000356{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000357 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000358 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000359}
360
Denis Vlasenko38f63192007-01-22 09:03:07 +0000361#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000362
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000363static void free_tab_completion_data(void)
364{
365 if (matches) {
366 while (num_matches)
367 free(matches[--num_matches]);
368 free(matches);
369 matches = NULL;
370 }
371}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000372
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000373static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000374{
375 int nm = num_matches;
376 int nm1 = nm + 1;
377
378 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000379 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000380 num_matches++;
381}
382
Denis Vlasenko38f63192007-01-22 09:03:07 +0000383#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000384static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000385{
386 struct passwd *entry;
387 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000388
Eric Andersenc470f442003-07-28 09:56:35 +0000389 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000390 userlen = strlen(ud);
391
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000392 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000393 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000394 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000395
Eric Andersenc470f442003-07-28 09:56:35 +0000396 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000397 home = home_pwd_buf;
398 } else {
399 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000400 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000401 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000402 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000403 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000404 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000405 ud = temp;
406 if (entry)
407 home = entry->pw_dir;
408 }
409 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000410 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000411 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000412 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000413 }
414 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000415 } else {
416 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000417 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000418 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000419 struct passwd pwd;
420 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000421
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000422 setpwent();
423 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000424 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000425 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
426 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000427 }
428 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000429 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430 }
431}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000432#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000433
434enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000435 FIND_EXE_ONLY = 0,
436 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000437 FIND_FILE_ONLY = 2,
438};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000439
Mark Whitley4e338752001-01-26 20:42:23 +0000440static int path_parse(char ***p, int flags)
441{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000442 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000443 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000444 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000445 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000446
Mark Whitley4e338752001-01-26 20:42:23 +0000447 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000448 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000449 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000450
451 if (state->flags & WITH_PATH_LOOKUP)
452 pth = state->path_lookup;
453 else
454 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000455 /* PATH=<empty> or PATH=:<empty> */
456 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
457 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000458
Denis Vlasenko253ce002007-01-22 08:34:44 +0000459 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000460 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000461 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000462 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000463 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000464 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000465 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000466 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000467 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000468 }
469
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000470 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000471 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000472 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000473 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000474 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000475 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000476 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000477 *tmp++ = '\0'; /* ':' -> '\0' */
478 if (*tmp == '\0')
479 break; /* :<empty> */
480 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000481 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000482 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000483 return npth;
484}
485
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000486static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000487{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000488 DIR *dir;
489 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000490 struct stat st;
491 char *path1[1];
492 char **paths = path1;
493 int npaths;
494 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000495 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000496 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000497/* char dirbuf[MAX_LINELEN]; */
498#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000499
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000500 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000501 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000502
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000503 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000504 /* no dir, if flags==EXE_ONLY - get paths, else "." */
505 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000506 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000507 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000508 /* dirbuf = ".../.../.../" */
509 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000510#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000511 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000512 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000513#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000514 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000515 /* point to 'l' in "..../last_component" */
516 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000517 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000518
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000519 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000520 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000521 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000522 continue;
523
524 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000525 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000526 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000527
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000528 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000529 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000530 continue;
531 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000532 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000533 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000534 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000535 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000536 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000537 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000538 /* hmm, remover in progress? */
Denis Vlasenko39487e22008-02-14 19:55:58 +0000539 if (lstat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000540 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000541 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000542 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000543 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000544
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000545 len1 = strlen(found);
546 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000547 found[len1] = '\0';
548 found[len1+1] = '\0';
549
Mark Whitley4e338752001-01-26 20:42:23 +0000550 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000551 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000552 if (found[len1-1] != '/') {
553 found[len1] = '/';
554 }
Mark Whitley4e338752001-01-26 20:42:23 +0000555 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000556 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000557 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000558 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000559 }
Mark Whitley4e338752001-01-26 20:42:23 +0000560 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000561 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000562 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000563 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000564 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000565 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000566 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000567 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000568 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000569 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000570 free(paths);
571 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000572#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000573}
Erik Andersenf0657d32000-04-12 17:49:52 +0000574
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000575#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000576
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000577#define collapse_pos(is, in) do { \
578 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
579 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
580} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000581
582static int find_match(char *matchBuf, int *len_with_quotes)
583{
584 int i, j;
585 int command_mode;
586 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000587/* int16_t int_buf[MAX_LINELEN + 1]; */
588/* int16_t pos_buf[MAX_LINELEN + 1]; */
589#define int_buf (S.find_match__int_buf)
590#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000591
592 /* set to integer dimension characters and own positions */
593 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000594 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000595 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000596 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000597 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000598 }
599 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000600 }
601
602 /* mask \+symbol and convert '\t' to ' ' */
603 for (i = j = 0; matchBuf[i]; i++, j++)
604 if (matchBuf[i] == '\\') {
605 collapse_pos(j, j + 1);
606 int_buf[j] |= QUOT;
607 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000608#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000609 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000610 int_buf[j] = ' ' | QUOT;
611#endif
612 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000613#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000614 else if (matchBuf[i] == '\t')
615 int_buf[j] = ' ';
616#endif
617
618 /* mask "symbols" or 'symbols' */
619 c2 = 0;
620 for (i = 0; int_buf[i]; i++) {
621 c = int_buf[i];
622 if (c == '\'' || c == '"') {
623 if (c2 == 0)
624 c2 = c;
625 else {
626 if (c == c2)
627 c2 = 0;
628 else
629 int_buf[i] |= QUOT;
630 }
631 } else if (c2 != 0 && c != '$')
632 int_buf[i] |= QUOT;
633 }
634
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000635 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000636 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
637 for (i = 0; int_buf[i]; i++) {
638 c = int_buf[i];
639 c2 = int_buf[i + 1];
640 j = i ? int_buf[i - 1] : -1;
641 command_mode = 0;
642 if (c == ';' || c == '&' || c == '|') {
643 command_mode = 1 + (c == c2);
644 if (c == '&') {
645 if (j == '>' || j == '<')
646 command_mode = 0;
647 } else if (c == '|' && j == '>')
648 command_mode = 0;
649 }
650 if (command_mode) {
651 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000652 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000653 }
654 }
655 /* collapse `command...` */
656 for (i = 0; int_buf[i]; i++)
657 if (int_buf[i] == '`') {
658 for (j = i + 1; int_buf[j]; j++)
659 if (int_buf[j] == '`') {
660 collapse_pos(i, j + 1);
661 j = 0;
662 break;
663 }
664 if (j) {
665 /* not found close ` - command mode, collapse all previous */
666 collapse_pos(0, i + 1);
667 break;
668 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000669 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000670 }
671
672 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000673 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000674 c2 = 0;
675 for (i = 0; int_buf[i]; i++)
676 if (int_buf[i] == '(' || int_buf[i] == '{') {
677 if (int_buf[i] == '(')
678 c++;
679 else
680 c2++;
681 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000682 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000683 }
684 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
685 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
686 if (int_buf[i] == ')')
687 c--;
688 else
689 c2--;
690 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000691 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000692 }
693
694 /* skip first not quote space */
695 for (i = 0; int_buf[i]; i++)
696 if (int_buf[i] != ' ')
697 break;
698 if (i)
699 collapse_pos(0, i);
700
701 /* set find mode for completion */
702 command_mode = FIND_EXE_ONLY;
703 for (i = 0; int_buf[i]; i++)
704 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
705 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000706 && matchBuf[pos_buf[0]] == 'c'
707 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000708 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000709 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000710 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000711 command_mode = FIND_FILE_ONLY;
712 break;
713 }
714 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000715 for (i = 0; int_buf[i]; i++)
716 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000717 /* find last word */
718 for (--i; i >= 0; i--) {
719 c = int_buf[i];
720 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
721 collapse_pos(0, i + 1);
722 break;
723 }
724 }
725 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000726 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
727 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000728 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000729 while ((int_buf[i] & ~QUOT) == '/'
730 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
731 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000732 i++;
733 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000734
735 /* set only match and destroy quotes */
736 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000737 for (c = 0; pos_buf[i] >= 0; i++) {
738 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000739 j = pos_buf[i] + 1;
740 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000741 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000742 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000743 *len_with_quotes = j ? j - pos_buf[0] : 0;
744
745 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000746#undef int_buf
747#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000748}
749
Glenn L McGrath4d001292003-01-06 01:11:50 +0000750/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000751 * display by column (original idea from ls applet,
752 * very optimized by me :)
753 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000754static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000755{
756 int ncols, row;
757 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000758 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000759 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000760 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000761
762 /* find the longest file name- use that as the column width */
763 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000764 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000765 if (column_width < l)
766 column_width = l;
767 }
768 column_width += 2; /* min space for columns */
769 ncols = cmdedit_termw / column_width;
770
771 if (ncols > 1) {
772 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000773 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000774 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000775 } else {
776 ncols = 1;
777 }
778 for (row = 0; row < nrows; row++) {
779 int n = row;
780 int nc;
781
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000782 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000783 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000784 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000785 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000786 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000787 }
788}
789
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000790static char *add_quote_for_spec_chars(char *found)
791{
792 int l = 0;
793 char *s = xmalloc((strlen(found) + 1) * 2);
794
795 while (*found) {
796 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
797 s[l++] = '\\';
798 s[l++] = *found++;
799 }
800 s[l] = 0;
801 return s;
802}
803
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000804/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000805static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000806{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000807 if (!(state->flags & TAB_COMPLETION))
808 return;
809
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000810 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000811 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000812 int len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000813/* char matchBuf[MAX_LINELEN]; */
814#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000815 int find_type;
816 int recalc_pos;
817
Eric Andersenc470f442003-07-28 09:56:35 +0000818 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000819
820 /* Make a local copy of the string -- up
821 * to the position of the cursor */
822 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000823 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000824
825 find_type = find_match(matchBuf, &recalc_pos);
826
827 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000828 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000829
Denis Vlasenko38f63192007-01-22 09:03:07 +0000830#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000831 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000832 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000833 if (state->flags & USERNAME_COMPLETION)
834 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
835 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000836#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000837 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000838 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000839 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000840 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000841 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000842 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000843 int i, n = 0;
Denis Vlasenkofb290382008-03-02 12:51:26 +0000844 qsort_string_vector(matches, num_matches);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000845 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000846 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000847 if (strcmp(matches[i], matches[i+1]) == 0) {
848 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000849 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000850 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000851 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000852 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000853 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000854 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000855 matches[n] = matches[i];
856 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000857 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000858 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000859 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000860 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000861 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000862 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000863 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000864 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000865 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000866 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000867 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000868 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000869 break;
870 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000871 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000872 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000873 return;
874 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000875 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000876 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000877 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000878 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000879 /* for next completion current found */
880 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000881
882 len_found = strlen(tmp);
883 if (tmp[len_found-1] != '/') {
884 tmp[len_found] = ' ';
885 tmp[len_found+1] = '\0';
886 }
Mark Whitley4e338752001-01-26 20:42:23 +0000887 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000888 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000889 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000890 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000891 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000892 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000893 /* save tail line */
894 strcpy(matchBuf, command_ps + cursor);
895 /* add match */
896 strcat(command_ps, tmp);
897 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000898 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000899 /* back to begin word for match */
900 input_backward(recalc_pos);
901 /* new pos */
902 recalc_pos = cursor + len_found;
903 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000904 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000905 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000906 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000907 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000908 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000909#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000910 } else {
911 /* Ok -- the last char was a TAB. Since they
912 * just hit TAB again, print a list of all the
913 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000914 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000915 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000916
917 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000918 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000919 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000920 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000921 }
922 }
923}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000924
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000925#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000926
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000927
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000928#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000929
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000930/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000931static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000932{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000933 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
934 free(state->history[state->cur_history]);
935 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000936 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000937 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000938}
939
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000940static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000941{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000942 if (state->flags & DO_HISTORY) {
943 int ch = state->cur_history;
944 if (ch < state->cnt_history) {
945 get_previous_history(); /* save the current history line */
946 state->cur_history = ch + 1;
947 return state->cur_history;
948 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000949 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000950 beep();
951 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000952}
Robert Griebl350d26b2002-12-03 22:45:46 +0000953
Denis Vlasenko38f63192007-01-22 09:03:07 +0000954#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000955/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000956static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000957{
Robert Griebl350d26b2002-12-03 22:45:46 +0000958 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000959 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000960
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000961 /* cleanup old */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000962 for (hi = state->cnt_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000963 hi--;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000964 free(state->history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000965 }
966
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000967 fp = fopen(fromfile, "r");
968 if (fp) {
969 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000970 char *hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000971 int l;
972
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000973 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000974 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000975 l = strlen(hl);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000976 if (l >= MAX_LINELEN)
977 hl[MAX_LINELEN-1] = '\0';
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000978 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000979 free(hl);
980 continue;
981 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000982 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000983 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000984 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000985 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000986 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000987}
988
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000989/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000990static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000991{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000992 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +0000993
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000994 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000995 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000996 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000997
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000998 for (i = 0; i < state->cnt_history; i++) {
999 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001000 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001001 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001002 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001003}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001004#else
1005#define load_history(a) ((void)0)
1006#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001007#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001008
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001009static void remember_in_history(const char *str)
1010{
1011 int i;
1012
1013 if (!(state->flags & DO_HISTORY))
1014 return;
1015
1016 i = state->cnt_history;
1017 free(state->history[MAX_HISTORY]);
1018 state->history[MAX_HISTORY] = NULL;
1019 /* After max history, remove the oldest command */
1020 if (i >= MAX_HISTORY) {
1021 free(state->history[0]);
1022 for (i = 0; i < MAX_HISTORY-1; i++)
1023 state->history[i] = state->history[i+1];
1024 }
1025// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
1026// (i.e. do not save dups?)
1027 state->history[i++] = xstrdup(str);
1028 state->cur_history = i;
1029 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001030#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001031 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001032 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +00001033#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00001034 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001035}
1036
1037#else /* MAX_HISTORY == 0 */
1038#define remember_in_history(a) ((void)0)
1039#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001040
1041
Erik Andersen6273f652000-03-17 01:12:41 +00001042/*
1043 * This function is used to grab a character buffer
1044 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001045 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001046 * a mini readline).
1047 *
1048 * The following standard commands are not implemented:
1049 * ESC-b -- Move back one word
1050 * ESC-f -- Move forward one word
1051 * ESC-d -- Delete back one word
1052 * ESC-h -- Delete forward one word
1053 * CTL-t -- Transpose two characters
1054 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001055 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001056 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001057 */
Eric Andersenc470f442003-07-28 09:56:35 +00001058
Denis Vlasenko38f63192007-01-22 09:03:07 +00001059#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001060static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001061vi_Word_motion(char *command, int eat)
1062{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001063 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001064 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001065 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001066 input_forward();
1067}
1068
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001069static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001070vi_word_motion(char *command, int eat)
1071{
1072 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001073 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001074 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001075 input_forward();
1076 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001077 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001078 input_forward();
1079 }
1080
Denis Vlasenko253ce002007-01-22 08:34:44 +00001081 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001082 input_forward();
1083
Denis Vlasenko253ce002007-01-22 08:34:44 +00001084 if (eat && cursor < command_len && isspace(command[cursor]))
1085 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001086 input_forward();
1087}
1088
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001089static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001090vi_End_motion(char *command)
1091{
1092 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001093 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001094 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001095 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001096 input_forward();
1097}
1098
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001099static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001100vi_end_motion(char *command)
1101{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001102 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001103 return;
1104 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001105 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001106 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001107 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001108 return;
1109 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001110 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001111 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1112 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001113 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001114 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001115 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001116 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001117 input_forward();
1118 }
1119}
1120
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001121static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001122vi_Back_motion(char *command)
1123{
1124 while (cursor > 0 && isspace(command[cursor-1]))
1125 input_backward(1);
1126 while (cursor > 0 && !isspace(command[cursor-1]))
1127 input_backward(1);
1128}
1129
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001130static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001131vi_back_motion(char *command)
1132{
1133 if (cursor <= 0)
1134 return;
1135 input_backward(1);
1136 while (cursor > 0 && isspace(command[cursor]))
1137 input_backward(1);
1138 if (cursor <= 0)
1139 return;
1140 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001141 while (cursor > 0
1142 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1143 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001144 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001145 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001146 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001147 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001148 input_backward(1);
1149 }
1150}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001151#endif
1152
1153
1154/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001155 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001156 */
1157
Denis Vlasenko38f63192007-01-22 09:03:07 +00001158#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001159static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001160{
1161 cmdedit_prompt = prmt_ptr;
1162 cmdedit_prmt_len = strlen(prmt_ptr);
1163 put_prompt();
1164}
1165#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001166static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001167{
1168 int prmt_len = 0;
1169 size_t cur_prmt_len = 0;
1170 char flg_not_length = '[';
1171 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001172 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1173 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001174 char c;
1175 char *pbuf;
1176
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001177 cmdedit_prmt_len = 0;
1178
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001179 if (!cwd_buf) {
1180 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001181 }
1182
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001183 cbuf[1] = '\0'; /* never changes */
1184
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001185 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001186 char *free_me = NULL;
1187
1188 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001189 c = *prmt_ptr++;
1190 if (c == '\\') {
1191 const char *cp = prmt_ptr;
1192 int l;
1193
1194 c = bb_process_escape_sequence(&prmt_ptr);
1195 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001196 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001197 break;
1198 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001199
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001200 switch (c) {
1201#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1202 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001203 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001204 break;
1205#endif
1206 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001207 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001208 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001209 break;
1210 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001211 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001212 break;
1213#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1214 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001215 /* /home/user[/something] -> ~[/something] */
1216 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001217 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001218 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001219 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1220 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1221 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001222 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001223 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001224 }
1225 break;
1226#endif
1227 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001228 pbuf = cwd_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 Vlasenko7221c8c2007-12-03 10:45:14 +00001234 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001235 break;
1236 case 'e': case 'E': /* \e \E = \033 */
1237 c = '\033';
1238 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001239 case 'x': case 'X': {
1240 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001241 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001242 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001243 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001244 buf2[l] = '\0';
1245 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001246 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001247 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001248 break;
1249 }
1250 prmt_ptr++;
1251 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001252 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001253 if (c == 0)
1254 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001255 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001256 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001257 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001258 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;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001264 } /* switch */
1265 } /* if */
1266 } /* if */
1267 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001268 cur_prmt_len = strlen(pbuf);
1269 prmt_len += cur_prmt_len;
1270 if (flg_not_length != ']')
1271 cmdedit_prmt_len += cur_prmt_len;
1272 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001273 free(free_me);
1274 } /* while */
1275
1276 if (cwd_buf != (char *)bb_msg_unknown)
1277 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001278 cmdedit_prompt = prmt_mem_ptr;
1279 put_prompt();
1280}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001281#endif
1282
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001283static void cmdedit_setwidth(unsigned w, int redraw_flg)
1284{
1285 cmdedit_termw = w;
1286 if (redraw_flg) {
1287 /* new y for current cursor */
1288 int new_y = (cursor + cmdedit_prmt_len) / w;
1289 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001290 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001291 fflush(stdout);
1292 }
1293}
1294
1295static void win_changed(int nsig)
1296{
1297 int width;
1298 get_terminal_width_height(0, &width, NULL);
1299 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1300 if (nsig == SIGWINCH)
1301 signal(SIGWINCH, win_changed); /* rearm ourself */
1302}
1303
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001304/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001305 * The emacs and vi modes share much of the code in the big
1306 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001307 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001308 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001309 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001310 */
1311
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001312#define vbit 0x100
1313
1314/* leave out the "vi-mode"-only case labels if vi editing isn't
1315 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001316#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001317
1318/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001319#undef CTRL
1320#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001321
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001322/* Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001323 * -1 on read errors or EOF, or on bare Ctrl-D,
1324 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001325 * >0 length of input string, including terminating '\n'
1326 */
Denis Vlasenko037576d2007-10-20 18:30:38 +00001327int read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001328{
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001329#if ENABLE_FEATURE_TAB_COMPLETION
1330 smallint lastWasTab = FALSE;
1331#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001332 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001333 unsigned char c;
1334 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001335#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001336 smallint vi_cmdmode = 0;
1337 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001338#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001339 struct termios initial_settings;
1340 struct termios new_settings;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001341
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001342 INIT_S();
1343
1344 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1345 || !(initial_settings.c_lflag & ECHO)
1346 ) {
1347 /* Happens when e.g. stty -echo was run before */
1348 int len;
1349 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001350 fflush(stdout);
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001351 if (fgets(command, maxsize, stdin) == NULL)
1352 len = -1; /* EOF or error */
1353 else
1354 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001355 DEINIT_S();
1356 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001357 }
1358
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001359// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001360 if (maxsize > MAX_LINELEN)
1361 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001362
1363 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001364 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001365#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001366 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001367 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001368#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001369
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001370 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001371 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001372 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001373 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001374 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001375
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001376 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001377 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1378 /* Turn off echoing and CTRL-C, so we can trap it */
1379 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001380 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001381 new_settings.c_cc[VMIN] = 1;
1382 new_settings.c_cc[VTIME] = 0;
1383 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001384#ifndef _POSIX_VDISABLE
1385#define _POSIX_VDISABLE '\0'
1386#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001387 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001388 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001389
Eric Andersen6faae7d2001-02-16 20:09:17 +00001390 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001391 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1392 win_changed(0); /* do initial resizing */
1393#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1394 {
1395 struct passwd *entry;
1396
1397 entry = getpwuid(geteuid());
1398 if (entry) {
1399 user_buf = xstrdup(entry->pw_name);
1400 home_pwd_buf = xstrdup(entry->pw_dir);
1401 }
1402 }
1403#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001404 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001405 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001406
Erik Andersenc7c634b2000-03-19 05:28:55 +00001407 while (1) {
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001408 fflush(NULL);
Mark Whitley4e338752001-01-26 20:42:23 +00001409
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001410 if (nonblock_safe_read(STDIN_FILENO, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001411 /* if we can't read input then exit */
1412 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001413 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001414
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001415 ic = c;
1416
Denis Vlasenko38f63192007-01-22 09:03:07 +00001417#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001418 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001419 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001420 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001421#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001422 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001423 case '\n':
1424 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001425 vi_case('\n'|vbit:)
1426 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001427 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001428 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001429 break_out = 1;
1430 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001431 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001432 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001433 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001434 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001435 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001436 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001437 vi_case('h'|vbit:)
1438 vi_case('\b'|vbit:)
1439 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001440 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001441 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001442 break;
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
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001463 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001464 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001465 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001466 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001467 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001468 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001469 vi_case('l'|vbit:)
1470 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001471 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001472 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001473 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001474
Erik Andersenf0657d32000-04-12 17:49:52 +00001475 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001476 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001477 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001478 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001479 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001480
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001481#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001482 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001483 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001484 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001485#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001486
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001487 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001488 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001489 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001490 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001491 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001492 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001493 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001494 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001495 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001496 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001497 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001498 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001499
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001500#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001501 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001502 vi_case(CTRL('N')|vbit:)
1503 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001504 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001505 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001506 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001507 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001508 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001509 vi_case(CTRL('P')|vbit:)
1510 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001511 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001512 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001513 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001514 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001515 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001516 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001517 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001518#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001519
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001520 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001521 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001522 /* Control-U -- Clear line before cursor */
1523 if (cursor) {
1524 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001525 command_len -= cursor;
1526 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001527 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001528 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001529 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001530 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001531 /* Control-W -- Remove the last word */
1532 while (cursor > 0 && isspace(command[cursor-1]))
1533 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001534 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001535 input_backspace();
1536 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001537
Denis Vlasenko38f63192007-01-22 09:03:07 +00001538#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001539 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001540 vi_cmdmode = 0;
1541 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001542 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001543 input_backward(cursor);
1544 vi_cmdmode = 0;
1545 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001546 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001547 input_forward();
1548 vi_cmdmode = 0;
1549 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001550 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001551 input_end();
1552 vi_cmdmode = 0;
1553 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001554 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001555 input_delete(1);
1556 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001557 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001558 if (cursor > 0) {
1559 input_backward(1);
1560 input_delete(1);
1561 }
1562 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001563 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001564 vi_Word_motion(command, 1);
1565 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001566 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001567 vi_word_motion(command, 1);
1568 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001569 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001570 vi_End_motion(command);
1571 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001572 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001573 vi_end_motion(command);
1574 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001575 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001576 vi_Back_motion(command);
1577 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001578 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001579 vi_back_motion(command);
1580 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001581 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001582 vi_cmdmode = 0;
1583 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001584 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001585 goto clear_to_eol;
1586
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 */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001590 case 'd'|vbit: {
1591 int nc, sc;
1592 sc = cursor;
1593 prevc = ic;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001594 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001595 goto prepare_to_die;
1596 if (c == (prevc & 0xff)) {
1597 /* "cc", "dd" */
1598 input_backward(cursor);
1599 goto clear_to_eol;
1600 break;
1601 }
1602 switch (c) {
1603 case 'w':
1604 case 'W':
1605 case 'e':
1606 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001607 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001608 case 'w': /* "dw", "cw" */
1609 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001610 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001611 case 'W': /* 'dW', 'cW' */
1612 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001613 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001614 case 'e': /* 'de', 'ce' */
1615 vi_end_motion(command);
1616 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001617 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001618 case 'E': /* 'dE', 'cE' */
1619 vi_End_motion(command);
1620 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001621 break;
1622 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001623 nc = cursor;
1624 input_backward(cursor - sc);
1625 while (nc-- > cursor)
1626 input_delete(1);
1627 break;
1628 case 'b': /* "db", "cb" */
1629 case 'B': /* implemented as B */
1630 if (c == 'b')
1631 vi_back_motion(command);
1632 else
1633 vi_Back_motion(command);
1634 while (sc-- > cursor)
1635 input_delete(1);
1636 break;
1637 case ' ': /* "d ", "c " */
1638 input_delete(1);
1639 break;
1640 case '$': /* "d$", "c$" */
1641 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001642 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001643 input_delete(1);
1644 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001645 }
1646 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001647 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001648 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001649 input_forward();
1650 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001651 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001652 put();
1653 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001654 case 'r'|vbit:
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001655 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001656 goto prepare_to_die;
1657 if (c == 0)
1658 beep();
1659 else {
1660 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001661 bb_putchar(c);
1662 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001663 }
1664 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001665#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001666
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001667 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001668
Denis Vlasenko38f63192007-01-22 09:03:07 +00001669#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001670 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001671 /* ESC: insert mode --> command mode */
1672 vi_cmdmode = 1;
1673 input_backward(1);
1674 break;
1675 }
1676#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001677 /* escape sequence follows */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001678 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001679 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001680 /* different vt100 emulations */
1681 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001682 vi_case('['|vbit:)
1683 vi_case('O'|vbit:)
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 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001687 if (c >= '1' && c <= '9') {
1688 unsigned char dummy;
1689
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001690 if (safe_read(STDIN_FILENO, &dummy, 1) < 1)
Glenn L McGrath475820c2004-01-22 12:42:23 +00001691 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001692 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001693 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001694 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001695
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001696 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001697#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001698 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001699 input_tab(&lastWasTab);
1700 break;
1701#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001702#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001703 case 'A':
1704 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001705 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001706 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001707 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001708 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001709 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001710 break;
1711 case 'B':
1712 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001713 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001714 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001715 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001716 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001717 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001718 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001719 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001720 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001721 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001722#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001723 case 'C':
1724 /* Right Arrow -- Move forward one character */
1725 input_forward();
1726 break;
1727 case 'D':
1728 /* Left Arrow -- Move back one character */
1729 input_backward(1);
1730 break;
1731 case '3':
1732 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001733 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001734 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001735 case '1': // vt100? linux vt? or what?
1736 case '7': // vt100? linux vt? or what?
1737 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001738 input_backward(cursor);
1739 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001740 case '4': // vt100? linux vt? or what?
1741 case '8': // vt100? linux vt? or what?
1742 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001743 input_end();
1744 break;
1745 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001746 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001747 beep();
1748 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001749 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001750
Eric Andersenc470f442003-07-28 09:56:35 +00001751 default: /* If it's regular input, do the normal thing */
Paul Fox84bbac52008-01-11 16:50:08 +00001752
1753 /* Control-V -- force insert of next char */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001754 if (c == CTRL('V')) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001755 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001756 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001757 if (c == 0) {
1758 beep();
1759 break;
1760 }
Paul Fox84bbac52008-01-11 16:50:08 +00001761 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001762
Denis Vlasenko38f63192007-01-22 09:03:07 +00001763#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001764 if (vi_cmdmode) /* Don't self-insert */
1765 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001766#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001767 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001768 break;
1769
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001770 command_len++;
1771 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001772 command[cursor] = c;
1773 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001774 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001775 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001776 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001777
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001778 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001779 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001780 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001781 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001782 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001783 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001784 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001785 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001786 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001787 }
Eric Andersenc470f442003-07-28 09:56:35 +00001788 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001789 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001790
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001791#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001792 if (c != '\t')
1793 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001794#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001795 }
1796
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001797 if (command_len > 0)
1798 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001799
Eric Andersen27bb7902003-12-23 20:24:51 +00001800 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001801 command[command_len++] = '\n';
1802 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001803 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001804
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001805#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001806 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001807#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001808
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001809 /* restore initial_settings */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001810 tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001811 /* restore SIGWINCH handler */
1812 signal(SIGWINCH, previous_SIGWINCH_handler);
1813 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001814
1815 DEINIT_S();
1816
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001817 return command_len;
1818}
1819
1820line_input_t *new_line_input_t(int flags)
1821{
1822 line_input_t *n = xzalloc(sizeof(*n));
1823 n->flags = flags;
1824 return n;
1825}
1826
1827#else
1828
1829#undef read_line_input
1830int read_line_input(const char* prompt, char* command, int maxsize)
1831{
1832 fputs(prompt, stdout);
1833 fflush(stdout);
1834 fgets(command, maxsize, stdin);
1835 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001836}
1837
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001838#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001839
1840
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001841/*
1842 * Testing
1843 */
1844
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001845#ifdef TEST
1846
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001847#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001848
1849const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001850
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001851int main(int argc, char **argv)
1852{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001853 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001854 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001855#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001856 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1857 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1858 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001859#else
1860 "% ";
1861#endif
1862
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001863#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001864 setlocale(LC_ALL, "");
1865#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001866 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001867 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001868 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001869 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001870 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001871 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001872 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001873 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001874 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001875 return 0;
1876}
1877
Eric Andersenc470f442003-07-28 09:56:35 +00001878#endif /* TEST */