blob: 6de66ba83e3b151fa2d894e4c11bd336da3a2945 [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]);
Eric Andersenc470f442003-07-28 09:56:35 +0000520 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000521 continue;
522
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 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +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);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000537 /* hmm, remover in progress? */
Denis Vlasenko39487e22008-02-14 19:55:58 +0000538 if (lstat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000539 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000540 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000541 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000542 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000543
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000544 len1 = strlen(found);
545 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000546 found[len1] = '\0';
547 found[len1+1] = '\0';
548
Mark Whitley4e338752001-01-26 20:42:23 +0000549 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000550 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000551 if (found[len1-1] != '/') {
552 found[len1] = '/';
553 }
Mark Whitley4e338752001-01-26 20:42:23 +0000554 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000555 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000556 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000557 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000558 }
Mark Whitley4e338752001-01-26 20:42:23 +0000559 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000560 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000561 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000562 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000563 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000564 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000565 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000566 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000567 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000568 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000569 free(paths);
570 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000571#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000572}
Erik Andersenf0657d32000-04-12 17:49:52 +0000573
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000574#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000575
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000576#define collapse_pos(is, in) do { \
577 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
578 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
579} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000580
581static int find_match(char *matchBuf, int *len_with_quotes)
582{
583 int i, j;
584 int command_mode;
585 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000586/* int16_t int_buf[MAX_LINELEN + 1]; */
587/* int16_t pos_buf[MAX_LINELEN + 1]; */
588#define int_buf (S.find_match__int_buf)
589#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000590
591 /* set to integer dimension characters and own positions */
592 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000593 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000594 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000595 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000596 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000597 }
598 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000599 }
600
601 /* mask \+symbol and convert '\t' to ' ' */
602 for (i = j = 0; matchBuf[i]; i++, j++)
603 if (matchBuf[i] == '\\') {
604 collapse_pos(j, j + 1);
605 int_buf[j] |= QUOT;
606 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000607#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000608 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000609 int_buf[j] = ' ' | QUOT;
610#endif
611 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000612#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000613 else if (matchBuf[i] == '\t')
614 int_buf[j] = ' ';
615#endif
616
617 /* mask "symbols" or 'symbols' */
618 c2 = 0;
619 for (i = 0; int_buf[i]; i++) {
620 c = int_buf[i];
621 if (c == '\'' || c == '"') {
622 if (c2 == 0)
623 c2 = c;
624 else {
625 if (c == c2)
626 c2 = 0;
627 else
628 int_buf[i] |= QUOT;
629 }
630 } else if (c2 != 0 && c != '$')
631 int_buf[i] |= QUOT;
632 }
633
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000634 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000635 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
636 for (i = 0; int_buf[i]; i++) {
637 c = int_buf[i];
638 c2 = int_buf[i + 1];
639 j = i ? int_buf[i - 1] : -1;
640 command_mode = 0;
641 if (c == ';' || c == '&' || c == '|') {
642 command_mode = 1 + (c == c2);
643 if (c == '&') {
644 if (j == '>' || j == '<')
645 command_mode = 0;
646 } else if (c == '|' && j == '>')
647 command_mode = 0;
648 }
649 if (command_mode) {
650 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000651 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000652 }
653 }
654 /* collapse `command...` */
655 for (i = 0; int_buf[i]; i++)
656 if (int_buf[i] == '`') {
657 for (j = i + 1; int_buf[j]; j++)
658 if (int_buf[j] == '`') {
659 collapse_pos(i, j + 1);
660 j = 0;
661 break;
662 }
663 if (j) {
664 /* not found close ` - command mode, collapse all previous */
665 collapse_pos(0, i + 1);
666 break;
667 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000668 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000669 }
670
671 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000672 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000673 c2 = 0;
674 for (i = 0; int_buf[i]; i++)
675 if (int_buf[i] == '(' || int_buf[i] == '{') {
676 if (int_buf[i] == '(')
677 c++;
678 else
679 c2++;
680 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000681 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000682 }
683 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
684 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
685 if (int_buf[i] == ')')
686 c--;
687 else
688 c2--;
689 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000690 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000691 }
692
693 /* skip first not quote space */
694 for (i = 0; int_buf[i]; i++)
695 if (int_buf[i] != ' ')
696 break;
697 if (i)
698 collapse_pos(0, i);
699
700 /* set find mode for completion */
701 command_mode = FIND_EXE_ONLY;
702 for (i = 0; int_buf[i]; i++)
703 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
704 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000705 && matchBuf[pos_buf[0]] == 'c'
706 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000707 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000708 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000709 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000710 command_mode = FIND_FILE_ONLY;
711 break;
712 }
713 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000714 for (i = 0; int_buf[i]; i++)
715 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000716 /* find last word */
717 for (--i; i >= 0; i--) {
718 c = int_buf[i];
719 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
720 collapse_pos(0, i + 1);
721 break;
722 }
723 }
724 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000725 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
726 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000727 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000728 while ((int_buf[i] & ~QUOT) == '/'
729 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
730 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000731 i++;
732 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000733
734 /* set only match and destroy quotes */
735 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000736 for (c = 0; pos_buf[i] >= 0; i++) {
737 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000738 j = pos_buf[i] + 1;
739 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000740 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000741 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000742 *len_with_quotes = j ? j - pos_buf[0] : 0;
743
744 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000745#undef int_buf
746#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000747}
748
Glenn L McGrath4d001292003-01-06 01:11:50 +0000749/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000750 * display by column (original idea from ls applet,
751 * very optimized by me :)
752 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000753static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000754{
755 int ncols, row;
756 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000757 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000758 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000759 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000760
761 /* find the longest file name- use that as the column width */
762 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000763 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000764 if (column_width < l)
765 column_width = l;
766 }
767 column_width += 2; /* min space for columns */
768 ncols = cmdedit_termw / column_width;
769
770 if (ncols > 1) {
771 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000772 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000773 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000774 } else {
775 ncols = 1;
776 }
777 for (row = 0; row < nrows; row++) {
778 int n = row;
779 int nc;
780
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000781 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000782 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000783 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000784 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000785 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000786 }
787}
788
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000789static char *add_quote_for_spec_chars(char *found)
790{
791 int l = 0;
792 char *s = xmalloc((strlen(found) + 1) * 2);
793
794 while (*found) {
795 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
796 s[l++] = '\\';
797 s[l++] = *found++;
798 }
799 s[l] = 0;
800 return s;
801}
802
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000803/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000804static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000805{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000806 if (!(state->flags & TAB_COMPLETION))
807 return;
808
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000809 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000810 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000811 int len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000812/* char matchBuf[MAX_LINELEN]; */
813#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000814 int find_type;
815 int recalc_pos;
816
Eric Andersenc470f442003-07-28 09:56:35 +0000817 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000818
819 /* Make a local copy of the string -- up
820 * to the position of the cursor */
821 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000822 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000823
824 find_type = find_match(matchBuf, &recalc_pos);
825
826 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000827 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000828
Denis Vlasenko38f63192007-01-22 09:03:07 +0000829#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000830 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000831 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000832 if (state->flags & USERNAME_COMPLETION)
833 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
834 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000835#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000836 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000837 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000838 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000839 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000840 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000841 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000842 int i, n = 0;
Denis Vlasenkofb290382008-03-02 12:51:26 +0000843 qsort_string_vector(matches, num_matches);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000844 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000845 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000846 if (strcmp(matches[i], matches[i+1]) == 0) {
847 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000848 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000849 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000850 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000851 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000852 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000853 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000854 matches[n] = matches[i];
855 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000856 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000857 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000858 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000859 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000860 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000861 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000862 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000863 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000864 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000865 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000866 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000867 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000868 break;
869 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000870 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000871 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000872 return;
873 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000874 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000875 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000876 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000877 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000878 /* for next completion current found */
879 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000880
881 len_found = strlen(tmp);
882 if (tmp[len_found-1] != '/') {
883 tmp[len_found] = ' ';
884 tmp[len_found+1] = '\0';
885 }
Mark Whitley4e338752001-01-26 20:42:23 +0000886 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000887 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000888 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000889 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000890 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000891 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000892 /* save tail line */
893 strcpy(matchBuf, command_ps + cursor);
894 /* add match */
895 strcat(command_ps, tmp);
896 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000897 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000898 /* back to begin word for match */
899 input_backward(recalc_pos);
900 /* new pos */
901 recalc_pos = cursor + len_found;
902 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000903 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000904 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000905 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000906 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000907 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000908#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000909 } else {
910 /* Ok -- the last char was a TAB. Since they
911 * just hit TAB again, print a list of all the
912 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000913 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000914 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000915
916 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000917 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000918 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000919 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000920 }
921 }
922}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000923
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000924#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000925
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000926
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000927#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000928
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000929/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000930static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000931{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000932 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
933 free(state->history[state->cur_history]);
934 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000935 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000936 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000937}
938
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000939static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000940{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000941 if (state->flags & DO_HISTORY) {
942 int ch = state->cur_history;
943 if (ch < state->cnt_history) {
944 get_previous_history(); /* save the current history line */
945 state->cur_history = ch + 1;
946 return state->cur_history;
947 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000948 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000949 beep();
950 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000951}
Robert Griebl350d26b2002-12-03 22:45:46 +0000952
Denis Vlasenko38f63192007-01-22 09:03:07 +0000953#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000954/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000955static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000956{
Robert Griebl350d26b2002-12-03 22:45:46 +0000957 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000958 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000959
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000960 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +0000961
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000962 fp = fopen(fromfile, "r");
963 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000964 /* clean up old history */
965 for (hi = state->cnt_history; hi > 0;) {
966 hi--;
967 free(state->history[hi]);
968 }
969
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000970 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000971 char *hl = xmalloc_fgetline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000972 int l;
973
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000974 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000975 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000976 l = strlen(hl);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000977 if (l >= MAX_LINELEN)
978 hl[MAX_LINELEN-1] = '\0';
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000979 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000980 free(hl);
981 continue;
982 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000983 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000984 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000985 fclose(fp);
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000986 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000987 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000988}
989
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000990/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000991static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000992{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000993 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +0000994
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000995 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000996 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000997 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000998
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000999 for (i = 0; i < state->cnt_history; i++) {
1000 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001001 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001002 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001003 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001004}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001005#else
1006#define load_history(a) ((void)0)
1007#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001008#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001009
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001010static void remember_in_history(const char *str)
1011{
1012 int i;
1013
1014 if (!(state->flags & DO_HISTORY))
1015 return;
1016
1017 i = state->cnt_history;
1018 free(state->history[MAX_HISTORY]);
1019 state->history[MAX_HISTORY] = NULL;
1020 /* After max history, remove the oldest command */
1021 if (i >= MAX_HISTORY) {
1022 free(state->history[0]);
1023 for (i = 0; i < MAX_HISTORY-1; i++)
1024 state->history[i] = state->history[i+1];
1025 }
1026// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
1027// (i.e. do not save dups?)
1028 state->history[i++] = xstrdup(str);
1029 state->cur_history = i;
1030 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001031#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001032 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001033 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +00001034#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00001035 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001036}
1037
1038#else /* MAX_HISTORY == 0 */
1039#define remember_in_history(a) ((void)0)
1040#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001041
1042
Erik Andersen6273f652000-03-17 01:12:41 +00001043/*
1044 * This function is used to grab a character buffer
1045 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001046 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001047 * a mini readline).
1048 *
1049 * The following standard commands are not implemented:
1050 * ESC-b -- Move back one word
1051 * ESC-f -- Move forward one word
1052 * ESC-d -- Delete back one word
1053 * ESC-h -- Delete forward one word
1054 * CTL-t -- Transpose two characters
1055 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001056 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001057 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001058 */
Eric Andersenc470f442003-07-28 09:56:35 +00001059
Denis Vlasenko38f63192007-01-22 09:03:07 +00001060#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001061static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001062vi_Word_motion(char *command, int eat)
1063{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001064 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001065 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001066 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001067 input_forward();
1068}
1069
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001070static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001071vi_word_motion(char *command, int eat)
1072{
1073 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001074 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001075 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001076 input_forward();
1077 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001078 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001079 input_forward();
1080 }
1081
Denis Vlasenko253ce002007-01-22 08:34:44 +00001082 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001083 input_forward();
1084
Denis Vlasenko253ce002007-01-22 08:34:44 +00001085 if (eat && cursor < command_len && isspace(command[cursor]))
1086 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001087 input_forward();
1088}
1089
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001090static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001091vi_End_motion(char *command)
1092{
1093 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001094 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001095 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001096 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001097 input_forward();
1098}
1099
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001100static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001101vi_end_motion(char *command)
1102{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001103 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001104 return;
1105 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001106 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001107 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001108 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001109 return;
1110 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001111 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001112 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1113 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001114 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001115 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001116 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001117 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001118 input_forward();
1119 }
1120}
1121
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001122static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001123vi_Back_motion(char *command)
1124{
1125 while (cursor > 0 && isspace(command[cursor-1]))
1126 input_backward(1);
1127 while (cursor > 0 && !isspace(command[cursor-1]))
1128 input_backward(1);
1129}
1130
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001131static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001132vi_back_motion(char *command)
1133{
1134 if (cursor <= 0)
1135 return;
1136 input_backward(1);
1137 while (cursor > 0 && isspace(command[cursor]))
1138 input_backward(1);
1139 if (cursor <= 0)
1140 return;
1141 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001142 while (cursor > 0
1143 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1144 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001145 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001146 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001147 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001148 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001149 input_backward(1);
1150 }
1151}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001152#endif
1153
1154
1155/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001156 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001157 */
1158
Denis Vlasenko38f63192007-01-22 09:03:07 +00001159#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001160static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001161{
1162 cmdedit_prompt = prmt_ptr;
1163 cmdedit_prmt_len = strlen(prmt_ptr);
1164 put_prompt();
1165}
1166#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001167static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001168{
1169 int prmt_len = 0;
1170 size_t cur_prmt_len = 0;
1171 char flg_not_length = '[';
1172 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001173 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1174 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001175 char c;
1176 char *pbuf;
1177
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001178 cmdedit_prmt_len = 0;
1179
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001180 if (!cwd_buf) {
1181 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001182 }
1183
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001184 cbuf[1] = '\0'; /* never changes */
1185
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001186 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001187 char *free_me = NULL;
1188
1189 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001190 c = *prmt_ptr++;
1191 if (c == '\\') {
1192 const char *cp = prmt_ptr;
1193 int l;
1194
1195 c = bb_process_escape_sequence(&prmt_ptr);
1196 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001197 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001198 break;
1199 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001200
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001201 switch (c) {
1202#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1203 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001204 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001205 break;
1206#endif
1207 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001208 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001209 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001210 break;
1211 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001212 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001213 break;
1214#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1215 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001216 /* /home/user[/something] -> ~[/something] */
1217 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001218 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001219 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001220 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1221 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1222 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001223 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001224 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001225 }
1226 break;
1227#endif
1228 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001229 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001230 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001231 if (cp != NULL && cp != pbuf)
1232 pbuf += (cp-pbuf) + 1;
1233 break;
1234 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001235 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001236 break;
1237 case 'e': case 'E': /* \e \E = \033 */
1238 c = '\033';
1239 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001240 case 'x': case 'X': {
1241 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001242 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001243 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001244 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001245 buf2[l] = '\0';
1246 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001247 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001248 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001249 break;
1250 }
1251 prmt_ptr++;
1252 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001253 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001254 if (c == 0)
1255 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001256 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001257 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001258 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001259 case '[': case ']':
1260 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001261 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001262 continue;
1263 }
1264 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001265 } /* switch */
1266 } /* if */
1267 } /* if */
1268 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001269 cur_prmt_len = strlen(pbuf);
1270 prmt_len += cur_prmt_len;
1271 if (flg_not_length != ']')
1272 cmdedit_prmt_len += cur_prmt_len;
1273 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001274 free(free_me);
1275 } /* while */
1276
1277 if (cwd_buf != (char *)bb_msg_unknown)
1278 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001279 cmdedit_prompt = prmt_mem_ptr;
1280 put_prompt();
1281}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001282#endif
1283
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001284static void cmdedit_setwidth(unsigned w, int redraw_flg)
1285{
1286 cmdedit_termw = w;
1287 if (redraw_flg) {
1288 /* new y for current cursor */
1289 int new_y = (cursor + cmdedit_prmt_len) / w;
1290 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001291 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001292 fflush(stdout);
1293 }
1294}
1295
1296static void win_changed(int nsig)
1297{
1298 int width;
1299 get_terminal_width_height(0, &width, NULL);
1300 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1301 if (nsig == SIGWINCH)
1302 signal(SIGWINCH, win_changed); /* rearm ourself */
1303}
1304
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001305/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001306 * The emacs and vi modes share much of the code in the big
1307 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001308 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001309 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001310 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001311 */
1312
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001313#define vbit 0x100
1314
1315/* leave out the "vi-mode"-only case labels if vi editing isn't
1316 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001317#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001318
1319/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001320#undef CTRL
1321#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001322
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001323/* Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001324 * -1 on read errors or EOF, or on bare Ctrl-D,
1325 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001326 * >0 length of input string, including terminating '\n'
1327 */
Denis Vlasenko037576d2007-10-20 18:30:38 +00001328int read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001329{
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001330#if ENABLE_FEATURE_TAB_COMPLETION
1331 smallint lastWasTab = FALSE;
1332#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001333 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001334 unsigned char c;
1335 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001336#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001337 smallint vi_cmdmode = 0;
1338 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001339#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001340 struct termios initial_settings;
1341 struct termios new_settings;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001342
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001343 INIT_S();
1344
1345 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1346 || !(initial_settings.c_lflag & ECHO)
1347 ) {
1348 /* Happens when e.g. stty -echo was run before */
1349 int len;
1350 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001351 fflush(stdout);
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001352 if (fgets(command, maxsize, stdin) == NULL)
1353 len = -1; /* EOF or error */
1354 else
1355 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001356 DEINIT_S();
1357 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001358 }
1359
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001360// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001361 if (maxsize > MAX_LINELEN)
1362 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001363
1364 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001365 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001366#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001367 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001368 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001369#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001370
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001371 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001372 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001373 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001374 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001375 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001376
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001377 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001378 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1379 /* Turn off echoing and CTRL-C, so we can trap it */
1380 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001381 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001382 new_settings.c_cc[VMIN] = 1;
1383 new_settings.c_cc[VTIME] = 0;
1384 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001385#ifndef _POSIX_VDISABLE
1386#define _POSIX_VDISABLE '\0'
1387#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001388 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001389 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001390
Eric Andersen6faae7d2001-02-16 20:09:17 +00001391 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001392 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1393 win_changed(0); /* do initial resizing */
1394#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1395 {
1396 struct passwd *entry;
1397
1398 entry = getpwuid(geteuid());
1399 if (entry) {
1400 user_buf = xstrdup(entry->pw_name);
1401 home_pwd_buf = xstrdup(entry->pw_dir);
1402 }
1403 }
1404#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001405 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001406 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001407
Erik Andersenc7c634b2000-03-19 05:28:55 +00001408 while (1) {
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001409 fflush(NULL);
Mark Whitley4e338752001-01-26 20:42:23 +00001410
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001411 if (nonblock_safe_read(STDIN_FILENO, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001412 /* if we can't read input then exit */
1413 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001414 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001415
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001416 ic = c;
1417
Denis Vlasenko38f63192007-01-22 09:03:07 +00001418#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001419 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001420 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001421 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001422#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001423 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001424 case '\n':
1425 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001426 vi_case('\n'|vbit:)
1427 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001428 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001429 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001430 break_out = 1;
1431 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001432 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001433 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001434 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001435 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001436 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001437 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001438 vi_case('h'|vbit:)
1439 vi_case('\b'|vbit:)
1440 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001441 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001442 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001443 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001444 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001445 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001446 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001447 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001448 command_len = 0;
1449 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001450 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001451 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001452 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001453 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001454 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001455 errno = 0;
1456 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001457 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001458 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001459 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001460 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001461 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001462 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001463
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001464 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001465 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001466 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001467 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001468 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001469 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001470 vi_case('l'|vbit:)
1471 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001472 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001473 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001474 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001475
Erik Andersenf0657d32000-04-12 17:49:52 +00001476 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001477 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001478 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001479 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001480 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001481
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001482#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001483 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001484 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001485 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001486#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001487
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001488 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001489 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001490 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001491 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001492 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001493 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001494 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001495 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001496 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001497 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001498 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001499 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001500
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001501#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001502 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001503 vi_case(CTRL('N')|vbit:)
1504 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001505 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001506 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001507 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001508 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001509 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001510 vi_case(CTRL('P')|vbit:)
1511 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001512 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001513 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001514 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001515 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001516 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001517 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001518 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001519#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001520
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001521 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001522 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001523 /* Control-U -- Clear line before cursor */
1524 if (cursor) {
1525 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001526 command_len -= cursor;
1527 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001528 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001529 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001530 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001531 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001532 /* Control-W -- Remove the last word */
1533 while (cursor > 0 && isspace(command[cursor-1]))
1534 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001535 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001536 input_backspace();
1537 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001538
Denis Vlasenko38f63192007-01-22 09:03:07 +00001539#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001540 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001541 vi_cmdmode = 0;
1542 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001543 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001544 input_backward(cursor);
1545 vi_cmdmode = 0;
1546 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001547 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001548 input_forward();
1549 vi_cmdmode = 0;
1550 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001551 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001552 input_end();
1553 vi_cmdmode = 0;
1554 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001555 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001556 input_delete(1);
1557 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001558 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001559 if (cursor > 0) {
1560 input_backward(1);
1561 input_delete(1);
1562 }
1563 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001564 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001565 vi_Word_motion(command, 1);
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 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001571 vi_End_motion(command);
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 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001577 vi_Back_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 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001583 vi_cmdmode = 0;
1584 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001585 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001586 goto clear_to_eol;
1587
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001588 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001589 vi_cmdmode = 0;
1590 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001591 case 'd'|vbit: {
1592 int nc, sc;
1593 sc = cursor;
1594 prevc = ic;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001595 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001596 goto prepare_to_die;
1597 if (c == (prevc & 0xff)) {
1598 /* "cc", "dd" */
1599 input_backward(cursor);
1600 goto clear_to_eol;
1601 break;
1602 }
1603 switch (c) {
1604 case 'w':
1605 case 'W':
1606 case 'e':
1607 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001608 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001609 case 'w': /* "dw", "cw" */
1610 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001611 break;
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 'e': /* 'de', 'ce' */
1616 vi_end_motion(command);
1617 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001618 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001619 case 'E': /* 'dE', 'cE' */
1620 vi_End_motion(command);
1621 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001622 break;
1623 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001624 nc = cursor;
1625 input_backward(cursor - sc);
1626 while (nc-- > cursor)
1627 input_delete(1);
1628 break;
1629 case 'b': /* "db", "cb" */
1630 case 'B': /* implemented as B */
1631 if (c == 'b')
1632 vi_back_motion(command);
1633 else
1634 vi_Back_motion(command);
1635 while (sc-- > cursor)
1636 input_delete(1);
1637 break;
1638 case ' ': /* "d ", "c " */
1639 input_delete(1);
1640 break;
1641 case '$': /* "d$", "c$" */
1642 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001643 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001644 input_delete(1);
1645 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001646 }
1647 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001648 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001649 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001650 input_forward();
1651 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001652 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001653 put();
1654 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001655 case 'r'|vbit:
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001656 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001657 goto prepare_to_die;
1658 if (c == 0)
1659 beep();
1660 else {
1661 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001662 bb_putchar(c);
1663 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001664 }
1665 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001666#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001667
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001668 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001669
Denis Vlasenko38f63192007-01-22 09:03:07 +00001670#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001671 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001672 /* ESC: insert mode --> command mode */
1673 vi_cmdmode = 1;
1674 input_backward(1);
1675 break;
1676 }
1677#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001678 /* escape sequence follows */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001679 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001680 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001681 /* different vt100 emulations */
1682 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001683 vi_case('['|vbit:)
1684 vi_case('O'|vbit:)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001685 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001686 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001687 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001688 if (c >= '1' && c <= '9') {
1689 unsigned char dummy;
1690
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001691 if (safe_read(STDIN_FILENO, &dummy, 1) < 1)
Glenn L McGrath475820c2004-01-22 12:42:23 +00001692 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001693 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001694 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001695 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001696
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001697 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001698#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001699 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001700 input_tab(&lastWasTab);
1701 break;
1702#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001703#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001704 case 'A':
1705 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001706 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001707 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001708 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001709 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001710 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001711 break;
1712 case 'B':
1713 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001714 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001715 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001716 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001717 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001718 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001719 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001720 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001721 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001722 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001723#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001724 case 'C':
1725 /* Right Arrow -- Move forward one character */
1726 input_forward();
1727 break;
1728 case 'D':
1729 /* Left Arrow -- Move back one character */
1730 input_backward(1);
1731 break;
1732 case '3':
1733 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001734 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001735 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001736 case '1': // vt100? linux vt? or what?
1737 case '7': // vt100? linux vt? or what?
1738 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001739 input_backward(cursor);
1740 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001741 case '4': // vt100? linux vt? or what?
1742 case '8': // vt100? linux vt? or what?
1743 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001744 input_end();
1745 break;
1746 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001747 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001748 beep();
1749 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001750 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001751
Eric Andersenc470f442003-07-28 09:56:35 +00001752 default: /* If it's regular input, do the normal thing */
Paul Fox84bbac52008-01-11 16:50:08 +00001753
1754 /* Control-V -- force insert of next char */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001755 if (c == CTRL('V')) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001756 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001757 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001758 if (c == 0) {
1759 beep();
1760 break;
1761 }
Paul Fox84bbac52008-01-11 16:50:08 +00001762 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001763
Denis Vlasenko38f63192007-01-22 09:03:07 +00001764#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001765 if (vi_cmdmode) /* Don't self-insert */
1766 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001767#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001768 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001769 break;
1770
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001771 command_len++;
1772 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001773 command[cursor] = c;
1774 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001775 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001776 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001777 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001778
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001779 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001780 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001781 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001782 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001783 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001784 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001785 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001786 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001787 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001788 }
Eric Andersenc470f442003-07-28 09:56:35 +00001789 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001790 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001791
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001792#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001793 if (c != '\t')
1794 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001795#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001796 }
1797
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001798 if (command_len > 0)
1799 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001800
Eric Andersen27bb7902003-12-23 20:24:51 +00001801 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001802 command[command_len++] = '\n';
1803 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001804 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001805
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001806#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001807 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001808#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001809
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001810 /* restore initial_settings */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001811 tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001812 /* restore SIGWINCH handler */
1813 signal(SIGWINCH, previous_SIGWINCH_handler);
1814 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001815
1816 DEINIT_S();
1817
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001818 return command_len;
1819}
1820
1821line_input_t *new_line_input_t(int flags)
1822{
1823 line_input_t *n = xzalloc(sizeof(*n));
1824 n->flags = flags;
1825 return n;
1826}
1827
1828#else
1829
1830#undef read_line_input
1831int read_line_input(const char* prompt, char* command, int maxsize)
1832{
1833 fputs(prompt, stdout);
1834 fflush(stdout);
1835 fgets(command, maxsize, stdin);
1836 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001837}
1838
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001839#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001840
1841
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001842/*
1843 * Testing
1844 */
1845
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001846#ifdef TEST
1847
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001848#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001849
1850const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001851
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001852int main(int argc, char **argv)
1853{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001854 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001855 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001856#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001857 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1858 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1859 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001860#else
1861 "% ";
1862#endif
1863
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001864#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001865 setlocale(LC_ALL, "");
1866#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001867 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001868 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001869 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001870 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001871 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001872 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001873 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001874 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001875 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001876 return 0;
1877}
1878
Eric Andersenc470f442003-07-28 09:56:35 +00001879#endif /* TEST */