blob: f999b88e9c0e4f0dd50092967a030fac116efba5 [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 *
Erik Andersen13456d12000-03-16 08:09:57 +000016 *
17 */
18
19/*
20 Usage and Known bugs:
21 Terminal key codes are not extensive, and more will probably
22 need to be added. This version was created on Debian GNU/Linux 2.x.
23 Delete, Backspace, Home, End, and the arrow keys were tested
24 to work in an Xterm and console. Ctrl-A also works as Home.
Mark Whitley4e338752001-01-26 20:42:23 +000025 Ctrl-E also works as End.
Erik Andersen13456d12000-03-16 08:09:57 +000026
Eric Andersen5f2c79d2001-02-16 18:36:04 +000027 Small bugs (simple effect):
28 - not true viewing if terminal size (x*y symbols) less
29 size (prompt + editor`s line + 2 symbols)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000030 - not true viewing if length prompt less terminal width
Erik Andersen13456d12000-03-16 08:09:57 +000031 */
32
Mark Whitley4e338752001-01-26 20:42:23 +000033
Eric Andersencbe31da2001-02-20 06:14:08 +000034#include <stdio.h>
35#include <errno.h>
36#include <unistd.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/ioctl.h>
40#include <ctype.h>
41#include <signal.h>
42#include <limits.h>
43
Eric Andersen3570a342000-09-25 21:45:58 +000044#include "busybox.h"
Mark Whitley4e338752001-01-26 20:42:23 +000045
Glenn L McGrath67285962004-01-14 09:34:51 +000046#include "../shell/cmdedit.h"
47
Glenn L McGrath475820c2004-01-22 12:42:23 +000048
Eric Andersenbdfd0d72001-10-24 05:00:29 +000049#ifdef CONFIG_LOCALE_SUPPORT
Eric Andersene5dfced2001-04-09 22:48:12 +000050#define Isprint(c) isprint((c))
51#else
52#define Isprint(c) ( (c) >= ' ' && (c) != ((unsigned char)'\233') )
53#endif
54
Glenn L McGrath3b251852004-01-03 12:07:32 +000055#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000056
Eric Andersen27bb7902003-12-23 20:24:51 +000057/* pretect redefined for test */
58#undef CONFIG_FEATURE_COMMAND_EDITING
59#undef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
60#undef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
61#undef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
62#undef CONFIG_FEATURE_CLEAN_UP
63
Eric Andersenbdfd0d72001-10-24 05:00:29 +000064#define CONFIG_FEATURE_COMMAND_EDITING
65#define CONFIG_FEATURE_COMMAND_TAB_COMPLETION
66#define CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
67#define CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
68#define CONFIG_FEATURE_CLEAN_UP
Eric Andersen5f2c79d2001-02-16 18:36:04 +000069
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000070#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000071
Eric Andersenbdfd0d72001-10-24 05:00:29 +000072#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5165fbe2001-02-20 06:42:29 +000073#include <dirent.h>
74#include <sys/stat.h>
75#endif
76
Eric Andersenbdfd0d72001-10-24 05:00:29 +000077#ifdef CONFIG_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000078
Eric Andersenbdfd0d72001-10-24 05:00:29 +000079#if defined(CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION) || defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
80#define CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +000081#endif
82
Eric Andersenbdfd0d72001-10-24 05:00:29 +000083#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Glenn L McGrath475820c2004-01-22 12:42:23 +000084#include "pwd_.h"
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000085#endif /* advanced FEATURES */
Eric Andersenaf4ac772001-02-01 22:43:49 +000086
87
Eric Andersen5f2c79d2001-02-16 18:36:04 +000088/* Maximum length of the linked list for the command line history */
Robert Griebl350d26b2002-12-03 22:45:46 +000089#ifndef CONFIG_FEATURE_COMMAND_HISTORY
90#define MAX_HISTORY 15
91#else
92#define MAX_HISTORY CONFIG_FEATURE_COMMAND_HISTORY
93#endif
94
Glenn L McGrath062c74f2002-11-27 09:29:49 +000095#if MAX_HISTORY < 1
96#warning cmdedit: You set MAX_HISTORY < 1. The history algorithm switched off.
97#else
98static char *history[MAX_HISTORY+1]; /* history + current */
99/* saved history lines */
100static int n_history;
101/* current pointer to history line */
102static int cur_history;
103#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +0000104
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000105#include <termios.h>
106#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
107#define getTermSettings(fd,argp) tcgetattr(fd, argp);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000108
109/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000110static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000111
112
Mark Whitley4e338752001-01-26 20:42:23 +0000113static
Eric Andersenc470f442003-07-28 09:56:35 +0000114volatile int cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +0000115static
Eric Andersenc470f442003-07-28 09:56:35 +0000116volatile int handlers_sets = 0; /* Set next bites: */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000117
Mark Whitley4e338752001-01-26 20:42:23 +0000118enum {
Eric Andersenc470f442003-07-28 09:56:35 +0000119 SET_ATEXIT = 1, /* when atexit() has been called
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000120 and get euid,uid,gid to fast compare */
Eric Andersen7467c8d2001-07-12 20:26:32 +0000121 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
122 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000123};
Erik Andersen13456d12000-03-16 08:09:57 +0000124
Mark Whitley4e338752001-01-26 20:42:23 +0000125
Eric Andersenc470f442003-07-28 09:56:35 +0000126static int cmdedit_x; /* real x terminal position */
127static int cmdedit_y; /* pseudoreal y terminal position */
128static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000129
Eric Andersenc470f442003-07-28 09:56:35 +0000130static int cursor; /* required global for signal handler */
131static int len; /* --- "" - - "" - -"- --""-- --""--- */
132static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000133static
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000134#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000135 const
136#endif
Eric Andersenc470f442003-07-28 09:56:35 +0000137char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000138
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000139#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000140static char *user_buf = "";
141static char *home_pwd_buf = "";
142static int my_euid;
143#endif
144
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000145#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000146static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000147static int num_ok_lines = 1;
148#endif
149
150
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000151#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000152
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000153#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000154static int my_euid;
155#endif
156
157static int my_uid;
158static int my_gid;
159
Eric Andersenc470f442003-07-28 09:56:35 +0000160#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
161
Mark Whitley4e338752001-01-26 20:42:23 +0000162static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000163
Mark Whitley4e338752001-01-26 20:42:23 +0000164static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000165{
Eric Andersenc470f442003-07-28 09:56:35 +0000166 static sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000167
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000168 /* emulate || signal call */
169 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Eric Andersen8efe9672003-09-15 08:33:45 +0000170 int width = 0;
171 get_terminal_width_height(0, &width, NULL);
172 cmdedit_setwidth(width, nsig == SIGWINCH);
Mark Whitley4e338752001-01-26 20:42:23 +0000173 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000174 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000175
Eric Andersenc470f442003-07-28 09:56:35 +0000176 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000177 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersenc470f442003-07-28 09:56:35 +0000178 else if (nsig == SIGWINCH) /* signaled called handler */
179 signal(SIGWINCH, win_changed); /* set for next call */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000180 else /* nsig == 0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000181 /* set previous handler */
Eric Andersenc470f442003-07-28 09:56:35 +0000182 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000183}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000184
185static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000186{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000187 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000188/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen70060d22004-03-27 10:02:48 +0000189 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000190 handlers_sets &= ~SET_RESET_TERM;
191 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000192 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000193 /* reset SIGWINCH handler to previous (default) */
194 win_changed(0);
195 handlers_sets &= ~SET_WCHG_HANDLERS;
196 }
197 fflush(stdout);
Erik Andersen13456d12000-03-16 08:09:57 +0000198}
199
Mark Whitley4e338752001-01-26 20:42:23 +0000200
Mark Whitley4e338752001-01-26 20:42:23 +0000201/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000202static void cmdedit_set_out_char(int next_char)
203{
204
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000205 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000206
207 if (c == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000208 c = ' '; /* destroy end char? */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000209#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000210 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000211 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000212 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000213 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000214 c += '@';
215 if (c == 127)
216 c = '?';
217 printf("\033[7m%c\033[0m", c);
218 } else
219#endif
220 putchar(c);
221 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000222 /* terminal is scrolled down */
223 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000224 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000225
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000226 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000227 next_char = ' ';
228 /* destroy "(auto)margin" */
229 putchar(next_char);
230 putchar('\b');
231 }
232 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000233}
234
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000235/* Move to end line. Bonus: rewrite line from cursor */
236static void input_end(void)
237{
238 while (cursor < len)
239 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000240}
241
242/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000243static void goto_new_line(void)
244{
Mark Whitley4e338752001-01-26 20:42:23 +0000245 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000246 if (cmdedit_x)
247 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000248}
249
250
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000251static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000252{
Robert Grieblb2301592002-07-30 23:13:51 +0000253 if ( s )
254 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000255}
Eric Andersen81fe1232003-07-29 06:38:40 +0000256
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000257static inline void beep(void)
258{
259 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000260}
261
Eric Andersenaff114c2004-04-14 17:51:38 +0000262/* Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000263/* special for slow terminal */
264static void input_backward(int num)
265{
266 if (num > cursor)
267 num = cursor;
Eric Andersenc470f442003-07-28 09:56:35 +0000268 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000269
Eric Andersenc470f442003-07-28 09:56:35 +0000270 if (cmdedit_x >= num) { /* no to up line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000271 cmdedit_x -= num;
272 if (num < 4)
273 while (num-- > 0)
274 putchar('\b');
275
276 else
277 printf("\033[%dD", num);
278 } else {
279 int count_y;
280
281 if (cmdedit_x) {
Eric Andersenc470f442003-07-28 09:56:35 +0000282 putchar('\r'); /* back to first terminal pos. */
283 num -= cmdedit_x; /* set previous backward */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000284 }
285 count_y = 1 + num / cmdedit_termw;
286 printf("\033[%dA", count_y);
287 cmdedit_y -= count_y;
288 /* require forward after uping */
289 cmdedit_x = cmdedit_termw * count_y - num;
Eric Andersenc470f442003-07-28 09:56:35 +0000290 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000291 }
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 Andersenc470f442003-07-28 09:56:35 +0000297 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000298 cursor = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000299 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000300}
301
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000302#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000303static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000304{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000305 cmdedit_prompt = prmt_ptr;
306 cmdedit_prmt_len = strlen(prmt_ptr);
307 put_prompt();
308}
309#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000310static void parse_prompt(const char *prmt_ptr)
311{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000312 int prmt_len = 0;
"Vladimir N. Oleynik"f0874802005-09-05 15:46:26 +0000313 size_t cur_prmt_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000314 char flg_not_length = '[';
315 char *prmt_mem_ptr = xcalloc(1, 1);
316 char *pwd_buf = xgetcwd(0);
317 char buf2[PATH_MAX + 1];
318 char buf[2];
319 char c;
320 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000321
Eric Andersen5f265b72001-05-11 16:58:46 +0000322 if (!pwd_buf) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000323 pwd_buf=(char *)bb_msg_unknown;
Eric Andersen5f265b72001-05-11 16:58:46 +0000324 }
325
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000326 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000327 pbuf = buf;
328 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000329 c = *prmt_ptr++;
330 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000331 const char *cp = prmt_ptr;
332 int l;
Eric Andersenc470f442003-07-28 09:56:35 +0000333
Manuel Novoa III cad53642003-03-19 09:13:01 +0000334 c = bb_process_escape_sequence(&prmt_ptr);
Eric Andersene5dfced2001-04-09 22:48:12 +0000335 if(prmt_ptr==cp) {
336 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000337 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000338 c = *prmt_ptr++;
339 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000340#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000341 case 'u':
342 pbuf = user_buf;
343 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000344#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000345 case 'h':
346 pbuf = hostname_buf;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000347 if (pbuf == 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000348 pbuf = xcalloc(256, 1);
349 if (gethostname(pbuf, 255) < 0) {
350 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000351 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000352 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000353
354 if (s)
355 *s = 0;
356 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000357 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000358 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000359 break;
360 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000361 c = my_euid == 0 ? '#' : '$';
362 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000363#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000364 case 'w':
365 pbuf = pwd_buf;
366 l = strlen(home_pwd_buf);
367 if (home_pwd_buf[0] != 0 &&
368 strncmp(home_pwd_buf, pbuf, l) == 0 &&
369 (pbuf[l]=='/' || pbuf[l]=='\0') &&
370 strlen(pwd_buf+l)<PATH_MAX) {
371 pbuf = buf2;
372 *pbuf = '~';
373 strcpy(pbuf+1, pwd_buf+l);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000374 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000375 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000376#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000377 case 'W':
378 pbuf = pwd_buf;
379 cp = strrchr(pbuf,'/');
380 if ( (cp != NULL) && (cp != pbuf) )
381 pbuf += (cp-pbuf)+1;
382 break;
383 case '!':
384 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
385 break;
386 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000387 c = '\033';
388 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000389 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000390 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000391 int h;
392 buf2[l++] = *prmt_ptr;
393 buf2[l] = 0;
394 h = strtol(buf2, &pbuf, 16);
395 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000396 l--;
397 break;
398 }
399 prmt_ptr++;
400 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000401 buf2[l] = 0;
402 c = (char)strtol(buf2, 0, 16);
403 if(c==0)
404 c = '?';
405 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000406 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000407 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000408 if (c == flg_not_length) {
409 flg_not_length = flg_not_length == '[' ? ']' : '[';
410 continue;
411 }
412 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000413 }
Eric Andersenc470f442003-07-28 09:56:35 +0000414 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000415 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000416 if(pbuf == buf)
417 *pbuf = c;
"Vladimir N. Oleynik"f0874802005-09-05 15:46:26 +0000418 cur_prmt_len = strlen(pbuf);
419 prmt_len += cur_prmt_len;
420 if (flg_not_length != ']')
421 cmdedit_prmt_len += cur_prmt_len;
Eric Andersene5dfced2001-04-09 22:48:12 +0000422 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000423 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000424 if(pwd_buf!=(char *)bb_msg_unknown)
Eric Andersen8f697842001-07-02 15:36:57 +0000425 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000426 cmdedit_prompt = prmt_mem_ptr;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000427 put_prompt();
428}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000429#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430
431
Eric Andersenaff114c2004-04-14 17:51:38 +0000432/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000433static void redraw(int y, int back_cursor)
434{
Eric Andersenc470f442003-07-28 09:56:35 +0000435 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000436 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000437 putchar('\r');
438 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000439 input_end(); /* rewrite */
440 printf("\033[J"); /* destroy tail after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000441 input_backward(back_cursor);
442}
443
Paul Fox3f11b1b2005-08-04 19:04:46 +0000444#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
445static char delbuf[BUFSIZ]; /* a place to store deleted characters */
446static char *delp = delbuf;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000447static int newdelflag; /* whether delbuf should be reused yet */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000448#endif
449
450/* Delete the char in front of the cursor, optionally saving it
451 * for later putback */
452static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000453{
Mark Whitley4e338752001-01-26 20:42:23 +0000454 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000455
Mark Whitley4e338752001-01-26 20:42:23 +0000456 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000457 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000458
Paul Fox3f11b1b2005-08-04 19:04:46 +0000459#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
460 if (save) {
461 if (newdelflag) {
462 delp = delbuf;
463 newdelflag = 0;
464 }
465 if (delp - delbuf < BUFSIZ)
466 *delp++ = command_ps[j];
467 }
468#endif
469
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000470 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000471 len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000472 input_end(); /* rewrite new line */
Eric Andersenc470f442003-07-28 09:56:35 +0000473 cmdedit_set_out_char(0); /* destroy end char */
474 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000475}
476
Paul Fox3f11b1b2005-08-04 19:04:46 +0000477#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
478static void put(void)
479{
480 int ocursor, j = delp - delbuf;
481 if (j == 0)
482 return;
483 ocursor = cursor;
484 /* open hole and then fill it */
485 memmove(command_ps + cursor + j, command_ps + cursor, len - cursor + 1);
486 strncpy(command_ps + cursor, delbuf, j);
487 len += j;
488 input_end(); /* rewrite new line */
489 input_backward(cursor-ocursor-j+1); /* at end of new text */
490}
491#endif
492
Mark Whitley4e338752001-01-26 20:42:23 +0000493/* Delete the char in back of the cursor */
494static void input_backspace(void)
495{
496 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000497 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000498 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000499 }
500}
501
502
Eric Andersenaff114c2004-04-14 17:51:38 +0000503/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000504static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000505{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000506 if (cursor < len)
507 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000508}
509
Mark Whitley4e338752001-01-26 20:42:23 +0000510static void cmdedit_setwidth(int w, int redraw_flg)
511{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000512 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000513 if (w <= cmdedit_termw) {
514 cmdedit_termw = cmdedit_termw % w;
515 }
Mark Whitley4e338752001-01-26 20:42:23 +0000516 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000517 cmdedit_termw = w;
518
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000519 if (redraw_flg) {
520 /* new y for current cursor */
521 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000522
523 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000524 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
525 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000526 }
Eric Andersenc470f442003-07-28 09:56:35 +0000527 }
Mark Whitley4e338752001-01-26 20:42:23 +0000528}
529
Eric Andersen7467c8d2001-07-12 20:26:32 +0000530static void cmdedit_init(void)
Mark Whitley4e338752001-01-26 20:42:23 +0000531{
Eric Andersen61173a52001-03-19 17:48:55 +0000532 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000533 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
534 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000535 win_changed(-SIGWINCH);
536 handlers_sets |= SET_WCHG_HANDLERS;
537 }
538
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000539 if ((handlers_sets & SET_ATEXIT) == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000540#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000541 struct passwd *entry;
542
543 my_euid = geteuid();
544 entry = getpwuid(my_euid);
545 if (entry) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000546 user_buf = bb_xstrdup(entry->pw_name);
547 home_pwd_buf = bb_xstrdup(entry->pw_dir);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000548 }
549#endif
550
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000551#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000552
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000553#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000554 my_euid = geteuid();
555#endif
556 my_uid = getuid();
557 my_gid = getgid();
Eric Andersenc470f442003-07-28 09:56:35 +0000558#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000559 handlers_sets |= SET_ATEXIT;
Eric Andersenc470f442003-07-28 09:56:35 +0000560 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000561 }
Mark Whitley4e338752001-01-26 20:42:23 +0000562}
Erik Andersenf0657d32000-04-12 17:49:52 +0000563
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000564#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000565
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000566static char **matches;
567static int num_matches;
568static char *add_char_to_match;
569
570static void add_match(char *matched, int add_char)
571{
572 int nm = num_matches;
573 int nm1 = nm + 1;
574
575 matches = xrealloc(matches, nm1 * sizeof(char *));
576 add_char_to_match = xrealloc(add_char_to_match, nm1);
577 matches[nm] = matched;
578 add_char_to_match[nm] = (char)add_char;
579 num_matches++;
580}
581
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000582static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000583{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000584 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
585 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
586 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
587 (st->st_mode & S_IXOTH)) return TRUE;
588 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000589}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000590
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000591#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000592
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000593static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000594{
595 struct passwd *entry;
596 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000597
Eric Andersenc470f442003-07-28 09:56:35 +0000598 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000599 userlen = strlen(ud);
600
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000601 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000602 char *sav_ud = ud - 1;
603 char *home = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000604 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000605
Eric Andersenc470f442003-07-28 09:56:35 +0000606 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000607 home = home_pwd_buf;
608 } else {
609 /* "~user/..." */
610 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000611 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000612 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000613 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000614 ud = temp;
615 if (entry)
616 home = entry->pw_dir;
617 }
618 if (home) {
619 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000620 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000621
622 /* /home/user/... */
623 sprintf(temp2, "%s%s", home, ud);
624 strcpy(sav_ud, temp2);
625 }
626 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000627 } else {
628 /* "~[^/]*" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000629 setpwent();
630
631 while ((entry = getpwent()) != NULL) {
632 /* Null usernames should result in all users as possible completions. */
633 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000634 add_match(bb_xasprintf("~%s", entry->pw_name), '/');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000635 }
636 }
637
638 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000639 }
640}
Eric Andersenc470f442003-07-28 09:56:35 +0000641#endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000642
643enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000644 FIND_EXE_ONLY = 0,
645 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000646 FIND_FILE_ONLY = 2,
647};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000648
Glenn L McGrath67285962004-01-14 09:34:51 +0000649#ifdef CONFIG_ASH
650const char *cmdedit_path_lookup;
651#else
652#define cmdedit_path_lookup getenv("PATH")
653#endif
654
Mark Whitley4e338752001-01-26 20:42:23 +0000655static int path_parse(char ***p, int flags)
656{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000657 int npth;
Glenn L McGrath67285962004-01-14 09:34:51 +0000658 const char *tmp;
659 const char *pth;
Mark Whitley4e338752001-01-26 20:42:23 +0000660
Mark Whitley4e338752001-01-26 20:42:23 +0000661 /* if not setenv PATH variable, to search cur dir "." */
Glenn L McGrath67285962004-01-14 09:34:51 +0000662 if (flags != FIND_EXE_ONLY || (pth = cmdedit_path_lookup) == 0 ||
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000663 /* PATH=<empty> or PATH=:<empty> */
664 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000665 return 1;
666 }
667
668 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000669 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000670
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000671 for (;;) {
Eric Andersenc470f442003-07-28 09:56:35 +0000672 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000673 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000674 if (tmp) {
675 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000676 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000677 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000678 break;
679 }
680
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000681 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000682
683 tmp = pth;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000684 (*p)[0] = bb_xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000685 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000686
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000687 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000688 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000689 if (tmp) {
Eric Andersenc470f442003-07-28 09:56:35 +0000690 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000691 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000692 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000693 } else
694 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000695 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000696 }
697
698 return npth;
699}
700
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000701static char *add_quote_for_spec_chars(char *found, int add)
Erik Andersen6273f652000-03-17 01:12:41 +0000702{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000703 int l = 0;
704 char *s = xmalloc((strlen(found) + 1) * 2);
705
706 while (*found) {
707 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
708 s[l++] = '\\';
709 s[l++] = *found++;
710 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000711 if(add)
712 s[l++] = (char)add;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000713 s[l] = 0;
714 return s;
715}
716
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000717static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000718{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000719 DIR *dir;
720 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000721 char dirbuf[BUFSIZ];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000722 struct stat st;
723 char *path1[1];
724 char **paths = path1;
725 int npaths;
726 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000727 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000728 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000729
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000730 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000731
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000732 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000733 /* no dir, if flags==EXE_ONLY - get paths, else "." */
734 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000735 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000736 } else {
737 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000738 /* save for change */
739 strcpy(dirbuf, command);
740 /* set dir only */
741 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000742#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000743 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000744 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000745#endif
746 /* "strip" dirname in command */
747 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000748
Mark Whitley4e338752001-01-26 20:42:23 +0000749 paths[0] = dirbuf;
Eric Andersenc470f442003-07-28 09:56:35 +0000750 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000751 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000752
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000753 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000754
Mark Whitley4e338752001-01-26 20:42:23 +0000755 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000756 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000757 continue;
758
759 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000760 char *str_found = next->d_name;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000761 int add_chr = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000762
Mark Whitley4e338752001-01-26 20:42:23 +0000763 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000764 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000765 continue;
766 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000767 if (*str_found == '.' && *pfind == 0) {
768 if (*paths[i] == '/' && paths[i][1] == 0
Eric Andersenc470f442003-07-28 09:56:35 +0000769 && str_found[1] == 0) str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000770 else
Mark Whitley4e338752001-01-26 20:42:23 +0000771 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000772 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000773 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000774 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000775 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000776 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000777 /* find with dirs ? */
778 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000779 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000780 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000781 /* name is directory */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000782 char *e = found + strlen(found) - 1;
783
784 add_chr = '/';
785 if(*e == '/')
786 *e = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +0000787 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000788 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000789 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000790 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000791 if (type == FIND_FILE_ONLY ||
Matt Kraai1f0c4362001-12-20 23:13:26 +0000792 (type == FIND_EXE_ONLY && is_execute(&st)))
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000793 add_chr = ' ';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000794 }
Mark Whitley4e338752001-01-26 20:42:23 +0000795 /* Add it to the list */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000796 add_match(found, add_chr);
797 continue;
Eric Andersene5dfced2001-04-09 22:48:12 +0000798cont:
799 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000800 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000801 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000802 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000803 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000804 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000805 free(paths);
806 }
Erik Andersen6273f652000-03-17 01:12:41 +0000807}
Erik Andersenf0657d32000-04-12 17:49:52 +0000808
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000809
810#define QUOT (UCHAR_MAX+1)
811
812#define collapse_pos(is, in) { \
Paul Fox574fee42005-07-19 20:41:06 +0000813 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
814 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000815
816static int find_match(char *matchBuf, int *len_with_quotes)
817{
818 int i, j;
819 int command_mode;
820 int c, c2;
821 int int_buf[BUFSIZ + 1];
822 int pos_buf[BUFSIZ + 1];
823
824 /* set to integer dimension characters and own positions */
825 for (i = 0;; i++) {
826 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
827 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000828 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000829 break;
830 } else
831 pos_buf[i] = i;
832 }
833
834 /* mask \+symbol and convert '\t' to ' ' */
835 for (i = j = 0; matchBuf[i]; i++, j++)
836 if (matchBuf[i] == '\\') {
837 collapse_pos(j, j + 1);
838 int_buf[j] |= QUOT;
839 i++;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000840#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000841 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000842 int_buf[j] = ' ' | QUOT;
843#endif
844 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000845#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000846 else if (matchBuf[i] == '\t')
847 int_buf[j] = ' ';
848#endif
849
850 /* mask "symbols" or 'symbols' */
851 c2 = 0;
852 for (i = 0; int_buf[i]; i++) {
853 c = int_buf[i];
854 if (c == '\'' || c == '"') {
855 if (c2 == 0)
856 c2 = c;
857 else {
858 if (c == c2)
859 c2 = 0;
860 else
861 int_buf[i] |= QUOT;
862 }
863 } else if (c2 != 0 && c != '$')
864 int_buf[i] |= QUOT;
865 }
866
867 /* skip commands with arguments if line have commands delimiters */
868 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
869 for (i = 0; int_buf[i]; i++) {
870 c = int_buf[i];
871 c2 = int_buf[i + 1];
872 j = i ? int_buf[i - 1] : -1;
873 command_mode = 0;
874 if (c == ';' || c == '&' || c == '|') {
875 command_mode = 1 + (c == c2);
876 if (c == '&') {
877 if (j == '>' || j == '<')
878 command_mode = 0;
879 } else if (c == '|' && j == '>')
880 command_mode = 0;
881 }
882 if (command_mode) {
883 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000884 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000885 }
886 }
887 /* collapse `command...` */
888 for (i = 0; int_buf[i]; i++)
889 if (int_buf[i] == '`') {
890 for (j = i + 1; int_buf[j]; j++)
891 if (int_buf[j] == '`') {
892 collapse_pos(i, j + 1);
893 j = 0;
894 break;
895 }
896 if (j) {
897 /* not found close ` - command mode, collapse all previous */
898 collapse_pos(0, i + 1);
899 break;
900 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000901 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000902 }
903
904 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000905 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000906 c2 = 0;
907 for (i = 0; int_buf[i]; i++)
908 if (int_buf[i] == '(' || int_buf[i] == '{') {
909 if (int_buf[i] == '(')
910 c++;
911 else
912 c2++;
913 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000914 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000915 }
916 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
917 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
918 if (int_buf[i] == ')')
919 c--;
920 else
921 c2--;
922 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000923 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000924 }
925
926 /* skip first not quote space */
927 for (i = 0; int_buf[i]; i++)
928 if (int_buf[i] != ' ')
929 break;
930 if (i)
931 collapse_pos(0, i);
932
933 /* set find mode for completion */
934 command_mode = FIND_EXE_ONLY;
935 for (i = 0; int_buf[i]; i++)
936 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
937 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000938 && matchBuf[pos_buf[0]]=='c'
939 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000940 command_mode = FIND_DIR_ONLY;
941 else {
942 command_mode = FIND_FILE_ONLY;
943 break;
944 }
945 }
946 /* "strlen" */
947 for (i = 0; int_buf[i]; i++);
948 /* find last word */
949 for (--i; i >= 0; i--) {
950 c = int_buf[i];
951 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
952 collapse_pos(0, i + 1);
953 break;
954 }
955 }
956 /* skip first not quoted '\'' or '"' */
957 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
958 /* collapse quote or unquote // or /~ */
Eric Andersenc470f442003-07-28 09:56:35 +0000959 while ((int_buf[i] & ~QUOT) == '/' &&
Mark Whitley7e5291f2001-03-08 19:31:12 +0000960 ((int_buf[i + 1] & ~QUOT) == '/'
961 || (int_buf[i + 1] & ~QUOT) == '~')) {
962 i++;
963 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000964
965 /* set only match and destroy quotes */
966 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000967 for (c = 0; pos_buf[i] >= 0; i++) {
968 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000969 j = pos_buf[i] + 1;
970 }
Eric Andersen4f990532001-05-31 17:15:57 +0000971 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000972 /* old lenght matchBuf with quotes symbols */
973 *len_with_quotes = j ? j - pos_buf[0] : 0;
974
975 return command_mode;
976}
977
Glenn L McGrath4d001292003-01-06 01:11:50 +0000978/*
979 display by column original ideas from ls applet,
980 very optimize by my :)
981*/
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000982static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000983{
984 int ncols, row;
985 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000986 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000987 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000988 char str_add_chr[2];
989 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000990
991 /* find the longest file name- use that as the column width */
992 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000993 l = strlen(matches[row]);
994 if(add_char_to_match[row])
995 l++;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000996 if (column_width < l)
997 column_width = l;
998 }
999 column_width += 2; /* min space for columns */
1000 ncols = cmdedit_termw / column_width;
1001
1002 if (ncols > 1) {
1003 nrows /= ncols;
1004 if(nfiles % ncols)
1005 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001006 } else {
1007 ncols = 1;
1008 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001009 str_add_chr[1] = 0;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001010 for (row = 0; row < nrows; row++) {
1011 int n = row;
1012 int nc;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001013 int acol;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001014
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001015 for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
1016 str_add_chr[0] = add_char_to_match[n];
1017 acol = str_add_chr[0] ? column_width - 1 : column_width;
1018 printf("%s%s", matches[n], str_add_chr);
1019 l = strlen(matches[n]);
1020 while(l < acol) {
1021 putchar(' ');
1022 l++;
1023 }
1024 }
1025 str_add_chr[0] = add_char_to_match[n];
1026 printf("%s%s\n", matches[n], str_add_chr);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001027 }
1028}
1029
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001030
1031static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001032{
1033 /* Do TAB completion */
Eric Andersenc470f442003-07-28 09:56:35 +00001034 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001035 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001036 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001037 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001038 free(matches);
1039 matches = (char **) NULL;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001040 free(add_char_to_match);
1041 add_char_to_match = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +00001042 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001043 return;
1044 }
Matt Kraai1f0c4362001-12-20 23:13:26 +00001045 if (! *lastWasTab) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001046
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001047 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001048 int len_found;
1049 char matchBuf[BUFSIZ];
1050 int find_type;
1051 int recalc_pos;
1052
Eric Andersenc470f442003-07-28 09:56:35 +00001053 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001054
1055 /* Make a local copy of the string -- up
1056 * to the position of the cursor */
1057 tmp = strncpy(matchBuf, command_ps, cursor);
1058 tmp[cursor] = 0;
1059
1060 find_type = find_match(matchBuf, &recalc_pos);
1061
1062 /* Free up any memory already allocated */
1063 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001064
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001065#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001066 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001067 * then try completing this word as a username. */
1068
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001069 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001070 username_tab_completion(matchBuf, NULL);
1071 if (!matches)
Mark Whitley4e338752001-01-26 20:42:23 +00001072#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001073 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001074 * in the current working directory that matches. */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001075 exe_n_cwd_tab_completion(matchBuf, find_type);
1076 /* Remove duplicate found and sort */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001077 if(matches) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001078 int i, j, n, srt;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001079 /* bubble */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001080 n = num_matches;
1081 for(i=0; i<(n-1); i++)
1082 for(j=i+1; j<n; j++)
1083 if(matches[i]!=NULL && matches[j]!=NULL) {
1084 srt = strcmp(matches[i], matches[j]);
1085 if(srt == 0) {
1086 free(matches[j]);
1087 matches[j]=0;
1088 } else if(srt > 0) {
1089 tmp1 = matches[i];
1090 matches[i] = matches[j];
1091 matches[j] = tmp1;
1092 srt = add_char_to_match[i];
1093 add_char_to_match[i] = add_char_to_match[j];
1094 add_char_to_match[j] = srt;
1095 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001096 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001097 j = n;
1098 n = 0;
1099 for(i=0; i<j; i++)
1100 if(matches[i]) {
1101 matches[n]=matches[i];
1102 add_char_to_match[n]=add_char_to_match[i];
1103 n++;
1104 }
1105 num_matches = n;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001106 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001107 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001108 if (!matches || num_matches > 1) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001109
Mark Whitley4e338752001-01-26 20:42:23 +00001110 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001111 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +00001112 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001113 /* find minimal match */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001114 tmp1 = bb_xstrdup(matches[0]);
1115 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001116 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001117 if (matches[len_found][(tmp - tmp1)] != *tmp) {
1118 *tmp = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001119 break;
1120 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001121 if (*tmp1 == 0) { /* have unique */
1122 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001123 return;
1124 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001125 tmp = add_quote_for_spec_chars(tmp1, 0);
1126 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +00001127 } else { /* one match */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001128 tmp = add_quote_for_spec_chars(matches[0], add_char_to_match[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001129 /* for next completion current found */
1130 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001131 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001132 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001133 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001134 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001135
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001136 /* before word for match */
1137 command_ps[cursor - recalc_pos] = 0;
1138 /* save tail line */
1139 strcpy(matchBuf, command_ps + cursor);
1140 /* add match */
1141 strcat(command_ps, tmp);
1142 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001143 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001144 /* back to begin word for match */
1145 input_backward(recalc_pos);
1146 /* new pos */
1147 recalc_pos = cursor + len_found;
1148 /* new len */
1149 len = strlen(command_ps);
1150 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001151 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001152 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001153 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001154 } else {
1155 /* Ok -- the last char was a TAB. Since they
1156 * just hit TAB again, print a list of all the
1157 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001158 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001159 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001160
1161 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001162 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001163 showfiles();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001164 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001165 }
1166 }
1167}
Eric Andersenc470f442003-07-28 09:56:35 +00001168#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001169
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001170#if MAX_HISTORY >= 1
1171static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001172{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001173 if(command_ps[0] != 0 || history[cur_history] == 0) {
1174 free(history[cur_history]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001175 history[cur_history] = bb_xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001176 }
1177 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +00001178}
1179
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001180static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001181{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001182 int ch = cur_history;
1183
1184 if (ch < n_history) {
1185 get_previous_history(); /* save the current history line */
1186 return (cur_history = ch+1);
1187 } else {
1188 beep();
1189 return 0;
1190 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001191}
Robert Griebl350d26b2002-12-03 22:45:46 +00001192
Robert Griebl350d26b2002-12-03 22:45:46 +00001193#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001194extern void load_history ( const char *fromfile )
1195{
Robert Griebl350d26b2002-12-03 22:45:46 +00001196 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001197 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001198
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001199 /* cleanup old */
1200
1201 for(hi = n_history; hi > 0; ) {
1202 hi--;
1203 free ( history [hi] );
Robert Griebl350d26b2002-12-03 22:45:46 +00001204 }
1205
1206 if (( fp = fopen ( fromfile, "r" ))) {
Eric Andersenc470f442003-07-28 09:56:35 +00001207
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001208 for ( hi = 0; hi < MAX_HISTORY; ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001209 char * hl = bb_get_chomped_line_from_file(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001210 int l;
1211
1212 if(!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001213 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001214 l = strlen(hl);
1215 if(l >= BUFSIZ)
1216 hl[BUFSIZ-1] = 0;
1217 if(l == 0 || hl[0] == ' ') {
1218 free(hl);
1219 continue;
1220 }
1221 history [hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001222 }
1223 fclose ( fp );
1224 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001225 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001226}
1227
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001228extern void save_history ( const char *tofile )
Robert Griebl350d26b2002-12-03 22:45:46 +00001229{
Robert Griebl350d26b2002-12-03 22:45:46 +00001230 FILE *fp = fopen ( tofile, "w" );
Eric Andersenc470f442003-07-28 09:56:35 +00001231
Robert Griebl350d26b2002-12-03 22:45:46 +00001232 if ( fp ) {
1233 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001234
Robert Griebl350d26b2002-12-03 22:45:46 +00001235 for ( i = 0; i < n_history; i++ ) {
Eric Andersen27bb7902003-12-23 20:24:51 +00001236 fprintf(fp, "%s\n", history [i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001237 }
1238 fclose ( fp );
1239 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001240}
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001241#endif
Robert Griebl350d26b2002-12-03 22:45:46 +00001242
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001243#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001244
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001245enum {
1246 ESC = 27,
1247 DEL = 127,
1248};
1249
1250
Erik Andersen6273f652000-03-17 01:12:41 +00001251/*
1252 * This function is used to grab a character buffer
1253 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001254 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001255 * a mini readline).
1256 *
1257 * The following standard commands are not implemented:
1258 * ESC-b -- Move back one word
1259 * ESC-f -- Move forward one word
1260 * ESC-d -- Delete back one word
1261 * ESC-h -- Delete forward one word
1262 * CTL-t -- Transpose two characters
1263 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001264 * Minimalist vi-style command line editing available if configured.
1265 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001266 *
Erik Andersen6273f652000-03-17 01:12:41 +00001267 */
Eric Andersenc470f442003-07-28 09:56:35 +00001268
Paul Fox3f11b1b2005-08-04 19:04:46 +00001269#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
1270static int vi_mode;
1271
1272void setvimode ( int viflag )
1273{
1274 vi_mode = viflag;
1275}
1276
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001277static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001278vi_Word_motion(char *command, int eat)
1279{
1280 while (cursor < len && !isspace(command[cursor]))
1281 input_forward();
1282 if (eat) while (cursor < len && isspace(command[cursor]))
1283 input_forward();
1284}
1285
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001286static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001287vi_word_motion(char *command, int eat)
1288{
1289 if (isalnum(command[cursor]) || command[cursor] == '_') {
1290 while (cursor < len &&
1291 (isalnum(command[cursor+1]) ||
1292 command[cursor+1] == '_'))
1293 input_forward();
1294 } else if (ispunct(command[cursor])) {
1295 while (cursor < len &&
1296 (ispunct(command[cursor+1])))
1297 input_forward();
1298 }
1299
1300 if (cursor < len)
1301 input_forward();
1302
1303 if (eat && cursor < len && isspace(command[cursor]))
1304 while (cursor < len && isspace(command[cursor]))
1305 input_forward();
1306}
1307
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001308static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001309vi_End_motion(char *command)
1310{
1311 input_forward();
1312 while (cursor < len && isspace(command[cursor]))
1313 input_forward();
1314 while (cursor < len-1 && !isspace(command[cursor+1]))
1315 input_forward();
1316}
1317
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001318static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001319vi_end_motion(char *command)
1320{
1321 if (cursor >= len-1)
1322 return;
1323 input_forward();
1324 while (cursor < len-1 && isspace(command[cursor]))
1325 input_forward();
1326 if (cursor >= len-1)
1327 return;
1328 if (isalnum(command[cursor]) || command[cursor] == '_') {
1329 while (cursor < len-1 &&
1330 (isalnum(command[cursor+1]) ||
1331 command[cursor+1] == '_'))
1332 input_forward();
1333 } else if (ispunct(command[cursor])) {
1334 while (cursor < len-1 &&
1335 (ispunct(command[cursor+1])))
1336 input_forward();
1337 }
1338}
1339
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001340static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001341vi_Back_motion(char *command)
1342{
1343 while (cursor > 0 && isspace(command[cursor-1]))
1344 input_backward(1);
1345 while (cursor > 0 && !isspace(command[cursor-1]))
1346 input_backward(1);
1347}
1348
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001349static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001350vi_back_motion(char *command)
1351{
1352 if (cursor <= 0)
1353 return;
1354 input_backward(1);
1355 while (cursor > 0 && isspace(command[cursor]))
1356 input_backward(1);
1357 if (cursor <= 0)
1358 return;
1359 if (isalnum(command[cursor]) || command[cursor] == '_') {
1360 while (cursor > 0 &&
1361 (isalnum(command[cursor-1]) ||
1362 command[cursor-1] == '_'))
1363 input_backward(1);
1364 } else if (ispunct(command[cursor])) {
1365 while (cursor > 0 &&
1366 (ispunct(command[cursor-1])))
1367 input_backward(1);
1368 }
1369}
1370#endif
1371
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001372/*
Paul Fox3f11b1b2005-08-04 19:04:46 +00001373 * the normal emacs mode and vi's insert mode are the same.
1374 * commands entered when in vi command mode ("escape mode") get
1375 * an extra bit added to distinguish them. this lets them share
1376 * much of the code in the big switch and while loop. i
1377 * experimented with an ugly macro to make the case labels for
1378 * these cases go away entirely when vi mode isn't configured, in
1379 * hopes of letting the jump tables get smaller:
1380 * #define vcase(caselabel) caselabel
1381 * and then
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001382 * case CNTRL('A'):
1383 * case vcase(VICMD('0'):)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001384 * but it didn't seem to make any difference in code size,
1385 * and the macro-ized code was too ugly.
1386 */
1387
1388#define VI_cmdbit 0x100
1389#define VICMD(somecmd) ((somecmd)|VI_cmdbit)
1390
1391/* convert uppercase ascii to equivalent control char, for readability */
1392#define CNTRL(uc_char) ((uc_char) - 0x40)
1393
Eric Andersen044228d2001-07-17 01:12:36 +00001394
1395int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001396{
1397
Erik Andersenc7c634b2000-03-19 05:28:55 +00001398 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001399 int lastWasTab = FALSE;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001400 unsigned char c;
1401#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
1402 unsigned int ic, prevc;
1403 int vi_cmdmode = 0;
1404#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001405 /* prepare before init handlers */
Eric Andersenc470f442003-07-28 09:56:35 +00001406 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001407 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001408 command_ps = command;
1409
Eric Andersen34506362001-08-02 05:02:46 +00001410 getTermSettings(0, (void *) &initial_settings);
1411 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1412 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1413 /* Turn off echoing and CTRL-C, so we can trap it */
1414 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Eric Andersen34506362001-08-02 05:02:46 +00001415 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1416 new_settings.c_cc[VMIN] = 1;
1417 new_settings.c_cc[VTIME] = 0;
1418 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001419# ifndef _POSIX_VDISABLE
1420# define _POSIX_VDISABLE '\0'
1421# endif
Eric Andersenc470f442003-07-28 09:56:35 +00001422 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001423 command[0] = 0;
1424
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001425 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001426 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001427
Eric Andersen6faae7d2001-02-16 20:09:17 +00001428 /* Now initialize things */
1429 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001430 /* Print out the command prompt */
1431 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001432
Erik Andersenc7c634b2000-03-19 05:28:55 +00001433 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001434
Eric Andersenc470f442003-07-28 09:56:35 +00001435 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001436
Eric Andersen7467c8d2001-07-12 20:26:32 +00001437 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001438 /* if we can't read input then exit */
1439 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001440
Paul Fox3f11b1b2005-08-04 19:04:46 +00001441#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
1442 newdelflag = 1;
1443 ic = c;
1444 if (vi_cmdmode)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001445 ic |= VI_cmdbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001446 switch (ic)
1447#else
1448 switch (c)
1449#endif
1450 {
Erik Andersenf0657d32000-04-12 17:49:52 +00001451 case '\n':
1452 case '\r':
Paul Fox3f11b1b2005-08-04 19:04:46 +00001453 case VICMD('\n'):
1454 case VICMD('\r'):
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;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001459 case CNTRL('A'):
1460 case VICMD('0'):
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;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001464 case CNTRL('B'):
1465 case VICMD('h'):
1466 case VICMD('\b'):
1467 case VICMD(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;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001471 case CNTRL('C'):
1472 case VICMD(CNTRL('C')):
Eric Andersen86349772000-12-18 20:25:50 +00001473 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001474 goto_new_line();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001475#ifndef CONFIG_ASH
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001476 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001477 len = 0;
1478 lastWasTab = FALSE;
1479 put_prompt();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001480#else
1481 len = 0;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001482 break_out = -1; /* to control traps */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001483#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001484 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001485 case CNTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001486 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001487 * if the len=0 and no chars to delete */
1488 if (len == 0) {
Eric Andersencb01bb12004-08-19 18:22:13 +00001489 errno = 0;
Eric Andersened424db2001-04-23 15:28:28 +00001490prepare_to_die:
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001491#if !defined(CONFIG_ASH)
Mark Whitley4e338752001-01-26 20:42:23 +00001492 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001493 goto_new_line();
1494 /* cmdedit_reset_term() called in atexit */
1495 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001496#else
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001497 /* to control stopped jobs */
1498 len = break_out = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001499 break;
1500#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001501 } else {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001502 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001503 }
1504 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001505 case CNTRL('E'):
1506 case VICMD('$'):
Erik Andersenc7c634b2000-03-19 05:28:55 +00001507 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001508 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001509 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001510 case CNTRL('F'):
1511 case VICMD('l'):
1512 case VICMD(' '):
Erik Andersenc7c634b2000-03-19 05:28:55 +00001513 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001514 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001515 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001516 case '\b':
1517 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001518 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001519 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001520 break;
1521 case '\t':
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001522#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001523 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001524#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001525 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001526 case CNTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001527 /* Control-k -- clear to end of line */
Eric Andersen65a07302002-04-13 13:26:49 +00001528 *(command + cursor) = 0;
1529 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001530 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001531 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001532 case CNTRL('L'):
1533 case VICMD(CNTRL('L')):
Eric Andersen27bb7902003-12-23 20:24:51 +00001534 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001535 printf("\033[H");
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001536 redraw(0, len-cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001537 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001538#if MAX_HISTORY >= 1
Paul Fox3f11b1b2005-08-04 19:04:46 +00001539 case CNTRL('N'):
1540 case VICMD(CNTRL('N')):
1541 case VICMD('j'):
Erik Andersenf0657d32000-04-12 17:49:52 +00001542 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001543 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001544 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001545 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001546 case CNTRL('P'):
1547 case VICMD(CNTRL('P')):
1548 case VICMD('k'):
Erik Andersenf0657d32000-04-12 17:49:52 +00001549 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001550 if (cur_history > 0) {
1551 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001552 goto rewrite_line;
1553 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001554 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001555 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001556 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001557#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001558 case CNTRL('U'):
1559 case VICMD(CNTRL('U')):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001560 /* Control-U -- Clear line before cursor */
1561 if (cursor) {
1562 strcpy(command, command + cursor);
1563 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001564 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001565 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001566 case CNTRL('W'):
1567 case VICMD(CNTRL('W')):
Eric Andersen27bb7902003-12-23 20:24:51 +00001568 /* Control-W -- Remove the last word */
1569 while (cursor > 0 && isspace(command[cursor-1]))
1570 input_backspace();
1571 while (cursor > 0 &&!isspace(command[cursor-1]))
1572 input_backspace();
1573 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001574#if CONFIG_FEATURE_COMMAND_EDITING_VI
1575 case VICMD('i'):
1576 vi_cmdmode = 0;
1577 break;
1578 case VICMD('I'):
1579 input_backward(cursor);
1580 vi_cmdmode = 0;
1581 break;
1582 case VICMD('a'):
1583 input_forward();
1584 vi_cmdmode = 0;
1585 break;
1586 case VICMD('A'):
1587 input_end();
1588 vi_cmdmode = 0;
1589 break;
1590 case VICMD('x'):
1591 input_delete(1);
1592 break;
1593 case VICMD('X'):
1594 if (cursor > 0) {
1595 input_backward(1);
1596 input_delete(1);
1597 }
1598 break;
1599 case VICMD('W'):
1600 vi_Word_motion(command, 1);
1601 break;
1602 case VICMD('w'):
1603 vi_word_motion(command, 1);
1604 break;
1605 case VICMD('E'):
1606 vi_End_motion(command);
1607 break;
1608 case VICMD('e'):
1609 vi_end_motion(command);
1610 break;
1611 case VICMD('B'):
1612 vi_Back_motion(command);
1613 break;
1614 case VICMD('b'):
1615 vi_back_motion(command);
1616 break;
1617 case VICMD('C'):
1618 vi_cmdmode = 0;
1619 /* fall through */
1620 case VICMD('D'):
1621 goto clear_to_eol;
1622
1623 case VICMD('c'):
1624 vi_cmdmode = 0;
1625 /* fall through */
1626 case VICMD('d'):
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001627 {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001628 int nc, sc;
1629 sc = cursor;
1630 prevc = ic;
1631 if (safe_read(0, &c, 1) < 1)
1632 goto prepare_to_die;
1633 if (c == (prevc & 0xff)) {
1634 /* "cc", "dd" */
1635 input_backward(cursor);
1636 goto clear_to_eol;
1637 break;
1638 }
1639 switch(c) {
1640 case 'w':
1641 case 'W':
1642 case 'e':
1643 case 'E':
1644 switch (c) {
1645 case 'w': /* "dw", "cw" */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001646 vi_word_motion(command, vi_cmdmode);
Paul Fox3f11b1b2005-08-04 19:04:46 +00001647 break;
1648 case 'W': /* 'dW', 'cW' */
1649 vi_Word_motion(command, vi_cmdmode);
1650 break;
1651 case 'e': /* 'de', 'ce' */
1652 vi_end_motion(command);
1653 input_forward();
1654 break;
1655 case 'E': /* 'dE', 'cE' */
1656 vi_End_motion(command);
1657 input_forward();
1658 break;
1659 }
1660 nc = cursor;
1661 input_backward(cursor - sc);
1662 while (nc-- > cursor)
1663 input_delete(1);
1664 break;
1665 case 'b': /* "db", "cb" */
1666 case 'B': /* implemented as B */
1667 if (c == 'b')
1668 vi_back_motion(command);
1669 else
1670 vi_Back_motion(command);
1671 while (sc-- > cursor)
1672 input_delete(1);
1673 break;
1674 case ' ': /* "d ", "c " */
1675 input_delete(1);
1676 break;
1677 case '$': /* "d$", "c$" */
1678 clear_to_eol:
1679 while (cursor < len)
1680 input_delete(1);
1681 break;
1682 }
1683 }
1684 break;
1685 case VICMD('p'):
1686 input_forward();
1687 /* fallthrough */
1688 case VICMD('P'):
1689 put();
1690 break;
1691 case VICMD('r'):
1692 if (safe_read(0, &c, 1) < 1)
1693 goto prepare_to_die;
1694 if (c == 0)
1695 beep();
1696 else {
1697 *(command + cursor) = c;
1698 putchar(c);
1699 putchar('\b');
1700 }
1701 break;
1702#endif /* CONFIG_FEATURE_COMMAND_EDITING_VI */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001703 case ESC:{
Paul Fox3f11b1b2005-08-04 19:04:46 +00001704#if CONFIG_FEATURE_COMMAND_EDITING_VI
1705 if (vi_mode) {
1706 /* ESC: insert mode --> command mode */
1707 vi_cmdmode = 1;
1708 input_backward(1);
1709 break;
1710 }
1711#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001712 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001713 if (safe_read(0, &c, 1) < 1)
1714 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001715 /* different vt100 emulations */
1716 if (c == '[' || c == 'O') {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001717 case VICMD('['):
1718 case VICMD('O'):
Eric Andersen7467c8d2001-07-12 20:26:32 +00001719 if (safe_read(0, &c, 1) < 1)
1720 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001721 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001722 if (c >= '1' && c <= '9') {
1723 unsigned char dummy;
1724
1725 if (safe_read(0, &dummy, 1) < 1)
1726 goto prepare_to_die;
1727 if(dummy != '~')
1728 c = 0;
1729 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001730 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001731#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001732 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001733
1734 input_tab(&lastWasTab);
1735 break;
1736#endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001737#if MAX_HISTORY >= 1
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001738 case 'A':
1739 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001740 if (cur_history > 0) {
1741 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001742 goto rewrite_line;
1743 } else {
1744 beep();
1745 }
1746 break;
1747 case 'B':
1748 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001749 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001750 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001751 /* Rewrite the line with the selected history item */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001752rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001753 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001754 len = strlen(strcpy(command, history[cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001755 /* redraw and go to eol (bol, in vi */
1756#if CONFIG_FEATURE_COMMAND_EDITING_VI
1757 redraw(cmdedit_y, vi_mode ? 9999:0);
1758#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001759 redraw(cmdedit_y, 0);
Paul Fox3f11b1b2005-08-04 19:04:46 +00001760#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001761 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001762#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001763 case 'C':
1764 /* Right Arrow -- Move forward one character */
1765 input_forward();
1766 break;
1767 case 'D':
1768 /* Left Arrow -- Move back one character */
1769 input_backward(1);
1770 break;
1771 case '3':
1772 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001773 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001774 break;
1775 case '1':
1776 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001777 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001778 input_backward(cursor);
1779 break;
1780 case '4':
1781 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001782 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001783 input_end();
1784 break;
1785 default:
Glenn L McGrath475820c2004-01-22 12:42:23 +00001786 c = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001787 beep();
1788 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001789 break;
1790 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001791
Eric Andersenc470f442003-07-28 09:56:35 +00001792 default: /* If it's regular input, do the normal thing */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001793#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001794 /* Control-V -- Add non-printable symbol */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001795 if (c == CNTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001796 if (safe_read(0, &c, 1) < 1)
1797 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001798 if (c == 0) {
1799 beep();
1800 break;
1801 }
1802 } else
1803#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001804 {
1805#if CONFIG_FEATURE_COMMAND_EDITING_VI
1806 if (vi_cmdmode) /* don't self-insert */
1807 break;
1808#endif
1809 if (!Isprint(c)) /* Skip non-printable characters */
1810 break;
1811 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001812
Eric Andersenc470f442003-07-28 09:56:35 +00001813 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001814 break;
1815
1816 len++;
1817
Eric Andersenc470f442003-07-28 09:56:35 +00001818 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001819 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001820 *(command + cursor + 1) = 0;
1821 cmdedit_set_out_char(0);
Eric Andersenc470f442003-07-28 09:56:35 +00001822 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001823 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001824
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001825 memmove(command + sc + 1, command + sc, len - sc);
1826 *(command + sc) = c;
1827 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001828 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001829 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001830 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001831 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001832 }
1833
Erik Andersenc7c634b2000-03-19 05:28:55 +00001834 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001835 }
Eric Andersenc470f442003-07-28 09:56:35 +00001836 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001837 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001838
1839 if (c != '\t')
1840 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001841 }
1842
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001843 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001844 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001845
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001846#if MAX_HISTORY >= 1
Erik Andersenc7c634b2000-03-19 05:28:55 +00001847 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001848 /* cleanup may be saved current command line */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001849 if (len> 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001850 int i = n_history;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001851
1852 free(history[MAX_HISTORY]);
1853 history[MAX_HISTORY] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001854 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001855 if (i >= MAX_HISTORY) {
1856 free(history[0]);
1857 for(i = 0; i < (MAX_HISTORY-1); i++)
1858 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001859 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001860 history[i++] = bb_xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001861 cur_history = i;
1862 n_history = i;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001863#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001864 num_ok_lines++;
1865#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001866 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001867#else /* MAX_HISTORY < 1 */
1868#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001869 if (len > 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001870 num_ok_lines++;
1871 }
1872#endif
1873#endif /* MAX_HISTORY >= 1 */
Eric Andersen27bb7902003-12-23 20:24:51 +00001874 if (break_out > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001875 command[len++] = '\n'; /* set '\n' */
1876 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001877 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001878#if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersenc470f442003-07-28 09:56:35 +00001879 input_tab(0); /* strong free */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001880#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001881#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001882 free(cmdedit_prompt);
1883#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001884 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001885 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001886}
1887
Eric Andersen7467c8d2001-07-12 20:26:32 +00001888
1889
Eric Andersenc470f442003-07-28 09:56:35 +00001890#endif /* CONFIG_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001891
1892
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001893#ifdef TEST
1894
Manuel Novoa III cad53642003-03-19 09:13:01 +00001895const char *bb_applet_name = "debug stuff usage";
Eric Andersene5dfced2001-04-09 22:48:12 +00001896
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001897#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001898#include <locale.h>
1899#endif
1900
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001901int main(int argc, char **argv)
1902{
1903 char buff[BUFSIZ];
1904 char *prompt =
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001905#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001906 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001907\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001908\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001909#else
1910 "% ";
1911#endif
1912
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001913#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001914 setlocale(LC_ALL, "");
1915#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001916 while(1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001917 int l;
Eric Andersen27bb7902003-12-23 20:24:51 +00001918 l = cmdedit_read_input(prompt, buff);
1919 if(l > 0 && buff[l-1] == '\n') {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001920 buff[l-1] = 0;
Eric Andersen27bb7902003-12-23 20:24:51 +00001921 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1922 } else {
1923 break;
1924 }
Eric Andersen7467c8d2001-07-12 20:26:32 +00001925 }
Eric Andersen27bb7902003-12-23 20:24:51 +00001926 printf("*** cmdedit_read_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001927 return 0;
1928}
1929
Eric Andersenc470f442003-07-28 09:56:35 +00001930#endif /* TEST */