blob: 3ef47ba71336ce8397cc887fd5624445cf6a71b6 [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 Vlasenko78ff8192008-06-28 21:03:43 +000018 * Usage and known bugs:
19 * 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.
23 * Ctrl-E also works as End.
24 *
25 * lineedit does not know that the terminal escape sequences do not
26 * take up space on the screen. The redisplay code assumes, unless
27 * told otherwise, that each character in the prompt is a printable
28 * character that takes up one character position on the screen.
29 * You need to tell lineedit that some sequences of characters
30 * in the prompt take up no screen space. Compatibly with readline,
31 * use the \[ escape to begin a sequence of non-printing characters,
32 * and the \] escape to signal the end of such a sequence. Example:
33 *
34 * PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '
Erik Andersen13456d12000-03-16 08:09:57 +000035 */
36
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000037#include "libbb.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000038
Glenn L McGrath475820c2004-01-22 12:42:23 +000039
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000040/* FIXME: obsolete CONFIG item? */
41#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
42
43
Glenn L McGrath3b251852004-01-03 12:07:32 +000044#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000045
Denis Vlasenko38f63192007-01-22 09:03:07 +000046#define ENABLE_FEATURE_EDITING 0
47#define ENABLE_FEATURE_TAB_COMPLETION 0
48#define ENABLE_FEATURE_USERNAME_COMPLETION 0
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000049#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +000050
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000051#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000052
Eric Andersen5165fbe2001-02-20 06:42:29 +000053
Denis Vlasenko82b39e82007-01-21 19:18:19 +000054/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko38f63192007-01-22 09:03:07 +000055#if ENABLE_FEATURE_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000056
Denis Vlasenko82b39e82007-01-21 19:18:19 +000057#if ENABLE_LOCALE_SUPPORT
58#define Isprint(c) isprint(c)
59#else
60#define Isprint(c) ((c) >= ' ' && (c) != ((unsigned char)'\233'))
61#endif
62
Denis Vlasenko7e46cf72006-12-23 01:21:55 +000063#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000064 (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
65#define USE_FEATURE_GETUSERNAME_AND_HOMEDIR(...)
66#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
67#undef USE_FEATURE_GETUSERNAME_AND_HOMEDIR
68#define USE_FEATURE_GETUSERNAME_AND_HOMEDIR(...) __VA_ARGS__
69#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000070
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000071enum {
72 /* We use int16_t for positions, need to limit line len */
73 MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN < 0x7ff0
Denis Vlasenko6b404432008-01-07 16:13:14 +000074 ? CONFIG_FEATURE_EDITING_MAX_LEN
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000075 : 0x7ff0
76};
Robert Griebl350d26b2002-12-03 22:45:46 +000077
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000078#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
79static const char null_str[] ALIGN1 = "";
80#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +000081
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000082/* We try to minimize both static and stack usage. */
Denis Vlasenko5d89fba2008-04-22 00:08:27 +000083struct lineedit_statics {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000084 line_input_t *state;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000085
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000086 volatile unsigned cmdedit_termw; /* = 80; */ /* actual terminal width */
87 sighandler_t previous_SIGWINCH_handler;
Mark Whitley4e338752001-01-26 20:42:23 +000088
Denis Vlasenkob267ed92008-05-25 21:52:03 +000089 unsigned cmdedit_x; /* real x terminal position */
90 unsigned cmdedit_y; /* pseudoreal y terminal position */
91 unsigned cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000092
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000093 unsigned cursor;
94 unsigned command_len;
95 char *command_ps;
96
97 const char *cmdedit_prompt;
Denis Vlasenko38f63192007-01-22 09:03:07 +000098#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000099 int num_ok_lines; /* = 1; */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000100#endif
101
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000102#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000103 char *user_buf;
104 char *home_pwd_buf; /* = (char*)null_str; */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000105#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000106
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000107#if ENABLE_FEATURE_TAB_COMPLETION
108 char **matches;
109 unsigned num_matches;
110#endif
111
112#if ENABLE_FEATURE_EDITING_VI
113#define DELBUFSIZ 128
114 char *delptr;
115 smallint newdelflag; /* whether delbuf should be reused yet */
116 char delbuf[DELBUFSIZ]; /* a place to store deleted characters */
117#endif
118
119 /* Formerly these were big buffers on stack: */
120#if ENABLE_FEATURE_TAB_COMPLETION
121 char exe_n_cwd_tab_completion__dirbuf[MAX_LINELEN];
122 char input_tab__matchBuf[MAX_LINELEN];
123 int16_t find_match__int_buf[MAX_LINELEN + 1]; /* need to have 9 bits at least */
124 int16_t find_match__pos_buf[MAX_LINELEN + 1];
125#endif
126};
127
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000128/* See lineedit_ptr_hack.c */
129extern struct lineedit_statics *const lineedit_ptr_to_statics;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000130
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000131#define S (*lineedit_ptr_to_statics)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000132#define state (S.state )
133#define cmdedit_termw (S.cmdedit_termw )
134#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
135#define cmdedit_x (S.cmdedit_x )
136#define cmdedit_y (S.cmdedit_y )
137#define cmdedit_prmt_len (S.cmdedit_prmt_len)
138#define cursor (S.cursor )
139#define command_len (S.command_len )
140#define command_ps (S.command_ps )
141#define cmdedit_prompt (S.cmdedit_prompt )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000142#define num_ok_lines (S.num_ok_lines )
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000143#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000144#define home_pwd_buf (S.home_pwd_buf )
145#define matches (S.matches )
146#define num_matches (S.num_matches )
147#define delptr (S.delptr )
148#define newdelflag (S.newdelflag )
149#define delbuf (S.delbuf )
150
151#define INIT_S() do { \
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000152 (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000153 barrier(); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000154 cmdedit_termw = 80; \
155 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
156 USE_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \
157} while (0)
158static void deinit_S(void)
159{
160#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000161 /* This one is allocated only if FANCY_PROMPT is on
162 * (otherwise it points to verbatim prompt (NOT malloced) */
163 free((char*)cmdedit_prompt);
164#endif
165#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
166 free(user_buf);
167 if (home_pwd_buf != null_str)
168 free(home_pwd_buf);
169#endif
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000170 free(lineedit_ptr_to_statics);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000171}
172#define DEINIT_S() deinit_S()
173
Denis Vlasenko2c844952008-04-25 18:44:35 +0000174
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000175/* Put 'command_ps[cursor]', cursor++.
176 * Advance cursor on screen. If we reached right margin, scroll text up
177 * and remove terminal margin effect by printing 'next_char' */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000178#define HACK_FOR_WRONG_WIDTH 1
179#if HACK_FOR_WRONG_WIDTH
180static void cmdedit_set_out_char(void)
181#define cmdedit_set_out_char(next_char) cmdedit_set_out_char()
182#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000183static void cmdedit_set_out_char(int next_char)
Denis Vlasenko2c844952008-04-25 18:44:35 +0000184#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000185{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000186 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000187
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000188 if (c == '\0') {
189 /* erase character after end of input string */
190 c = ' ';
191 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000192#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000193 /* Display non-printable characters in reverse */
194 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000195 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000196 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000197 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000198 c += '@';
199 if (c == 127)
200 c = '?';
201 printf("\033[7m%c\033[0m", c);
202 } else
203#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000204 {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000205 bb_putchar(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000206 }
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000207 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000208 /* terminal is scrolled down */
209 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000210 cmdedit_x = 0;
Denis Vlasenko2c844952008-04-25 18:44:35 +0000211#if HACK_FOR_WRONG_WIDTH
212 /* This works better if our idea of term width is wrong
213 * and it is actually wider (often happens on serial lines).
214 * Printing CR,LF *forces* cursor to next line.
215 * OTOH if terminal width is correct AND terminal does NOT
216 * have automargin (IOW: it is moving cursor to next line
217 * by itself (which is wrong for VT-10x terminals)),
218 * this will break things: there will be one extra empty line */
219 puts("\r"); /* + implicit '\n' */
220#else
221 /* Works ok only if cmdedit_termw is correct */
Mark Whitley4e338752001-01-26 20:42:23 +0000222 /* destroy "(auto)margin" */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000223 bb_putchar(next_char);
224 bb_putchar('\b');
Denis Vlasenko2c844952008-04-25 18:44:35 +0000225#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000226 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000227// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000228 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000229}
230
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000231/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000232static void input_end(void)
233{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000234 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000235 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000236}
237
238/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000239static void goto_new_line(void)
240{
Mark Whitley4e338752001-01-26 20:42:23 +0000241 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000242 if (cmdedit_x)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000243 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000244}
245
246
Rob Landley88621d72006-08-29 19:41:06 +0000247static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000248{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000249 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000250 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000251}
Eric Andersen81fe1232003-07-29 06:38:40 +0000252
Rob Landley88621d72006-08-29 19:41:06 +0000253static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000254{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000255 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000256}
257
Eric Andersenaff114c2004-04-14 17:51:38 +0000258/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000259/* (optimized for slow terminals) */
260static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000261{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000262 int count_y;
263
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000264 if (num > cursor)
265 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000266 if (!num)
267 return;
268 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000269
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000270 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000271 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000272 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000273 /* This is longer by 5 bytes on x86.
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000274 * Also gets miscompiled for ARM users
275 * (busybox.net/bugs/view.php?id=2274).
Denis Vlasenko6f043912008-02-18 22:28:03 +0000276 * printf(("\b\b\b\b" + 4) - num);
277 * return;
278 */
279 do {
280 bb_putchar('\b');
281 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000282 return;
283 }
284 printf("\033[%uD", num);
285 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000286 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000287
288 /* Need to go one or more lines up */
289 num -= cmdedit_x;
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000290 {
291 unsigned w = cmdedit_termw; /* volatile var */
292 count_y = 1 + (num / w);
293 cmdedit_y -= count_y;
294 cmdedit_x = w * count_y - num;
295 }
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000296 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000297 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000298}
299
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000300static void put_prompt(void)
301{
302 out1str(cmdedit_prompt);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000303 cursor = 0;
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000304 {
305 unsigned w = cmdedit_termw; /* volatile var */
306 cmdedit_y = cmdedit_prmt_len / w; /* new quasireal y */
307 cmdedit_x = cmdedit_prmt_len % w;
308 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000309}
310
Eric Andersenaff114c2004-04-14 17:51:38 +0000311/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000312static void redraw(int y, int back_cursor)
313{
Eric Andersenc470f442003-07-28 09:56:35 +0000314 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000315 printf("\033[%dA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000316 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000317 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000318 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000319 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000320 input_backward(back_cursor);
321}
322
Paul Fox3f11b1b2005-08-04 19:04:46 +0000323/* Delete the char in front of the cursor, optionally saving it
324 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000325#if !ENABLE_FEATURE_EDITING_VI
326static void input_delete(void)
327#define input_delete(save) input_delete()
328#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000329static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000330#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000331{
Mark Whitley4e338752001-01-26 20:42:23 +0000332 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000333
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000334 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000335 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000336
Denis Vlasenko38f63192007-01-22 09:03:07 +0000337#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000338 if (save) {
339 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000340 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000341 newdelflag = 0;
342 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000343 if ((delptr - delbuf) < DELBUFSIZ)
344 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000345 }
346#endif
347
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000348 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000349 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000350 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000351 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000352 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000353}
354
Denis Vlasenko38f63192007-01-22 09:03:07 +0000355#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000356static void put(void)
357{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000358 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000359 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000360
Paul Fox3f11b1b2005-08-04 19:04:46 +0000361 if (j == 0)
362 return;
363 ocursor = cursor;
364 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000365 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000366 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000367 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000368 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000369 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000370}
371#endif
372
Mark Whitley4e338752001-01-26 20:42:23 +0000373/* Delete the char in back of the cursor */
374static void input_backspace(void)
375{
376 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000377 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000378 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000379 }
380}
381
Eric Andersenaff114c2004-04-14 17:51:38 +0000382/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000383static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000384{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000385 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000386 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000387}
388
Denis Vlasenko38f63192007-01-22 09:03:07 +0000389#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000390
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000391static void free_tab_completion_data(void)
392{
393 if (matches) {
394 while (num_matches)
395 free(matches[--num_matches]);
396 free(matches);
397 matches = NULL;
398 }
399}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000400
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000401static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000402{
403 int nm = num_matches;
404 int nm1 = nm + 1;
405
406 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000407 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000408 num_matches++;
409}
410
Denis Vlasenko38f63192007-01-22 09:03:07 +0000411#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000412static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000413{
414 struct passwd *entry;
415 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000416
Eric Andersenc470f442003-07-28 09:56:35 +0000417 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000418 userlen = strlen(ud);
419
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000420 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000421 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000422 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000423
Eric Andersenc470f442003-07-28 09:56:35 +0000424 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000425 home = home_pwd_buf;
426 } else {
427 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000428 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000429 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000430 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000431 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000432 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000433 ud = temp;
434 if (entry)
435 home = entry->pw_dir;
436 }
437 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000438 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000439 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000440 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000441 }
442 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000443 } else {
444 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000445 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000446 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000447 struct passwd pwd;
448 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000449
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000450 setpwent();
451 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000452 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000453 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
454 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000455 }
456 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000457 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000458 }
459}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000460#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000461
462enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000463 FIND_EXE_ONLY = 0,
464 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000465 FIND_FILE_ONLY = 2,
466};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000467
Mark Whitley4e338752001-01-26 20:42:23 +0000468static int path_parse(char ***p, int flags)
469{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000470 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000471 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000472 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000473 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000474
Mark Whitley4e338752001-01-26 20:42:23 +0000475 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000476 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000477 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000478
479 if (state->flags & WITH_PATH_LOOKUP)
480 pth = state->path_lookup;
481 else
482 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000483 /* PATH=<empty> or PATH=:<empty> */
484 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
485 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000486
Denis Vlasenko253ce002007-01-22 08:34:44 +0000487 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000488 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000489 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000490 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000491 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000492 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000493 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000494 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000495 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000496 }
497
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000498 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000499 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000500 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000501 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000502 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000503 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000504 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000505 *tmp++ = '\0'; /* ':' -> '\0' */
506 if (*tmp == '\0')
507 break; /* :<empty> */
508 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000509 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000510 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000511 return npth;
512}
513
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000514static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000515{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000516 DIR *dir;
517 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518 struct stat st;
519 char *path1[1];
520 char **paths = path1;
521 int npaths;
522 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000523 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000524 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000525/* char dirbuf[MAX_LINELEN]; */
526#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000527
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000528 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000529 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000530
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000531 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000532 /* no dir, if flags==EXE_ONLY - get paths, else "." */
533 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000534 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000535 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000536 /* dirbuf = ".../.../.../" */
537 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000538#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000539 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000540 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000541#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000542 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000543 /* point to 'l' in "..../last_component" */
544 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000545 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000546
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000547 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000548 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000549 if (!dir)
550 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000551
552 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000553 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000554 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000555
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000556 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000557 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000558 continue;
559 /* not see .name without .match */
Denis Vlasenkob5202712008-04-24 04:42:52 +0000560 if (*str_found == '.' && *pfind == '\0') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000561 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000562 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000563 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000564 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000565 found = concat_path_file(paths[i], str_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000566 /* hmm, remove in progress? */
567 /* NB: stat() first so that we see is it a directory;
568 * but if that fails, use lstat() so that
569 * we still match dangling links */
570 if (stat(found, &st) && lstat(found, &st))
Eric Andersene5dfced2001-04-09 22:48:12 +0000571 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000572 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000573 if (paths[i] != dirbuf)
Denis Vlasenkob5202712008-04-24 04:42:52 +0000574 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000575
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000576 len1 = strlen(found);
577 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000578 found[len1] = '\0';
579 found[len1+1] = '\0';
580
Mark Whitley4e338752001-01-26 20:42:23 +0000581 if (S_ISDIR(st.st_mode)) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000582 /* name is a directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000583 if (found[len1-1] != '/') {
584 found[len1] = '/';
585 }
Mark Whitley4e338752001-01-26 20:42:23 +0000586 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000587 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000588 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000589 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000590 }
Mark Whitley4e338752001-01-26 20:42:23 +0000591 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000592 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000593 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000594 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000595 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000596 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000597 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000598 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000599 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000600 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000601 free(paths);
602 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000603#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000604}
Erik Andersenf0657d32000-04-12 17:49:52 +0000605
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000606#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000607
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000608#define collapse_pos(is, in) do { \
609 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
610 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
611} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000612
613static int find_match(char *matchBuf, int *len_with_quotes)
614{
615 int i, j;
616 int command_mode;
617 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000618/* int16_t int_buf[MAX_LINELEN + 1]; */
619/* int16_t pos_buf[MAX_LINELEN + 1]; */
620#define int_buf (S.find_match__int_buf)
621#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000622
623 /* set to integer dimension characters and own positions */
624 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000625 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000626 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000627 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000628 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000629 }
630 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000631 }
632
633 /* mask \+symbol and convert '\t' to ' ' */
634 for (i = j = 0; matchBuf[i]; i++, j++)
635 if (matchBuf[i] == '\\') {
636 collapse_pos(j, j + 1);
637 int_buf[j] |= QUOT;
638 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000639#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000640 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000641 int_buf[j] = ' ' | QUOT;
642#endif
643 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000644#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000645 else if (matchBuf[i] == '\t')
646 int_buf[j] = ' ';
647#endif
648
649 /* mask "symbols" or 'symbols' */
650 c2 = 0;
651 for (i = 0; int_buf[i]; i++) {
652 c = int_buf[i];
653 if (c == '\'' || c == '"') {
654 if (c2 == 0)
655 c2 = c;
656 else {
657 if (c == c2)
658 c2 = 0;
659 else
660 int_buf[i] |= QUOT;
661 }
662 } else if (c2 != 0 && c != '$')
663 int_buf[i] |= QUOT;
664 }
665
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000666 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000667 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
668 for (i = 0; int_buf[i]; i++) {
669 c = int_buf[i];
670 c2 = int_buf[i + 1];
671 j = i ? int_buf[i - 1] : -1;
672 command_mode = 0;
673 if (c == ';' || c == '&' || c == '|') {
674 command_mode = 1 + (c == c2);
675 if (c == '&') {
676 if (j == '>' || j == '<')
677 command_mode = 0;
678 } else if (c == '|' && j == '>')
679 command_mode = 0;
680 }
681 if (command_mode) {
682 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000683 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000684 }
685 }
686 /* collapse `command...` */
687 for (i = 0; int_buf[i]; i++)
688 if (int_buf[i] == '`') {
689 for (j = i + 1; int_buf[j]; j++)
690 if (int_buf[j] == '`') {
691 collapse_pos(i, j + 1);
692 j = 0;
693 break;
694 }
695 if (j) {
696 /* not found close ` - command mode, collapse all previous */
697 collapse_pos(0, i + 1);
698 break;
699 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000700 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000701 }
702
703 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000704 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000705 c2 = 0;
706 for (i = 0; int_buf[i]; i++)
707 if (int_buf[i] == '(' || int_buf[i] == '{') {
708 if (int_buf[i] == '(')
709 c++;
710 else
711 c2++;
712 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000713 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000714 }
715 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
716 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
717 if (int_buf[i] == ')')
718 c--;
719 else
720 c2--;
721 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000722 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000723 }
724
725 /* skip first not quote space */
726 for (i = 0; int_buf[i]; i++)
727 if (int_buf[i] != ' ')
728 break;
729 if (i)
730 collapse_pos(0, i);
731
732 /* set find mode for completion */
733 command_mode = FIND_EXE_ONLY;
734 for (i = 0; int_buf[i]; i++)
735 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
736 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000737 && matchBuf[pos_buf[0]] == 'c'
738 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000739 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000740 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000741 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000742 command_mode = FIND_FILE_ONLY;
743 break;
744 }
745 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000746 for (i = 0; int_buf[i]; i++)
747 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000748 /* find last word */
749 for (--i; i >= 0; i--) {
750 c = int_buf[i];
751 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
752 collapse_pos(0, i + 1);
753 break;
754 }
755 }
756 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000757 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
758 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000759 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000760 while ((int_buf[i] & ~QUOT) == '/'
761 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
762 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000763 i++;
764 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000765
766 /* set only match and destroy quotes */
767 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000768 for (c = 0; pos_buf[i] >= 0; i++) {
769 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000770 j = pos_buf[i] + 1;
771 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000772 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000773 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000774 *len_with_quotes = j ? j - pos_buf[0] : 0;
775
776 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000777#undef int_buf
778#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000779}
780
Glenn L McGrath4d001292003-01-06 01:11:50 +0000781/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000782 * display by column (original idea from ls applet,
783 * very optimized by me :)
784 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000785static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000786{
787 int ncols, row;
788 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000789 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000790 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000791 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000792
793 /* find the longest file name- use that as the column width */
794 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000795 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000796 if (column_width < l)
797 column_width = l;
798 }
799 column_width += 2; /* min space for columns */
800 ncols = cmdedit_termw / column_width;
801
802 if (ncols > 1) {
803 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000804 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000805 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000806 } else {
807 ncols = 1;
808 }
809 for (row = 0; row < nrows; row++) {
810 int n = row;
811 int nc;
812
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000813 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000814 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000815 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000816 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000817 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000818 }
819}
820
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000821static char *add_quote_for_spec_chars(char *found)
822{
823 int l = 0;
824 char *s = xmalloc((strlen(found) + 1) * 2);
825
826 while (*found) {
827 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
828 s[l++] = '\\';
829 s[l++] = *found++;
830 }
831 s[l] = 0;
832 return s;
833}
834
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000835/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000836static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000837{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000838 if (!(state->flags & TAB_COMPLETION))
839 return;
840
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000841 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000842 char *tmp, *tmp1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000843 size_t len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000844/* char matchBuf[MAX_LINELEN]; */
845#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000846 int find_type;
847 int recalc_pos;
848
Eric Andersenc470f442003-07-28 09:56:35 +0000849 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000850
851 /* Make a local copy of the string -- up
852 * to the position of the cursor */
853 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000854 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000855
856 find_type = find_match(matchBuf, &recalc_pos);
857
858 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000859 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000860
Denis Vlasenko38f63192007-01-22 09:03:07 +0000861#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000862 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000863 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000864 if (state->flags & USERNAME_COMPLETION)
865 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
866 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000867#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000868 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000869 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000870 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000871 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000872 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000873 if (matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000874 unsigned i;
875 int n = 0;
Denis Vlasenkofb290382008-03-02 12:51:26 +0000876 qsort_string_vector(matches, num_matches);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000877 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000878 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000879 if (strcmp(matches[i], matches[i+1]) == 0) {
880 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000881 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000882 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000883 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000884 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000885 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000886 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000887 matches[n] = matches[i];
888 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000889 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000890 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000891 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000892 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000893 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000894 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000895 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000896 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000897 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000898 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000899 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000900 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000901 break;
902 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000903 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000904 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000905 return;
906 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000907 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000908 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000909 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000910 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000911 /* for next completion current found */
912 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000913
914 len_found = strlen(tmp);
915 if (tmp[len_found-1] != '/') {
916 tmp[len_found] = ' ';
917 tmp[len_found+1] = '\0';
918 }
Mark Whitley4e338752001-01-26 20:42:23 +0000919 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000920 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000921 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000922 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000923 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000924 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000925 /* save tail line */
926 strcpy(matchBuf, command_ps + cursor);
927 /* add match */
928 strcat(command_ps, tmp);
929 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000930 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000931 /* back to begin word for match */
932 input_backward(recalc_pos);
933 /* new pos */
934 recalc_pos = cursor + len_found;
935 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000936 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000937 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000938 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000939 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000940 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000941#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000942 } else {
943 /* Ok -- the last char was a TAB. Since they
944 * just hit TAB again, print a list of all the
945 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000946 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000947 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000948
949 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000950 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000951 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000952 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000953 }
954 }
955}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000956
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000957#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000958
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000959
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000960#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000961
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000962/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000963static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000964{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000965 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
966 free(state->history[state->cur_history]);
967 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000968 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000969 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000970}
971
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000972static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000973{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000974 if (state->flags & DO_HISTORY) {
975 int ch = state->cur_history;
976 if (ch < state->cnt_history) {
977 get_previous_history(); /* save the current history line */
978 state->cur_history = ch + 1;
979 return state->cur_history;
980 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000981 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000982 beep();
983 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000984}
Robert Griebl350d26b2002-12-03 22:45:46 +0000985
Denis Vlasenko38f63192007-01-22 09:03:07 +0000986#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000987/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000988static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000989{
Robert Griebl350d26b2002-12-03 22:45:46 +0000990 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000991 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000992
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000993 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +0000994
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000995 fp = fopen(fromfile, "r");
996 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000997 /* clean up old history */
998 for (hi = state->cnt_history; hi > 0;) {
999 hi--;
1000 free(state->history[hi]);
1001 }
1002
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001003 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko8ee649a2008-03-26 20:04:27 +00001004 char *hl = xmalloc_fgetline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001005 int l;
1006
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001007 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001008 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001009 l = strlen(hl);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001010 if (l >= MAX_LINELEN)
1011 hl[MAX_LINELEN-1] = '\0';
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001012 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001013 free(hl);
1014 continue;
1015 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001016 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001017 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001018 fclose(fp);
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001019 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001020 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001021}
1022
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001023/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +00001024static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +00001025{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001026 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +00001027
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001028 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001029 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +00001030 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001031
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001032 for (i = 0; i < state->cnt_history; i++) {
1033 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001034 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001035 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001036 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001037}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001038#else
1039#define load_history(a) ((void)0)
1040#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001041#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001042
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001043static void remember_in_history(const char *str)
1044{
1045 int i;
1046
1047 if (!(state->flags & DO_HISTORY))
1048 return;
1049
1050 i = state->cnt_history;
1051 free(state->history[MAX_HISTORY]);
1052 state->history[MAX_HISTORY] = NULL;
1053 /* After max history, remove the oldest command */
1054 if (i >= MAX_HISTORY) {
1055 free(state->history[0]);
1056 for (i = 0; i < MAX_HISTORY-1; i++)
1057 state->history[i] = state->history[i+1];
1058 }
1059// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
1060// (i.e. do not save dups?)
1061 state->history[i++] = xstrdup(str);
1062 state->cur_history = i;
1063 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001064#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001065 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001066 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +00001067#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00001068 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001069}
1070
1071#else /* MAX_HISTORY == 0 */
1072#define remember_in_history(a) ((void)0)
1073#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001074
1075
Erik Andersen6273f652000-03-17 01:12:41 +00001076/*
1077 * This function is used to grab a character buffer
1078 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001079 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001080 * a mini readline).
1081 *
1082 * The following standard commands are not implemented:
1083 * ESC-b -- Move back one word
1084 * ESC-f -- Move forward one word
1085 * ESC-d -- Delete back one word
1086 * ESC-h -- Delete forward one word
1087 * CTL-t -- Transpose two characters
1088 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001089 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001090 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001091 */
Eric Andersenc470f442003-07-28 09:56:35 +00001092
Denis Vlasenko38f63192007-01-22 09:03:07 +00001093#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001094static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001095vi_Word_motion(char *command, int eat)
1096{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001097 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001098 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001099 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001100 input_forward();
1101}
1102
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001103static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001104vi_word_motion(char *command, int eat)
1105{
1106 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001107 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001108 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001109 input_forward();
1110 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001111 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001112 input_forward();
1113 }
1114
Denis Vlasenko253ce002007-01-22 08:34:44 +00001115 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001116 input_forward();
1117
Denis Vlasenko253ce002007-01-22 08:34:44 +00001118 if (eat && cursor < command_len && isspace(command[cursor]))
1119 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001120 input_forward();
1121}
1122
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001123static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001124vi_End_motion(char *command)
1125{
1126 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001127 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001128 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001129 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001130 input_forward();
1131}
1132
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001133static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001134vi_end_motion(char *command)
1135{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001136 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001137 return;
1138 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001139 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001140 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001141 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001142 return;
1143 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001144 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001145 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1146 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001147 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001148 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001149 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001150 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001151 input_forward();
1152 }
1153}
1154
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001155static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001156vi_Back_motion(char *command)
1157{
1158 while (cursor > 0 && isspace(command[cursor-1]))
1159 input_backward(1);
1160 while (cursor > 0 && !isspace(command[cursor-1]))
1161 input_backward(1);
1162}
1163
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001164static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001165vi_back_motion(char *command)
1166{
1167 if (cursor <= 0)
1168 return;
1169 input_backward(1);
1170 while (cursor > 0 && isspace(command[cursor]))
1171 input_backward(1);
1172 if (cursor <= 0)
1173 return;
1174 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001175 while (cursor > 0
1176 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1177 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001178 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001179 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001180 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001181 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001182 input_backward(1);
1183 }
1184}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001185#endif
1186
1187
1188/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001189 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001190 */
1191
Denis Vlasenko38f63192007-01-22 09:03:07 +00001192#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001193static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001194{
1195 cmdedit_prompt = prmt_ptr;
1196 cmdedit_prmt_len = strlen(prmt_ptr);
1197 put_prompt();
1198}
1199#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001200static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001201{
1202 int prmt_len = 0;
1203 size_t cur_prmt_len = 0;
1204 char flg_not_length = '[';
1205 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001206 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1207 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001208 char c;
1209 char *pbuf;
1210
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001211 cmdedit_prmt_len = 0;
1212
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001213 if (!cwd_buf) {
1214 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001215 }
1216
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001217 cbuf[1] = '\0'; /* never changes */
1218
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001219 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001220 char *free_me = NULL;
1221
1222 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001223 c = *prmt_ptr++;
1224 if (c == '\\') {
1225 const char *cp = prmt_ptr;
1226 int l;
1227
1228 c = bb_process_escape_sequence(&prmt_ptr);
1229 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001230 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001231 break;
1232 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001233
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001234 switch (c) {
1235#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1236 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001237 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001238 break;
1239#endif
1240 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001241 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001242 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001243 break;
1244 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001245 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001246 break;
1247#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1248 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001249 /* /home/user[/something] -> ~[/something] */
1250 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001251 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001252 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001253 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1254 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1255 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001256 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001257 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001258 }
1259 break;
1260#endif
1261 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001262 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001263 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001264 if (cp != NULL && cp != pbuf)
1265 pbuf += (cp-pbuf) + 1;
1266 break;
1267 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001268 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001269 break;
1270 case 'e': case 'E': /* \e \E = \033 */
1271 c = '\033';
1272 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001273 case 'x': case 'X': {
1274 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001275 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001276 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001277 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001278 buf2[l] = '\0';
1279 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001280 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001281 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001282 break;
1283 }
1284 prmt_ptr++;
1285 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001286 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001287 if (c == 0)
1288 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001289 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001290 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001291 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001292 case '[': case ']':
1293 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001294 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001295 continue;
1296 }
1297 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001298 } /* switch */
1299 } /* if */
1300 } /* if */
1301 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001302 cur_prmt_len = strlen(pbuf);
1303 prmt_len += cur_prmt_len;
1304 if (flg_not_length != ']')
1305 cmdedit_prmt_len += cur_prmt_len;
1306 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001307 free(free_me);
1308 } /* while */
1309
1310 if (cwd_buf != (char *)bb_msg_unknown)
1311 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001312 cmdedit_prompt = prmt_mem_ptr;
1313 put_prompt();
1314}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001315#endif
1316
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001317static void cmdedit_setwidth(unsigned w, int redraw_flg)
1318{
1319 cmdedit_termw = w;
1320 if (redraw_flg) {
1321 /* new y for current cursor */
1322 int new_y = (cursor + cmdedit_prmt_len) / w;
1323 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001324 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001325 fflush(stdout);
1326 }
1327}
1328
1329static void win_changed(int nsig)
1330{
Denis Vlasenko55995022008-05-18 22:28:26 +00001331 unsigned width;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001332 get_terminal_width_height(0, &width, NULL);
1333 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1334 if (nsig == SIGWINCH)
1335 signal(SIGWINCH, win_changed); /* rearm ourself */
1336}
1337
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001338/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001339 * The emacs and vi modes share much of the code in the big
1340 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001341 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001342 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001343 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001344 */
1345
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001346#define vbit 0x100
1347
1348/* leave out the "vi-mode"-only case labels if vi editing isn't
1349 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001350#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001351
1352/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001353#undef CTRL
1354#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001355
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001356/* Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001357 * -1 on read errors or EOF, or on bare Ctrl-D,
1358 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001359 * >0 length of input string, including terminating '\n'
1360 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001361int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001362{
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001363#if ENABLE_FEATURE_TAB_COMPLETION
1364 smallint lastWasTab = FALSE;
1365#endif
Denis Vlasenko55995022008-05-18 22:28:26 +00001366 unsigned ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001367 unsigned char c;
1368 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001369#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001370 smallint vi_cmdmode = 0;
1371 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001372#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001373 struct termios initial_settings;
1374 struct termios new_settings;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001375
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001376 INIT_S();
1377
1378 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1379 || !(initial_settings.c_lflag & ECHO)
1380 ) {
1381 /* Happens when e.g. stty -echo was run before */
1382 int len;
1383 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001384 fflush(stdout);
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001385 if (fgets(command, maxsize, stdin) == NULL)
1386 len = -1; /* EOF or error */
1387 else
1388 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001389 DEINIT_S();
1390 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001391 }
1392
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001393// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001394 if (maxsize > MAX_LINELEN)
1395 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001396
1397 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001398 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001399#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001400 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001401 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001402#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001403
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001404 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001405 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001406 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001407 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001408 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001409
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001410 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001411 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1412 /* Turn off echoing and CTRL-C, so we can trap it */
1413 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001414 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001415 new_settings.c_cc[VMIN] = 1;
1416 new_settings.c_cc[VTIME] = 0;
1417 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001418#ifndef _POSIX_VDISABLE
1419#define _POSIX_VDISABLE '\0'
1420#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001421 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001422 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001423
Eric Andersen6faae7d2001-02-16 20:09:17 +00001424 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001425 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1426 win_changed(0); /* do initial resizing */
1427#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1428 {
1429 struct passwd *entry;
1430
1431 entry = getpwuid(geteuid());
1432 if (entry) {
1433 user_buf = xstrdup(entry->pw_name);
1434 home_pwd_buf = xstrdup(entry->pw_dir);
1435 }
1436 }
1437#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001438 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001439 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001440
Erik Andersenc7c634b2000-03-19 05:28:55 +00001441 while (1) {
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001442 fflush(NULL);
Mark Whitley4e338752001-01-26 20:42:23 +00001443
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001444 if (nonblock_safe_read(STDIN_FILENO, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001445 /* if we can't read input then exit */
1446 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001447 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001448
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001449 ic = c;
1450
Denis Vlasenko38f63192007-01-22 09:03:07 +00001451#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001452 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001453 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001454 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001455#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001456 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001457 case '\n':
1458 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001459 vi_case('\n'|vbit:)
1460 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001461 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001462 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001463 break_out = 1;
1464 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001465 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001466 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001467 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001468 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001469 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001470 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001471 vi_case('h'|vbit:)
1472 vi_case('\b'|vbit:)
1473 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001474 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001475 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001476 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001477 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001478 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001479 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001480 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001481 command_len = 0;
1482 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001483 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001484 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001485 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001486 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001487 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001488 errno = 0;
1489 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001490 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001491 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001492 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001493 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001494 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001495 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001496
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001497 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001498 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001499 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001500 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001501 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001502 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001503 vi_case('l'|vbit:)
1504 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001505 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001506 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001507 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001508
Erik Andersenf0657d32000-04-12 17:49:52 +00001509 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001510 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001511 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001512 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001513 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001514
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001515#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001516 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001517 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001518 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001519#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001520
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001521 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001522 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001523 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001524 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001525 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001526 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001527 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001528 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001529 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001530 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001531 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001532 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001533
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001534#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001535 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001536 vi_case(CTRL('N')|vbit:)
1537 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001538 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001539 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001540 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001541 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001542 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001543 vi_case(CTRL('P')|vbit:)
1544 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001545 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001546 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001547 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001548 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001549 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001550 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001551 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001552#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001553
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001554 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001555 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001556 /* Control-U -- Clear line before cursor */
1557 if (cursor) {
1558 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001559 command_len -= cursor;
1560 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001561 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001562 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001563 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001564 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001565 /* Control-W -- Remove the last word */
1566 while (cursor > 0 && isspace(command[cursor-1]))
1567 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001568 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001569 input_backspace();
1570 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001571
Denis Vlasenko38f63192007-01-22 09:03:07 +00001572#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001573 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001574 vi_cmdmode = 0;
1575 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001576 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001577 input_backward(cursor);
1578 vi_cmdmode = 0;
1579 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001580 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001581 input_forward();
1582 vi_cmdmode = 0;
1583 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001584 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001585 input_end();
1586 vi_cmdmode = 0;
1587 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001588 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001589 input_delete(1);
1590 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001591 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001592 if (cursor > 0) {
1593 input_backward(1);
1594 input_delete(1);
1595 }
1596 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001597 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001598 vi_Word_motion(command, 1);
1599 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001600 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001601 vi_word_motion(command, 1);
1602 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001603 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001604 vi_End_motion(command);
1605 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001606 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001607 vi_end_motion(command);
1608 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001609 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001610 vi_Back_motion(command);
1611 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001612 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001613 vi_back_motion(command);
1614 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001615 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001616 vi_cmdmode = 0;
1617 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001618 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001619 goto clear_to_eol;
1620
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001621 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001622 vi_cmdmode = 0;
1623 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001624 case 'd'|vbit: {
1625 int nc, sc;
1626 sc = cursor;
1627 prevc = ic;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001628 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001629 goto prepare_to_die;
1630 if (c == (prevc & 0xff)) {
1631 /* "cc", "dd" */
1632 input_backward(cursor);
1633 goto clear_to_eol;
1634 break;
1635 }
1636 switch (c) {
1637 case 'w':
1638 case 'W':
1639 case 'e':
1640 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001641 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001642 case 'w': /* "dw", "cw" */
1643 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001644 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001645 case 'W': /* 'dW', 'cW' */
1646 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001647 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001648 case 'e': /* 'de', 'ce' */
1649 vi_end_motion(command);
1650 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001651 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001652 case 'E': /* 'dE', 'cE' */
1653 vi_End_motion(command);
1654 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001655 break;
1656 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001657 nc = cursor;
1658 input_backward(cursor - sc);
1659 while (nc-- > cursor)
1660 input_delete(1);
1661 break;
1662 case 'b': /* "db", "cb" */
1663 case 'B': /* implemented as B */
1664 if (c == 'b')
1665 vi_back_motion(command);
1666 else
1667 vi_Back_motion(command);
1668 while (sc-- > cursor)
1669 input_delete(1);
1670 break;
1671 case ' ': /* "d ", "c " */
1672 input_delete(1);
1673 break;
1674 case '$': /* "d$", "c$" */
1675 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001676 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001677 input_delete(1);
1678 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001679 }
1680 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001681 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001682 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001683 input_forward();
1684 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001685 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001686 put();
1687 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001688 case 'r'|vbit:
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001689 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001690 goto prepare_to_die;
1691 if (c == 0)
1692 beep();
1693 else {
1694 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001695 bb_putchar(c);
1696 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001697 }
1698 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001699#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001700
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001701 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001702
Denis Vlasenko38f63192007-01-22 09:03:07 +00001703#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001704 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001705 /* ESC: insert mode --> command mode */
1706 vi_cmdmode = 1;
1707 input_backward(1);
1708 break;
1709 }
1710#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001711 /* escape sequence follows */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001712 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001713 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001714 /* different vt100 emulations */
1715 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001716 vi_case('['|vbit:)
1717 vi_case('O'|vbit:)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001718 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001719 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001720 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001721 if (c >= '1' && c <= '9') {
1722 unsigned char dummy;
1723
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001724 if (safe_read(STDIN_FILENO, &dummy, 1) < 1)
Glenn L McGrath475820c2004-01-22 12:42:23 +00001725 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001726 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001727 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001728 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001729
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001730 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001731#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001732 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001733 input_tab(&lastWasTab);
1734 break;
1735#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001736#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001737 case 'A':
1738 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001739 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001740 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001741 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001742 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001743 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001744 break;
1745 case 'B':
1746 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001747 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001748 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001749 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001750 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001751 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001752 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001753 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001754 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001755 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001756#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001757 case 'C':
1758 /* Right Arrow -- Move forward one character */
1759 input_forward();
1760 break;
1761 case 'D':
1762 /* Left Arrow -- Move back one character */
1763 input_backward(1);
1764 break;
1765 case '3':
1766 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001767 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001768 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001769 case '1': // vt100? linux vt? or what?
1770 case '7': // vt100? linux vt? or what?
1771 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001772 input_backward(cursor);
1773 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001774 case '4': // vt100? linux vt? or what?
1775 case '8': // vt100? linux vt? or what?
1776 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001777 input_end();
1778 break;
1779 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001780 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001781 beep();
1782 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001783 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001784
Eric Andersenc470f442003-07-28 09:56:35 +00001785 default: /* If it's regular input, do the normal thing */
Paul Fox84bbac52008-01-11 16:50:08 +00001786
1787 /* Control-V -- force insert of next char */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001788 if (c == CTRL('V')) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001789 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001790 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001791 if (c == 0) {
1792 beep();
1793 break;
1794 }
Paul Fox84bbac52008-01-11 16:50:08 +00001795 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001796
Denis Vlasenko38f63192007-01-22 09:03:07 +00001797#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001798 if (vi_cmdmode) /* Don't self-insert */
1799 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001800#endif
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00001801 if ((int)command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001802 break;
1803
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001804 command_len++;
1805 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001806 command[cursor] = c;
1807 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001808 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001809 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001810 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001811
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001812 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001813 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001814 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001815 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001816 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001817 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001818 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001819 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001820 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001821 }
Eric Andersenc470f442003-07-28 09:56:35 +00001822 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001823 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001824
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001825#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001826 if (c != '\t')
1827 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001828#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001829 }
1830
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001831 if (command_len > 0)
1832 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001833
Eric Andersen27bb7902003-12-23 20:24:51 +00001834 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001835 command[command_len++] = '\n';
1836 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001837 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001838
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001839#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001840 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001841#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001842
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001843 /* restore initial_settings */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001844 tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001845 /* restore SIGWINCH handler */
1846 signal(SIGWINCH, previous_SIGWINCH_handler);
1847 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001848
1849 DEINIT_S();
1850
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001851 return command_len;
1852}
1853
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001854line_input_t* FAST_FUNC new_line_input_t(int flags)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001855{
1856 line_input_t *n = xzalloc(sizeof(*n));
1857 n->flags = flags;
1858 return n;
1859}
1860
1861#else
1862
1863#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001864int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001865{
1866 fputs(prompt, stdout);
1867 fflush(stdout);
1868 fgets(command, maxsize, stdin);
1869 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001870}
1871
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001872#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001873
1874
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001875/*
1876 * Testing
1877 */
1878
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001879#ifdef TEST
1880
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001881#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001882
1883const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001884
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001885int main(int argc, char **argv)
1886{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001887 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001888 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001889#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001890 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1891 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1892 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001893#else
1894 "% ";
1895#endif
1896
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001897#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001898 setlocale(LC_ALL, "");
1899#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001900 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001901 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001902 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001903 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001904 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001905 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001906 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001907 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001908 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001909 return 0;
1910}
1911
Eric Andersenc470f442003-07-28 09:56:35 +00001912#endif /* TEST */