blob: 7e408207c94e09284473b6479d6c34e49ee1cdb5 [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 */
297static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000298{
Mark Whitley4e338752001-01-26 20:42:23 +0000299 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000300
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000301 if (j == command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000302 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000303
Denis Vlasenko38f63192007-01-22 09:03:07 +0000304#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000305 if (save) {
306 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000307 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000308 newdelflag = 0;
309 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000310 if ((delptr - delbuf) < DELBUFSIZ)
311 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000312 }
313#endif
314
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000315 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000316 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000317 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000318 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000319 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000320}
321
Denis Vlasenko38f63192007-01-22 09:03:07 +0000322#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000323static void put(void)
324{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000325 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000326 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000327
Paul Fox3f11b1b2005-08-04 19:04:46 +0000328 if (j == 0)
329 return;
330 ocursor = cursor;
331 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000332 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000333 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000334 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000335 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000336 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000337}
338#endif
339
Mark Whitley4e338752001-01-26 20:42:23 +0000340/* Delete the char in back of the cursor */
341static void input_backspace(void)
342{
343 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000344 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000345 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000346 }
347}
348
Eric Andersenaff114c2004-04-14 17:51:38 +0000349/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000350static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000351{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000352 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000353 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000354}
355
Denis Vlasenko38f63192007-01-22 09:03:07 +0000356#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000357
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000358static void free_tab_completion_data(void)
359{
360 if (matches) {
361 while (num_matches)
362 free(matches[--num_matches]);
363 free(matches);
364 matches = NULL;
365 }
366}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000367
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000368static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000369{
370 int nm = num_matches;
371 int nm1 = nm + 1;
372
373 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000374 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000375 num_matches++;
376}
377
Denis Vlasenko38f63192007-01-22 09:03:07 +0000378#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000379static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000380{
381 struct passwd *entry;
382 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000383
Eric Andersenc470f442003-07-28 09:56:35 +0000384 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000385 userlen = strlen(ud);
386
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000387 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000388 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000389 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000390
Eric Andersenc470f442003-07-28 09:56:35 +0000391 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000392 home = home_pwd_buf;
393 } else {
394 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000395 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000396 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000397 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000398 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000399 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000400 ud = temp;
401 if (entry)
402 home = entry->pw_dir;
403 }
404 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000405 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000406 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000407 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000408 }
409 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000410 } else {
411 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000412 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000413 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000414 struct passwd pwd;
415 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000416
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000417 setpwent();
418 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000419 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000420 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
421 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000422 }
423 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000424 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000425 }
426}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000427#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000428
429enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430 FIND_EXE_ONLY = 0,
431 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000432 FIND_FILE_ONLY = 2,
433};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000434
Mark Whitley4e338752001-01-26 20:42:23 +0000435static int path_parse(char ***p, int flags)
436{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000437 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000438 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000439 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000440 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000441
Mark Whitley4e338752001-01-26 20:42:23 +0000442 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000443 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000444 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000445
446 if (state->flags & WITH_PATH_LOOKUP)
447 pth = state->path_lookup;
448 else
449 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000450 /* PATH=<empty> or PATH=:<empty> */
451 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
452 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000453
Denis Vlasenko253ce002007-01-22 08:34:44 +0000454 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000455 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000456 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000457 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000458 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000459 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000460 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000461 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000462 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000463 }
464
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000465 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000466 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000467 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000468 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000469 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000470 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000471 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000472 *tmp++ = '\0'; /* ':' -> '\0' */
473 if (*tmp == '\0')
474 break; /* :<empty> */
475 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000476 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000477 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000478 return npth;
479}
480
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000481static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000482{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000483 DIR *dir;
484 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000485 struct stat st;
486 char *path1[1];
487 char **paths = path1;
488 int npaths;
489 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000490 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000491 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000492/* char dirbuf[MAX_LINELEN]; */
493#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000494
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000495 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000496 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000497
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000498 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000499 /* no dir, if flags==EXE_ONLY - get paths, else "." */
500 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000501 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000502 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000503 /* dirbuf = ".../.../.../" */
504 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000505#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000506 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000507 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000508#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000509 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000510 /* point to 'l' in "..../last_component" */
511 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000512 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000513
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000514 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000515 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000516 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000517 continue;
518
519 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000520 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000521 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000522
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000523 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000524 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000525 continue;
526 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000527 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000528 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000529 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000530 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000531 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000532 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000533 /* hmm, remover in progress? */
Denis Vlasenko39487e22008-02-14 19:55:58 +0000534 if (lstat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000535 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000536 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000537 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000538 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000539
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000540 len1 = strlen(found);
541 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000542 found[len1] = '\0';
543 found[len1+1] = '\0';
544
Mark Whitley4e338752001-01-26 20:42:23 +0000545 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000546 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000547 if (found[len1-1] != '/') {
548 found[len1] = '/';
549 }
Mark Whitley4e338752001-01-26 20:42:23 +0000550 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000551 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000552 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000553 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000554 }
Mark Whitley4e338752001-01-26 20:42:23 +0000555 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000556 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000557 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000558 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000559 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000560 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000561 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000562 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000563 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000564 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000565 free(paths);
566 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000567#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000568}
Erik Andersenf0657d32000-04-12 17:49:52 +0000569
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000570#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000571
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000572#define collapse_pos(is, in) do { \
573 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
574 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
575} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000576
577static int find_match(char *matchBuf, int *len_with_quotes)
578{
579 int i, j;
580 int command_mode;
581 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000582/* int16_t int_buf[MAX_LINELEN + 1]; */
583/* int16_t pos_buf[MAX_LINELEN + 1]; */
584#define int_buf (S.find_match__int_buf)
585#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000586
587 /* set to integer dimension characters and own positions */
588 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000589 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000590 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000591 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000592 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000593 }
594 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000595 }
596
597 /* mask \+symbol and convert '\t' to ' ' */
598 for (i = j = 0; matchBuf[i]; i++, j++)
599 if (matchBuf[i] == '\\') {
600 collapse_pos(j, j + 1);
601 int_buf[j] |= QUOT;
602 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000603#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000604 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000605 int_buf[j] = ' ' | QUOT;
606#endif
607 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000608#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000609 else if (matchBuf[i] == '\t')
610 int_buf[j] = ' ';
611#endif
612
613 /* mask "symbols" or 'symbols' */
614 c2 = 0;
615 for (i = 0; int_buf[i]; i++) {
616 c = int_buf[i];
617 if (c == '\'' || c == '"') {
618 if (c2 == 0)
619 c2 = c;
620 else {
621 if (c == c2)
622 c2 = 0;
623 else
624 int_buf[i] |= QUOT;
625 }
626 } else if (c2 != 0 && c != '$')
627 int_buf[i] |= QUOT;
628 }
629
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000630 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000631 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
632 for (i = 0; int_buf[i]; i++) {
633 c = int_buf[i];
634 c2 = int_buf[i + 1];
635 j = i ? int_buf[i - 1] : -1;
636 command_mode = 0;
637 if (c == ';' || c == '&' || c == '|') {
638 command_mode = 1 + (c == c2);
639 if (c == '&') {
640 if (j == '>' || j == '<')
641 command_mode = 0;
642 } else if (c == '|' && j == '>')
643 command_mode = 0;
644 }
645 if (command_mode) {
646 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000647 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000648 }
649 }
650 /* collapse `command...` */
651 for (i = 0; int_buf[i]; i++)
652 if (int_buf[i] == '`') {
653 for (j = i + 1; int_buf[j]; j++)
654 if (int_buf[j] == '`') {
655 collapse_pos(i, j + 1);
656 j = 0;
657 break;
658 }
659 if (j) {
660 /* not found close ` - command mode, collapse all previous */
661 collapse_pos(0, i + 1);
662 break;
663 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000664 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000665 }
666
667 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000668 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000669 c2 = 0;
670 for (i = 0; int_buf[i]; i++)
671 if (int_buf[i] == '(' || int_buf[i] == '{') {
672 if (int_buf[i] == '(')
673 c++;
674 else
675 c2++;
676 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000677 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000678 }
679 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
680 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
681 if (int_buf[i] == ')')
682 c--;
683 else
684 c2--;
685 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000686 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000687 }
688
689 /* skip first not quote space */
690 for (i = 0; int_buf[i]; i++)
691 if (int_buf[i] != ' ')
692 break;
693 if (i)
694 collapse_pos(0, i);
695
696 /* set find mode for completion */
697 command_mode = FIND_EXE_ONLY;
698 for (i = 0; int_buf[i]; i++)
699 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
700 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000701 && matchBuf[pos_buf[0]] == 'c'
702 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000703 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000704 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000705 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000706 command_mode = FIND_FILE_ONLY;
707 break;
708 }
709 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000710 for (i = 0; int_buf[i]; i++)
711 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000712 /* find last word */
713 for (--i; i >= 0; i--) {
714 c = int_buf[i];
715 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
716 collapse_pos(0, i + 1);
717 break;
718 }
719 }
720 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000721 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
722 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000723 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000724 while ((int_buf[i] & ~QUOT) == '/'
725 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
726 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000727 i++;
728 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000729
730 /* set only match and destroy quotes */
731 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000732 for (c = 0; pos_buf[i] >= 0; i++) {
733 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000734 j = pos_buf[i] + 1;
735 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000736 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000737 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000738 *len_with_quotes = j ? j - pos_buf[0] : 0;
739
740 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000741#undef int_buf
742#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000743}
744
Glenn L McGrath4d001292003-01-06 01:11:50 +0000745/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000746 * display by column (original idea from ls applet,
747 * very optimized by me :)
748 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000749static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000750{
751 int ncols, row;
752 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000753 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000754 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000755 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000756
757 /* find the longest file name- use that as the column width */
758 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000759 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000760 if (column_width < l)
761 column_width = l;
762 }
763 column_width += 2; /* min space for columns */
764 ncols = cmdedit_termw / column_width;
765
766 if (ncols > 1) {
767 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000768 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000769 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000770 } else {
771 ncols = 1;
772 }
773 for (row = 0; row < nrows; row++) {
774 int n = row;
775 int nc;
776
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000777 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000778 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000779 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000780 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000781 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000782 }
783}
784
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000785static char *add_quote_for_spec_chars(char *found)
786{
787 int l = 0;
788 char *s = xmalloc((strlen(found) + 1) * 2);
789
790 while (*found) {
791 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
792 s[l++] = '\\';
793 s[l++] = *found++;
794 }
795 s[l] = 0;
796 return s;
797}
798
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000799/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000800static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000801{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000802 if (!(state->flags & TAB_COMPLETION))
803 return;
804
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000805 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000806 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000807 int len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000808/* char matchBuf[MAX_LINELEN]; */
809#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000810 int find_type;
811 int recalc_pos;
812
Eric Andersenc470f442003-07-28 09:56:35 +0000813 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000814
815 /* Make a local copy of the string -- up
816 * to the position of the cursor */
817 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000818 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000819
820 find_type = find_match(matchBuf, &recalc_pos);
821
822 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000823 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000824
Denis Vlasenko38f63192007-01-22 09:03:07 +0000825#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000826 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000827 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000828 if (state->flags & USERNAME_COMPLETION)
829 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
830 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000831#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000832 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000833 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000834 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000835 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000836 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000837 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000838 int i, n = 0;
Denis Vlasenkofb290382008-03-02 12:51:26 +0000839 qsort_string_vector(matches, num_matches);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000840 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000841 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000842 if (strcmp(matches[i], matches[i+1]) == 0) {
843 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000844 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000845 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000846 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000847 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000848 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000849 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000850 matches[n] = matches[i];
851 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000852 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000853 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000854 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000855 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000856 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000857 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000858 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000859 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000860 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000861 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000862 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000863 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000864 break;
865 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000866 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000867 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000868 return;
869 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000870 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000871 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000872 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000873 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000874 /* for next completion current found */
875 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000876
877 len_found = strlen(tmp);
878 if (tmp[len_found-1] != '/') {
879 tmp[len_found] = ' ';
880 tmp[len_found+1] = '\0';
881 }
Mark Whitley4e338752001-01-26 20:42:23 +0000882 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000883 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000884 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000885 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000886 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000887 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000888 /* save tail line */
889 strcpy(matchBuf, command_ps + cursor);
890 /* add match */
891 strcat(command_ps, tmp);
892 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000893 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000894 /* back to begin word for match */
895 input_backward(recalc_pos);
896 /* new pos */
897 recalc_pos = cursor + len_found;
898 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000899 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000900 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000901 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000902 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000903 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000904#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000905 } else {
906 /* Ok -- the last char was a TAB. Since they
907 * just hit TAB again, print a list of all the
908 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000909 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000910 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000911
912 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000913 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000914 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000915 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000916 }
917 }
918}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000919
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000920#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000921
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000922
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000923#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000924
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000925/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000926static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000927{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000928 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
929 free(state->history[state->cur_history]);
930 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000931 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000932 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000933}
934
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000935static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000936{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000937 if (state->flags & DO_HISTORY) {
938 int ch = state->cur_history;
939 if (ch < state->cnt_history) {
940 get_previous_history(); /* save the current history line */
941 state->cur_history = ch + 1;
942 return state->cur_history;
943 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000944 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000945 beep();
946 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000947}
Robert Griebl350d26b2002-12-03 22:45:46 +0000948
Denis Vlasenko38f63192007-01-22 09:03:07 +0000949#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000950/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000951static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000952{
Robert Griebl350d26b2002-12-03 22:45:46 +0000953 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000954 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000955
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000956 /* cleanup old */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000957 for (hi = state->cnt_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000958 hi--;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000959 free(state->history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000960 }
961
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000962 fp = fopen(fromfile, "r");
963 if (fp) {
964 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000965 char *hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000966 int l;
967
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000968 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000969 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000970 l = strlen(hl);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000971 if (l >= MAX_LINELEN)
972 hl[MAX_LINELEN-1] = '\0';
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000973 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000974 free(hl);
975 continue;
976 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000977 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000978 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000979 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000980 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000981 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000982}
983
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000984/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000985static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000986{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000987 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +0000988
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000989 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000990 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000991 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000992
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000993 for (i = 0; i < state->cnt_history; i++) {
994 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000995 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000996 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000997 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000998}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000999#else
1000#define load_history(a) ((void)0)
1001#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001002#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001003
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001004static void remember_in_history(const char *str)
1005{
1006 int i;
1007
1008 if (!(state->flags & DO_HISTORY))
1009 return;
1010
1011 i = state->cnt_history;
1012 free(state->history[MAX_HISTORY]);
1013 state->history[MAX_HISTORY] = NULL;
1014 /* After max history, remove the oldest command */
1015 if (i >= MAX_HISTORY) {
1016 free(state->history[0]);
1017 for (i = 0; i < MAX_HISTORY-1; i++)
1018 state->history[i] = state->history[i+1];
1019 }
1020// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
1021// (i.e. do not save dups?)
1022 state->history[i++] = xstrdup(str);
1023 state->cur_history = i;
1024 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001025#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001026 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001027 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +00001028#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00001029 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001030}
1031
1032#else /* MAX_HISTORY == 0 */
1033#define remember_in_history(a) ((void)0)
1034#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001035
1036
Erik Andersen6273f652000-03-17 01:12:41 +00001037/*
1038 * This function is used to grab a character buffer
1039 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001040 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001041 * a mini readline).
1042 *
1043 * The following standard commands are not implemented:
1044 * ESC-b -- Move back one word
1045 * ESC-f -- Move forward one word
1046 * ESC-d -- Delete back one word
1047 * ESC-h -- Delete forward one word
1048 * CTL-t -- Transpose two characters
1049 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001050 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001051 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001052 */
Eric Andersenc470f442003-07-28 09:56:35 +00001053
Denis Vlasenko38f63192007-01-22 09:03:07 +00001054#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001055static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001056vi_Word_motion(char *command, int eat)
1057{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001058 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001059 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001060 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001061 input_forward();
1062}
1063
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001064static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001065vi_word_motion(char *command, int eat)
1066{
1067 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001068 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001069 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001070 input_forward();
1071 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001072 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001073 input_forward();
1074 }
1075
Denis Vlasenko253ce002007-01-22 08:34:44 +00001076 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001077 input_forward();
1078
Denis Vlasenko253ce002007-01-22 08:34:44 +00001079 if (eat && cursor < command_len && isspace(command[cursor]))
1080 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001081 input_forward();
1082}
1083
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001084static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001085vi_End_motion(char *command)
1086{
1087 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001088 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001089 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001090 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001091 input_forward();
1092}
1093
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001094static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001095vi_end_motion(char *command)
1096{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001097 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001098 return;
1099 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001100 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001101 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001102 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001103 return;
1104 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001105 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001106 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1107 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001108 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001109 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001110 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001111 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001112 input_forward();
1113 }
1114}
1115
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001116static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001117vi_Back_motion(char *command)
1118{
1119 while (cursor > 0 && isspace(command[cursor-1]))
1120 input_backward(1);
1121 while (cursor > 0 && !isspace(command[cursor-1]))
1122 input_backward(1);
1123}
1124
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001125static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001126vi_back_motion(char *command)
1127{
1128 if (cursor <= 0)
1129 return;
1130 input_backward(1);
1131 while (cursor > 0 && isspace(command[cursor]))
1132 input_backward(1);
1133 if (cursor <= 0)
1134 return;
1135 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001136 while (cursor > 0
1137 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1138 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001139 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001140 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001141 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001142 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001143 input_backward(1);
1144 }
1145}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001146#endif
1147
1148
1149/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001150 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001151 */
1152
Denis Vlasenko38f63192007-01-22 09:03:07 +00001153#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001154static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001155{
1156 cmdedit_prompt = prmt_ptr;
1157 cmdedit_prmt_len = strlen(prmt_ptr);
1158 put_prompt();
1159}
1160#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001161static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001162{
1163 int prmt_len = 0;
1164 size_t cur_prmt_len = 0;
1165 char flg_not_length = '[';
1166 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001167 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1168 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001169 char c;
1170 char *pbuf;
1171
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001172 cmdedit_prmt_len = 0;
1173
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001174 if (!cwd_buf) {
1175 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001176 }
1177
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001178 cbuf[1] = '\0'; /* never changes */
1179
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001180 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001181 char *free_me = NULL;
1182
1183 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001184 c = *prmt_ptr++;
1185 if (c == '\\') {
1186 const char *cp = prmt_ptr;
1187 int l;
1188
1189 c = bb_process_escape_sequence(&prmt_ptr);
1190 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001191 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001192 break;
1193 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001194
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001195 switch (c) {
1196#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1197 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001198 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001199 break;
1200#endif
1201 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001202 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001203 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001204 break;
1205 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001206 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001207 break;
1208#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1209 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001210 /* /home/user[/something] -> ~[/something] */
1211 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001212 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001213 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001214 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1215 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1216 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001217 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001218 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001219 }
1220 break;
1221#endif
1222 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001223 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001224 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001225 if (cp != NULL && cp != pbuf)
1226 pbuf += (cp-pbuf) + 1;
1227 break;
1228 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001229 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001230 break;
1231 case 'e': case 'E': /* \e \E = \033 */
1232 c = '\033';
1233 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001234 case 'x': case 'X': {
1235 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001236 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001237 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001238 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001239 buf2[l] = '\0';
1240 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001241 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001242 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001243 break;
1244 }
1245 prmt_ptr++;
1246 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001247 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001248 if (c == 0)
1249 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001250 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001251 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001252 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001253 case '[': case ']':
1254 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001255 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001256 continue;
1257 }
1258 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001259 } /* switch */
1260 } /* if */
1261 } /* if */
1262 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001263 cur_prmt_len = strlen(pbuf);
1264 prmt_len += cur_prmt_len;
1265 if (flg_not_length != ']')
1266 cmdedit_prmt_len += cur_prmt_len;
1267 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001268 free(free_me);
1269 } /* while */
1270
1271 if (cwd_buf != (char *)bb_msg_unknown)
1272 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001273 cmdedit_prompt = prmt_mem_ptr;
1274 put_prompt();
1275}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001276#endif
1277
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001278static void cmdedit_setwidth(unsigned w, int redraw_flg)
1279{
1280 cmdedit_termw = w;
1281 if (redraw_flg) {
1282 /* new y for current cursor */
1283 int new_y = (cursor + cmdedit_prmt_len) / w;
1284 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001285 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001286 fflush(stdout);
1287 }
1288}
1289
1290static void win_changed(int nsig)
1291{
1292 int width;
1293 get_terminal_width_height(0, &width, NULL);
1294 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1295 if (nsig == SIGWINCH)
1296 signal(SIGWINCH, win_changed); /* rearm ourself */
1297}
1298
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001299/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001300 * The emacs and vi modes share much of the code in the big
1301 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001302 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001303 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001304 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001305 */
1306
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001307#define vbit 0x100
1308
1309/* leave out the "vi-mode"-only case labels if vi editing isn't
1310 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001311#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001312
1313/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001314#undef CTRL
1315#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001316
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001317/* Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001318 * -1 on read errors or EOF, or on bare Ctrl-D,
1319 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001320 * >0 length of input string, including terminating '\n'
1321 */
Denis Vlasenko037576d2007-10-20 18:30:38 +00001322int read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001323{
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001324#if ENABLE_FEATURE_TAB_COMPLETION
1325 smallint lastWasTab = FALSE;
1326#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001327 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001328 unsigned char c;
1329 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001330#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001331 smallint vi_cmdmode = 0;
1332 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001333#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001334 struct termios initial_settings;
1335 struct termios new_settings;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001336
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001337 INIT_S();
1338
1339 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1340 || !(initial_settings.c_lflag & ECHO)
1341 ) {
1342 /* Happens when e.g. stty -echo was run before */
1343 int len;
1344 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001345 fflush(stdout);
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001346 if (fgets(command, maxsize, stdin) == NULL)
1347 len = -1; /* EOF or error */
1348 else
1349 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001350 DEINIT_S();
1351 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001352 }
1353
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001354// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001355 if (maxsize > MAX_LINELEN)
1356 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001357
1358 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001359 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001360#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001361 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001362 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001363#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001364
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001365 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001366 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001367 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001368 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001369 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001370
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001371 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001372 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1373 /* Turn off echoing and CTRL-C, so we can trap it */
1374 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001375 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001376 new_settings.c_cc[VMIN] = 1;
1377 new_settings.c_cc[VTIME] = 0;
1378 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001379#ifndef _POSIX_VDISABLE
1380#define _POSIX_VDISABLE '\0'
1381#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001382 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001383 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001384
Eric Andersen6faae7d2001-02-16 20:09:17 +00001385 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001386 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1387 win_changed(0); /* do initial resizing */
1388#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1389 {
1390 struct passwd *entry;
1391
1392 entry = getpwuid(geteuid());
1393 if (entry) {
1394 user_buf = xstrdup(entry->pw_name);
1395 home_pwd_buf = xstrdup(entry->pw_dir);
1396 }
1397 }
1398#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001399 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001400 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001401
Erik Andersenc7c634b2000-03-19 05:28:55 +00001402 while (1) {
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001403 fflush(NULL);
Mark Whitley4e338752001-01-26 20:42:23 +00001404
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001405 if (nonblock_safe_read(STDIN_FILENO, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001406 /* if we can't read input then exit */
1407 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001408 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001409
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001410 ic = c;
1411
Denis Vlasenko38f63192007-01-22 09:03:07 +00001412#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001413 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001414 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001415 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001416#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001417 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001418 case '\n':
1419 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001420 vi_case('\n'|vbit:)
1421 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001422 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001423 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001424 break_out = 1;
1425 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001426 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001427 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001428 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001429 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001430 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001431 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001432 vi_case('h'|vbit:)
1433 vi_case('\b'|vbit:)
1434 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001435 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001436 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001437 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001438 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001439 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001440 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001441 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001442 command_len = 0;
1443 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001444 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001445 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001446 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001447 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001448 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001449 errno = 0;
1450 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001451 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001452 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001453 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001454 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001455 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001456 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001457
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001458 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001459 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001460 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001461 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001462 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001463 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001464 vi_case('l'|vbit:)
1465 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001466 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001467 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001468 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001469
Erik Andersenf0657d32000-04-12 17:49:52 +00001470 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001471 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001472 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001473 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001474 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001475
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001476#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001477 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001478 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001479 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001480#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001481
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001482 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001483 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001484 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001485 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001486 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001487 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001488 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001489 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001490 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001491 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001492 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001493 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001494
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001495#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001496 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001497 vi_case(CTRL('N')|vbit:)
1498 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001499 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001500 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001501 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001502 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001503 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001504 vi_case(CTRL('P')|vbit:)
1505 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001506 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001507 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001508 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001509 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001510 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001511 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001512 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001513#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001514
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001515 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001516 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001517 /* Control-U -- Clear line before cursor */
1518 if (cursor) {
1519 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001520 command_len -= cursor;
1521 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001522 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001523 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001524 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001525 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001526 /* Control-W -- Remove the last word */
1527 while (cursor > 0 && isspace(command[cursor-1]))
1528 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001529 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001530 input_backspace();
1531 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001532
Denis Vlasenko38f63192007-01-22 09:03:07 +00001533#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001534 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001535 vi_cmdmode = 0;
1536 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001537 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001538 input_backward(cursor);
1539 vi_cmdmode = 0;
1540 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001541 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001542 input_forward();
1543 vi_cmdmode = 0;
1544 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001545 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001546 input_end();
1547 vi_cmdmode = 0;
1548 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001549 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001550 input_delete(1);
1551 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001552 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001553 if (cursor > 0) {
1554 input_backward(1);
1555 input_delete(1);
1556 }
1557 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001558 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001559 vi_Word_motion(command, 1);
1560 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001561 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001562 vi_word_motion(command, 1);
1563 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001564 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001565 vi_End_motion(command);
1566 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001567 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001568 vi_end_motion(command);
1569 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001570 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001571 vi_Back_motion(command);
1572 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001573 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001574 vi_back_motion(command);
1575 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001576 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001577 vi_cmdmode = 0;
1578 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001579 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001580 goto clear_to_eol;
1581
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001582 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001583 vi_cmdmode = 0;
1584 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001585 case 'd'|vbit: {
1586 int nc, sc;
1587 sc = cursor;
1588 prevc = ic;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001589 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001590 goto prepare_to_die;
1591 if (c == (prevc & 0xff)) {
1592 /* "cc", "dd" */
1593 input_backward(cursor);
1594 goto clear_to_eol;
1595 break;
1596 }
1597 switch (c) {
1598 case 'w':
1599 case 'W':
1600 case 'e':
1601 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001602 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001603 case 'w': /* "dw", "cw" */
1604 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001605 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001606 case 'W': /* 'dW', 'cW' */
1607 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001608 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001609 case 'e': /* 'de', 'ce' */
1610 vi_end_motion(command);
1611 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001612 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001613 case 'E': /* 'dE', 'cE' */
1614 vi_End_motion(command);
1615 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001616 break;
1617 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001618 nc = cursor;
1619 input_backward(cursor - sc);
1620 while (nc-- > cursor)
1621 input_delete(1);
1622 break;
1623 case 'b': /* "db", "cb" */
1624 case 'B': /* implemented as B */
1625 if (c == 'b')
1626 vi_back_motion(command);
1627 else
1628 vi_Back_motion(command);
1629 while (sc-- > cursor)
1630 input_delete(1);
1631 break;
1632 case ' ': /* "d ", "c " */
1633 input_delete(1);
1634 break;
1635 case '$': /* "d$", "c$" */
1636 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001637 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001638 input_delete(1);
1639 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001640 }
1641 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001642 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001643 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001644 input_forward();
1645 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001646 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001647 put();
1648 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001649 case 'r'|vbit:
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001650 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001651 goto prepare_to_die;
1652 if (c == 0)
1653 beep();
1654 else {
1655 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001656 bb_putchar(c);
1657 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001658 }
1659 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001660#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001661
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001662 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001663
Denis Vlasenko38f63192007-01-22 09:03:07 +00001664#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001665 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001666 /* ESC: insert mode --> command mode */
1667 vi_cmdmode = 1;
1668 input_backward(1);
1669 break;
1670 }
1671#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001672 /* escape sequence follows */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001673 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001674 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001675 /* different vt100 emulations */
1676 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001677 vi_case('['|vbit:)
1678 vi_case('O'|vbit:)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001679 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001680 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001681 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001682 if (c >= '1' && c <= '9') {
1683 unsigned char dummy;
1684
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001685 if (safe_read(STDIN_FILENO, &dummy, 1) < 1)
Glenn L McGrath475820c2004-01-22 12:42:23 +00001686 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001687 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001688 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001689 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001690
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001691 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001692#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001693 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001694 input_tab(&lastWasTab);
1695 break;
1696#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001697#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001698 case 'A':
1699 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001700 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001701 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001702 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001703 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001704 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001705 break;
1706 case 'B':
1707 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001708 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001709 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001710 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001711 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001712 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001713 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001714 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001715 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001716 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001717#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001718 case 'C':
1719 /* Right Arrow -- Move forward one character */
1720 input_forward();
1721 break;
1722 case 'D':
1723 /* Left Arrow -- Move back one character */
1724 input_backward(1);
1725 break;
1726 case '3':
1727 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001728 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001729 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001730 case '1': // vt100? linux vt? or what?
1731 case '7': // vt100? linux vt? or what?
1732 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001733 input_backward(cursor);
1734 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001735 case '4': // vt100? linux vt? or what?
1736 case '8': // vt100? linux vt? or what?
1737 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001738 input_end();
1739 break;
1740 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001741 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001742 beep();
1743 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001744 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001745
Eric Andersenc470f442003-07-28 09:56:35 +00001746 default: /* If it's regular input, do the normal thing */
Paul Fox84bbac52008-01-11 16:50:08 +00001747
1748 /* Control-V -- force insert of next char */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001749 if (c == CTRL('V')) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001750 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001751 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001752 if (c == 0) {
1753 beep();
1754 break;
1755 }
Paul Fox84bbac52008-01-11 16:50:08 +00001756 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001757
Denis Vlasenko38f63192007-01-22 09:03:07 +00001758#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001759 if (vi_cmdmode) /* Don't self-insert */
1760 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001761#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001762 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001763 break;
1764
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001765 command_len++;
1766 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001767 command[cursor] = c;
1768 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001769 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001770 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001771 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001772
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001773 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001774 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001775 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001776 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001777 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001778 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001779 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001780 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001781 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001782 }
Eric Andersenc470f442003-07-28 09:56:35 +00001783 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001784 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001785
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001786#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001787 if (c != '\t')
1788 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001789#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001790 }
1791
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001792 if (command_len > 0)
1793 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001794
Eric Andersen27bb7902003-12-23 20:24:51 +00001795 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001796 command[command_len++] = '\n';
1797 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001798 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001799
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001800#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001801 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001802#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001803
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001804 /* restore initial_settings */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001805 tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001806 /* restore SIGWINCH handler */
1807 signal(SIGWINCH, previous_SIGWINCH_handler);
1808 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001809
1810 DEINIT_S();
1811
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001812 return command_len;
1813}
1814
1815line_input_t *new_line_input_t(int flags)
1816{
1817 line_input_t *n = xzalloc(sizeof(*n));
1818 n->flags = flags;
1819 return n;
1820}
1821
1822#else
1823
1824#undef read_line_input
1825int read_line_input(const char* prompt, char* command, int maxsize)
1826{
1827 fputs(prompt, stdout);
1828 fflush(stdout);
1829 fgets(command, maxsize, stdin);
1830 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001831}
1832
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001833#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001834
1835
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001836/*
1837 * Testing
1838 */
1839
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001840#ifdef TEST
1841
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001842#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001843
1844const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001845
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001846int main(int argc, char **argv)
1847{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001848 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001849 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001850#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001851 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1852 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1853 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001854#else
1855 "% ";
1856#endif
1857
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001858#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001859 setlocale(LC_ALL, "");
1860#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001861 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001862 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001863 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001864 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001865 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001866 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001867 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001868 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001869 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001870 return 0;
1871}
1872
Eric Andersenc470f442003-07-28 09:56:35 +00001873#endif /* TEST */