blob: c6aa45c93f32479b5b0f77f2fc06de481c950c68 [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)); \
149 cmdedit_termw = 80; \
150 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
151 USE_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \
152} while (0)
153static void deinit_S(void)
154{
155#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000156 /* This one is allocated only if FANCY_PROMPT is on
157 * (otherwise it points to verbatim prompt (NOT malloced) */
158 free((char*)cmdedit_prompt);
159#endif
160#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
161 free(user_buf);
162 if (home_pwd_buf != null_str)
163 free(home_pwd_buf);
164#endif
165 free(ptr_to_statics);
166}
167#define DEINIT_S() deinit_S()
168
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000169/* Put 'command_ps[cursor]', cursor++.
170 * Advance cursor on screen. If we reached right margin, scroll text up
171 * and remove terminal margin effect by printing 'next_char' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000172static void cmdedit_set_out_char(int next_char)
173{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000174 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000175
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000176 if (c == '\0') {
177 /* erase character after end of input string */
178 c = ' ';
179 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000180#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000181 /* Display non-printable characters in reverse */
182 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000183 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000184 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000185 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000186 c += '@';
187 if (c == 127)
188 c = '?';
189 printf("\033[7m%c\033[0m", c);
190 } else
191#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000192 {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000193 bb_putchar(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000194 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000195 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000196 /* terminal is scrolled down */
197 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000198 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000199 /* destroy "(auto)margin" */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000200 bb_putchar(next_char);
201 bb_putchar('\b');
Mark Whitley4e338752001-01-26 20:42:23 +0000202 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000203// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000204 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000205}
206
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000207/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000208static void input_end(void)
209{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000210 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000211 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000212}
213
214/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000215static void goto_new_line(void)
216{
Mark Whitley4e338752001-01-26 20:42:23 +0000217 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000218 if (cmdedit_x)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000219 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000220}
221
222
Rob Landley88621d72006-08-29 19:41:06 +0000223static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000224{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000225 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000226 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000227}
Eric Andersen81fe1232003-07-29 06:38:40 +0000228
Rob Landley88621d72006-08-29 19:41:06 +0000229static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000230{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000231 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000232}
233
Eric Andersenaff114c2004-04-14 17:51:38 +0000234/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000235/* (optimized for slow terminals) */
236static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000237{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000238 int count_y;
239
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000240 if (num > cursor)
241 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000242 if (!num)
243 return;
244 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000245
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000246 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000247 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000248 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000249 /* This is longer by 5 bytes on x86.
250 * Also gets mysteriously
251 * miscompiled for some ARM users.
252 * printf(("\b\b\b\b" + 4) - num);
253 * return;
254 */
255 do {
256 bb_putchar('\b');
257 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000258 return;
259 }
260 printf("\033[%uD", num);
261 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000262 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000263
264 /* Need to go one or more lines up */
265 num -= cmdedit_x;
266 count_y = 1 + (num / cmdedit_termw);
267 cmdedit_y -= count_y;
268 cmdedit_x = cmdedit_termw * count_y - num;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000269 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000270 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000271}
272
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000273static void put_prompt(void)
274{
275 out1str(cmdedit_prompt);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000276 cmdedit_x = cmdedit_prmt_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000277 cursor = 0;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000278// Huh? what if cmdedit_prmt_len >= width?
Eric Andersen7467c8d2001-07-12 20:26:32 +0000279 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000280}
281
Eric Andersenaff114c2004-04-14 17:51:38 +0000282/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000283static void redraw(int y, int back_cursor)
284{
Eric Andersenc470f442003-07-28 09:56:35 +0000285 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000286 printf("\033[%dA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000287 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000288 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000289 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000290 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000291 input_backward(back_cursor);
292}
293
Paul Fox3f11b1b2005-08-04 19:04:46 +0000294/* Delete the char in front of the cursor, optionally saving it
295 * for later putback */
296static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000297{
Mark Whitley4e338752001-01-26 20:42:23 +0000298 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000299
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000300 if (j == command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000301 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000302
Denis Vlasenko38f63192007-01-22 09:03:07 +0000303#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000304 if (save) {
305 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000306 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000307 newdelflag = 0;
308 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000309 if ((delptr - delbuf) < DELBUFSIZ)
310 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000311 }
312#endif
313
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000314 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000315 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000316 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000317 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000318 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000319}
320
Denis Vlasenko38f63192007-01-22 09:03:07 +0000321#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000322static void put(void)
323{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000324 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000325 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000326
Paul Fox3f11b1b2005-08-04 19:04:46 +0000327 if (j == 0)
328 return;
329 ocursor = cursor;
330 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000331 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000332 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000333 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000334 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000335 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000336}
337#endif
338
Mark Whitley4e338752001-01-26 20:42:23 +0000339/* Delete the char in back of the cursor */
340static void input_backspace(void)
341{
342 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000343 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000344 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000345 }
346}
347
Eric Andersenaff114c2004-04-14 17:51:38 +0000348/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000349static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000350{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000351 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000352 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000353}
354
Denis Vlasenko38f63192007-01-22 09:03:07 +0000355#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000356
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000357static void free_tab_completion_data(void)
358{
359 if (matches) {
360 while (num_matches)
361 free(matches[--num_matches]);
362 free(matches);
363 matches = NULL;
364 }
365}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000366
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000367static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000368{
369 int nm = num_matches;
370 int nm1 = nm + 1;
371
372 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000373 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000374 num_matches++;
375}
376
Denis Vlasenko38f63192007-01-22 09:03:07 +0000377#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000378static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000379{
380 struct passwd *entry;
381 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000382
Eric Andersenc470f442003-07-28 09:56:35 +0000383 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000384 userlen = strlen(ud);
385
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000386 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000387 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000388 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000389
Eric Andersenc470f442003-07-28 09:56:35 +0000390 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000391 home = home_pwd_buf;
392 } else {
393 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000394 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000395 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000396 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000397 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000398 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000399 ud = temp;
400 if (entry)
401 home = entry->pw_dir;
402 }
403 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000404 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000405 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000406 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000407 }
408 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000409 } else {
410 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000411 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000412 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000413 struct passwd pwd;
414 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000415
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000416 setpwent();
417 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000418 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000419 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
420 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000421 }
422 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000423 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000424 }
425}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000426#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000427
428enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000429 FIND_EXE_ONLY = 0,
430 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000431 FIND_FILE_ONLY = 2,
432};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000433
Mark Whitley4e338752001-01-26 20:42:23 +0000434static int path_parse(char ***p, int flags)
435{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000436 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000437 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000438 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000439 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000440
Mark Whitley4e338752001-01-26 20:42:23 +0000441 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000442 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000443 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000444
445 if (state->flags & WITH_PATH_LOOKUP)
446 pth = state->path_lookup;
447 else
448 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000449 /* PATH=<empty> or PATH=:<empty> */
450 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
451 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000452
Denis Vlasenko253ce002007-01-22 08:34:44 +0000453 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000454 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000455 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000456 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000457 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000458 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000459 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000460 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000461 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000462 }
463
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000464 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000465 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000466 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000467 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000468 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000469 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000470 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000471 *tmp++ = '\0'; /* ':' -> '\0' */
472 if (*tmp == '\0')
473 break; /* :<empty> */
474 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000475 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000476 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000477 return npth;
478}
479
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000480static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000481{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000482 DIR *dir;
483 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000484 struct stat st;
485 char *path1[1];
486 char **paths = path1;
487 int npaths;
488 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000489 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000490 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000491/* char dirbuf[MAX_LINELEN]; */
492#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000493
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000494 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000495 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000496
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000497 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000498 /* no dir, if flags==EXE_ONLY - get paths, else "." */
499 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000500 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000501 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000502 /* dirbuf = ".../.../.../" */
503 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000504#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000505 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000506 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000507#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000508 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000509 /* point to 'l' in "..../last_component" */
510 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000511 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000512
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000513 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000514 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000515 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000516 continue;
517
518 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000519 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000520 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000521
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000522 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000523 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000524 continue;
525 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000526 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000527 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000528 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000529 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000530 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000531 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000532 /* hmm, remover in progress? */
Denis Vlasenko39487e22008-02-14 19:55:58 +0000533 if (lstat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000534 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000535 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000536 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000537 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000538
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000539 len1 = strlen(found);
540 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000541 found[len1] = '\0';
542 found[len1+1] = '\0';
543
Mark Whitley4e338752001-01-26 20:42:23 +0000544 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000545 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000546 if (found[len1-1] != '/') {
547 found[len1] = '/';
548 }
Mark Whitley4e338752001-01-26 20:42:23 +0000549 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000550 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000551 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000552 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000553 }
Mark Whitley4e338752001-01-26 20:42:23 +0000554 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000555 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000556 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000557 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000558 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000559 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000560 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000561 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000562 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000563 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000564 free(paths);
565 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000566#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000567}
Erik Andersenf0657d32000-04-12 17:49:52 +0000568
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000569#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000570
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000571#define collapse_pos(is, in) do { \
572 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
573 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
574} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000575
576static int find_match(char *matchBuf, int *len_with_quotes)
577{
578 int i, j;
579 int command_mode;
580 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000581/* int16_t int_buf[MAX_LINELEN + 1]; */
582/* int16_t pos_buf[MAX_LINELEN + 1]; */
583#define int_buf (S.find_match__int_buf)
584#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000585
586 /* set to integer dimension characters and own positions */
587 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000588 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000589 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000590 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000591 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000592 }
593 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000594 }
595
596 /* mask \+symbol and convert '\t' to ' ' */
597 for (i = j = 0; matchBuf[i]; i++, j++)
598 if (matchBuf[i] == '\\') {
599 collapse_pos(j, j + 1);
600 int_buf[j] |= QUOT;
601 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000602#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000603 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000604 int_buf[j] = ' ' | QUOT;
605#endif
606 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000607#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000608 else if (matchBuf[i] == '\t')
609 int_buf[j] = ' ';
610#endif
611
612 /* mask "symbols" or 'symbols' */
613 c2 = 0;
614 for (i = 0; int_buf[i]; i++) {
615 c = int_buf[i];
616 if (c == '\'' || c == '"') {
617 if (c2 == 0)
618 c2 = c;
619 else {
620 if (c == c2)
621 c2 = 0;
622 else
623 int_buf[i] |= QUOT;
624 }
625 } else if (c2 != 0 && c != '$')
626 int_buf[i] |= QUOT;
627 }
628
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000629 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000630 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
631 for (i = 0; int_buf[i]; i++) {
632 c = int_buf[i];
633 c2 = int_buf[i + 1];
634 j = i ? int_buf[i - 1] : -1;
635 command_mode = 0;
636 if (c == ';' || c == '&' || c == '|') {
637 command_mode = 1 + (c == c2);
638 if (c == '&') {
639 if (j == '>' || j == '<')
640 command_mode = 0;
641 } else if (c == '|' && j == '>')
642 command_mode = 0;
643 }
644 if (command_mode) {
645 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000646 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000647 }
648 }
649 /* collapse `command...` */
650 for (i = 0; int_buf[i]; i++)
651 if (int_buf[i] == '`') {
652 for (j = i + 1; int_buf[j]; j++)
653 if (int_buf[j] == '`') {
654 collapse_pos(i, j + 1);
655 j = 0;
656 break;
657 }
658 if (j) {
659 /* not found close ` - command mode, collapse all previous */
660 collapse_pos(0, i + 1);
661 break;
662 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000663 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000664 }
665
666 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000667 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000668 c2 = 0;
669 for (i = 0; int_buf[i]; i++)
670 if (int_buf[i] == '(' || int_buf[i] == '{') {
671 if (int_buf[i] == '(')
672 c++;
673 else
674 c2++;
675 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000676 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000677 }
678 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
679 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
680 if (int_buf[i] == ')')
681 c--;
682 else
683 c2--;
684 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000685 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000686 }
687
688 /* skip first not quote space */
689 for (i = 0; int_buf[i]; i++)
690 if (int_buf[i] != ' ')
691 break;
692 if (i)
693 collapse_pos(0, i);
694
695 /* set find mode for completion */
696 command_mode = FIND_EXE_ONLY;
697 for (i = 0; int_buf[i]; i++)
698 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
699 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000700 && matchBuf[pos_buf[0]] == 'c'
701 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000702 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000703 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000704 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000705 command_mode = FIND_FILE_ONLY;
706 break;
707 }
708 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000709 for (i = 0; int_buf[i]; i++)
710 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000711 /* find last word */
712 for (--i; i >= 0; i--) {
713 c = int_buf[i];
714 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
715 collapse_pos(0, i + 1);
716 break;
717 }
718 }
719 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000720 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
721 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000722 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000723 while ((int_buf[i] & ~QUOT) == '/'
724 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
725 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000726 i++;
727 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000728
729 /* set only match and destroy quotes */
730 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000731 for (c = 0; pos_buf[i] >= 0; i++) {
732 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000733 j = pos_buf[i] + 1;
734 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000735 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000736 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000737 *len_with_quotes = j ? j - pos_buf[0] : 0;
738
739 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000740#undef int_buf
741#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000742}
743
Glenn L McGrath4d001292003-01-06 01:11:50 +0000744/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000745 * display by column (original idea from ls applet,
746 * very optimized by me :)
747 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000748static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000749{
750 int ncols, row;
751 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000752 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000753 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000754 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000755
756 /* find the longest file name- use that as the column width */
757 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000758 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000759 if (column_width < l)
760 column_width = l;
761 }
762 column_width += 2; /* min space for columns */
763 ncols = cmdedit_termw / column_width;
764
765 if (ncols > 1) {
766 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000767 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000768 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000769 } else {
770 ncols = 1;
771 }
772 for (row = 0; row < nrows; row++) {
773 int n = row;
774 int nc;
775
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000776 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000777 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000778 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000779 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000780 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000781 }
782}
783
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000784static char *add_quote_for_spec_chars(char *found)
785{
786 int l = 0;
787 char *s = xmalloc((strlen(found) + 1) * 2);
788
789 while (*found) {
790 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
791 s[l++] = '\\';
792 s[l++] = *found++;
793 }
794 s[l] = 0;
795 return s;
796}
797
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000798static int match_compare(const void *a, const void *b)
799{
800 return strcmp(*(char**)a, *(char**)b);
801}
802
803/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000804static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000805{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000806 if (!(state->flags & TAB_COMPLETION))
807 return;
808
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000809 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000810 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000811 int len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000812/* char matchBuf[MAX_LINELEN]; */
813#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000814 int find_type;
815 int recalc_pos;
816
Eric Andersenc470f442003-07-28 09:56:35 +0000817 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000818
819 /* Make a local copy of the string -- up
820 * to the position of the cursor */
821 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000822 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000823
824 find_type = find_match(matchBuf, &recalc_pos);
825
826 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000827 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000828
Denis Vlasenko38f63192007-01-22 09:03:07 +0000829#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000830 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000831 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000832 if (state->flags & USERNAME_COMPLETION)
833 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
834 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000835#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000836 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000837 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000838 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000839 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000840 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000841 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000842 int i, n = 0;
843 qsort(matches, num_matches, sizeof(char*), match_compare);
844 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000845 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000846 if (strcmp(matches[i], matches[i+1]) == 0) {
847 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000848 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000849 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000850 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000851 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000852 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000853 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000854 matches[n] = matches[i];
855 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000856 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000857 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000858 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000859 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000860 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000861 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000862 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000863 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000864 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000865 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000866 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000867 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000868 break;
869 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000870 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000871 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000872 return;
873 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000874 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000875 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000876 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000877 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000878 /* for next completion current found */
879 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000880
881 len_found = strlen(tmp);
882 if (tmp[len_found-1] != '/') {
883 tmp[len_found] = ' ';
884 tmp[len_found+1] = '\0';
885 }
Mark Whitley4e338752001-01-26 20:42:23 +0000886 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000887 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000888 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000889 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000890 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000891 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000892 /* save tail line */
893 strcpy(matchBuf, command_ps + cursor);
894 /* add match */
895 strcat(command_ps, tmp);
896 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000897 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000898 /* back to begin word for match */
899 input_backward(recalc_pos);
900 /* new pos */
901 recalc_pos = cursor + len_found;
902 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000903 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000904 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000905 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000906 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000907 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000908#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000909 } else {
910 /* Ok -- the last char was a TAB. Since they
911 * just hit TAB again, print a list of all the
912 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000913 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000914 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000915
916 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000917 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000918 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000919 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000920 }
921 }
922}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000923
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000924#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000925
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000926
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000927#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000928
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000929/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000930static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000931{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000932 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
933 free(state->history[state->cur_history]);
934 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000935 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000936 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000937}
938
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000939static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000940{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000941 if (state->flags & DO_HISTORY) {
942 int ch = state->cur_history;
943 if (ch < state->cnt_history) {
944 get_previous_history(); /* save the current history line */
945 state->cur_history = ch + 1;
946 return state->cur_history;
947 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000948 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000949 beep();
950 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000951}
Robert Griebl350d26b2002-12-03 22:45:46 +0000952
Denis Vlasenko38f63192007-01-22 09:03:07 +0000953#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000954/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000955static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000956{
Robert Griebl350d26b2002-12-03 22:45:46 +0000957 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000958 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000959
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000960 /* cleanup old */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000961 for (hi = state->cnt_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000962 hi--;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000963 free(state->history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000964 }
965
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000966 fp = fopen(fromfile, "r");
967 if (fp) {
968 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000969 char *hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000970 int l;
971
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000972 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000973 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000974 l = strlen(hl);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000975 if (l >= MAX_LINELEN)
976 hl[MAX_LINELEN-1] = '\0';
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000977 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000978 free(hl);
979 continue;
980 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000981 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000982 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000983 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000984 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000985 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000986}
987
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000988/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000989static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000990{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000991 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +0000992
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000993 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000994 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000995 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000996
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000997 for (i = 0; i < state->cnt_history; i++) {
998 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000999 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001000 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001001 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001002}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001003#else
1004#define load_history(a) ((void)0)
1005#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001006#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001007
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001008static void remember_in_history(const char *str)
1009{
1010 int i;
1011
1012 if (!(state->flags & DO_HISTORY))
1013 return;
1014
1015 i = state->cnt_history;
1016 free(state->history[MAX_HISTORY]);
1017 state->history[MAX_HISTORY] = NULL;
1018 /* After max history, remove the oldest command */
1019 if (i >= MAX_HISTORY) {
1020 free(state->history[0]);
1021 for (i = 0; i < MAX_HISTORY-1; i++)
1022 state->history[i] = state->history[i+1];
1023 }
1024// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
1025// (i.e. do not save dups?)
1026 state->history[i++] = xstrdup(str);
1027 state->cur_history = i;
1028 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001029#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001030 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001031 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +00001032#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00001033 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001034}
1035
1036#else /* MAX_HISTORY == 0 */
1037#define remember_in_history(a) ((void)0)
1038#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001039
1040
Erik Andersen6273f652000-03-17 01:12:41 +00001041/*
1042 * This function is used to grab a character buffer
1043 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001044 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001045 * a mini readline).
1046 *
1047 * The following standard commands are not implemented:
1048 * ESC-b -- Move back one word
1049 * ESC-f -- Move forward one word
1050 * ESC-d -- Delete back one word
1051 * ESC-h -- Delete forward one word
1052 * CTL-t -- Transpose two characters
1053 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001054 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001055 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001056 */
Eric Andersenc470f442003-07-28 09:56:35 +00001057
Denis Vlasenko38f63192007-01-22 09:03:07 +00001058#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001059static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001060vi_Word_motion(char *command, int eat)
1061{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001062 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001063 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001064 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001065 input_forward();
1066}
1067
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001068static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001069vi_word_motion(char *command, int eat)
1070{
1071 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001072 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001073 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001074 input_forward();
1075 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001076 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001077 input_forward();
1078 }
1079
Denis Vlasenko253ce002007-01-22 08:34:44 +00001080 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001081 input_forward();
1082
Denis Vlasenko253ce002007-01-22 08:34:44 +00001083 if (eat && cursor < command_len && isspace(command[cursor]))
1084 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001085 input_forward();
1086}
1087
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001088static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001089vi_End_motion(char *command)
1090{
1091 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001092 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001093 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001094 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001095 input_forward();
1096}
1097
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001098static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001099vi_end_motion(char *command)
1100{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001101 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001102 return;
1103 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001104 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001105 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001106 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001107 return;
1108 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001109 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001110 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1111 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001112 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001113 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001114 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001115 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001116 input_forward();
1117 }
1118}
1119
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001120static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001121vi_Back_motion(char *command)
1122{
1123 while (cursor > 0 && isspace(command[cursor-1]))
1124 input_backward(1);
1125 while (cursor > 0 && !isspace(command[cursor-1]))
1126 input_backward(1);
1127}
1128
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001129static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001130vi_back_motion(char *command)
1131{
1132 if (cursor <= 0)
1133 return;
1134 input_backward(1);
1135 while (cursor > 0 && isspace(command[cursor]))
1136 input_backward(1);
1137 if (cursor <= 0)
1138 return;
1139 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001140 while (cursor > 0
1141 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1142 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001143 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001144 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001145 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001146 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001147 input_backward(1);
1148 }
1149}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001150#endif
1151
1152
1153/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001154 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001155 */
1156
Denis Vlasenko38f63192007-01-22 09:03:07 +00001157#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001158static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001159{
1160 cmdedit_prompt = prmt_ptr;
1161 cmdedit_prmt_len = strlen(prmt_ptr);
1162 put_prompt();
1163}
1164#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001165static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001166{
1167 int prmt_len = 0;
1168 size_t cur_prmt_len = 0;
1169 char flg_not_length = '[';
1170 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001171 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1172 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001173 char c;
1174 char *pbuf;
1175
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001176 cmdedit_prmt_len = 0;
1177
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001178 if (!cwd_buf) {
1179 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001180 }
1181
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001182 cbuf[1] = '\0'; /* never changes */
1183
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001184 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001185 char *free_me = NULL;
1186
1187 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001188 c = *prmt_ptr++;
1189 if (c == '\\') {
1190 const char *cp = prmt_ptr;
1191 int l;
1192
1193 c = bb_process_escape_sequence(&prmt_ptr);
1194 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001195 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001196 break;
1197 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001198
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001199 switch (c) {
1200#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1201 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001202 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001203 break;
1204#endif
1205 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001206 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001207 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001208 break;
1209 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001210 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001211 break;
1212#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1213 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001214 /* /home/user[/something] -> ~[/something] */
1215 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001216 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001217 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001218 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1219 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1220 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001221 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001222 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001223 }
1224 break;
1225#endif
1226 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001227 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001228 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001229 if (cp != NULL && cp != pbuf)
1230 pbuf += (cp-pbuf) + 1;
1231 break;
1232 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001233 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001234 break;
1235 case 'e': case 'E': /* \e \E = \033 */
1236 c = '\033';
1237 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001238 case 'x': case 'X': {
1239 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001240 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001241 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001242 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001243 buf2[l] = '\0';
1244 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001245 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001246 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001247 break;
1248 }
1249 prmt_ptr++;
1250 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001251 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001252 if (c == 0)
1253 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001254 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001255 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001256 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001257 case '[': case ']':
1258 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001259 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001260 continue;
1261 }
1262 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001263 } /* switch */
1264 } /* if */
1265 } /* if */
1266 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001267 cur_prmt_len = strlen(pbuf);
1268 prmt_len += cur_prmt_len;
1269 if (flg_not_length != ']')
1270 cmdedit_prmt_len += cur_prmt_len;
1271 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001272 free(free_me);
1273 } /* while */
1274
1275 if (cwd_buf != (char *)bb_msg_unknown)
1276 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001277 cmdedit_prompt = prmt_mem_ptr;
1278 put_prompt();
1279}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001280#endif
1281
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001282static void cmdedit_setwidth(unsigned w, int redraw_flg)
1283{
1284 cmdedit_termw = w;
1285 if (redraw_flg) {
1286 /* new y for current cursor */
1287 int new_y = (cursor + cmdedit_prmt_len) / w;
1288 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001289 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001290 fflush(stdout);
1291 }
1292}
1293
1294static void win_changed(int nsig)
1295{
1296 int width;
1297 get_terminal_width_height(0, &width, NULL);
1298 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1299 if (nsig == SIGWINCH)
1300 signal(SIGWINCH, win_changed); /* rearm ourself */
1301}
1302
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001303/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001304 * The emacs and vi modes share much of the code in the big
1305 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001306 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001307 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001308 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001309 */
1310
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001311#define vbit 0x100
1312
1313/* leave out the "vi-mode"-only case labels if vi editing isn't
1314 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001315#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001316
1317/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001318#undef CTRL
1319#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001320
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001321/* Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001322 * -1 on read errors or EOF, or on bare Ctrl-D,
1323 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001324 * >0 length of input string, including terminating '\n'
1325 */
Denis Vlasenko037576d2007-10-20 18:30:38 +00001326int read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001327{
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001328#if ENABLE_FEATURE_TAB_COMPLETION
1329 smallint lastWasTab = FALSE;
1330#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001331 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001332 unsigned char c;
1333 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001334#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001335 smallint vi_cmdmode = 0;
1336 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001337#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001338 struct termios initial_settings;
1339 struct termios new_settings;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001340
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001341 INIT_S();
1342
1343 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1344 || !(initial_settings.c_lflag & ECHO)
1345 ) {
1346 /* Happens when e.g. stty -echo was run before */
1347 int len;
1348 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001349 fflush(stdout);
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001350 if (fgets(command, maxsize, stdin) == NULL)
1351 len = -1; /* EOF or error */
1352 else
1353 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001354 DEINIT_S();
1355 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001356 }
1357
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001358// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001359 if (maxsize > MAX_LINELEN)
1360 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001361
1362 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001363 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001364#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001365 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001366 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001367#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001368
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001369 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001370 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001371 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001372 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001373 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001374
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001375 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001376 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1377 /* Turn off echoing and CTRL-C, so we can trap it */
1378 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001379 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001380 new_settings.c_cc[VMIN] = 1;
1381 new_settings.c_cc[VTIME] = 0;
1382 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001383#ifndef _POSIX_VDISABLE
1384#define _POSIX_VDISABLE '\0'
1385#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001386 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001387 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001388
Eric Andersen6faae7d2001-02-16 20:09:17 +00001389 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001390 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1391 win_changed(0); /* do initial resizing */
1392#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1393 {
1394 struct passwd *entry;
1395
1396 entry = getpwuid(geteuid());
1397 if (entry) {
1398 user_buf = xstrdup(entry->pw_name);
1399 home_pwd_buf = xstrdup(entry->pw_dir);
1400 }
1401 }
1402#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001403 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001404 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001405
Erik Andersenc7c634b2000-03-19 05:28:55 +00001406 while (1) {
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001407 fflush(NULL);
Mark Whitley4e338752001-01-26 20:42:23 +00001408
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001409 if (nonblock_safe_read(STDIN_FILENO, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001410 /* if we can't read input then exit */
1411 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001412 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001413
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001414 ic = c;
1415
Denis Vlasenko38f63192007-01-22 09:03:07 +00001416#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001417 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001418 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001419 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001420#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001421 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001422 case '\n':
1423 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001424 vi_case('\n'|vbit:)
1425 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001426 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001427 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001428 break_out = 1;
1429 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001430 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001431 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001432 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001433 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001434 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001435 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001436 vi_case('h'|vbit:)
1437 vi_case('\b'|vbit:)
1438 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001439 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001440 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001441 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001442 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001443 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001444 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001445 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001446 command_len = 0;
1447 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001448 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001449 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001450 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001451 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001452 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001453 errno = 0;
1454 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001455 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001456 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001457 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001458 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001459 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001460 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001461
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001462 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001463 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001464 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001465 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001466 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001467 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001468 vi_case('l'|vbit:)
1469 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001470 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001471 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001472 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001473
Erik Andersenf0657d32000-04-12 17:49:52 +00001474 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001475 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001476 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001477 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001478 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001479
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001480#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001481 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001482 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001483 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001484#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001485
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001486 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001487 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001488 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001489 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001490 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001491 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001492 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001493 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001494 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001495 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001496 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001497 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001498
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001499#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001500 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001501 vi_case(CTRL('N')|vbit:)
1502 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001503 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001504 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001505 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001506 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001507 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001508 vi_case(CTRL('P')|vbit:)
1509 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001510 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001511 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001512 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001513 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001514 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001515 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001516 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001517#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001518
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001519 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001520 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001521 /* Control-U -- Clear line before cursor */
1522 if (cursor) {
1523 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001524 command_len -= cursor;
1525 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001526 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001527 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001528 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001529 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001530 /* Control-W -- Remove the last word */
1531 while (cursor > 0 && isspace(command[cursor-1]))
1532 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001533 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001534 input_backspace();
1535 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001536
Denis Vlasenko38f63192007-01-22 09:03:07 +00001537#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001538 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001539 vi_cmdmode = 0;
1540 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001541 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001542 input_backward(cursor);
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_forward();
1547 vi_cmdmode = 0;
1548 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001549 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001550 input_end();
1551 vi_cmdmode = 0;
1552 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001553 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001554 input_delete(1);
1555 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001556 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001557 if (cursor > 0) {
1558 input_backward(1);
1559 input_delete(1);
1560 }
1561 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001562 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001563 vi_Word_motion(command, 1);
1564 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001565 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001566 vi_word_motion(command, 1);
1567 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001568 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001569 vi_End_motion(command);
1570 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001571 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001572 vi_end_motion(command);
1573 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001574 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001575 vi_Back_motion(command);
1576 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001577 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001578 vi_back_motion(command);
1579 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001580 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001581 vi_cmdmode = 0;
1582 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001583 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001584 goto clear_to_eol;
1585
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001586 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001587 vi_cmdmode = 0;
1588 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001589 case 'd'|vbit: {
1590 int nc, sc;
1591 sc = cursor;
1592 prevc = ic;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001593 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001594 goto prepare_to_die;
1595 if (c == (prevc & 0xff)) {
1596 /* "cc", "dd" */
1597 input_backward(cursor);
1598 goto clear_to_eol;
1599 break;
1600 }
1601 switch (c) {
1602 case 'w':
1603 case 'W':
1604 case 'e':
1605 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001606 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001607 case 'w': /* "dw", "cw" */
1608 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001609 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001610 case 'W': /* 'dW', 'cW' */
1611 vi_Word_motion(command, vi_cmdmode);
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;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001617 case 'E': /* 'dE', 'cE' */
1618 vi_End_motion(command);
1619 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001620 break;
1621 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001622 nc = cursor;
1623 input_backward(cursor - sc);
1624 while (nc-- > cursor)
1625 input_delete(1);
1626 break;
1627 case 'b': /* "db", "cb" */
1628 case 'B': /* implemented as B */
1629 if (c == 'b')
1630 vi_back_motion(command);
1631 else
1632 vi_Back_motion(command);
1633 while (sc-- > cursor)
1634 input_delete(1);
1635 break;
1636 case ' ': /* "d ", "c " */
1637 input_delete(1);
1638 break;
1639 case '$': /* "d$", "c$" */
1640 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001641 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001642 input_delete(1);
1643 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001644 }
1645 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001646 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001647 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001648 input_forward();
1649 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001650 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001651 put();
1652 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001653 case 'r'|vbit:
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001654 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001655 goto prepare_to_die;
1656 if (c == 0)
1657 beep();
1658 else {
1659 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001660 bb_putchar(c);
1661 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001662 }
1663 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001664#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001665
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001666 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001667
Denis Vlasenko38f63192007-01-22 09:03:07 +00001668#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001669 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001670 /* ESC: insert mode --> command mode */
1671 vi_cmdmode = 1;
1672 input_backward(1);
1673 break;
1674 }
1675#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001676 /* escape sequence follows */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001677 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001678 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001679 /* different vt100 emulations */
1680 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001681 vi_case('['|vbit:)
1682 vi_case('O'|vbit:)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001683 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001684 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001685 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001686 if (c >= '1' && c <= '9') {
1687 unsigned char dummy;
1688
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001689 if (safe_read(STDIN_FILENO, &dummy, 1) < 1)
Glenn L McGrath475820c2004-01-22 12:42:23 +00001690 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001691 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001692 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001693 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001694
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001695 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001696#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001697 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001698 input_tab(&lastWasTab);
1699 break;
1700#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001701#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001702 case 'A':
1703 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001704 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001705 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001706 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001707 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001708 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001709 break;
1710 case 'B':
1711 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001712 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001713 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001714 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001715 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001716 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001717 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001718 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001719 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001720 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001721#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001722 case 'C':
1723 /* Right Arrow -- Move forward one character */
1724 input_forward();
1725 break;
1726 case 'D':
1727 /* Left Arrow -- Move back one character */
1728 input_backward(1);
1729 break;
1730 case '3':
1731 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001732 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001733 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001734 case '1': // vt100? linux vt? or what?
1735 case '7': // vt100? linux vt? or what?
1736 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001737 input_backward(cursor);
1738 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001739 case '4': // vt100? linux vt? or what?
1740 case '8': // vt100? linux vt? or what?
1741 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001742 input_end();
1743 break;
1744 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001745 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001746 beep();
1747 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001748 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001749
Eric Andersenc470f442003-07-28 09:56:35 +00001750 default: /* If it's regular input, do the normal thing */
Paul Fox84bbac52008-01-11 16:50:08 +00001751
1752 /* Control-V -- force insert of next char */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001753 if (c == CTRL('V')) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001754 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001755 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001756 if (c == 0) {
1757 beep();
1758 break;
1759 }
Paul Fox84bbac52008-01-11 16:50:08 +00001760 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001761
Denis Vlasenko38f63192007-01-22 09:03:07 +00001762#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001763 if (vi_cmdmode) /* Don't self-insert */
1764 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001765#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001766 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001767 break;
1768
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001769 command_len++;
1770 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001771 command[cursor] = c;
1772 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001773 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001774 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001775 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001776
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001777 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001778 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001779 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001780 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001781 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001782 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001783 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001784 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001785 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001786 }
Eric Andersenc470f442003-07-28 09:56:35 +00001787 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001788 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001789
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001790#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001791 if (c != '\t')
1792 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001793#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001794 }
1795
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001796 if (command_len > 0)
1797 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001798
Eric Andersen27bb7902003-12-23 20:24:51 +00001799 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001800 command[command_len++] = '\n';
1801 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001802 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001803
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001804#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001805 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001806#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001807
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001808 /* restore initial_settings */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001809 tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001810 /* restore SIGWINCH handler */
1811 signal(SIGWINCH, previous_SIGWINCH_handler);
1812 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001813
1814 DEINIT_S();
1815
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001816 return command_len;
1817}
1818
1819line_input_t *new_line_input_t(int flags)
1820{
1821 line_input_t *n = xzalloc(sizeof(*n));
1822 n->flags = flags;
1823 return n;
1824}
1825
1826#else
1827
1828#undef read_line_input
1829int read_line_input(const char* prompt, char* command, int maxsize)
1830{
1831 fputs(prompt, stdout);
1832 fflush(stdout);
1833 fgets(command, maxsize, stdin);
1834 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001835}
1836
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001837#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001838
1839
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001840/*
1841 * Testing
1842 */
1843
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001844#ifdef TEST
1845
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001846#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001847
1848const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001849
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001850int main(int argc, char **argv)
1851{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001852 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001853 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001854#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001855 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1856 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1857 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001858#else
1859 "% ";
1860#endif
1861
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001862#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001863 setlocale(LC_ALL, "");
1864#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001865 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001866 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001867 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001868 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001869 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001870 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001871 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001872 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001873 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001874 return 0;
1875}
1876
Eric Andersenc470f442003-07-28 09:56:35 +00001877#endif /* TEST */