blob: c91efd40c5c4917f58f0b0677daaf3203a462cca [file] [log] [blame]
Erik Andersenc7c634b2000-03-19 05:28:55 +00001/* vi: set sw=4 ts=4: */
Erik Andersen13456d12000-03-16 08:09:57 +00002/*
Eric Andersenaff114c2004-04-14 17:51:38 +00003 * Termios command line History and Editing.
Erik Andersen13456d12000-03-16 08:09:57 +00004 *
Eric Andersen81fe1232003-07-29 06:38:40 +00005 * Copyright (c) 1986-2003 may safely be consumed by a BSD or GPL license.
Eric Andersen7467c8d2001-07-12 20:26:32 +00006 * Written by: Vladimir Oleynik <dzo@simtreas.ru>
Eric Andersen5f2c79d2001-02-16 18:36:04 +00007 *
8 * Used ideas:
9 * Adam Rogoyski <rogoyski@cs.utexas.edu>
10 * Dave Cinege <dcinege@psychosis.com>
11 * Jakub Jelinek (c) 1995
Eric Andersen81fe1232003-07-29 06:38:40 +000012 * Erik Andersen <andersen@codepoet.org> (Majorly adjusted for busybox)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000013 *
Erik Andersen13456d12000-03-16 08:09:57 +000014 * This code is 'as is' with no warranty.
Erik Andersen13456d12000-03-16 08:09:57 +000015 */
16
17/*
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000018 Usage and known bugs:
Erik Andersen13456d12000-03-16 08:09:57 +000019 Terminal key codes are not extensive, and more will probably
20 need to be added. This version was created on Debian GNU/Linux 2.x.
21 Delete, Backspace, Home, End, and the arrow keys were tested
22 to work in an Xterm and console. Ctrl-A also works as Home.
Mark Whitley4e338752001-01-26 20:42:23 +000023 Ctrl-E also works as End.
Erik Andersen13456d12000-03-16 08:09:57 +000024
Eric Andersen5f2c79d2001-02-16 18:36:04 +000025 Small bugs (simple effect):
26 - not true viewing if terminal size (x*y symbols) less
Denis Vlasenko00cdbd82007-01-21 19:21:21 +000027 size (prompt + editor's line + 2 symbols)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000028 - not true viewing if length prompt less terminal width
Erik Andersen13456d12000-03-16 08:09:57 +000029 */
30
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000031#include "libbb.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000032
Glenn L McGrath475820c2004-01-22 12:42:23 +000033
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000034/* FIXME: obsolete CONFIG item? */
35#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
36
37
Glenn L McGrath3b251852004-01-03 12:07:32 +000038#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000039
Denis Vlasenko38f63192007-01-22 09:03:07 +000040#define ENABLE_FEATURE_EDITING 0
41#define ENABLE_FEATURE_TAB_COMPLETION 0
42#define ENABLE_FEATURE_USERNAME_COMPLETION 0
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000043#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +000044
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000045#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000046
Eric Andersen5165fbe2001-02-20 06:42:29 +000047
Denis Vlasenko82b39e82007-01-21 19:18:19 +000048/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko38f63192007-01-22 09:03:07 +000049#if ENABLE_FEATURE_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000050
Denis Vlasenko82b39e82007-01-21 19:18:19 +000051#if ENABLE_LOCALE_SUPPORT
52#define Isprint(c) isprint(c)
53#else
54#define Isprint(c) ((c) >= ' ' && (c) != ((unsigned char)'\233'))
55#endif
56
Denis Vlasenko7e46cf72006-12-23 01:21:55 +000057#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000058 (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
59#define USE_FEATURE_GETUSERNAME_AND_HOMEDIR(...)
60#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
61#undef USE_FEATURE_GETUSERNAME_AND_HOMEDIR
62#define USE_FEATURE_GETUSERNAME_AND_HOMEDIR(...) __VA_ARGS__
63#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000064
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000065enum {
66 /* We use int16_t for positions, need to limit line len */
67 MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN < 0x7ff0
Denis Vlasenko6b404432008-01-07 16:13:14 +000068 ? CONFIG_FEATURE_EDITING_MAX_LEN
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000069 : 0x7ff0
70};
Robert Griebl350d26b2002-12-03 22:45:46 +000071
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000072#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
73static const char null_str[] ALIGN1 = "";
74#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +000075
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000076/* We try to minimize both static and stack usage. */
Denis Vlasenko5d89fba2008-04-22 00:08:27 +000077struct lineedit_statics {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000078 line_input_t *state;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000079
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000080 volatile unsigned cmdedit_termw; /* = 80; */ /* actual terminal width */
81 sighandler_t previous_SIGWINCH_handler;
Mark Whitley4e338752001-01-26 20:42:23 +000082
Eric Andersen86349772000-12-18 20:25:50 +000083
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000084 int cmdedit_x; /* real x terminal position */
85 int cmdedit_y; /* pseudoreal y terminal position */
86 int cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000087
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000088 unsigned cursor;
89 unsigned command_len;
90 char *command_ps;
91
92 const char *cmdedit_prompt;
Denis Vlasenko38f63192007-01-22 09:03:07 +000093#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000094 int num_ok_lines; /* = 1; */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000095#endif
96
Denis Vlasenko82b39e82007-01-21 19:18:19 +000097#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000098 char *user_buf;
99 char *home_pwd_buf; /* = (char*)null_str; */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000100#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000101
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000102#if ENABLE_FEATURE_TAB_COMPLETION
103 char **matches;
104 unsigned num_matches;
105#endif
106
107#if ENABLE_FEATURE_EDITING_VI
108#define DELBUFSIZ 128
109 char *delptr;
110 smallint newdelflag; /* whether delbuf should be reused yet */
111 char delbuf[DELBUFSIZ]; /* a place to store deleted characters */
112#endif
113
114 /* Formerly these were big buffers on stack: */
115#if ENABLE_FEATURE_TAB_COMPLETION
116 char exe_n_cwd_tab_completion__dirbuf[MAX_LINELEN];
117 char input_tab__matchBuf[MAX_LINELEN];
118 int16_t find_match__int_buf[MAX_LINELEN + 1]; /* need to have 9 bits at least */
119 int16_t find_match__pos_buf[MAX_LINELEN + 1];
120#endif
121};
122
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000123/* See lineedit_ptr_hack.c */
124extern struct lineedit_statics *const lineedit_ptr_to_statics;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000125
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000126#define S (*lineedit_ptr_to_statics)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000127#define state (S.state )
128#define cmdedit_termw (S.cmdedit_termw )
129#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
130#define cmdedit_x (S.cmdedit_x )
131#define cmdedit_y (S.cmdedit_y )
132#define cmdedit_prmt_len (S.cmdedit_prmt_len)
133#define cursor (S.cursor )
134#define command_len (S.command_len )
135#define command_ps (S.command_ps )
136#define cmdedit_prompt (S.cmdedit_prompt )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000137#define num_ok_lines (S.num_ok_lines )
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000138#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000139#define home_pwd_buf (S.home_pwd_buf )
140#define matches (S.matches )
141#define num_matches (S.num_matches )
142#define delptr (S.delptr )
143#define newdelflag (S.newdelflag )
144#define delbuf (S.delbuf )
145
146#define INIT_S() do { \
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000147 (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000148 barrier(); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000149 cmdedit_termw = 80; \
150 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
151 USE_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \
152} while (0)
153static void deinit_S(void)
154{
155#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000156 /* This one is allocated only if FANCY_PROMPT is on
157 * (otherwise it points to verbatim prompt (NOT malloced) */
158 free((char*)cmdedit_prompt);
159#endif
160#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
161 free(user_buf);
162 if (home_pwd_buf != null_str)
163 free(home_pwd_buf);
164#endif
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000165 free(lineedit_ptr_to_statics);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000166}
167#define DEINIT_S() deinit_S()
168
Denis Vlasenko2c844952008-04-25 18:44:35 +0000169
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000170/* Put 'command_ps[cursor]', cursor++.
171 * Advance cursor on screen. If we reached right margin, scroll text up
172 * and remove terminal margin effect by printing 'next_char' */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000173#define HACK_FOR_WRONG_WIDTH 1
174#if HACK_FOR_WRONG_WIDTH
175static void cmdedit_set_out_char(void)
176#define cmdedit_set_out_char(next_char) cmdedit_set_out_char()
177#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000178static void cmdedit_set_out_char(int next_char)
Denis Vlasenko2c844952008-04-25 18:44:35 +0000179#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000180{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000181 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000182
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000183 if (c == '\0') {
184 /* erase character after end of input string */
185 c = ' ';
186 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000187#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000188 /* Display non-printable characters in reverse */
189 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000190 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000191 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000192 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000193 c += '@';
194 if (c == 127)
195 c = '?';
196 printf("\033[7m%c\033[0m", c);
197 } else
198#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000199 {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000200 bb_putchar(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000201 }
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000202 if (++cmdedit_x >= (int)cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000203 /* terminal is scrolled down */
204 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000205 cmdedit_x = 0;
Denis Vlasenko2c844952008-04-25 18:44:35 +0000206#if HACK_FOR_WRONG_WIDTH
207 /* This works better if our idea of term width is wrong
208 * and it is actually wider (often happens on serial lines).
209 * Printing CR,LF *forces* cursor to next line.
210 * OTOH if terminal width is correct AND terminal does NOT
211 * have automargin (IOW: it is moving cursor to next line
212 * by itself (which is wrong for VT-10x terminals)),
213 * this will break things: there will be one extra empty line */
214 puts("\r"); /* + implicit '\n' */
215#else
216 /* Works ok only if cmdedit_termw is correct */
Mark Whitley4e338752001-01-26 20:42:23 +0000217 /* destroy "(auto)margin" */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000218 bb_putchar(next_char);
219 bb_putchar('\b');
Denis Vlasenko2c844952008-04-25 18:44:35 +0000220#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000221 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000222// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000223 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000224}
225
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000226/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000227static void input_end(void)
228{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000229 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000230 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000231}
232
233/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000234static void goto_new_line(void)
235{
Mark Whitley4e338752001-01-26 20:42:23 +0000236 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000237 if (cmdedit_x)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000238 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000239}
240
241
Rob Landley88621d72006-08-29 19:41:06 +0000242static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000243{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000244 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000245 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000246}
Eric Andersen81fe1232003-07-29 06:38:40 +0000247
Rob Landley88621d72006-08-29 19:41:06 +0000248static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000249{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000250 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000251}
252
Eric Andersenaff114c2004-04-14 17:51:38 +0000253/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000254/* (optimized for slow terminals) */
255static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000256{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000257 int count_y;
258
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000259 if (num > cursor)
260 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000261 if (!num)
262 return;
263 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000264
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000265 if ((unsigned)cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000266 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000267 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000268 /* This is longer by 5 bytes on x86.
269 * Also gets mysteriously
270 * miscompiled for some ARM users.
271 * printf(("\b\b\b\b" + 4) - num);
272 * return;
273 */
274 do {
275 bb_putchar('\b');
276 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000277 return;
278 }
279 printf("\033[%uD", num);
280 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000281 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000282
283 /* Need to go one or more lines up */
284 num -= cmdedit_x;
285 count_y = 1 + (num / cmdedit_termw);
286 cmdedit_y -= count_y;
287 cmdedit_x = cmdedit_termw * count_y - num;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000288 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000289 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000290}
291
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000292static void put_prompt(void)
293{
294 out1str(cmdedit_prompt);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000295 cmdedit_x = cmdedit_prmt_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000296 cursor = 0;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000297// Huh? what if cmdedit_prmt_len >= width?
Eric Andersen7467c8d2001-07-12 20:26:32 +0000298 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000299}
300
Eric Andersenaff114c2004-04-14 17:51:38 +0000301/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000302static void redraw(int y, int back_cursor)
303{
Eric Andersenc470f442003-07-28 09:56:35 +0000304 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000305 printf("\033[%dA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000306 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000307 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000308 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000309 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000310 input_backward(back_cursor);
311}
312
Paul Fox3f11b1b2005-08-04 19:04:46 +0000313/* Delete the char in front of the cursor, optionally saving it
314 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000315#if !ENABLE_FEATURE_EDITING_VI
316static void input_delete(void)
317#define input_delete(save) input_delete()
318#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000319static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000320#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000321{
Mark Whitley4e338752001-01-26 20:42:23 +0000322 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000323
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000324 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000325 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000326
Denis Vlasenko38f63192007-01-22 09:03:07 +0000327#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000328 if (save) {
329 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000330 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000331 newdelflag = 0;
332 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000333 if ((delptr - delbuf) < DELBUFSIZ)
334 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000335 }
336#endif
337
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000338 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000339 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000340 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000341 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000342 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000343}
344
Denis Vlasenko38f63192007-01-22 09:03:07 +0000345#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000346static void put(void)
347{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000348 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000349 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000350
Paul Fox3f11b1b2005-08-04 19:04:46 +0000351 if (j == 0)
352 return;
353 ocursor = cursor;
354 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000355 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000356 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000357 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000358 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000359 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000360}
361#endif
362
Mark Whitley4e338752001-01-26 20:42:23 +0000363/* Delete the char in back of the cursor */
364static void input_backspace(void)
365{
366 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000367 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000368 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000369 }
370}
371
Eric Andersenaff114c2004-04-14 17:51:38 +0000372/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000373static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000374{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000375 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000376 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000377}
378
Denis Vlasenko38f63192007-01-22 09:03:07 +0000379#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000380
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000381static void free_tab_completion_data(void)
382{
383 if (matches) {
384 while (num_matches)
385 free(matches[--num_matches]);
386 free(matches);
387 matches = NULL;
388 }
389}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000390
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000391static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000392{
393 int nm = num_matches;
394 int nm1 = nm + 1;
395
396 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000397 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000398 num_matches++;
399}
400
Denis Vlasenko38f63192007-01-22 09:03:07 +0000401#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000402static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000403{
404 struct passwd *entry;
405 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000406
Eric Andersenc470f442003-07-28 09:56:35 +0000407 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000408 userlen = strlen(ud);
409
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000410 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000411 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000412 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000413
Eric Andersenc470f442003-07-28 09:56:35 +0000414 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000415 home = home_pwd_buf;
416 } else {
417 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000418 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000419 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000420 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000421 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000422 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000423 ud = temp;
424 if (entry)
425 home = entry->pw_dir;
426 }
427 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000428 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000429 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000430 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000431 }
432 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000433 } else {
434 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000435 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000436 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000437 struct passwd pwd;
438 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000439
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000440 setpwent();
441 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000442 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000443 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
444 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000445 }
446 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000447 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000448 }
449}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000450#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000451
452enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000453 FIND_EXE_ONLY = 0,
454 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000455 FIND_FILE_ONLY = 2,
456};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000457
Mark Whitley4e338752001-01-26 20:42:23 +0000458static int path_parse(char ***p, int flags)
459{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000460 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000461 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000462 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000463 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000464
Mark Whitley4e338752001-01-26 20:42:23 +0000465 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000466 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000467 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000468
469 if (state->flags & WITH_PATH_LOOKUP)
470 pth = state->path_lookup;
471 else
472 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000473 /* PATH=<empty> or PATH=:<empty> */
474 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
475 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000476
Denis Vlasenko253ce002007-01-22 08:34:44 +0000477 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000478 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000479 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000480 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000481 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000482 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000483 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000484 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000485 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000486 }
487
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000488 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000489 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000490 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000491 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000492 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000493 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000494 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000495 *tmp++ = '\0'; /* ':' -> '\0' */
496 if (*tmp == '\0')
497 break; /* :<empty> */
498 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000499 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000500 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000501 return npth;
502}
503
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000504static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000505{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000506 DIR *dir;
507 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000508 struct stat st;
509 char *path1[1];
510 char **paths = path1;
511 int npaths;
512 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000513 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000514 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000515/* char dirbuf[MAX_LINELEN]; */
516#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000517
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000518 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000519 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000520
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000521 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000522 /* no dir, if flags==EXE_ONLY - get paths, else "." */
523 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000524 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000525 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000526 /* dirbuf = ".../.../.../" */
527 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000528#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000529 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000530 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000531#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000532 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000533 /* point to 'l' in "..../last_component" */
534 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000535 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000536
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000537 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000538 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000539 if (!dir)
540 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000541
542 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000543 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000544 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000545
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000546 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000547 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000548 continue;
549 /* not see .name without .match */
Denis Vlasenkob5202712008-04-24 04:42:52 +0000550 if (*str_found == '.' && *pfind == '\0') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000551 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000552 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000553 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000554 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000555 found = concat_path_file(paths[i], str_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000556 /* hmm, remove in progress? */
557 /* NB: stat() first so that we see is it a directory;
558 * but if that fails, use lstat() so that
559 * we still match dangling links */
560 if (stat(found, &st) && lstat(found, &st))
Eric Andersene5dfced2001-04-09 22:48:12 +0000561 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000562 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000563 if (paths[i] != dirbuf)
Denis Vlasenkob5202712008-04-24 04:42:52 +0000564 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000565
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000566 len1 = strlen(found);
567 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000568 found[len1] = '\0';
569 found[len1+1] = '\0';
570
Mark Whitley4e338752001-01-26 20:42:23 +0000571 if (S_ISDIR(st.st_mode)) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000572 /* name is a directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000573 if (found[len1-1] != '/') {
574 found[len1] = '/';
575 }
Mark Whitley4e338752001-01-26 20:42:23 +0000576 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000577 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000578 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000579 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000580 }
Mark Whitley4e338752001-01-26 20:42:23 +0000581 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000582 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000583 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000584 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000585 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000586 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000587 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000588 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000589 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000590 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000591 free(paths);
592 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000593#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000594}
Erik Andersenf0657d32000-04-12 17:49:52 +0000595
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000596#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000597
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000598#define collapse_pos(is, in) do { \
599 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
600 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
601} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000602
603static int find_match(char *matchBuf, int *len_with_quotes)
604{
605 int i, j;
606 int command_mode;
607 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000608/* int16_t int_buf[MAX_LINELEN + 1]; */
609/* int16_t pos_buf[MAX_LINELEN + 1]; */
610#define int_buf (S.find_match__int_buf)
611#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000612
613 /* set to integer dimension characters and own positions */
614 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000615 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000616 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000617 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000618 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000619 }
620 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000621 }
622
623 /* mask \+symbol and convert '\t' to ' ' */
624 for (i = j = 0; matchBuf[i]; i++, j++)
625 if (matchBuf[i] == '\\') {
626 collapse_pos(j, j + 1);
627 int_buf[j] |= QUOT;
628 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000629#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000630 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000631 int_buf[j] = ' ' | QUOT;
632#endif
633 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000634#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000635 else if (matchBuf[i] == '\t')
636 int_buf[j] = ' ';
637#endif
638
639 /* mask "symbols" or 'symbols' */
640 c2 = 0;
641 for (i = 0; int_buf[i]; i++) {
642 c = int_buf[i];
643 if (c == '\'' || c == '"') {
644 if (c2 == 0)
645 c2 = c;
646 else {
647 if (c == c2)
648 c2 = 0;
649 else
650 int_buf[i] |= QUOT;
651 }
652 } else if (c2 != 0 && c != '$')
653 int_buf[i] |= QUOT;
654 }
655
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000656 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000657 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
658 for (i = 0; int_buf[i]; i++) {
659 c = int_buf[i];
660 c2 = int_buf[i + 1];
661 j = i ? int_buf[i - 1] : -1;
662 command_mode = 0;
663 if (c == ';' || c == '&' || c == '|') {
664 command_mode = 1 + (c == c2);
665 if (c == '&') {
666 if (j == '>' || j == '<')
667 command_mode = 0;
668 } else if (c == '|' && j == '>')
669 command_mode = 0;
670 }
671 if (command_mode) {
672 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000673 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000674 }
675 }
676 /* collapse `command...` */
677 for (i = 0; int_buf[i]; i++)
678 if (int_buf[i] == '`') {
679 for (j = i + 1; int_buf[j]; j++)
680 if (int_buf[j] == '`') {
681 collapse_pos(i, j + 1);
682 j = 0;
683 break;
684 }
685 if (j) {
686 /* not found close ` - command mode, collapse all previous */
687 collapse_pos(0, i + 1);
688 break;
689 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000690 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000691 }
692
693 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000694 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000695 c2 = 0;
696 for (i = 0; int_buf[i]; i++)
697 if (int_buf[i] == '(' || int_buf[i] == '{') {
698 if (int_buf[i] == '(')
699 c++;
700 else
701 c2++;
702 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000703 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000704 }
705 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
706 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
707 if (int_buf[i] == ')')
708 c--;
709 else
710 c2--;
711 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000712 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000713 }
714
715 /* skip first not quote space */
716 for (i = 0; int_buf[i]; i++)
717 if (int_buf[i] != ' ')
718 break;
719 if (i)
720 collapse_pos(0, i);
721
722 /* set find mode for completion */
723 command_mode = FIND_EXE_ONLY;
724 for (i = 0; int_buf[i]; i++)
725 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
726 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000727 && matchBuf[pos_buf[0]] == 'c'
728 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000729 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000730 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000731 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000732 command_mode = FIND_FILE_ONLY;
733 break;
734 }
735 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000736 for (i = 0; int_buf[i]; i++)
737 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000738 /* find last word */
739 for (--i; i >= 0; i--) {
740 c = int_buf[i];
741 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
742 collapse_pos(0, i + 1);
743 break;
744 }
745 }
746 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000747 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
748 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000749 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000750 while ((int_buf[i] & ~QUOT) == '/'
751 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
752 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000753 i++;
754 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000755
756 /* set only match and destroy quotes */
757 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000758 for (c = 0; pos_buf[i] >= 0; i++) {
759 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000760 j = pos_buf[i] + 1;
761 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000762 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000763 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000764 *len_with_quotes = j ? j - pos_buf[0] : 0;
765
766 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000767#undef int_buf
768#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000769}
770
Glenn L McGrath4d001292003-01-06 01:11:50 +0000771/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000772 * display by column (original idea from ls applet,
773 * very optimized by me :)
774 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000775static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000776{
777 int ncols, row;
778 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000779 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000780 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000781 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000782
783 /* find the longest file name- use that as the column width */
784 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000785 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000786 if (column_width < l)
787 column_width = l;
788 }
789 column_width += 2; /* min space for columns */
790 ncols = cmdedit_termw / column_width;
791
792 if (ncols > 1) {
793 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000794 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000795 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000796 } else {
797 ncols = 1;
798 }
799 for (row = 0; row < nrows; row++) {
800 int n = row;
801 int nc;
802
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000803 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000804 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000805 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000806 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000807 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000808 }
809}
810
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000811static char *add_quote_for_spec_chars(char *found)
812{
813 int l = 0;
814 char *s = xmalloc((strlen(found) + 1) * 2);
815
816 while (*found) {
817 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
818 s[l++] = '\\';
819 s[l++] = *found++;
820 }
821 s[l] = 0;
822 return s;
823}
824
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000825/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000826static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000827{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000828 if (!(state->flags & TAB_COMPLETION))
829 return;
830
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000831 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000832 char *tmp, *tmp1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000833 size_t len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000834/* char matchBuf[MAX_LINELEN]; */
835#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000836 int find_type;
837 int recalc_pos;
838
Eric Andersenc470f442003-07-28 09:56:35 +0000839 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000840
841 /* Make a local copy of the string -- up
842 * to the position of the cursor */
843 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000844 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000845
846 find_type = find_match(matchBuf, &recalc_pos);
847
848 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000849 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000850
Denis Vlasenko38f63192007-01-22 09:03:07 +0000851#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000852 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000853 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000854 if (state->flags & USERNAME_COMPLETION)
855 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
856 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000857#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000858 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000859 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000860 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000861 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000862 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000863 if (matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000864 unsigned i;
865 int n = 0;
Denis Vlasenkofb290382008-03-02 12:51:26 +0000866 qsort_string_vector(matches, num_matches);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000867 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000868 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000869 if (strcmp(matches[i], matches[i+1]) == 0) {
870 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000871 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000872 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000873 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000874 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000875 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000876 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000877 matches[n] = matches[i];
878 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000879 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000880 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000881 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000882 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000883 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000884 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000885 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000886 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000887 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000888 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000889 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000890 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000891 break;
892 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000893 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000894 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000895 return;
896 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000897 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000898 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000899 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000900 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000901 /* for next completion current found */
902 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000903
904 len_found = strlen(tmp);
905 if (tmp[len_found-1] != '/') {
906 tmp[len_found] = ' ';
907 tmp[len_found+1] = '\0';
908 }
Mark Whitley4e338752001-01-26 20:42:23 +0000909 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000910 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000911 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000912 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000913 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000914 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000915 /* save tail line */
916 strcpy(matchBuf, command_ps + cursor);
917 /* add match */
918 strcat(command_ps, tmp);
919 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000920 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000921 /* back to begin word for match */
922 input_backward(recalc_pos);
923 /* new pos */
924 recalc_pos = cursor + len_found;
925 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000926 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000927 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000928 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000929 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000930 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000931#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000932 } else {
933 /* Ok -- the last char was a TAB. Since they
934 * just hit TAB again, print a list of all the
935 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000936 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000937 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000938
939 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000940 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000941 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000942 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000943 }
944 }
945}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000946
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000947#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000948
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000949
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000950#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000951
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000952/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000953static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000954{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000955 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
956 free(state->history[state->cur_history]);
957 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000958 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000959 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000960}
961
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000962static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000963{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000964 if (state->flags & DO_HISTORY) {
965 int ch = state->cur_history;
966 if (ch < state->cnt_history) {
967 get_previous_history(); /* save the current history line */
968 state->cur_history = ch + 1;
969 return state->cur_history;
970 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000971 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000972 beep();
973 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000974}
Robert Griebl350d26b2002-12-03 22:45:46 +0000975
Denis Vlasenko38f63192007-01-22 09:03:07 +0000976#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000977/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000978static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000979{
Robert Griebl350d26b2002-12-03 22:45:46 +0000980 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000981 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000982
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000983 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +0000984
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000985 fp = fopen(fromfile, "r");
986 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000987 /* clean up old history */
988 for (hi = state->cnt_history; hi > 0;) {
989 hi--;
990 free(state->history[hi]);
991 }
992
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000993 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000994 char *hl = xmalloc_fgetline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000995 int l;
996
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000997 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000998 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000999 l = strlen(hl);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001000 if (l >= MAX_LINELEN)
1001 hl[MAX_LINELEN-1] = '\0';
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001002 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001003 free(hl);
1004 continue;
1005 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001006 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001007 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001008 fclose(fp);
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001009 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001010 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001011}
1012
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001013/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +00001014static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +00001015{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001016 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +00001017
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001018 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001019 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +00001020 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001021
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001022 for (i = 0; i < state->cnt_history; i++) {
1023 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001024 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001025 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001026 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001027}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001028#else
1029#define load_history(a) ((void)0)
1030#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001031#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001032
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001033static void remember_in_history(const char *str)
1034{
1035 int i;
1036
1037 if (!(state->flags & DO_HISTORY))
1038 return;
1039
1040 i = state->cnt_history;
1041 free(state->history[MAX_HISTORY]);
1042 state->history[MAX_HISTORY] = NULL;
1043 /* After max history, remove the oldest command */
1044 if (i >= MAX_HISTORY) {
1045 free(state->history[0]);
1046 for (i = 0; i < MAX_HISTORY-1; i++)
1047 state->history[i] = state->history[i+1];
1048 }
1049// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
1050// (i.e. do not save dups?)
1051 state->history[i++] = xstrdup(str);
1052 state->cur_history = i;
1053 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001054#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001055 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001056 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +00001057#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00001058 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001059}
1060
1061#else /* MAX_HISTORY == 0 */
1062#define remember_in_history(a) ((void)0)
1063#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001064
1065
Erik Andersen6273f652000-03-17 01:12:41 +00001066/*
1067 * This function is used to grab a character buffer
1068 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001069 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001070 * a mini readline).
1071 *
1072 * The following standard commands are not implemented:
1073 * ESC-b -- Move back one word
1074 * ESC-f -- Move forward one word
1075 * ESC-d -- Delete back one word
1076 * ESC-h -- Delete forward one word
1077 * CTL-t -- Transpose two characters
1078 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001079 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001080 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001081 */
Eric Andersenc470f442003-07-28 09:56:35 +00001082
Denis Vlasenko38f63192007-01-22 09:03:07 +00001083#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001084static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001085vi_Word_motion(char *command, int eat)
1086{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001087 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001088 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001089 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001090 input_forward();
1091}
1092
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001093static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001094vi_word_motion(char *command, int eat)
1095{
1096 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001097 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001098 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001099 input_forward();
1100 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001101 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001102 input_forward();
1103 }
1104
Denis Vlasenko253ce002007-01-22 08:34:44 +00001105 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001106 input_forward();
1107
Denis Vlasenko253ce002007-01-22 08:34:44 +00001108 if (eat && cursor < command_len && isspace(command[cursor]))
1109 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001110 input_forward();
1111}
1112
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001113static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001114vi_End_motion(char *command)
1115{
1116 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001117 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001118 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001119 while (cursor < command_len-1 && !isspace(command[cursor+1]))
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{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001126 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001127 return;
1128 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001129 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001130 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001131 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001132 return;
1133 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001134 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001135 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1136 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001137 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001138 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001139 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001140 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001141 input_forward();
1142 }
1143}
1144
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001145static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001146vi_Back_motion(char *command)
1147{
1148 while (cursor > 0 && isspace(command[cursor-1]))
1149 input_backward(1);
1150 while (cursor > 0 && !isspace(command[cursor-1]))
1151 input_backward(1);
1152}
1153
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001154static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001155vi_back_motion(char *command)
1156{
1157 if (cursor <= 0)
1158 return;
1159 input_backward(1);
1160 while (cursor > 0 && isspace(command[cursor]))
1161 input_backward(1);
1162 if (cursor <= 0)
1163 return;
1164 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001165 while (cursor > 0
1166 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1167 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001168 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001169 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001170 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001171 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001172 input_backward(1);
1173 }
1174}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001175#endif
1176
1177
1178/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001179 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001180 */
1181
Denis Vlasenko38f63192007-01-22 09:03:07 +00001182#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001183static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001184{
1185 cmdedit_prompt = prmt_ptr;
1186 cmdedit_prmt_len = strlen(prmt_ptr);
1187 put_prompt();
1188}
1189#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001190static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001191{
1192 int prmt_len = 0;
1193 size_t cur_prmt_len = 0;
1194 char flg_not_length = '[';
1195 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001196 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1197 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001198 char c;
1199 char *pbuf;
1200
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001201 cmdedit_prmt_len = 0;
1202
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001203 if (!cwd_buf) {
1204 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001205 }
1206
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001207 cbuf[1] = '\0'; /* never changes */
1208
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001209 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001210 char *free_me = NULL;
1211
1212 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001213 c = *prmt_ptr++;
1214 if (c == '\\') {
1215 const char *cp = prmt_ptr;
1216 int l;
1217
1218 c = bb_process_escape_sequence(&prmt_ptr);
1219 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001220 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001221 break;
1222 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001223
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001224 switch (c) {
1225#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1226 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001227 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001228 break;
1229#endif
1230 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001231 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001232 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001233 break;
1234 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001235 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001236 break;
1237#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1238 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001239 /* /home/user[/something] -> ~[/something] */
1240 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001241 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001242 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001243 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1244 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1245 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001246 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001247 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001248 }
1249 break;
1250#endif
1251 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001252 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001253 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001254 if (cp != NULL && cp != pbuf)
1255 pbuf += (cp-pbuf) + 1;
1256 break;
1257 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001258 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001259 break;
1260 case 'e': case 'E': /* \e \E = \033 */
1261 c = '\033';
1262 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001263 case 'x': case 'X': {
1264 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001265 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001266 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001267 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001268 buf2[l] = '\0';
1269 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001270 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001271 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001272 break;
1273 }
1274 prmt_ptr++;
1275 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001276 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001277 if (c == 0)
1278 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001279 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001280 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001281 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001282 case '[': case ']':
1283 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001284 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001285 continue;
1286 }
1287 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001288 } /* switch */
1289 } /* if */
1290 } /* if */
1291 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001292 cur_prmt_len = strlen(pbuf);
1293 prmt_len += cur_prmt_len;
1294 if (flg_not_length != ']')
1295 cmdedit_prmt_len += cur_prmt_len;
1296 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001297 free(free_me);
1298 } /* while */
1299
1300 if (cwd_buf != (char *)bb_msg_unknown)
1301 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001302 cmdedit_prompt = prmt_mem_ptr;
1303 put_prompt();
1304}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001305#endif
1306
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001307static void cmdedit_setwidth(unsigned w, int redraw_flg)
1308{
1309 cmdedit_termw = w;
1310 if (redraw_flg) {
1311 /* new y for current cursor */
1312 int new_y = (cursor + cmdedit_prmt_len) / w;
1313 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001314 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001315 fflush(stdout);
1316 }
1317}
1318
1319static void win_changed(int nsig)
1320{
Denis Vlasenko55995022008-05-18 22:28:26 +00001321 unsigned width;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001322 get_terminal_width_height(0, &width, NULL);
1323 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1324 if (nsig == SIGWINCH)
1325 signal(SIGWINCH, win_changed); /* rearm ourself */
1326}
1327
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001328/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001329 * The emacs and vi modes share much of the code in the big
1330 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001331 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001332 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001333 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001334 */
1335
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001336#define vbit 0x100
1337
1338/* leave out the "vi-mode"-only case labels if vi editing isn't
1339 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001340#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001341
1342/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001343#undef CTRL
1344#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001345
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001346/* Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001347 * -1 on read errors or EOF, or on bare Ctrl-D,
1348 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001349 * >0 length of input string, including terminating '\n'
1350 */
Denis Vlasenko037576d2007-10-20 18:30:38 +00001351int read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001352{
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001353#if ENABLE_FEATURE_TAB_COMPLETION
1354 smallint lastWasTab = FALSE;
1355#endif
Denis Vlasenko55995022008-05-18 22:28:26 +00001356 unsigned ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001357 unsigned char c;
1358 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001359#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001360 smallint vi_cmdmode = 0;
1361 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001362#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001363 struct termios initial_settings;
1364 struct termios new_settings;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001365
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001366 INIT_S();
1367
1368 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1369 || !(initial_settings.c_lflag & ECHO)
1370 ) {
1371 /* Happens when e.g. stty -echo was run before */
1372 int len;
1373 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001374 fflush(stdout);
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001375 if (fgets(command, maxsize, stdin) == NULL)
1376 len = -1; /* EOF or error */
1377 else
1378 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001379 DEINIT_S();
1380 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001381 }
1382
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001383// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001384 if (maxsize > MAX_LINELEN)
1385 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001386
1387 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001388 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001389#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001390 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001391 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001392#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001393
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001394 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001395 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001396 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001397 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001398 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001399
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001400 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001401 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1402 /* Turn off echoing and CTRL-C, so we can trap it */
1403 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001404 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001405 new_settings.c_cc[VMIN] = 1;
1406 new_settings.c_cc[VTIME] = 0;
1407 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001408#ifndef _POSIX_VDISABLE
1409#define _POSIX_VDISABLE '\0'
1410#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001411 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001412 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001413
Eric Andersen6faae7d2001-02-16 20:09:17 +00001414 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001415 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1416 win_changed(0); /* do initial resizing */
1417#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1418 {
1419 struct passwd *entry;
1420
1421 entry = getpwuid(geteuid());
1422 if (entry) {
1423 user_buf = xstrdup(entry->pw_name);
1424 home_pwd_buf = xstrdup(entry->pw_dir);
1425 }
1426 }
1427#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001428 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001429 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001430
Erik Andersenc7c634b2000-03-19 05:28:55 +00001431 while (1) {
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001432 fflush(NULL);
Mark Whitley4e338752001-01-26 20:42:23 +00001433
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001434 if (nonblock_safe_read(STDIN_FILENO, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001435 /* if we can't read input then exit */
1436 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001437 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001438
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001439 ic = c;
1440
Denis Vlasenko38f63192007-01-22 09:03:07 +00001441#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001442 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001443 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001444 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001445#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001446 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001447 case '\n':
1448 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001449 vi_case('\n'|vbit:)
1450 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001451 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001452 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001453 break_out = 1;
1454 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001455 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001456 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001457 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001458 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001459 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001460 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001461 vi_case('h'|vbit:)
1462 vi_case('\b'|vbit:)
1463 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001464 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001465 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001466 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001467 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001468 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001469 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001470 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001471 command_len = 0;
1472 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001473 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001474 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001475 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001476 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001477 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001478 errno = 0;
1479 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001480 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001481 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001482 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001483 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001484 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001485 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001486
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001487 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001488 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001489 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001490 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001491 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001492 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001493 vi_case('l'|vbit:)
1494 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001495 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001496 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001497 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001498
Erik Andersenf0657d32000-04-12 17:49:52 +00001499 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001500 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001501 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001502 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001503 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001504
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001505#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001506 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001507 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001508 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001509#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001510
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001511 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001512 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001513 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001514 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001515 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001516 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001517 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001518 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001519 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001520 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001521 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001522 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001523
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001524#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001525 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001526 vi_case(CTRL('N')|vbit:)
1527 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001528 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001529 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001530 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001531 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001532 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001533 vi_case(CTRL('P')|vbit:)
1534 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001535 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001536 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001537 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001538 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001539 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001540 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001541 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001542#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001543
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001544 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001545 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001546 /* Control-U -- Clear line before cursor */
1547 if (cursor) {
1548 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001549 command_len -= cursor;
1550 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001551 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001552 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001553 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001554 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001555 /* Control-W -- Remove the last word */
1556 while (cursor > 0 && isspace(command[cursor-1]))
1557 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001558 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001559 input_backspace();
1560 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001561
Denis Vlasenko38f63192007-01-22 09:03:07 +00001562#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001563 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001564 vi_cmdmode = 0;
1565 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001566 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001567 input_backward(cursor);
1568 vi_cmdmode = 0;
1569 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001570 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001571 input_forward();
1572 vi_cmdmode = 0;
1573 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001574 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001575 input_end();
1576 vi_cmdmode = 0;
1577 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001578 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001579 input_delete(1);
1580 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001581 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001582 if (cursor > 0) {
1583 input_backward(1);
1584 input_delete(1);
1585 }
1586 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001587 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001588 vi_Word_motion(command, 1);
1589 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001590 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001591 vi_word_motion(command, 1);
1592 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001593 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001594 vi_End_motion(command);
1595 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001596 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001597 vi_end_motion(command);
1598 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001599 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001600 vi_Back_motion(command);
1601 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001602 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001603 vi_back_motion(command);
1604 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001605 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001606 vi_cmdmode = 0;
1607 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001608 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001609 goto clear_to_eol;
1610
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001611 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001612 vi_cmdmode = 0;
1613 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001614 case 'd'|vbit: {
1615 int nc, sc;
1616 sc = cursor;
1617 prevc = ic;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001618 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001619 goto prepare_to_die;
1620 if (c == (prevc & 0xff)) {
1621 /* "cc", "dd" */
1622 input_backward(cursor);
1623 goto clear_to_eol;
1624 break;
1625 }
1626 switch (c) {
1627 case 'w':
1628 case 'W':
1629 case 'e':
1630 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001631 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001632 case 'w': /* "dw", "cw" */
1633 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001634 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001635 case 'W': /* 'dW', 'cW' */
1636 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001637 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001638 case 'e': /* 'de', 'ce' */
1639 vi_end_motion(command);
1640 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001641 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001642 case 'E': /* 'dE', 'cE' */
1643 vi_End_motion(command);
1644 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001645 break;
1646 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001647 nc = cursor;
1648 input_backward(cursor - sc);
1649 while (nc-- > cursor)
1650 input_delete(1);
1651 break;
1652 case 'b': /* "db", "cb" */
1653 case 'B': /* implemented as B */
1654 if (c == 'b')
1655 vi_back_motion(command);
1656 else
1657 vi_Back_motion(command);
1658 while (sc-- > cursor)
1659 input_delete(1);
1660 break;
1661 case ' ': /* "d ", "c " */
1662 input_delete(1);
1663 break;
1664 case '$': /* "d$", "c$" */
1665 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001666 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001667 input_delete(1);
1668 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001669 }
1670 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001671 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001672 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001673 input_forward();
1674 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001675 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001676 put();
1677 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001678 case 'r'|vbit:
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001679 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001680 goto prepare_to_die;
1681 if (c == 0)
1682 beep();
1683 else {
1684 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001685 bb_putchar(c);
1686 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001687 }
1688 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001689#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001690
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001691 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001692
Denis Vlasenko38f63192007-01-22 09:03:07 +00001693#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001694 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001695 /* ESC: insert mode --> command mode */
1696 vi_cmdmode = 1;
1697 input_backward(1);
1698 break;
1699 }
1700#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001701 /* escape sequence follows */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001702 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001703 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001704 /* different vt100 emulations */
1705 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001706 vi_case('['|vbit:)
1707 vi_case('O'|vbit:)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001708 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001709 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001710 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001711 if (c >= '1' && c <= '9') {
1712 unsigned char dummy;
1713
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001714 if (safe_read(STDIN_FILENO, &dummy, 1) < 1)
Glenn L McGrath475820c2004-01-22 12:42:23 +00001715 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001716 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001717 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001718 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001719
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001720 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001721#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001722 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001723 input_tab(&lastWasTab);
1724 break;
1725#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001726#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001727 case 'A':
1728 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001729 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001730 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001731 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001732 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001733 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001734 break;
1735 case 'B':
1736 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001737 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001738 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001739 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001740 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001741 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001742 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001743 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001744 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001745 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001746#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001747 case 'C':
1748 /* Right Arrow -- Move forward one character */
1749 input_forward();
1750 break;
1751 case 'D':
1752 /* Left Arrow -- Move back one character */
1753 input_backward(1);
1754 break;
1755 case '3':
1756 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001757 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001758 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001759 case '1': // vt100? linux vt? or what?
1760 case '7': // vt100? linux vt? or what?
1761 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001762 input_backward(cursor);
1763 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001764 case '4': // vt100? linux vt? or what?
1765 case '8': // vt100? linux vt? or what?
1766 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001767 input_end();
1768 break;
1769 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001770 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001771 beep();
1772 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001773 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001774
Eric Andersenc470f442003-07-28 09:56:35 +00001775 default: /* If it's regular input, do the normal thing */
Paul Fox84bbac52008-01-11 16:50:08 +00001776
1777 /* Control-V -- force insert of next char */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001778 if (c == CTRL('V')) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001779 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001780 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001781 if (c == 0) {
1782 beep();
1783 break;
1784 }
Paul Fox84bbac52008-01-11 16:50:08 +00001785 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001786
Denis Vlasenko38f63192007-01-22 09:03:07 +00001787#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001788 if (vi_cmdmode) /* Don't self-insert */
1789 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001790#endif
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00001791 if ((int)command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001792 break;
1793
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001794 command_len++;
1795 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001796 command[cursor] = c;
1797 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001798 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001799 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001800 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001801
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001802 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001803 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001804 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001805 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001806 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001807 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001808 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001809 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001810 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001811 }
Eric Andersenc470f442003-07-28 09:56:35 +00001812 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001813 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001814
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001815#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001816 if (c != '\t')
1817 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001818#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001819 }
1820
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001821 if (command_len > 0)
1822 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001823
Eric Andersen27bb7902003-12-23 20:24:51 +00001824 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001825 command[command_len++] = '\n';
1826 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001827 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001828
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001829#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001830 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001831#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001832
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001833 /* restore initial_settings */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001834 tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001835 /* restore SIGWINCH handler */
1836 signal(SIGWINCH, previous_SIGWINCH_handler);
1837 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001838
1839 DEINIT_S();
1840
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001841 return command_len;
1842}
1843
1844line_input_t *new_line_input_t(int flags)
1845{
1846 line_input_t *n = xzalloc(sizeof(*n));
1847 n->flags = flags;
1848 return n;
1849}
1850
1851#else
1852
1853#undef read_line_input
1854int read_line_input(const char* prompt, char* command, int maxsize)
1855{
1856 fputs(prompt, stdout);
1857 fflush(stdout);
1858 fgets(command, maxsize, stdin);
1859 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001860}
1861
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001862#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001863
1864
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001865/*
1866 * Testing
1867 */
1868
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001869#ifdef TEST
1870
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001871#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001872
1873const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001874
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001875int main(int argc, char **argv)
1876{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001877 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001878 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001879#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001880 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1881 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1882 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001883#else
1884 "% ";
1885#endif
1886
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001887#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001888 setlocale(LC_ALL, "");
1889#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001890 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001891 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001892 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001893 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001894 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001895 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001896 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001897 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001898 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001899 return 0;
1900}
1901
Eric Andersenc470f442003-07-28 09:56:35 +00001902#endif /* TEST */