blob: 5a0d03eeb8fb127b25e06792594ee76df7d2a467 [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. */
Denis Vlasenko5d89fba2008-04-22 00:08:27 +000077struct lineedit_statics {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000078 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 Vlasenko5d89fba2008-04-22 00:08:27 +0000123/* See lineedit_ptr_hack.c */
124extern struct lineedit_statics *const lineedit_ptr_to_statics;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000125
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000126#define S (*lineedit_ptr_to_statics)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000127#define state (S.state )
128#define cmdedit_termw (S.cmdedit_termw )
129#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
130#define cmdedit_x (S.cmdedit_x )
131#define cmdedit_y (S.cmdedit_y )
132#define cmdedit_prmt_len (S.cmdedit_prmt_len)
133#define cursor (S.cursor )
134#define command_len (S.command_len )
135#define command_ps (S.command_ps )
136#define cmdedit_prompt (S.cmdedit_prompt )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000137#define num_ok_lines (S.num_ok_lines )
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000138#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000139#define home_pwd_buf (S.home_pwd_buf )
140#define matches (S.matches )
141#define num_matches (S.num_matches )
142#define delptr (S.delptr )
143#define newdelflag (S.newdelflag )
144#define delbuf (S.delbuf )
145
146#define INIT_S() do { \
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000147 (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000148 barrier(); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000149 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
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000165 free(lineedit_ptr_to_statics);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000166}
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 */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000296#if !ENABLE_FEATURE_EDITING_VI
297static void input_delete(void)
298#define input_delete(save) input_delete()
299#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000300static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000301#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000302{
Mark Whitley4e338752001-01-26 20:42:23 +0000303 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000304
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000305 if (j == command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000306 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000307
Denis Vlasenko38f63192007-01-22 09:03:07 +0000308#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000309 if (save) {
310 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000311 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000312 newdelflag = 0;
313 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000314 if ((delptr - delbuf) < DELBUFSIZ)
315 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000316 }
317#endif
318
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000319 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000320 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000321 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000322 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000323 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000324}
325
Denis Vlasenko38f63192007-01-22 09:03:07 +0000326#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000327static void put(void)
328{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000329 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000330 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000331
Paul Fox3f11b1b2005-08-04 19:04:46 +0000332 if (j == 0)
333 return;
334 ocursor = cursor;
335 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000336 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000337 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000338 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000339 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000340 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000341}
342#endif
343
Mark Whitley4e338752001-01-26 20:42:23 +0000344/* Delete the char in back of the cursor */
345static void input_backspace(void)
346{
347 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000348 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000349 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000350 }
351}
352
Eric Andersenaff114c2004-04-14 17:51:38 +0000353/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000354static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000355{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000356 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000357 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000358}
359
Denis Vlasenko38f63192007-01-22 09:03:07 +0000360#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000361
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000362static void free_tab_completion_data(void)
363{
364 if (matches) {
365 while (num_matches)
366 free(matches[--num_matches]);
367 free(matches);
368 matches = NULL;
369 }
370}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000371
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000372static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000373{
374 int nm = num_matches;
375 int nm1 = nm + 1;
376
377 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000378 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000379 num_matches++;
380}
381
Denis Vlasenko38f63192007-01-22 09:03:07 +0000382#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000383static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000384{
385 struct passwd *entry;
386 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000387
Eric Andersenc470f442003-07-28 09:56:35 +0000388 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000389 userlen = strlen(ud);
390
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000391 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000392 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000393 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000394
Eric Andersenc470f442003-07-28 09:56:35 +0000395 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000396 home = home_pwd_buf;
397 } else {
398 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000399 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000400 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000401 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000402 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000403 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000404 ud = temp;
405 if (entry)
406 home = entry->pw_dir;
407 }
408 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000409 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000410 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000411 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000412 }
413 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000414 } else {
415 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000416 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000417 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000418 struct passwd pwd;
419 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000420
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000421 setpwent();
422 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000423 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000424 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
425 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000426 }
427 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000428 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000429 }
430}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000431#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000432
433enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000434 FIND_EXE_ONLY = 0,
435 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000436 FIND_FILE_ONLY = 2,
437};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000438
Mark Whitley4e338752001-01-26 20:42:23 +0000439static int path_parse(char ***p, int flags)
440{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000441 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000442 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000443 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000444 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000445
Mark Whitley4e338752001-01-26 20:42:23 +0000446 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000447 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000448 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000449
450 if (state->flags & WITH_PATH_LOOKUP)
451 pth = state->path_lookup;
452 else
453 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000454 /* PATH=<empty> or PATH=:<empty> */
455 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
456 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000457
Denis Vlasenko253ce002007-01-22 08:34:44 +0000458 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000459 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000460 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000461 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000462 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000463 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000464 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000465 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000466 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000467 }
468
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000469 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000470 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000471 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000472 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000473 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000474 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000475 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000476 *tmp++ = '\0'; /* ':' -> '\0' */
477 if (*tmp == '\0')
478 break; /* :<empty> */
479 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000480 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000481 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000482 return npth;
483}
484
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000485static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000486{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000487 DIR *dir;
488 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000489 struct stat st;
490 char *path1[1];
491 char **paths = path1;
492 int npaths;
493 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000494 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000495 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000496/* char dirbuf[MAX_LINELEN]; */
497#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000498
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000499 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000500 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000501
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000502 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000503 /* no dir, if flags==EXE_ONLY - get paths, else "." */
504 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000505 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000506 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000507 /* dirbuf = ".../.../.../" */
508 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000509#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000510 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000511 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000512#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000513 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000514 /* point to 'l' in "..../last_component" */
515 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000516 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000517
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000519 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000520 if (!dir)
521 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000522
523 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000524 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000525 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000526
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000527 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000528 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000529 continue;
530 /* not see .name without .match */
Denis Vlasenkob5202712008-04-24 04:42:52 +0000531 if (*str_found == '.' && *pfind == '\0') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000532 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000533 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000534 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000535 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000536 found = concat_path_file(paths[i], str_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000537 /* hmm, remove in progress? */
538 /* NB: stat() first so that we see is it a directory;
539 * but if that fails, use lstat() so that
540 * we still match dangling links */
541 if (stat(found, &st) && lstat(found, &st))
Eric Andersene5dfced2001-04-09 22:48:12 +0000542 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000543 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000544 if (paths[i] != dirbuf)
Denis Vlasenkob5202712008-04-24 04:42:52 +0000545 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000546
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000547 len1 = strlen(found);
548 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000549 found[len1] = '\0';
550 found[len1+1] = '\0';
551
Mark Whitley4e338752001-01-26 20:42:23 +0000552 if (S_ISDIR(st.st_mode)) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000553 /* name is a directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000554 if (found[len1-1] != '/') {
555 found[len1] = '/';
556 }
Mark Whitley4e338752001-01-26 20:42:23 +0000557 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000558 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000559 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000560 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000561 }
Mark Whitley4e338752001-01-26 20:42:23 +0000562 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000563 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000564 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000565 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000566 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000567 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000568 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000569 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000570 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000571 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000572 free(paths);
573 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000574#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000575}
Erik Andersenf0657d32000-04-12 17:49:52 +0000576
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000577#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000578
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000579#define collapse_pos(is, in) do { \
580 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
581 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
582} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000583
584static int find_match(char *matchBuf, int *len_with_quotes)
585{
586 int i, j;
587 int command_mode;
588 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000589/* int16_t int_buf[MAX_LINELEN + 1]; */
590/* int16_t pos_buf[MAX_LINELEN + 1]; */
591#define int_buf (S.find_match__int_buf)
592#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000593
594 /* set to integer dimension characters and own positions */
595 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000596 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000597 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000598 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000599 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000600 }
601 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000602 }
603
604 /* mask \+symbol and convert '\t' to ' ' */
605 for (i = j = 0; matchBuf[i]; i++, j++)
606 if (matchBuf[i] == '\\') {
607 collapse_pos(j, j + 1);
608 int_buf[j] |= QUOT;
609 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000610#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000611 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000612 int_buf[j] = ' ' | QUOT;
613#endif
614 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000615#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000616 else if (matchBuf[i] == '\t')
617 int_buf[j] = ' ';
618#endif
619
620 /* mask "symbols" or 'symbols' */
621 c2 = 0;
622 for (i = 0; int_buf[i]; i++) {
623 c = int_buf[i];
624 if (c == '\'' || c == '"') {
625 if (c2 == 0)
626 c2 = c;
627 else {
628 if (c == c2)
629 c2 = 0;
630 else
631 int_buf[i] |= QUOT;
632 }
633 } else if (c2 != 0 && c != '$')
634 int_buf[i] |= QUOT;
635 }
636
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000637 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000638 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
639 for (i = 0; int_buf[i]; i++) {
640 c = int_buf[i];
641 c2 = int_buf[i + 1];
642 j = i ? int_buf[i - 1] : -1;
643 command_mode = 0;
644 if (c == ';' || c == '&' || c == '|') {
645 command_mode = 1 + (c == c2);
646 if (c == '&') {
647 if (j == '>' || j == '<')
648 command_mode = 0;
649 } else if (c == '|' && j == '>')
650 command_mode = 0;
651 }
652 if (command_mode) {
653 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000654 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000655 }
656 }
657 /* collapse `command...` */
658 for (i = 0; int_buf[i]; i++)
659 if (int_buf[i] == '`') {
660 for (j = i + 1; int_buf[j]; j++)
661 if (int_buf[j] == '`') {
662 collapse_pos(i, j + 1);
663 j = 0;
664 break;
665 }
666 if (j) {
667 /* not found close ` - command mode, collapse all previous */
668 collapse_pos(0, i + 1);
669 break;
670 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000671 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000672 }
673
674 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000675 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000676 c2 = 0;
677 for (i = 0; int_buf[i]; i++)
678 if (int_buf[i] == '(' || int_buf[i] == '{') {
679 if (int_buf[i] == '(')
680 c++;
681 else
682 c2++;
683 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000684 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000685 }
686 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
687 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
688 if (int_buf[i] == ')')
689 c--;
690 else
691 c2--;
692 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000693 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000694 }
695
696 /* skip first not quote space */
697 for (i = 0; int_buf[i]; i++)
698 if (int_buf[i] != ' ')
699 break;
700 if (i)
701 collapse_pos(0, i);
702
703 /* set find mode for completion */
704 command_mode = FIND_EXE_ONLY;
705 for (i = 0; int_buf[i]; i++)
706 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
707 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000708 && matchBuf[pos_buf[0]] == 'c'
709 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000710 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000711 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000712 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000713 command_mode = FIND_FILE_ONLY;
714 break;
715 }
716 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000717 for (i = 0; int_buf[i]; i++)
718 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000719 /* find last word */
720 for (--i; i >= 0; i--) {
721 c = int_buf[i];
722 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
723 collapse_pos(0, i + 1);
724 break;
725 }
726 }
727 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000728 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
729 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000730 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000731 while ((int_buf[i] & ~QUOT) == '/'
732 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
733 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000734 i++;
735 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000736
737 /* set only match and destroy quotes */
738 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000739 for (c = 0; pos_buf[i] >= 0; i++) {
740 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000741 j = pos_buf[i] + 1;
742 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000743 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000744 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000745 *len_with_quotes = j ? j - pos_buf[0] : 0;
746
747 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000748#undef int_buf
749#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000750}
751
Glenn L McGrath4d001292003-01-06 01:11:50 +0000752/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000753 * display by column (original idea from ls applet,
754 * very optimized by me :)
755 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000756static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000757{
758 int ncols, row;
759 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000760 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000761 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000762 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000763
764 /* find the longest file name- use that as the column width */
765 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000766 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000767 if (column_width < l)
768 column_width = l;
769 }
770 column_width += 2; /* min space for columns */
771 ncols = cmdedit_termw / column_width;
772
773 if (ncols > 1) {
774 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000775 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000776 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000777 } else {
778 ncols = 1;
779 }
780 for (row = 0; row < nrows; row++) {
781 int n = row;
782 int nc;
783
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000784 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000785 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000786 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000787 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000788 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000789 }
790}
791
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000792static char *add_quote_for_spec_chars(char *found)
793{
794 int l = 0;
795 char *s = xmalloc((strlen(found) + 1) * 2);
796
797 while (*found) {
798 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
799 s[l++] = '\\';
800 s[l++] = *found++;
801 }
802 s[l] = 0;
803 return s;
804}
805
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000806/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000807static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000808{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000809 if (!(state->flags & TAB_COMPLETION))
810 return;
811
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000812 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000813 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000814 int len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000815/* char matchBuf[MAX_LINELEN]; */
816#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000817 int find_type;
818 int recalc_pos;
819
Eric Andersenc470f442003-07-28 09:56:35 +0000820 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000821
822 /* Make a local copy of the string -- up
823 * to the position of the cursor */
824 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000825 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000826
827 find_type = find_match(matchBuf, &recalc_pos);
828
829 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000830 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000831
Denis Vlasenko38f63192007-01-22 09:03:07 +0000832#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000833 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000834 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000835 if (state->flags & USERNAME_COMPLETION)
836 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
837 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000838#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000839 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000840 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000841 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000842 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000843 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000844 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000845 int i, n = 0;
Denis Vlasenkofb290382008-03-02 12:51:26 +0000846 qsort_string_vector(matches, num_matches);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000847 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000848 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000849 if (strcmp(matches[i], matches[i+1]) == 0) {
850 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000851 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000852 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000853 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000854 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000855 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000856 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000857 matches[n] = matches[i];
858 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000859 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000860 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000861 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000862 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000863 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000864 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000865 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000866 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000867 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000868 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000869 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000870 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000871 break;
872 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000873 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000874 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000875 return;
876 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000877 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000878 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000879 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000880 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000881 /* for next completion current found */
882 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000883
884 len_found = strlen(tmp);
885 if (tmp[len_found-1] != '/') {
886 tmp[len_found] = ' ';
887 tmp[len_found+1] = '\0';
888 }
Mark Whitley4e338752001-01-26 20:42:23 +0000889 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000890 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000891 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000892 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000893 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000894 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000895 /* save tail line */
896 strcpy(matchBuf, command_ps + cursor);
897 /* add match */
898 strcat(command_ps, tmp);
899 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000900 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000901 /* back to begin word for match */
902 input_backward(recalc_pos);
903 /* new pos */
904 recalc_pos = cursor + len_found;
905 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000906 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000907 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000908 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000909 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000910 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000911#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000912 } else {
913 /* Ok -- the last char was a TAB. Since they
914 * just hit TAB again, print a list of all the
915 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000916 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000917 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000918
919 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000920 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000921 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000922 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000923 }
924 }
925}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000926
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000927#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000928
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000929
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000930#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000931
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000932/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000933static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000934{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000935 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
936 free(state->history[state->cur_history]);
937 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000938 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000939 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000940}
941
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000942static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000943{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000944 if (state->flags & DO_HISTORY) {
945 int ch = state->cur_history;
946 if (ch < state->cnt_history) {
947 get_previous_history(); /* save the current history line */
948 state->cur_history = ch + 1;
949 return state->cur_history;
950 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000951 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000952 beep();
953 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000954}
Robert Griebl350d26b2002-12-03 22:45:46 +0000955
Denis Vlasenko38f63192007-01-22 09:03:07 +0000956#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000957/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000958static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000959{
Robert Griebl350d26b2002-12-03 22:45:46 +0000960 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000961 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000962
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000963 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +0000964
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000965 fp = fopen(fromfile, "r");
966 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000967 /* clean up old history */
968 for (hi = state->cnt_history; hi > 0;) {
969 hi--;
970 free(state->history[hi]);
971 }
972
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000973 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000974 char *hl = xmalloc_fgetline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000975 int l;
976
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000977 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000978 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000979 l = strlen(hl);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000980 if (l >= MAX_LINELEN)
981 hl[MAX_LINELEN-1] = '\0';
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000982 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000983 free(hl);
984 continue;
985 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000986 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000987 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000988 fclose(fp);
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000989 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000990 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000991}
992
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000993/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000994static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000995{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000996 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +0000997
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000998 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000999 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +00001000 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001001
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001002 for (i = 0; i < state->cnt_history; i++) {
1003 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001004 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001005 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001006 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001007}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001008#else
1009#define load_history(a) ((void)0)
1010#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001011#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001012
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001013static void remember_in_history(const char *str)
1014{
1015 int i;
1016
1017 if (!(state->flags & DO_HISTORY))
1018 return;
1019
1020 i = state->cnt_history;
1021 free(state->history[MAX_HISTORY]);
1022 state->history[MAX_HISTORY] = NULL;
1023 /* After max history, remove the oldest command */
1024 if (i >= MAX_HISTORY) {
1025 free(state->history[0]);
1026 for (i = 0; i < MAX_HISTORY-1; i++)
1027 state->history[i] = state->history[i+1];
1028 }
1029// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
1030// (i.e. do not save dups?)
1031 state->history[i++] = xstrdup(str);
1032 state->cur_history = i;
1033 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001034#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001035 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001036 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +00001037#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00001038 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001039}
1040
1041#else /* MAX_HISTORY == 0 */
1042#define remember_in_history(a) ((void)0)
1043#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001044
1045
Erik Andersen6273f652000-03-17 01:12:41 +00001046/*
1047 * This function is used to grab a character buffer
1048 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001049 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001050 * a mini readline).
1051 *
1052 * The following standard commands are not implemented:
1053 * ESC-b -- Move back one word
1054 * ESC-f -- Move forward one word
1055 * ESC-d -- Delete back one word
1056 * ESC-h -- Delete forward one word
1057 * CTL-t -- Transpose two characters
1058 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001059 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001060 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001061 */
Eric Andersenc470f442003-07-28 09:56:35 +00001062
Denis Vlasenko38f63192007-01-22 09:03:07 +00001063#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001064static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001065vi_Word_motion(char *command, int eat)
1066{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001067 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001068 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001069 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001070 input_forward();
1071}
1072
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001073static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001074vi_word_motion(char *command, int eat)
1075{
1076 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001077 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001078 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001079 input_forward();
1080 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001081 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001082 input_forward();
1083 }
1084
Denis Vlasenko253ce002007-01-22 08:34:44 +00001085 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001086 input_forward();
1087
Denis Vlasenko253ce002007-01-22 08:34:44 +00001088 if (eat && cursor < command_len && isspace(command[cursor]))
1089 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001090 input_forward();
1091}
1092
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001093static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001094vi_End_motion(char *command)
1095{
1096 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001097 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001098 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001099 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001100 input_forward();
1101}
1102
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001103static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001104vi_end_motion(char *command)
1105{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001106 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001107 return;
1108 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001109 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001110 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001111 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001112 return;
1113 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001114 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001115 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1116 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001117 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001118 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001119 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001120 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001121 input_forward();
1122 }
1123}
1124
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001125static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001126vi_Back_motion(char *command)
1127{
1128 while (cursor > 0 && isspace(command[cursor-1]))
1129 input_backward(1);
1130 while (cursor > 0 && !isspace(command[cursor-1]))
1131 input_backward(1);
1132}
1133
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001134static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001135vi_back_motion(char *command)
1136{
1137 if (cursor <= 0)
1138 return;
1139 input_backward(1);
1140 while (cursor > 0 && isspace(command[cursor]))
1141 input_backward(1);
1142 if (cursor <= 0)
1143 return;
1144 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001145 while (cursor > 0
1146 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1147 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001148 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001149 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001150 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001151 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001152 input_backward(1);
1153 }
1154}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001155#endif
1156
1157
1158/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001159 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001160 */
1161
Denis Vlasenko38f63192007-01-22 09:03:07 +00001162#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001163static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001164{
1165 cmdedit_prompt = prmt_ptr;
1166 cmdedit_prmt_len = strlen(prmt_ptr);
1167 put_prompt();
1168}
1169#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001170static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001171{
1172 int prmt_len = 0;
1173 size_t cur_prmt_len = 0;
1174 char flg_not_length = '[';
1175 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001176 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1177 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001178 char c;
1179 char *pbuf;
1180
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001181 cmdedit_prmt_len = 0;
1182
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001183 if (!cwd_buf) {
1184 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001185 }
1186
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001187 cbuf[1] = '\0'; /* never changes */
1188
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001189 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001190 char *free_me = NULL;
1191
1192 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001193 c = *prmt_ptr++;
1194 if (c == '\\') {
1195 const char *cp = prmt_ptr;
1196 int l;
1197
1198 c = bb_process_escape_sequence(&prmt_ptr);
1199 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001200 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001201 break;
1202 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001203
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001204 switch (c) {
1205#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1206 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001207 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001208 break;
1209#endif
1210 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001211 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001212 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001213 break;
1214 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001215 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001216 break;
1217#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1218 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001219 /* /home/user[/something] -> ~[/something] */
1220 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001221 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001222 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001223 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1224 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1225 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001226 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001227 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001228 }
1229 break;
1230#endif
1231 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001232 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001233 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001234 if (cp != NULL && cp != pbuf)
1235 pbuf += (cp-pbuf) + 1;
1236 break;
1237 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001238 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001239 break;
1240 case 'e': case 'E': /* \e \E = \033 */
1241 c = '\033';
1242 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001243 case 'x': case 'X': {
1244 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001245 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001246 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001247 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001248 buf2[l] = '\0';
1249 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001250 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001251 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001252 break;
1253 }
1254 prmt_ptr++;
1255 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001256 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001257 if (c == 0)
1258 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001259 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001260 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001261 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001262 case '[': case ']':
1263 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001264 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001265 continue;
1266 }
1267 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001268 } /* switch */
1269 } /* if */
1270 } /* if */
1271 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001272 cur_prmt_len = strlen(pbuf);
1273 prmt_len += cur_prmt_len;
1274 if (flg_not_length != ']')
1275 cmdedit_prmt_len += cur_prmt_len;
1276 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001277 free(free_me);
1278 } /* while */
1279
1280 if (cwd_buf != (char *)bb_msg_unknown)
1281 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001282 cmdedit_prompt = prmt_mem_ptr;
1283 put_prompt();
1284}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001285#endif
1286
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001287static void cmdedit_setwidth(unsigned w, int redraw_flg)
1288{
1289 cmdedit_termw = w;
1290 if (redraw_flg) {
1291 /* new y for current cursor */
1292 int new_y = (cursor + cmdedit_prmt_len) / w;
1293 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001294 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001295 fflush(stdout);
1296 }
1297}
1298
1299static void win_changed(int nsig)
1300{
1301 int width;
1302 get_terminal_width_height(0, &width, NULL);
1303 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1304 if (nsig == SIGWINCH)
1305 signal(SIGWINCH, win_changed); /* rearm ourself */
1306}
1307
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001308/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001309 * The emacs and vi modes share much of the code in the big
1310 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001311 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001312 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001313 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001314 */
1315
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001316#define vbit 0x100
1317
1318/* leave out the "vi-mode"-only case labels if vi editing isn't
1319 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001320#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001321
1322/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001323#undef CTRL
1324#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001325
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001326/* Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001327 * -1 on read errors or EOF, or on bare Ctrl-D,
1328 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001329 * >0 length of input string, including terminating '\n'
1330 */
Denis Vlasenko037576d2007-10-20 18:30:38 +00001331int read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001332{
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001333#if ENABLE_FEATURE_TAB_COMPLETION
1334 smallint lastWasTab = FALSE;
1335#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001336 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001337 unsigned char c;
1338 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001339#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001340 smallint vi_cmdmode = 0;
1341 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001342#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001343 struct termios initial_settings;
1344 struct termios new_settings;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001345
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001346 INIT_S();
1347
1348 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1349 || !(initial_settings.c_lflag & ECHO)
1350 ) {
1351 /* Happens when e.g. stty -echo was run before */
1352 int len;
1353 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001354 fflush(stdout);
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001355 if (fgets(command, maxsize, stdin) == NULL)
1356 len = -1; /* EOF or error */
1357 else
1358 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001359 DEINIT_S();
1360 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001361 }
1362
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001363// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001364 if (maxsize > MAX_LINELEN)
1365 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001366
1367 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001368 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001369#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001370 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001371 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001372#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001373
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001374 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001375 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001376 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001377 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001378 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001379
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001380 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001381 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1382 /* Turn off echoing and CTRL-C, so we can trap it */
1383 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001384 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001385 new_settings.c_cc[VMIN] = 1;
1386 new_settings.c_cc[VTIME] = 0;
1387 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001388#ifndef _POSIX_VDISABLE
1389#define _POSIX_VDISABLE '\0'
1390#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001391 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001392 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001393
Eric Andersen6faae7d2001-02-16 20:09:17 +00001394 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001395 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1396 win_changed(0); /* do initial resizing */
1397#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1398 {
1399 struct passwd *entry;
1400
1401 entry = getpwuid(geteuid());
1402 if (entry) {
1403 user_buf = xstrdup(entry->pw_name);
1404 home_pwd_buf = xstrdup(entry->pw_dir);
1405 }
1406 }
1407#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001408 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001409 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001410
Erik Andersenc7c634b2000-03-19 05:28:55 +00001411 while (1) {
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001412 fflush(NULL);
Mark Whitley4e338752001-01-26 20:42:23 +00001413
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001414 if (nonblock_safe_read(STDIN_FILENO, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001415 /* if we can't read input then exit */
1416 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001417 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001418
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001419 ic = c;
1420
Denis Vlasenko38f63192007-01-22 09:03:07 +00001421#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001422 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001423 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001424 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001425#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001426 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001427 case '\n':
1428 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001429 vi_case('\n'|vbit:)
1430 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001431 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001432 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001433 break_out = 1;
1434 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001435 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001436 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001437 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001438 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001439 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001440 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001441 vi_case('h'|vbit:)
1442 vi_case('\b'|vbit:)
1443 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001444 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001445 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001446 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001447 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001448 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001449 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001450 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001451 command_len = 0;
1452 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001453 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001454 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001455 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001456 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001457 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001458 errno = 0;
1459 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001460 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001461 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001462 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001463 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001464 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001465 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001466
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001467 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001468 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001469 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001470 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001471 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001472 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001473 vi_case('l'|vbit:)
1474 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001475 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001476 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001477 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001478
Erik Andersenf0657d32000-04-12 17:49:52 +00001479 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001480 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001481 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001482 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001483 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001484
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001485#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001486 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001487 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001488 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001489#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001490
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001491 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001492 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001493 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001494 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001495 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001496 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001497 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001498 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001499 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001500 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001501 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001502 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001503
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001504#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001505 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001506 vi_case(CTRL('N')|vbit:)
1507 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001508 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001509 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001510 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001511 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001512 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001513 vi_case(CTRL('P')|vbit:)
1514 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001515 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001516 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001517 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001518 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001519 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001520 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001521 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001522#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001523
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001524 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001525 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001526 /* Control-U -- Clear line before cursor */
1527 if (cursor) {
1528 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001529 command_len -= cursor;
1530 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001531 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001532 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001533 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001534 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001535 /* Control-W -- Remove the last word */
1536 while (cursor > 0 && isspace(command[cursor-1]))
1537 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001538 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001539 input_backspace();
1540 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001541
Denis Vlasenko38f63192007-01-22 09:03:07 +00001542#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001543 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001544 vi_cmdmode = 0;
1545 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001546 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001547 input_backward(cursor);
1548 vi_cmdmode = 0;
1549 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001550 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001551 input_forward();
1552 vi_cmdmode = 0;
1553 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001554 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001555 input_end();
1556 vi_cmdmode = 0;
1557 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001558 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001559 input_delete(1);
1560 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001561 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001562 if (cursor > 0) {
1563 input_backward(1);
1564 input_delete(1);
1565 }
1566 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001567 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001568 vi_Word_motion(command, 1);
1569 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001570 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001571 vi_word_motion(command, 1);
1572 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001573 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001574 vi_End_motion(command);
1575 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001576 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001577 vi_end_motion(command);
1578 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001579 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001580 vi_Back_motion(command);
1581 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001582 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001583 vi_back_motion(command);
1584 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001585 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001586 vi_cmdmode = 0;
1587 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001588 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001589 goto clear_to_eol;
1590
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001591 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001592 vi_cmdmode = 0;
1593 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001594 case 'd'|vbit: {
1595 int nc, sc;
1596 sc = cursor;
1597 prevc = ic;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001598 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001599 goto prepare_to_die;
1600 if (c == (prevc & 0xff)) {
1601 /* "cc", "dd" */
1602 input_backward(cursor);
1603 goto clear_to_eol;
1604 break;
1605 }
1606 switch (c) {
1607 case 'w':
1608 case 'W':
1609 case 'e':
1610 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001611 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001612 case 'w': /* "dw", "cw" */
1613 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001614 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001615 case 'W': /* 'dW', 'cW' */
1616 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001617 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001618 case 'e': /* 'de', 'ce' */
1619 vi_end_motion(command);
1620 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001621 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001622 case 'E': /* 'dE', 'cE' */
1623 vi_End_motion(command);
1624 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001625 break;
1626 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001627 nc = cursor;
1628 input_backward(cursor - sc);
1629 while (nc-- > cursor)
1630 input_delete(1);
1631 break;
1632 case 'b': /* "db", "cb" */
1633 case 'B': /* implemented as B */
1634 if (c == 'b')
1635 vi_back_motion(command);
1636 else
1637 vi_Back_motion(command);
1638 while (sc-- > cursor)
1639 input_delete(1);
1640 break;
1641 case ' ': /* "d ", "c " */
1642 input_delete(1);
1643 break;
1644 case '$': /* "d$", "c$" */
1645 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001646 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001647 input_delete(1);
1648 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001649 }
1650 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001651 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001652 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001653 input_forward();
1654 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001655 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001656 put();
1657 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001658 case 'r'|vbit:
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001659 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001660 goto prepare_to_die;
1661 if (c == 0)
1662 beep();
1663 else {
1664 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001665 bb_putchar(c);
1666 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001667 }
1668 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001669#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001670
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001671 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001672
Denis Vlasenko38f63192007-01-22 09:03:07 +00001673#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001674 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001675 /* ESC: insert mode --> command mode */
1676 vi_cmdmode = 1;
1677 input_backward(1);
1678 break;
1679 }
1680#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001681 /* escape sequence follows */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001682 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001683 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001684 /* different vt100 emulations */
1685 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001686 vi_case('['|vbit:)
1687 vi_case('O'|vbit:)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001688 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001689 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001690 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001691 if (c >= '1' && c <= '9') {
1692 unsigned char dummy;
1693
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001694 if (safe_read(STDIN_FILENO, &dummy, 1) < 1)
Glenn L McGrath475820c2004-01-22 12:42:23 +00001695 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001696 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001697 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001698 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001699
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001700 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001701#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001702 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001703 input_tab(&lastWasTab);
1704 break;
1705#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001706#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001707 case 'A':
1708 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001709 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001710 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001711 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001712 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001713 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001714 break;
1715 case 'B':
1716 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001717 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001718 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001719 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001720 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001721 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001722 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001723 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001724 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001725 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001726#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001727 case 'C':
1728 /* Right Arrow -- Move forward one character */
1729 input_forward();
1730 break;
1731 case 'D':
1732 /* Left Arrow -- Move back one character */
1733 input_backward(1);
1734 break;
1735 case '3':
1736 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001737 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001738 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001739 case '1': // vt100? linux vt? or what?
1740 case '7': // vt100? linux vt? or what?
1741 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001742 input_backward(cursor);
1743 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001744 case '4': // vt100? linux vt? or what?
1745 case '8': // vt100? linux vt? or what?
1746 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001747 input_end();
1748 break;
1749 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001750 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001751 beep();
1752 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001753 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001754
Eric Andersenc470f442003-07-28 09:56:35 +00001755 default: /* If it's regular input, do the normal thing */
Paul Fox84bbac52008-01-11 16:50:08 +00001756
1757 /* Control-V -- force insert of next char */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001758 if (c == CTRL('V')) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001759 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001760 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001761 if (c == 0) {
1762 beep();
1763 break;
1764 }
Paul Fox84bbac52008-01-11 16:50:08 +00001765 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001766
Denis Vlasenko38f63192007-01-22 09:03:07 +00001767#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001768 if (vi_cmdmode) /* Don't self-insert */
1769 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001770#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001771 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001772 break;
1773
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001774 command_len++;
1775 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001776 command[cursor] = c;
1777 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001778 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001779 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001780 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001781
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001782 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001783 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001784 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001785 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001786 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001787 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001788 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001789 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001790 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001791 }
Eric Andersenc470f442003-07-28 09:56:35 +00001792 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001793 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001794
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001795#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001796 if (c != '\t')
1797 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001798#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001799 }
1800
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001801 if (command_len > 0)
1802 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001803
Eric Andersen27bb7902003-12-23 20:24:51 +00001804 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001805 command[command_len++] = '\n';
1806 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001807 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001808
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001809#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001810 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001811#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001812
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001813 /* restore initial_settings */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001814 tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001815 /* restore SIGWINCH handler */
1816 signal(SIGWINCH, previous_SIGWINCH_handler);
1817 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001818
1819 DEINIT_S();
1820
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001821 return command_len;
1822}
1823
1824line_input_t *new_line_input_t(int flags)
1825{
1826 line_input_t *n = xzalloc(sizeof(*n));
1827 n->flags = flags;
1828 return n;
1829}
1830
1831#else
1832
1833#undef read_line_input
1834int read_line_input(const char* prompt, char* command, int maxsize)
1835{
1836 fputs(prompt, stdout);
1837 fflush(stdout);
1838 fgets(command, maxsize, stdin);
1839 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001840}
1841
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001842#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001843
1844
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001845/*
1846 * Testing
1847 */
1848
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001849#ifdef TEST
1850
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001851#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001852
1853const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001854
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001855int main(int argc, char **argv)
1856{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001857 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001858 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001859#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001860 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1861 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1862 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001863#else
1864 "% ";
1865#endif
1866
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001867#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001868 setlocale(LC_ALL, "");
1869#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001870 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001871 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001872 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001873 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001874 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001875 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001876 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001877 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001878 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001879 return 0;
1880}
1881
Eric Andersenc470f442003-07-28 09:56:35 +00001882#endif /* TEST */