blob: 529344f6c2e4e2c42350137943b9075fc286e5e8 [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 Vlasenko7221c8c2007-12-03 10:45:14 +00001206 pbuf = free_me = xzalloc(256);
1207 if (gethostname(pbuf, 255) < 0) {
1208 pbuf[0] = '?';
1209 pbuf[1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001210 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001211 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001212 break;
1213 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001214 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001215 break;
1216#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1217 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001218 /* /home/user[/something] -> ~[/something] */
1219 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001220 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001221 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001222 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1223 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1224 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001225 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001226 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001227 }
1228 break;
1229#endif
1230 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001231 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001232 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001233 if (cp != NULL && cp != pbuf)
1234 pbuf += (cp-pbuf) + 1;
1235 break;
1236 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001237 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001238 break;
1239 case 'e': case 'E': /* \e \E = \033 */
1240 c = '\033';
1241 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001242 case 'x': case 'X': {
1243 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001244 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001245 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001246 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001247 buf2[l] = '\0';
1248 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001249 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001250 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001251 break;
1252 }
1253 prmt_ptr++;
1254 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001255 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001256 if (c == 0)
1257 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001258 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001259 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001260 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001261 case '[': case ']':
1262 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001263 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001264 continue;
1265 }
1266 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001267 } /* switch */
1268 } /* if */
1269 } /* if */
1270 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001271 cur_prmt_len = strlen(pbuf);
1272 prmt_len += cur_prmt_len;
1273 if (flg_not_length != ']')
1274 cmdedit_prmt_len += cur_prmt_len;
1275 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001276 free(free_me);
1277 } /* while */
1278
1279 if (cwd_buf != (char *)bb_msg_unknown)
1280 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001281 cmdedit_prompt = prmt_mem_ptr;
1282 put_prompt();
1283}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001284#endif
1285
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001286static void cmdedit_setwidth(unsigned w, int redraw_flg)
1287{
1288 cmdedit_termw = w;
1289 if (redraw_flg) {
1290 /* new y for current cursor */
1291 int new_y = (cursor + cmdedit_prmt_len) / w;
1292 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001293 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001294 fflush(stdout);
1295 }
1296}
1297
1298static void win_changed(int nsig)
1299{
1300 int width;
1301 get_terminal_width_height(0, &width, NULL);
1302 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1303 if (nsig == SIGWINCH)
1304 signal(SIGWINCH, win_changed); /* rearm ourself */
1305}
1306
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001307/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001308 * The emacs and vi modes share much of the code in the big
1309 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001310 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001311 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001312 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001313 */
1314
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001315#define vbit 0x100
1316
1317/* leave out the "vi-mode"-only case labels if vi editing isn't
1318 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001319#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001320
1321/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001322#undef CTRL
1323#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001324
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001325/* Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001326 * -1 on read errors or EOF, or on bare Ctrl-D,
1327 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001328 * >0 length of input string, including terminating '\n'
1329 */
Denis Vlasenko037576d2007-10-20 18:30:38 +00001330int read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001331{
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001332#if ENABLE_FEATURE_TAB_COMPLETION
1333 smallint lastWasTab = FALSE;
1334#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001335 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001336 unsigned char c;
1337 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001338#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001339 smallint vi_cmdmode = 0;
1340 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001341#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001342 struct termios initial_settings;
1343 struct termios new_settings;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001344
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001345 INIT_S();
1346
1347 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1348 || !(initial_settings.c_lflag & ECHO)
1349 ) {
1350 /* Happens when e.g. stty -echo was run before */
1351 int len;
1352 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001353 fflush(stdout);
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001354 if (fgets(command, maxsize, stdin) == NULL)
1355 len = -1; /* EOF or error */
1356 else
1357 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001358 DEINIT_S();
1359 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001360 }
1361
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001362// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001363 if (maxsize > MAX_LINELEN)
1364 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001365
1366 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001367 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001368#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001369 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001370 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001371#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001372
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001373 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001374 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001375 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001376 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001377 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001378
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001379 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001380 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1381 /* Turn off echoing and CTRL-C, so we can trap it */
1382 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001383 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001384 new_settings.c_cc[VMIN] = 1;
1385 new_settings.c_cc[VTIME] = 0;
1386 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001387#ifndef _POSIX_VDISABLE
1388#define _POSIX_VDISABLE '\0'
1389#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001390 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001391 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001392
Eric Andersen6faae7d2001-02-16 20:09:17 +00001393 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001394 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1395 win_changed(0); /* do initial resizing */
1396#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1397 {
1398 struct passwd *entry;
1399
1400 entry = getpwuid(geteuid());
1401 if (entry) {
1402 user_buf = xstrdup(entry->pw_name);
1403 home_pwd_buf = xstrdup(entry->pw_dir);
1404 }
1405 }
1406#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001407 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001408 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001409
Erik Andersenc7c634b2000-03-19 05:28:55 +00001410 while (1) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001411 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +00001412
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001413 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001414 /* if we can't read input then exit */
1415 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001416 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001417
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001418 ic = c;
1419
Denis Vlasenko38f63192007-01-22 09:03:07 +00001420#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001421 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001422 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001423 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001424#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001425 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001426 case '\n':
1427 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001428 vi_case('\n'|vbit:)
1429 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001430 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001431 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001432 break_out = 1;
1433 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001434 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001435 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001436 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001437 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001438 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001439 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001440 vi_case('h'|vbit:)
1441 vi_case('\b'|vbit:)
1442 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001443 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001444 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001445 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001446 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001447 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001448 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001449 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001450 command_len = 0;
1451 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001452 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001453 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001454 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001455 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001456 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001457 errno = 0;
1458 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001459 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001460 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001461 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001462 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001463 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001464 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001465
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001466 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001467 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001468 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001469 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001470 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001471 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001472 vi_case('l'|vbit:)
1473 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001474 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001475 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001476 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001477
Erik Andersenf0657d32000-04-12 17:49:52 +00001478 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001479 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001480 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001481 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001482 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001483
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001484#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001485 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001486 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001487 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001488#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001489
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001490 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001491 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001492 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001493 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001494 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001495 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001496 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001497 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001498 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001499 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001500 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001501 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001502
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001503#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001504 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001505 vi_case(CTRL('N')|vbit:)
1506 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001507 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001508 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001509 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001510 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001511 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001512 vi_case(CTRL('P')|vbit:)
1513 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001514 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001515 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001516 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001517 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001518 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001519 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001520 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001521#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001522
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001523 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001524 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001525 /* Control-U -- Clear line before cursor */
1526 if (cursor) {
1527 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001528 command_len -= cursor;
1529 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001530 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001531 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001532 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001533 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001534 /* Control-W -- Remove the last word */
1535 while (cursor > 0 && isspace(command[cursor-1]))
1536 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001537 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001538 input_backspace();
1539 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001540
Denis Vlasenko38f63192007-01-22 09:03:07 +00001541#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001542 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001543 vi_cmdmode = 0;
1544 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001545 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001546 input_backward(cursor);
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_forward();
1551 vi_cmdmode = 0;
1552 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001553 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001554 input_end();
1555 vi_cmdmode = 0;
1556 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001557 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001558 input_delete(1);
1559 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001560 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001561 if (cursor > 0) {
1562 input_backward(1);
1563 input_delete(1);
1564 }
1565 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001566 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001567 vi_Word_motion(command, 1);
1568 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001569 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001570 vi_word_motion(command, 1);
1571 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001572 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001573 vi_End_motion(command);
1574 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001575 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001576 vi_end_motion(command);
1577 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001578 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001579 vi_Back_motion(command);
1580 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001581 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001582 vi_back_motion(command);
1583 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001584 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001585 vi_cmdmode = 0;
1586 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001587 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001588 goto clear_to_eol;
1589
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001590 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001591 vi_cmdmode = 0;
1592 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001593 case 'd'|vbit: {
1594 int nc, sc;
1595 sc = cursor;
1596 prevc = ic;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001597 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001598 goto prepare_to_die;
1599 if (c == (prevc & 0xff)) {
1600 /* "cc", "dd" */
1601 input_backward(cursor);
1602 goto clear_to_eol;
1603 break;
1604 }
1605 switch (c) {
1606 case 'w':
1607 case 'W':
1608 case 'e':
1609 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001610 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001611 case 'w': /* "dw", "cw" */
1612 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001613 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001614 case 'W': /* 'dW', 'cW' */
1615 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001616 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001617 case 'e': /* 'de', 'ce' */
1618 vi_end_motion(command);
1619 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001620 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001621 case 'E': /* 'dE', 'cE' */
1622 vi_End_motion(command);
1623 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001624 break;
1625 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001626 nc = cursor;
1627 input_backward(cursor - sc);
1628 while (nc-- > cursor)
1629 input_delete(1);
1630 break;
1631 case 'b': /* "db", "cb" */
1632 case 'B': /* implemented as B */
1633 if (c == 'b')
1634 vi_back_motion(command);
1635 else
1636 vi_Back_motion(command);
1637 while (sc-- > cursor)
1638 input_delete(1);
1639 break;
1640 case ' ': /* "d ", "c " */
1641 input_delete(1);
1642 break;
1643 case '$': /* "d$", "c$" */
1644 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001645 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001646 input_delete(1);
1647 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001648 }
1649 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001650 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001651 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001652 input_forward();
1653 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001654 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001655 put();
1656 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001657 case 'r'|vbit:
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001658 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001659 goto prepare_to_die;
1660 if (c == 0)
1661 beep();
1662 else {
1663 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001664 bb_putchar(c);
1665 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001666 }
1667 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001668#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001669
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001670 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001671
Denis Vlasenko38f63192007-01-22 09:03:07 +00001672#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001673 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001674 /* ESC: insert mode --> command mode */
1675 vi_cmdmode = 1;
1676 input_backward(1);
1677 break;
1678 }
1679#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001680 /* escape sequence follows */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001681 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001682 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001683 /* different vt100 emulations */
1684 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001685 vi_case('['|vbit:)
1686 vi_case('O'|vbit:)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001687 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001688 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001689 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001690 if (c >= '1' && c <= '9') {
1691 unsigned char dummy;
1692
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001693 if (safe_read(STDIN_FILENO, &dummy, 1) < 1)
Glenn L McGrath475820c2004-01-22 12:42:23 +00001694 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001695 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001696 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001697 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001698
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001699 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001700#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001701 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001702 input_tab(&lastWasTab);
1703 break;
1704#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001705#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001706 case 'A':
1707 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001708 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001709 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001710 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001711 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001712 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001713 break;
1714 case 'B':
1715 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001716 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001717 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001718 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001719 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001720 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001721 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001722 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001723 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001724 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001725#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001726 case 'C':
1727 /* Right Arrow -- Move forward one character */
1728 input_forward();
1729 break;
1730 case 'D':
1731 /* Left Arrow -- Move back one character */
1732 input_backward(1);
1733 break;
1734 case '3':
1735 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001736 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001737 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001738 case '1': // vt100? linux vt? or what?
1739 case '7': // vt100? linux vt? or what?
1740 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001741 input_backward(cursor);
1742 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001743 case '4': // vt100? linux vt? or what?
1744 case '8': // vt100? linux vt? or what?
1745 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001746 input_end();
1747 break;
1748 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001749 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001750 beep();
1751 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001752 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001753
Eric Andersenc470f442003-07-28 09:56:35 +00001754 default: /* If it's regular input, do the normal thing */
Paul Fox84bbac52008-01-11 16:50:08 +00001755
1756 /* Control-V -- force insert of next char */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001757 if (c == CTRL('V')) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001758 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001759 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001760 if (c == 0) {
1761 beep();
1762 break;
1763 }
Paul Fox84bbac52008-01-11 16:50:08 +00001764 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001765
Denis Vlasenko38f63192007-01-22 09:03:07 +00001766#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001767 if (vi_cmdmode) /* Don't self-insert */
1768 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001769#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001770 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001771 break;
1772
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001773 command_len++;
1774 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001775 command[cursor] = c;
1776 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001777 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001778 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001779 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001780
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001781 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001782 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001783 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001784 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001785 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001786 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001787 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001788 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001789 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001790 }
Eric Andersenc470f442003-07-28 09:56:35 +00001791 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001792 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001793
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001794#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001795 if (c != '\t')
1796 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001797#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001798 }
1799
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001800 if (command_len > 0)
1801 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001802
Eric Andersen27bb7902003-12-23 20:24:51 +00001803 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001804 command[command_len++] = '\n';
1805 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001806 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001807
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001808#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001809 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001810#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001811
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001812 /* restore initial_settings */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001813 tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001814 /* restore SIGWINCH handler */
1815 signal(SIGWINCH, previous_SIGWINCH_handler);
1816 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001817
1818 DEINIT_S();
1819
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001820 return command_len;
1821}
1822
1823line_input_t *new_line_input_t(int flags)
1824{
1825 line_input_t *n = xzalloc(sizeof(*n));
1826 n->flags = flags;
1827 return n;
1828}
1829
1830#else
1831
1832#undef read_line_input
1833int read_line_input(const char* prompt, char* command, int maxsize)
1834{
1835 fputs(prompt, stdout);
1836 fflush(stdout);
1837 fgets(command, maxsize, stdin);
1838 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001839}
1840
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001841#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001842
1843
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001844/*
1845 * Testing
1846 */
1847
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001848#ifdef TEST
1849
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001850#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001851
1852const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001853
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001854int main(int argc, char **argv)
1855{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001856 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001857 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001858#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001859 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1860 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1861 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001862#else
1863 "% ";
1864#endif
1865
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001866#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001867 setlocale(LC_ALL, "");
1868#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001869 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001870 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001871 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001872 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001873 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001874 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001875 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001876 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001877 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001878 return 0;
1879}
1880
Eric Andersenc470f442003-07-28 09:56:35 +00001881#endif /* TEST */