blob: 42f372fb944611170cad6038251141fd8abfe4f7 [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
Denis Vlasenkob267ed92008-05-25 21:52:03 +000083 unsigned cmdedit_x; /* real x terminal position */
84 unsigned cmdedit_y; /* pseudoreal y terminal position */
85 unsigned cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000086
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000087 unsigned cursor;
88 unsigned command_len;
89 char *command_ps;
90
91 const char *cmdedit_prompt;
Denis Vlasenko38f63192007-01-22 09:03:07 +000092#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000093 int num_ok_lines; /* = 1; */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000094#endif
95
Denis Vlasenko82b39e82007-01-21 19:18:19 +000096#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +000097 char *user_buf;
98 char *home_pwd_buf; /* = (char*)null_str; */
Denis Vlasenko82b39e82007-01-21 19:18:19 +000099#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000100
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000101#if ENABLE_FEATURE_TAB_COMPLETION
102 char **matches;
103 unsigned num_matches;
104#endif
105
106#if ENABLE_FEATURE_EDITING_VI
107#define DELBUFSIZ 128
108 char *delptr;
109 smallint newdelflag; /* whether delbuf should be reused yet */
110 char delbuf[DELBUFSIZ]; /* a place to store deleted characters */
111#endif
112
113 /* Formerly these were big buffers on stack: */
114#if ENABLE_FEATURE_TAB_COMPLETION
115 char exe_n_cwd_tab_completion__dirbuf[MAX_LINELEN];
116 char input_tab__matchBuf[MAX_LINELEN];
117 int16_t find_match__int_buf[MAX_LINELEN + 1]; /* need to have 9 bits at least */
118 int16_t find_match__pos_buf[MAX_LINELEN + 1];
119#endif
120};
121
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000122/* See lineedit_ptr_hack.c */
123extern struct lineedit_statics *const lineedit_ptr_to_statics;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000124
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000125#define S (*lineedit_ptr_to_statics)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000126#define state (S.state )
127#define cmdedit_termw (S.cmdedit_termw )
128#define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler)
129#define cmdedit_x (S.cmdedit_x )
130#define cmdedit_y (S.cmdedit_y )
131#define cmdedit_prmt_len (S.cmdedit_prmt_len)
132#define cursor (S.cursor )
133#define command_len (S.command_len )
134#define command_ps (S.command_ps )
135#define cmdedit_prompt (S.cmdedit_prompt )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000136#define num_ok_lines (S.num_ok_lines )
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000137#define user_buf (S.user_buf )
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000138#define home_pwd_buf (S.home_pwd_buf )
139#define matches (S.matches )
140#define num_matches (S.num_matches )
141#define delptr (S.delptr )
142#define newdelflag (S.newdelflag )
143#define delbuf (S.delbuf )
144
145#define INIT_S() do { \
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000146 (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000147 barrier(); \
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000148 cmdedit_termw = 80; \
149 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
150 USE_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \
151} while (0)
152static void deinit_S(void)
153{
154#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000155 /* This one is allocated only if FANCY_PROMPT is on
156 * (otherwise it points to verbatim prompt (NOT malloced) */
157 free((char*)cmdedit_prompt);
158#endif
159#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
160 free(user_buf);
161 if (home_pwd_buf != null_str)
162 free(home_pwd_buf);
163#endif
Denis Vlasenko5d89fba2008-04-22 00:08:27 +0000164 free(lineedit_ptr_to_statics);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000165}
166#define DEINIT_S() deinit_S()
167
Denis Vlasenko2c844952008-04-25 18:44:35 +0000168
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000169/* Put 'command_ps[cursor]', cursor++.
170 * Advance cursor on screen. If we reached right margin, scroll text up
171 * and remove terminal margin effect by printing 'next_char' */
Denis Vlasenko2c844952008-04-25 18:44:35 +0000172#define HACK_FOR_WRONG_WIDTH 1
173#if HACK_FOR_WRONG_WIDTH
174static void cmdedit_set_out_char(void)
175#define cmdedit_set_out_char(next_char) cmdedit_set_out_char()
176#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000177static void cmdedit_set_out_char(int next_char)
Denis Vlasenko2c844952008-04-25 18:44:35 +0000178#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000179{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000180 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000181
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000182 if (c == '\0') {
183 /* erase character after end of input string */
184 c = ' ';
185 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000186#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000187 /* Display non-printable characters in reverse */
188 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000189 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000190 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000191 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000192 c += '@';
193 if (c == 127)
194 c = '?';
195 printf("\033[7m%c\033[0m", c);
196 } else
197#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000198 {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000199 bb_putchar(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000200 }
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000201 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000202 /* terminal is scrolled down */
203 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000204 cmdedit_x = 0;
Denis Vlasenko2c844952008-04-25 18:44:35 +0000205#if HACK_FOR_WRONG_WIDTH
206 /* This works better if our idea of term width is wrong
207 * and it is actually wider (often happens on serial lines).
208 * Printing CR,LF *forces* cursor to next line.
209 * OTOH if terminal width is correct AND terminal does NOT
210 * have automargin (IOW: it is moving cursor to next line
211 * by itself (which is wrong for VT-10x terminals)),
212 * this will break things: there will be one extra empty line */
213 puts("\r"); /* + implicit '\n' */
214#else
215 /* Works ok only if cmdedit_termw is correct */
Mark Whitley4e338752001-01-26 20:42:23 +0000216 /* destroy "(auto)margin" */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000217 bb_putchar(next_char);
218 bb_putchar('\b');
Denis Vlasenko2c844952008-04-25 18:44:35 +0000219#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000220 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000221// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000222 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000223}
224
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000225/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000226static void input_end(void)
227{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000228 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000229 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000230}
231
232/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000233static void goto_new_line(void)
234{
Mark Whitley4e338752001-01-26 20:42:23 +0000235 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000236 if (cmdedit_x)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000237 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000238}
239
240
Rob Landley88621d72006-08-29 19:41:06 +0000241static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000242{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000243 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000244 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000245}
Eric Andersen81fe1232003-07-29 06:38:40 +0000246
Rob Landley88621d72006-08-29 19:41:06 +0000247static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000248{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000249 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000250}
251
Eric Andersenaff114c2004-04-14 17:51:38 +0000252/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000253/* (optimized for slow terminals) */
254static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000255{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000256 int count_y;
257
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000258 if (num > cursor)
259 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000260 if (!num)
261 return;
262 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000263
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000264 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000265 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000266 if (num <= 4) {
Denis Vlasenko6f043912008-02-18 22:28:03 +0000267 /* This is longer by 5 bytes on x86.
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000268 * Also gets miscompiled for ARM users
269 * (busybox.net/bugs/view.php?id=2274).
Denis Vlasenko6f043912008-02-18 22:28:03 +0000270 * printf(("\b\b\b\b" + 4) - num);
271 * return;
272 */
273 do {
274 bb_putchar('\b');
275 } while (--num);
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000276 return;
277 }
278 printf("\033[%uD", num);
279 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000280 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000281
282 /* Need to go one or more lines up */
283 num -= cmdedit_x;
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000284 {
285 unsigned w = cmdedit_termw; /* volatile var */
286 count_y = 1 + (num / w);
287 cmdedit_y -= count_y;
288 cmdedit_x = w * count_y - num;
289 }
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000290 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000291 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000292}
293
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000294static void put_prompt(void)
295{
296 out1str(cmdedit_prompt);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000297 cursor = 0;
Denis Vlasenkob267ed92008-05-25 21:52:03 +0000298 {
299 unsigned w = cmdedit_termw; /* volatile var */
300 cmdedit_y = cmdedit_prmt_len / w; /* new quasireal y */
301 cmdedit_x = cmdedit_prmt_len % w;
302 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000303}
304
Eric Andersenaff114c2004-04-14 17:51:38 +0000305/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000306static void redraw(int y, int back_cursor)
307{
Eric Andersenc470f442003-07-28 09:56:35 +0000308 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000309 printf("\033[%dA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000310 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000311 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000312 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000313 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000314 input_backward(back_cursor);
315}
316
Paul Fox3f11b1b2005-08-04 19:04:46 +0000317/* Delete the char in front of the cursor, optionally saving it
318 * for later putback */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000319#if !ENABLE_FEATURE_EDITING_VI
320static void input_delete(void)
321#define input_delete(save) input_delete()
322#else
Paul Fox3f11b1b2005-08-04 19:04:46 +0000323static void input_delete(int save)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000324#endif
Erik Andersenf0657d32000-04-12 17:49:52 +0000325{
Mark Whitley4e338752001-01-26 20:42:23 +0000326 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000327
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000328 if (j == (int)command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000329 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000330
Denis Vlasenko38f63192007-01-22 09:03:07 +0000331#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000332 if (save) {
333 if (newdelflag) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000334 delptr = delbuf;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000335 newdelflag = 0;
336 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000337 if ((delptr - delbuf) < DELBUFSIZ)
338 *delptr++ = command_ps[j];
Paul Fox3f11b1b2005-08-04 19:04:46 +0000339 }
340#endif
341
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000342 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000343 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000344 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000345 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000346 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000347}
348
Denis Vlasenko38f63192007-01-22 09:03:07 +0000349#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000350static void put(void)
351{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000352 int ocursor;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000353 int j = delptr - delbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000354
Paul Fox3f11b1b2005-08-04 19:04:46 +0000355 if (j == 0)
356 return;
357 ocursor = cursor;
358 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000359 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000360 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000361 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000362 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000363 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000364}
365#endif
366
Mark Whitley4e338752001-01-26 20:42:23 +0000367/* Delete the char in back of the cursor */
368static void input_backspace(void)
369{
370 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000371 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000372 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000373 }
374}
375
Eric Andersenaff114c2004-04-14 17:51:38 +0000376/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000377static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000378{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000379 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000380 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000381}
382
Denis Vlasenko38f63192007-01-22 09:03:07 +0000383#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000384
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000385static void free_tab_completion_data(void)
386{
387 if (matches) {
388 while (num_matches)
389 free(matches[--num_matches]);
390 free(matches);
391 matches = NULL;
392 }
393}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000394
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000395static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000396{
397 int nm = num_matches;
398 int nm1 = nm + 1;
399
400 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000401 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000402 num_matches++;
403}
404
Denis Vlasenko38f63192007-01-22 09:03:07 +0000405#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000406static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000407{
408 struct passwd *entry;
409 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000410
Eric Andersenc470f442003-07-28 09:56:35 +0000411 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000412 userlen = strlen(ud);
413
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000414 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000415 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000416 char *home = NULL;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000417
Eric Andersenc470f442003-07-28 09:56:35 +0000418 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000419 home = home_pwd_buf;
420 } else {
421 /* "~user/..." */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000422 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000423 temp = strchr(ud, '/');
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000424 *temp = '\0'; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000425 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000426 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000427 ud = temp;
428 if (entry)
429 home = entry->pw_dir;
430 }
431 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000432 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000433 /* /home/user/... */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000434 sprintf(sav_ud, "%s%s", home, ud);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000435 }
436 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000437 } else {
438 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000439 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000440 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000441 struct passwd pwd;
442 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000443
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000444 setpwent();
445 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000446 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000447 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
448 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000449 }
450 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000451 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000452 }
453}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000454#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000455
456enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000457 FIND_EXE_ONLY = 0,
458 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000459 FIND_FILE_ONLY = 2,
460};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000461
Mark Whitley4e338752001-01-26 20:42:23 +0000462static int path_parse(char ***p, int flags)
463{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000464 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000465 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000466 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000467 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000468
Mark Whitley4e338752001-01-26 20:42:23 +0000469 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000470 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000471 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000472
473 if (state->flags & WITH_PATH_LOOKUP)
474 pth = state->path_lookup;
475 else
476 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000477 /* PATH=<empty> or PATH=:<empty> */
478 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
479 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000480
Denis Vlasenko253ce002007-01-22 08:34:44 +0000481 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000482 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000483 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000484 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000485 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000486 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000487 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000488 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000489 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000490 }
491
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000492 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000493 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000494 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000495 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000496 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000497 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000498 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000499 *tmp++ = '\0'; /* ':' -> '\0' */
500 if (*tmp == '\0')
501 break; /* :<empty> */
502 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000503 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000504 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000505 return npth;
506}
507
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000508static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000509{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000510 DIR *dir;
511 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000512 struct stat st;
513 char *path1[1];
514 char **paths = path1;
515 int npaths;
516 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000517 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518 char *pfind = strrchr(command, '/');
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000519/* char dirbuf[MAX_LINELEN]; */
520#define dirbuf (S.exe_n_cwd_tab_completion__dirbuf)
Mark Whitley4e338752001-01-26 20:42:23 +0000521
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000522 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000523 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000524
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000525 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000526 /* no dir, if flags==EXE_ONLY - get paths, else "." */
527 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000528 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000529 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000530 /* dirbuf = ".../.../.../" */
531 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000532#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000533 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000534 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000535#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000536 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000537 /* point to 'l' in "..../last_component" */
538 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000539 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000540
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000541 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000542 dir = opendir(paths[i]);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000543 if (!dir)
544 continue; /* don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000545
546 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000547 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000548 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000549
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000550 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000551 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000552 continue;
553 /* not see .name without .match */
Denis Vlasenkob5202712008-04-24 04:42:52 +0000554 if (*str_found == '.' && *pfind == '\0') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000555 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000556 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000557 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000558 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000559 found = concat_path_file(paths[i], str_found);
Denis Vlasenkob5202712008-04-24 04:42:52 +0000560 /* hmm, remove in progress? */
561 /* NB: stat() first so that we see is it a directory;
562 * but if that fails, use lstat() so that
563 * we still match dangling links */
564 if (stat(found, &st) && lstat(found, &st))
Eric Andersene5dfced2001-04-09 22:48:12 +0000565 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000566 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000567 if (paths[i] != dirbuf)
Denis Vlasenkob5202712008-04-24 04:42:52 +0000568 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000569
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000570 len1 = strlen(found);
571 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000572 found[len1] = '\0';
573 found[len1+1] = '\0';
574
Mark Whitley4e338752001-01-26 20:42:23 +0000575 if (S_ISDIR(st.st_mode)) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000576 /* name is a directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000577 if (found[len1-1] != '/') {
578 found[len1] = '/';
579 }
Mark Whitley4e338752001-01-26 20:42:23 +0000580 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000581 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000582 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000583 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000584 }
Mark Whitley4e338752001-01-26 20:42:23 +0000585 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000586 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000587 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000588 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000589 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000590 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000591 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000592 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000593 if (paths != path1) {
Denis Vlasenkob5202712008-04-24 04:42:52 +0000594 free(paths[0]); /* allocated memory is only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000595 free(paths);
596 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000597#undef dirbuf
Erik Andersen6273f652000-03-17 01:12:41 +0000598}
Erik Andersenf0657d32000-04-12 17:49:52 +0000599
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000600#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000601
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000602#define collapse_pos(is, in) do { \
603 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
604 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in)) * sizeof(pos_buf[0])); \
605} while (0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000606
607static int find_match(char *matchBuf, int *len_with_quotes)
608{
609 int i, j;
610 int command_mode;
611 int c, c2;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000612/* int16_t int_buf[MAX_LINELEN + 1]; */
613/* int16_t pos_buf[MAX_LINELEN + 1]; */
614#define int_buf (S.find_match__int_buf)
615#define pos_buf (S.find_match__pos_buf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000616
617 /* set to integer dimension characters and own positions */
618 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000619 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000620 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000621 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000622 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000623 }
624 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000625 }
626
627 /* mask \+symbol and convert '\t' to ' ' */
628 for (i = j = 0; matchBuf[i]; i++, j++)
629 if (matchBuf[i] == '\\') {
630 collapse_pos(j, j + 1);
631 int_buf[j] |= QUOT;
632 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000633#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000634 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000635 int_buf[j] = ' ' | QUOT;
636#endif
637 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000638#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000639 else if (matchBuf[i] == '\t')
640 int_buf[j] = ' ';
641#endif
642
643 /* mask "symbols" or 'symbols' */
644 c2 = 0;
645 for (i = 0; int_buf[i]; i++) {
646 c = int_buf[i];
647 if (c == '\'' || c == '"') {
648 if (c2 == 0)
649 c2 = c;
650 else {
651 if (c == c2)
652 c2 = 0;
653 else
654 int_buf[i] |= QUOT;
655 }
656 } else if (c2 != 0 && c != '$')
657 int_buf[i] |= QUOT;
658 }
659
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000660 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000661 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
662 for (i = 0; int_buf[i]; i++) {
663 c = int_buf[i];
664 c2 = int_buf[i + 1];
665 j = i ? int_buf[i - 1] : -1;
666 command_mode = 0;
667 if (c == ';' || c == '&' || c == '|') {
668 command_mode = 1 + (c == c2);
669 if (c == '&') {
670 if (j == '>' || j == '<')
671 command_mode = 0;
672 } else if (c == '|' && j == '>')
673 command_mode = 0;
674 }
675 if (command_mode) {
676 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000677 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000678 }
679 }
680 /* collapse `command...` */
681 for (i = 0; int_buf[i]; i++)
682 if (int_buf[i] == '`') {
683 for (j = i + 1; int_buf[j]; j++)
684 if (int_buf[j] == '`') {
685 collapse_pos(i, j + 1);
686 j = 0;
687 break;
688 }
689 if (j) {
690 /* not found close ` - command mode, collapse all previous */
691 collapse_pos(0, i + 1);
692 break;
693 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000694 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000695 }
696
697 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000698 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000699 c2 = 0;
700 for (i = 0; int_buf[i]; i++)
701 if (int_buf[i] == '(' || int_buf[i] == '{') {
702 if (int_buf[i] == '(')
703 c++;
704 else
705 c2++;
706 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000707 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000708 }
709 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
710 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
711 if (int_buf[i] == ')')
712 c--;
713 else
714 c2--;
715 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000716 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000717 }
718
719 /* skip first not quote space */
720 for (i = 0; int_buf[i]; i++)
721 if (int_buf[i] != ' ')
722 break;
723 if (i)
724 collapse_pos(0, i);
725
726 /* set find mode for completion */
727 command_mode = FIND_EXE_ONLY;
728 for (i = 0; int_buf[i]; i++)
729 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
730 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000731 && matchBuf[pos_buf[0]] == 'c'
732 && matchBuf[pos_buf[1]] == 'd'
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000733 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000734 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000735 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000736 command_mode = FIND_FILE_ONLY;
737 break;
738 }
739 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000740 for (i = 0; int_buf[i]; i++)
741 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000742 /* find last word */
743 for (--i; i >= 0; i--) {
744 c = int_buf[i];
745 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
746 collapse_pos(0, i + 1);
747 break;
748 }
749 }
750 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000751 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
752 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000753 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000754 while ((int_buf[i] & ~QUOT) == '/'
755 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
756 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000757 i++;
758 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000759
760 /* set only match and destroy quotes */
761 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000762 for (c = 0; pos_buf[i] >= 0; i++) {
763 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000764 j = pos_buf[i] + 1;
765 }
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000766 matchBuf[c] = '\0';
Denis Vlasenkof74194e2007-10-18 12:54:39 +0000767 /* old length matchBuf with quotes symbols */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000768 *len_with_quotes = j ? j - pos_buf[0] : 0;
769
770 return command_mode;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000771#undef int_buf
772#undef pos_buf
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000773}
774
Glenn L McGrath4d001292003-01-06 01:11:50 +0000775/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000776 * display by column (original idea from ls applet,
777 * very optimized by me :)
778 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000779static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000780{
781 int ncols, row;
782 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000783 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000784 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000785 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000786
787 /* find the longest file name- use that as the column width */
788 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000789 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000790 if (column_width < l)
791 column_width = l;
792 }
793 column_width += 2; /* min space for columns */
794 ncols = cmdedit_termw / column_width;
795
796 if (ncols > 1) {
797 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000798 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000799 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000800 } else {
801 ncols = 1;
802 }
803 for (row = 0; row < nrows; row++) {
804 int n = row;
805 int nc;
806
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000807 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000808 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000809 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000810 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000811 puts(matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000812 }
813}
814
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000815static char *add_quote_for_spec_chars(char *found)
816{
817 int l = 0;
818 char *s = xmalloc((strlen(found) + 1) * 2);
819
820 while (*found) {
821 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
822 s[l++] = '\\';
823 s[l++] = *found++;
824 }
825 s[l] = 0;
826 return s;
827}
828
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000829/* Do TAB completion */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000830static void input_tab(smallint *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000831{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000832 if (!(state->flags & TAB_COMPLETION))
833 return;
834
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000835 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000836 char *tmp, *tmp1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000837 size_t len_found;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000838/* char matchBuf[MAX_LINELEN]; */
839#define matchBuf (S.input_tab__matchBuf)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000840 int find_type;
841 int recalc_pos;
842
Eric Andersenc470f442003-07-28 09:56:35 +0000843 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000844
845 /* Make a local copy of the string -- up
846 * to the position of the cursor */
847 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000848 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000849
850 find_type = find_match(matchBuf, &recalc_pos);
851
852 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000853 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000854
Denis Vlasenko38f63192007-01-22 09:03:07 +0000855#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000856 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000857 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000858 if (state->flags & USERNAME_COMPLETION)
859 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
860 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000861#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000862 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000863 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000864 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000865 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000866 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000867 if (matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000868 unsigned i;
869 int n = 0;
Denis Vlasenkofb290382008-03-02 12:51:26 +0000870 qsort_string_vector(matches, num_matches);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000871 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000872 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000873 if (strcmp(matches[i], matches[i+1]) == 0) {
874 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000875 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000876 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000877 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000878 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000879 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000880 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000881 matches[n] = matches[i];
882 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000883 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000884 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000885 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000886 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000887 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000888 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000889 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000890 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000891 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000892 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000893 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000894 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000895 break;
896 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000897 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000898 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000899 return;
900 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000901 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000902 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000903 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000904 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000905 /* for next completion current found */
906 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000907
908 len_found = strlen(tmp);
909 if (tmp[len_found-1] != '/') {
910 tmp[len_found] = ' ';
911 tmp[len_found+1] = '\0';
912 }
Mark Whitley4e338752001-01-26 20:42:23 +0000913 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000914 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000915 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000916 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000917 /* before word for match */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000918 command_ps[cursor - recalc_pos] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000919 /* save tail line */
920 strcpy(matchBuf, command_ps + cursor);
921 /* add match */
922 strcat(command_ps, tmp);
923 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000924 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000925 /* back to begin word for match */
926 input_backward(recalc_pos);
927 /* new pos */
928 recalc_pos = cursor + len_found;
929 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000930 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000931 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000932 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000933 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000934 free(tmp);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +0000935#undef matchBuf
Erik Andersenf0657d32000-04-12 17:49:52 +0000936 } else {
937 /* Ok -- the last char was a TAB. Since they
938 * just hit TAB again, print a list of all the
939 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000940 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000941 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000942
943 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000944 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000945 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000946 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000947 }
948 }
949}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000950
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000951#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000952
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000953
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000954#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000955
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000956/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000957static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000958{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000959 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
960 free(state->history[state->cur_history]);
961 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000962 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000963 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000964}
965
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000966static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000967{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000968 if (state->flags & DO_HISTORY) {
969 int ch = state->cur_history;
970 if (ch < state->cnt_history) {
971 get_previous_history(); /* save the current history line */
972 state->cur_history = ch + 1;
973 return state->cur_history;
974 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000975 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000976 beep();
977 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000978}
Robert Griebl350d26b2002-12-03 22:45:46 +0000979
Denis Vlasenko38f63192007-01-22 09:03:07 +0000980#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000981/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000982static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000983{
Robert Griebl350d26b2002-12-03 22:45:46 +0000984 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000985 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000986
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000987 /* NB: do not trash old history if file can't be opened */
Robert Griebl350d26b2002-12-03 22:45:46 +0000988
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000989 fp = fopen(fromfile, "r");
990 if (fp) {
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000991 /* clean up old history */
992 for (hi = state->cnt_history; hi > 0;) {
993 hi--;
994 free(state->history[hi]);
995 }
996
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000997 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000998 char *hl = xmalloc_fgetline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000999 int l;
1000
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001001 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001002 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001003 l = strlen(hl);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001004 if (l >= MAX_LINELEN)
1005 hl[MAX_LINELEN-1] = '\0';
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001006 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001007 free(hl);
1008 continue;
1009 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001010 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001011 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001012 fclose(fp);
Denis Vlasenko08ec67b2008-03-26 13:32:30 +00001013 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001014 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001015}
1016
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001017/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +00001018static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +00001019{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001020 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +00001021
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001022 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001023 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +00001024 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001025
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001026 for (i = 0; i < state->cnt_history; i++) {
1027 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001028 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001029 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001030 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001031}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001032#else
1033#define load_history(a) ((void)0)
1034#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001035#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001036
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001037static void remember_in_history(const char *str)
1038{
1039 int i;
1040
1041 if (!(state->flags & DO_HISTORY))
1042 return;
1043
1044 i = state->cnt_history;
1045 free(state->history[MAX_HISTORY]);
1046 state->history[MAX_HISTORY] = NULL;
1047 /* After max history, remove the oldest command */
1048 if (i >= MAX_HISTORY) {
1049 free(state->history[0]);
1050 for (i = 0; i < MAX_HISTORY-1; i++)
1051 state->history[i] = state->history[i+1];
1052 }
1053// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
1054// (i.e. do not save dups?)
1055 state->history[i++] = xstrdup(str);
1056 state->cur_history = i;
1057 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +00001058#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001059 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001060 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +00001061#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00001062 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001063}
1064
1065#else /* MAX_HISTORY == 0 */
1066#define remember_in_history(a) ((void)0)
1067#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001068
1069
Erik Andersen6273f652000-03-17 01:12:41 +00001070/*
1071 * This function is used to grab a character buffer
1072 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001073 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001074 * a mini readline).
1075 *
1076 * The following standard commands are not implemented:
1077 * ESC-b -- Move back one word
1078 * ESC-f -- Move forward one word
1079 * ESC-d -- Delete back one word
1080 * ESC-h -- Delete forward one word
1081 * CTL-t -- Transpose two characters
1082 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001083 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001084 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001085 */
Eric Andersenc470f442003-07-28 09:56:35 +00001086
Denis Vlasenko38f63192007-01-22 09:03:07 +00001087#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001088static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001089vi_Word_motion(char *command, int eat)
1090{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001091 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001092 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001093 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001094 input_forward();
1095}
1096
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001097static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001098vi_word_motion(char *command, int eat)
1099{
1100 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001101 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001102 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001103 input_forward();
1104 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001105 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001106 input_forward();
1107 }
1108
Denis Vlasenko253ce002007-01-22 08:34:44 +00001109 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001110 input_forward();
1111
Denis Vlasenko253ce002007-01-22 08:34:44 +00001112 if (eat && cursor < command_len && isspace(command[cursor]))
1113 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001114 input_forward();
1115}
1116
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001117static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001118vi_End_motion(char *command)
1119{
1120 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001121 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001122 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001123 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001124 input_forward();
1125}
1126
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001127static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001128vi_end_motion(char *command)
1129{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001130 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001131 return;
1132 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001133 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001134 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001135 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001136 return;
1137 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001138 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001139 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1140 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001141 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001142 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001143 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001144 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001145 input_forward();
1146 }
1147}
1148
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001149static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001150vi_Back_motion(char *command)
1151{
1152 while (cursor > 0 && isspace(command[cursor-1]))
1153 input_backward(1);
1154 while (cursor > 0 && !isspace(command[cursor-1]))
1155 input_backward(1);
1156}
1157
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001158static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001159vi_back_motion(char *command)
1160{
1161 if (cursor <= 0)
1162 return;
1163 input_backward(1);
1164 while (cursor > 0 && isspace(command[cursor]))
1165 input_backward(1);
1166 if (cursor <= 0)
1167 return;
1168 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001169 while (cursor > 0
1170 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1171 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001172 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001173 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001174 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001175 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001176 input_backward(1);
1177 }
1178}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001179#endif
1180
1181
1182/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001183 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001184 */
1185
Denis Vlasenko38f63192007-01-22 09:03:07 +00001186#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001187static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001188{
1189 cmdedit_prompt = prmt_ptr;
1190 cmdedit_prmt_len = strlen(prmt_ptr);
1191 put_prompt();
1192}
1193#else
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001194static void parse_and_put_prompt(const char *prmt_ptr)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001195{
1196 int prmt_len = 0;
1197 size_t cur_prmt_len = 0;
1198 char flg_not_length = '[';
1199 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001200 char *cwd_buf = xrealloc_getcwd_or_warn(NULL);
1201 char cbuf[2];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001202 char c;
1203 char *pbuf;
1204
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001205 cmdedit_prmt_len = 0;
1206
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001207 if (!cwd_buf) {
1208 cwd_buf = (char *)bb_msg_unknown;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001209 }
1210
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001211 cbuf[1] = '\0'; /* never changes */
1212
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001213 while (*prmt_ptr) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001214 char *free_me = NULL;
1215
1216 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001217 c = *prmt_ptr++;
1218 if (c == '\\') {
1219 const char *cp = prmt_ptr;
1220 int l;
1221
1222 c = bb_process_escape_sequence(&prmt_ptr);
1223 if (prmt_ptr == cp) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001224 if (*cp == '\0')
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001225 break;
1226 c = *prmt_ptr++;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001227
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001228 switch (c) {
1229#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1230 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001231 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001232 break;
1233#endif
1234 case 'h':
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001235 pbuf = free_me = safe_gethostname();
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001236 *strchrnul(pbuf, '.') = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001237 break;
1238 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001239 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001240 break;
1241#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1242 case 'w':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001243 /* /home/user[/something] -> ~[/something] */
1244 pbuf = cwd_buf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001245 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001246 if (l != 0
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001247 && strncmp(home_pwd_buf, cwd_buf, l) == 0
1248 && (cwd_buf[l]=='/' || cwd_buf[l]=='\0')
1249 && strlen(cwd_buf + l) < PATH_MAX
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001250 ) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001251 pbuf = free_me = xasprintf("~%s", cwd_buf + l);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001252 }
1253 break;
1254#endif
1255 case 'W':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001256 pbuf = cwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001257 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001258 if (cp != NULL && cp != pbuf)
1259 pbuf += (cp-pbuf) + 1;
1260 break;
1261 case '!':
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001262 pbuf = free_me = xasprintf("%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001263 break;
1264 case 'e': case 'E': /* \e \E = \033 */
1265 c = '\033';
1266 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001267 case 'x': case 'X': {
1268 char buf2[4];
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001269 for (l = 0; l < 3;) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001270 unsigned h;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001271 buf2[l++] = *prmt_ptr;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001272 buf2[l] = '\0';
1273 h = strtoul(buf2, &pbuf, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001274 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001275 buf2[--l] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001276 break;
1277 }
1278 prmt_ptr++;
1279 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001280 c = (char)strtoul(buf2, NULL, 16);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001281 if (c == 0)
1282 c = '?';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001283 pbuf = cbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001284 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001285 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001286 case '[': case ']':
1287 if (c == flg_not_length) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001288 flg_not_length = (flg_not_length == '[' ? ']' : '[');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001289 continue;
1290 }
1291 break;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001292 } /* switch */
1293 } /* if */
1294 } /* if */
1295 cbuf[0] = c;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001296 cur_prmt_len = strlen(pbuf);
1297 prmt_len += cur_prmt_len;
1298 if (flg_not_length != ']')
1299 cmdedit_prmt_len += cur_prmt_len;
1300 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Denis Vlasenko7221c8c2007-12-03 10:45:14 +00001301 free(free_me);
1302 } /* while */
1303
1304 if (cwd_buf != (char *)bb_msg_unknown)
1305 free(cwd_buf);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001306 cmdedit_prompt = prmt_mem_ptr;
1307 put_prompt();
1308}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001309#endif
1310
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001311static void cmdedit_setwidth(unsigned w, int redraw_flg)
1312{
1313 cmdedit_termw = w;
1314 if (redraw_flg) {
1315 /* new y for current cursor */
1316 int new_y = (cursor + cmdedit_prmt_len) / w;
1317 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001318 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001319 fflush(stdout);
1320 }
1321}
1322
1323static void win_changed(int nsig)
1324{
Denis Vlasenko55995022008-05-18 22:28:26 +00001325 unsigned width;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001326 get_terminal_width_height(0, &width, NULL);
1327 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1328 if (nsig == SIGWINCH)
1329 signal(SIGWINCH, win_changed); /* rearm ourself */
1330}
1331
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001332/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001333 * The emacs and vi modes share much of the code in the big
1334 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001335 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001336 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001337 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001338 */
1339
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001340#define vbit 0x100
1341
1342/* leave out the "vi-mode"-only case labels if vi editing isn't
1343 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001344#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001345
1346/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001347#undef CTRL
1348#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001349
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001350/* Returns:
Denis Vlasenko80667e32008-02-02 18:35:55 +00001351 * -1 on read errors or EOF, or on bare Ctrl-D,
1352 * 0 on ctrl-C (the line entered is still returned in 'command'),
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001353 * >0 length of input string, including terminating '\n'
1354 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001355int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001356{
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001357#if ENABLE_FEATURE_TAB_COMPLETION
1358 smallint lastWasTab = FALSE;
1359#endif
Denis Vlasenko55995022008-05-18 22:28:26 +00001360 unsigned ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001361 unsigned char c;
1362 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001363#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001364 smallint vi_cmdmode = 0;
1365 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001366#endif
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001367 struct termios initial_settings;
1368 struct termios new_settings;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001369
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001370 INIT_S();
1371
1372 if (tcgetattr(STDIN_FILENO, &initial_settings) < 0
1373 || !(initial_settings.c_lflag & ECHO)
1374 ) {
1375 /* Happens when e.g. stty -echo was run before */
1376 int len;
1377 parse_and_put_prompt(prompt);
Denis Vlasenko037576d2007-10-20 18:30:38 +00001378 fflush(stdout);
Denis Vlasenko9cb220b2007-12-09 10:03:28 +00001379 if (fgets(command, maxsize, stdin) == NULL)
1380 len = -1; /* EOF or error */
1381 else
1382 len = strlen(command);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001383 DEINIT_S();
1384 return len;
Denis Vlasenko037576d2007-10-20 18:30:38 +00001385 }
1386
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001387// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001388 if (maxsize > MAX_LINELEN)
1389 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001390
1391 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001392 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001393#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001394 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001395 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001396#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001397
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001398 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001399 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001400 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001401 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001402 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001403
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001404 new_settings = initial_settings;
Eric Andersen34506362001-08-02 05:02:46 +00001405 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1406 /* Turn off echoing and CTRL-C, so we can trap it */
1407 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001408 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001409 new_settings.c_cc[VMIN] = 1;
1410 new_settings.c_cc[VTIME] = 0;
1411 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001412#ifndef _POSIX_VDISABLE
1413#define _POSIX_VDISABLE '\0'
1414#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001415 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001416 tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001417
Eric Andersen6faae7d2001-02-16 20:09:17 +00001418 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001419 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1420 win_changed(0); /* do initial resizing */
1421#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1422 {
1423 struct passwd *entry;
1424
1425 entry = getpwuid(geteuid());
1426 if (entry) {
1427 user_buf = xstrdup(entry->pw_name);
1428 home_pwd_buf = xstrdup(entry->pw_dir);
1429 }
1430 }
1431#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001432 /* Print out the command prompt */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001433 parse_and_put_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001434
Erik Andersenc7c634b2000-03-19 05:28:55 +00001435 while (1) {
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001436 fflush(NULL);
Mark Whitley4e338752001-01-26 20:42:23 +00001437
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001438 if (nonblock_safe_read(STDIN_FILENO, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001439 /* if we can't read input then exit */
1440 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001441 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001442
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001443 ic = c;
1444
Denis Vlasenko38f63192007-01-22 09:03:07 +00001445#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001446 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001447 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001448 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001449#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001450 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001451 case '\n':
1452 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001453 vi_case('\n'|vbit:)
1454 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001455 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001456 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001457 break_out = 1;
1458 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001459 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001460 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001461 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001462 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001463 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001464 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001465 vi_case('h'|vbit:)
1466 vi_case('\b'|vbit:)
1467 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001468 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001469 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001470 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001471 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001472 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001473 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001474 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001475 command_len = 0;
1476 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001477 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001478 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001479 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001480 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001481 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001482 errno = 0;
1483 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001484 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001485 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001486 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001487 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001488 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001489 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001490
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001491 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001492 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001493 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001494 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001495 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001496 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001497 vi_case('l'|vbit:)
1498 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001499 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001500 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001501 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001502
Erik Andersenf0657d32000-04-12 17:49:52 +00001503 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001504 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001505 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001506 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001507 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001508
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001509#if ENABLE_FEATURE_TAB_COMPLETION
Erik Andersenc7c634b2000-03-19 05:28:55 +00001510 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001511 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001512 break;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001513#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001514
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001515 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001516 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001517 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001518 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001519 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001520 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001521 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001522 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001523 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001524 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001525 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001526 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001527
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001528#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001529 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001530 vi_case(CTRL('N')|vbit:)
1531 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001532 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001533 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001534 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001535 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001536 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001537 vi_case(CTRL('P')|vbit:)
1538 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001539 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001540 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001541 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001542 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001543 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001544 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001545 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001546#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001547
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001548 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001549 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001550 /* Control-U -- Clear line before cursor */
1551 if (cursor) {
1552 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001553 command_len -= cursor;
1554 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001555 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001556 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001557 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001558 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001559 /* Control-W -- Remove the last word */
1560 while (cursor > 0 && isspace(command[cursor-1]))
1561 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001562 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001563 input_backspace();
1564 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001565
Denis Vlasenko38f63192007-01-22 09:03:07 +00001566#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001567 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001568 vi_cmdmode = 0;
1569 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001570 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001571 input_backward(cursor);
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_forward();
1576 vi_cmdmode = 0;
1577 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001578 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001579 input_end();
1580 vi_cmdmode = 0;
1581 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001582 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001583 input_delete(1);
1584 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001585 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001586 if (cursor > 0) {
1587 input_backward(1);
1588 input_delete(1);
1589 }
1590 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001591 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001592 vi_Word_motion(command, 1);
1593 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001594 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001595 vi_word_motion(command, 1);
1596 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001597 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001598 vi_End_motion(command);
1599 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001600 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001601 vi_end_motion(command);
1602 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001603 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001604 vi_Back_motion(command);
1605 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001606 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001607 vi_back_motion(command);
1608 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001609 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001610 vi_cmdmode = 0;
1611 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001612 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001613 goto clear_to_eol;
1614
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001615 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001616 vi_cmdmode = 0;
1617 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001618 case 'd'|vbit: {
1619 int nc, sc;
1620 sc = cursor;
1621 prevc = ic;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001622 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001623 goto prepare_to_die;
1624 if (c == (prevc & 0xff)) {
1625 /* "cc", "dd" */
1626 input_backward(cursor);
1627 goto clear_to_eol;
1628 break;
1629 }
1630 switch (c) {
1631 case 'w':
1632 case 'W':
1633 case 'e':
1634 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001635 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001636 case 'w': /* "dw", "cw" */
1637 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001638 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001639 case 'W': /* 'dW', 'cW' */
1640 vi_Word_motion(command, vi_cmdmode);
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;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001646 case 'E': /* 'dE', 'cE' */
1647 vi_End_motion(command);
1648 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001649 break;
1650 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001651 nc = cursor;
1652 input_backward(cursor - sc);
1653 while (nc-- > cursor)
1654 input_delete(1);
1655 break;
1656 case 'b': /* "db", "cb" */
1657 case 'B': /* implemented as B */
1658 if (c == 'b')
1659 vi_back_motion(command);
1660 else
1661 vi_Back_motion(command);
1662 while (sc-- > cursor)
1663 input_delete(1);
1664 break;
1665 case ' ': /* "d ", "c " */
1666 input_delete(1);
1667 break;
1668 case '$': /* "d$", "c$" */
1669 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001670 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001671 input_delete(1);
1672 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001673 }
1674 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001675 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001676 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001677 input_forward();
1678 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001679 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001680 put();
1681 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001682 case 'r'|vbit:
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001683 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001684 goto prepare_to_die;
1685 if (c == 0)
1686 beep();
1687 else {
1688 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001689 bb_putchar(c);
1690 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001691 }
1692 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001693#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001694
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001695 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001696
Denis Vlasenko38f63192007-01-22 09:03:07 +00001697#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001698 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001699 /* ESC: insert mode --> command mode */
1700 vi_cmdmode = 1;
1701 input_backward(1);
1702 break;
1703 }
1704#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001705 /* escape sequence follows */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001706 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001707 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001708 /* different vt100 emulations */
1709 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001710 vi_case('['|vbit:)
1711 vi_case('O'|vbit:)
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001712 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001713 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001714 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001715 if (c >= '1' && c <= '9') {
1716 unsigned char dummy;
1717
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001718 if (safe_read(STDIN_FILENO, &dummy, 1) < 1)
Glenn L McGrath475820c2004-01-22 12:42:23 +00001719 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001720 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001721 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001722 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001723
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001724 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001725#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001726 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001727 input_tab(&lastWasTab);
1728 break;
1729#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001730#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001731 case 'A':
1732 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001733 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001734 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001735 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001736 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001737 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001738 break;
1739 case 'B':
1740 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001741 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001742 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001743 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001744 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001745 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001746 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001747 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001748 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001749 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001750#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001751 case 'C':
1752 /* Right Arrow -- Move forward one character */
1753 input_forward();
1754 break;
1755 case 'D':
1756 /* Left Arrow -- Move back one character */
1757 input_backward(1);
1758 break;
1759 case '3':
1760 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001761 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001762 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001763 case '1': // vt100? linux vt? or what?
1764 case '7': // vt100? linux vt? or what?
1765 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001766 input_backward(cursor);
1767 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001768 case '4': // vt100? linux vt? or what?
1769 case '8': // vt100? linux vt? or what?
1770 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001771 input_end();
1772 break;
1773 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001774 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001775 beep();
1776 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001777 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001778
Eric Andersenc470f442003-07-28 09:56:35 +00001779 default: /* If it's regular input, do the normal thing */
Paul Fox84bbac52008-01-11 16:50:08 +00001780
1781 /* Control-V -- force insert of next char */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001782 if (c == CTRL('V')) {
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001783 if (safe_read(STDIN_FILENO, &c, 1) < 1)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001784 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001785 if (c == 0) {
1786 beep();
1787 break;
1788 }
Paul Fox84bbac52008-01-11 16:50:08 +00001789 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001790
Denis Vlasenko38f63192007-01-22 09:03:07 +00001791#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001792 if (vi_cmdmode) /* Don't self-insert */
1793 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001794#endif
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00001795 if ((int)command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001796 break;
1797
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001798 command_len++;
1799 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001800 command[cursor] = c;
1801 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001802 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001803 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001804 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001805
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001806 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001807 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001808 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001809 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001810 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001811 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001812 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001813 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001814 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001815 }
Eric Andersenc470f442003-07-28 09:56:35 +00001816 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001817 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001818
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001819#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001820 if (c != '\t')
1821 lastWasTab = FALSE;
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001822#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001823 }
1824
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001825 if (command_len > 0)
1826 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001827
Eric Andersen27bb7902003-12-23 20:24:51 +00001828 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001829 command[command_len++] = '\n';
1830 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001831 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001832
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001833#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001834 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001835#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001836
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001837 /* restore initial_settings */
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001838 tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001839 /* restore SIGWINCH handler */
1840 signal(SIGWINCH, previous_SIGWINCH_handler);
1841 fflush(stdout);
Denis Vlasenko73cb1fd2007-11-10 01:35:47 +00001842
1843 DEINIT_S();
1844
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001845 return command_len;
1846}
1847
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001848line_input_t* FAST_FUNC new_line_input_t(int flags)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001849{
1850 line_input_t *n = xzalloc(sizeof(*n));
1851 n->flags = flags;
1852 return n;
1853}
1854
1855#else
1856
1857#undef read_line_input
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001858int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001859{
1860 fputs(prompt, stdout);
1861 fflush(stdout);
1862 fgets(command, maxsize, stdin);
1863 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001864}
1865
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001866#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001867
1868
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001869/*
1870 * Testing
1871 */
1872
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001873#ifdef TEST
1874
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001875#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001876
1877const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001878
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001879int main(int argc, char **argv)
1880{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001881 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001882 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001883#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001884 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1885 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1886 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001887#else
1888 "% ";
1889#endif
1890
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001891#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001892 setlocale(LC_ALL, "");
1893#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001894 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001895 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001896 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001897 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001898 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001899 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001900 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001901 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001902 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001903 return 0;
1904}
1905
Eric Andersenc470f442003-07-28 09:56:35 +00001906#endif /* TEST */