blob: d0e6422502ab23c98f0d293e013ee9cd7d970960 [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
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000445#define DELBUFSIZ 128
446static char *delbuf; /* a (malloced) place to store deleted characters */
447static char *delp;
448static char newdelflag; /* whether delbuf should be reused yet */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000449#endif
450
451/* Delete the char in front of the cursor, optionally saving it
452 * for later putback */
453static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000454{
Mark Whitley4e338752001-01-26 20:42:23 +0000455 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000456
Mark Whitley4e338752001-01-26 20:42:23 +0000457 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000458 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000459
Paul Fox3f11b1b2005-08-04 19:04:46 +0000460#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
461 if (save) {
462 if (newdelflag) {
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000463 if (!delbuf)
464 delbuf = malloc(DELBUFSIZ);
465 /* safe if malloc fails */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000466 delp = delbuf;
467 newdelflag = 0;
468 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000469 if (delbuf && (delp - delbuf < DELBUFSIZ))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000470 *delp++ = command_ps[j];
471 }
472#endif
473
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000474 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000475 len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000476 input_end(); /* rewrite new line */
Eric Andersenc470f442003-07-28 09:56:35 +0000477 cmdedit_set_out_char(0); /* destroy end char */
478 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000479}
480
Paul Fox3f11b1b2005-08-04 19:04:46 +0000481#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
482static void put(void)
483{
484 int ocursor, j = delp - delbuf;
485 if (j == 0)
486 return;
487 ocursor = cursor;
488 /* open hole and then fill it */
489 memmove(command_ps + cursor + j, command_ps + cursor, len - cursor + 1);
490 strncpy(command_ps + cursor, delbuf, j);
491 len += j;
492 input_end(); /* rewrite new line */
493 input_backward(cursor-ocursor-j+1); /* at end of new text */
494}
495#endif
496
Mark Whitley4e338752001-01-26 20:42:23 +0000497/* Delete the char in back of the cursor */
498static void input_backspace(void)
499{
500 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000501 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000502 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000503 }
504}
505
506
Eric Andersenaff114c2004-04-14 17:51:38 +0000507/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000508static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000509{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000510 if (cursor < len)
511 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000512}
513
Mark Whitley4e338752001-01-26 20:42:23 +0000514static void cmdedit_setwidth(int w, int redraw_flg)
515{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000516 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000517 if (w <= cmdedit_termw) {
518 cmdedit_termw = cmdedit_termw % w;
519 }
Mark Whitley4e338752001-01-26 20:42:23 +0000520 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000521 cmdedit_termw = w;
522
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000523 if (redraw_flg) {
524 /* new y for current cursor */
525 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000526
527 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000528 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
529 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000530 }
Eric Andersenc470f442003-07-28 09:56:35 +0000531 }
Mark Whitley4e338752001-01-26 20:42:23 +0000532}
533
Eric Andersen7467c8d2001-07-12 20:26:32 +0000534static void cmdedit_init(void)
Mark Whitley4e338752001-01-26 20:42:23 +0000535{
Eric Andersen61173a52001-03-19 17:48:55 +0000536 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000537 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
538 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000539 win_changed(-SIGWINCH);
540 handlers_sets |= SET_WCHG_HANDLERS;
541 }
542
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000543 if ((handlers_sets & SET_ATEXIT) == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000544#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000545 struct passwd *entry;
546
547 my_euid = geteuid();
548 entry = getpwuid(my_euid);
549 if (entry) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000550 user_buf = bb_xstrdup(entry->pw_name);
551 home_pwd_buf = bb_xstrdup(entry->pw_dir);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000552 }
553#endif
554
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000555#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000556
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000557#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000558 my_euid = geteuid();
559#endif
560 my_uid = getuid();
561 my_gid = getgid();
Eric Andersenc470f442003-07-28 09:56:35 +0000562#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000563 handlers_sets |= SET_ATEXIT;
Eric Andersenc470f442003-07-28 09:56:35 +0000564 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000565 }
Mark Whitley4e338752001-01-26 20:42:23 +0000566}
Erik Andersenf0657d32000-04-12 17:49:52 +0000567
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000568#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000569
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000570static char **matches;
571static int num_matches;
572static char *add_char_to_match;
573
574static void add_match(char *matched, int add_char)
575{
576 int nm = num_matches;
577 int nm1 = nm + 1;
578
579 matches = xrealloc(matches, nm1 * sizeof(char *));
580 add_char_to_match = xrealloc(add_char_to_match, nm1);
581 matches[nm] = matched;
582 add_char_to_match[nm] = (char)add_char;
583 num_matches++;
584}
585
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000586static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000587{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000588 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
589 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
590 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
591 (st->st_mode & S_IXOTH)) return TRUE;
592 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000593}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000594
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000595#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000596
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000597static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000598{
599 struct passwd *entry;
600 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000601
Eric Andersenc470f442003-07-28 09:56:35 +0000602 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000603 userlen = strlen(ud);
604
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000605 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000606 char *sav_ud = ud - 1;
607 char *home = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000608 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000609
Eric Andersenc470f442003-07-28 09:56:35 +0000610 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000611 home = home_pwd_buf;
612 } else {
613 /* "~user/..." */
614 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000615 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000616 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000617 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000618 ud = temp;
619 if (entry)
620 home = entry->pw_dir;
621 }
622 if (home) {
623 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000624 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000625
626 /* /home/user/... */
627 sprintf(temp2, "%s%s", home, ud);
628 strcpy(sav_ud, temp2);
629 }
630 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000631 } else {
632 /* "~[^/]*" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000633 setpwent();
634
635 while ((entry = getpwent()) != NULL) {
636 /* Null usernames should result in all users as possible completions. */
637 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000638 add_match(bb_xasprintf("~%s", entry->pw_name), '/');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000639 }
640 }
641
642 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000643 }
644}
Eric Andersenc470f442003-07-28 09:56:35 +0000645#endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000646
647enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000648 FIND_EXE_ONLY = 0,
649 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000650 FIND_FILE_ONLY = 2,
651};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000652
Glenn L McGrath67285962004-01-14 09:34:51 +0000653#ifdef CONFIG_ASH
654const char *cmdedit_path_lookup;
655#else
656#define cmdedit_path_lookup getenv("PATH")
657#endif
658
Mark Whitley4e338752001-01-26 20:42:23 +0000659static int path_parse(char ***p, int flags)
660{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000661 int npth;
Glenn L McGrath67285962004-01-14 09:34:51 +0000662 const char *tmp;
663 const char *pth;
Mark Whitley4e338752001-01-26 20:42:23 +0000664
Mark Whitley4e338752001-01-26 20:42:23 +0000665 /* if not setenv PATH variable, to search cur dir "." */
Glenn L McGrath67285962004-01-14 09:34:51 +0000666 if (flags != FIND_EXE_ONLY || (pth = cmdedit_path_lookup) == 0 ||
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000667 /* PATH=<empty> or PATH=:<empty> */
668 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000669 return 1;
670 }
671
672 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000673 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000674
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000675 for (;;) {
Eric Andersenc470f442003-07-28 09:56:35 +0000676 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000677 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000678 if (tmp) {
679 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000680 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000681 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000682 break;
683 }
684
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000685 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000686
687 tmp = pth;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000688 (*p)[0] = bb_xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000689 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000690
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000691 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000692 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000693 if (tmp) {
Eric Andersenc470f442003-07-28 09:56:35 +0000694 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000695 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000696 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000697 } else
698 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000699 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000700 }
701
702 return npth;
703}
704
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000705static char *add_quote_for_spec_chars(char *found, int add)
Erik Andersen6273f652000-03-17 01:12:41 +0000706{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000707 int l = 0;
708 char *s = xmalloc((strlen(found) + 1) * 2);
709
710 while (*found) {
711 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
712 s[l++] = '\\';
713 s[l++] = *found++;
714 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000715 if(add)
716 s[l++] = (char)add;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000717 s[l] = 0;
718 return s;
719}
720
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000721static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000722{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000723 DIR *dir;
724 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000725 char dirbuf[BUFSIZ];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000726 struct stat st;
727 char *path1[1];
728 char **paths = path1;
729 int npaths;
730 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000731 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000732 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000733
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000734 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000735
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000736 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000737 /* no dir, if flags==EXE_ONLY - get paths, else "." */
738 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000739 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000740 } else {
741 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000742 /* save for change */
743 strcpy(dirbuf, command);
744 /* set dir only */
745 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000746#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000747 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000748 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000749#endif
750 /* "strip" dirname in command */
751 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000752
Mark Whitley4e338752001-01-26 20:42:23 +0000753 paths[0] = dirbuf;
Eric Andersenc470f442003-07-28 09:56:35 +0000754 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000755 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000756
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000757 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000758
Mark Whitley4e338752001-01-26 20:42:23 +0000759 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000760 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000761 continue;
762
763 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000764 char *str_found = next->d_name;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000765 int add_chr = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000766
Mark Whitley4e338752001-01-26 20:42:23 +0000767 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000768 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000769 continue;
770 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000771 if (*str_found == '.' && *pfind == 0) {
772 if (*paths[i] == '/' && paths[i][1] == 0
Eric Andersenc470f442003-07-28 09:56:35 +0000773 && str_found[1] == 0) str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000774 else
Mark Whitley4e338752001-01-26 20:42:23 +0000775 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000776 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000777 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000778 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000779 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000780 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000781 /* find with dirs ? */
782 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000783 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000784 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000785 /* name is directory */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000786 char *e = found + strlen(found) - 1;
787
788 add_chr = '/';
789 if(*e == '/')
790 *e = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +0000791 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000792 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000793 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000794 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000795 if (type == FIND_FILE_ONLY ||
Matt Kraai1f0c4362001-12-20 23:13:26 +0000796 (type == FIND_EXE_ONLY && is_execute(&st)))
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000797 add_chr = ' ';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000798 }
Mark Whitley4e338752001-01-26 20:42:23 +0000799 /* Add it to the list */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000800 add_match(found, add_chr);
801 continue;
Eric Andersene5dfced2001-04-09 22:48:12 +0000802cont:
803 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000804 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000805 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000806 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000807 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000808 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000809 free(paths);
810 }
Erik Andersen6273f652000-03-17 01:12:41 +0000811}
Erik Andersenf0657d32000-04-12 17:49:52 +0000812
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000813
814#define QUOT (UCHAR_MAX+1)
815
816#define collapse_pos(is, in) { \
Paul Fox574fee42005-07-19 20:41:06 +0000817 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
818 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000819
820static int find_match(char *matchBuf, int *len_with_quotes)
821{
822 int i, j;
823 int command_mode;
824 int c, c2;
825 int int_buf[BUFSIZ + 1];
826 int pos_buf[BUFSIZ + 1];
827
828 /* set to integer dimension characters and own positions */
829 for (i = 0;; i++) {
830 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
831 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000832 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000833 break;
834 } else
835 pos_buf[i] = i;
836 }
837
838 /* mask \+symbol and convert '\t' to ' ' */
839 for (i = j = 0; matchBuf[i]; i++, j++)
840 if (matchBuf[i] == '\\') {
841 collapse_pos(j, j + 1);
842 int_buf[j] |= QUOT;
843 i++;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000844#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000845 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000846 int_buf[j] = ' ' | QUOT;
847#endif
848 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000849#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000850 else if (matchBuf[i] == '\t')
851 int_buf[j] = ' ';
852#endif
853
854 /* mask "symbols" or 'symbols' */
855 c2 = 0;
856 for (i = 0; int_buf[i]; i++) {
857 c = int_buf[i];
858 if (c == '\'' || c == '"') {
859 if (c2 == 0)
860 c2 = c;
861 else {
862 if (c == c2)
863 c2 = 0;
864 else
865 int_buf[i] |= QUOT;
866 }
867 } else if (c2 != 0 && c != '$')
868 int_buf[i] |= QUOT;
869 }
870
871 /* skip commands with arguments if line have commands delimiters */
872 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
873 for (i = 0; int_buf[i]; i++) {
874 c = int_buf[i];
875 c2 = int_buf[i + 1];
876 j = i ? int_buf[i - 1] : -1;
877 command_mode = 0;
878 if (c == ';' || c == '&' || c == '|') {
879 command_mode = 1 + (c == c2);
880 if (c == '&') {
881 if (j == '>' || j == '<')
882 command_mode = 0;
883 } else if (c == '|' && j == '>')
884 command_mode = 0;
885 }
886 if (command_mode) {
887 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000888 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000889 }
890 }
891 /* collapse `command...` */
892 for (i = 0; int_buf[i]; i++)
893 if (int_buf[i] == '`') {
894 for (j = i + 1; int_buf[j]; j++)
895 if (int_buf[j] == '`') {
896 collapse_pos(i, j + 1);
897 j = 0;
898 break;
899 }
900 if (j) {
901 /* not found close ` - command mode, collapse all previous */
902 collapse_pos(0, i + 1);
903 break;
904 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000905 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000906 }
907
908 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000909 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000910 c2 = 0;
911 for (i = 0; int_buf[i]; i++)
912 if (int_buf[i] == '(' || int_buf[i] == '{') {
913 if (int_buf[i] == '(')
914 c++;
915 else
916 c2++;
917 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000918 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000919 }
920 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
921 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
922 if (int_buf[i] == ')')
923 c--;
924 else
925 c2--;
926 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000927 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000928 }
929
930 /* skip first not quote space */
931 for (i = 0; int_buf[i]; i++)
932 if (int_buf[i] != ' ')
933 break;
934 if (i)
935 collapse_pos(0, i);
936
937 /* set find mode for completion */
938 command_mode = FIND_EXE_ONLY;
939 for (i = 0; int_buf[i]; i++)
940 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
941 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000942 && matchBuf[pos_buf[0]]=='c'
943 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000944 command_mode = FIND_DIR_ONLY;
945 else {
946 command_mode = FIND_FILE_ONLY;
947 break;
948 }
949 }
950 /* "strlen" */
951 for (i = 0; int_buf[i]; i++);
952 /* find last word */
953 for (--i; i >= 0; i--) {
954 c = int_buf[i];
955 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
956 collapse_pos(0, i + 1);
957 break;
958 }
959 }
960 /* skip first not quoted '\'' or '"' */
961 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
962 /* collapse quote or unquote // or /~ */
Eric Andersenc470f442003-07-28 09:56:35 +0000963 while ((int_buf[i] & ~QUOT) == '/' &&
Mark Whitley7e5291f2001-03-08 19:31:12 +0000964 ((int_buf[i + 1] & ~QUOT) == '/'
965 || (int_buf[i + 1] & ~QUOT) == '~')) {
966 i++;
967 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000968
969 /* set only match and destroy quotes */
970 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000971 for (c = 0; pos_buf[i] >= 0; i++) {
972 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000973 j = pos_buf[i] + 1;
974 }
Eric Andersen4f990532001-05-31 17:15:57 +0000975 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000976 /* old lenght matchBuf with quotes symbols */
977 *len_with_quotes = j ? j - pos_buf[0] : 0;
978
979 return command_mode;
980}
981
Glenn L McGrath4d001292003-01-06 01:11:50 +0000982/*
983 display by column original ideas from ls applet,
984 very optimize by my :)
985*/
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000986static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000987{
988 int ncols, row;
989 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000990 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000991 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000992 char str_add_chr[2];
993 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000994
995 /* find the longest file name- use that as the column width */
996 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000997 l = strlen(matches[row]);
998 if(add_char_to_match[row])
999 l++;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001000 if (column_width < l)
1001 column_width = l;
1002 }
1003 column_width += 2; /* min space for columns */
1004 ncols = cmdedit_termw / column_width;
1005
1006 if (ncols > 1) {
1007 nrows /= ncols;
1008 if(nfiles % ncols)
1009 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001010 } else {
1011 ncols = 1;
1012 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001013 str_add_chr[1] = 0;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001014 for (row = 0; row < nrows; row++) {
1015 int n = row;
1016 int nc;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001017 int acol;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001018
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001019 for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
1020 str_add_chr[0] = add_char_to_match[n];
1021 acol = str_add_chr[0] ? column_width - 1 : column_width;
1022 printf("%s%s", matches[n], str_add_chr);
1023 l = strlen(matches[n]);
1024 while(l < acol) {
1025 putchar(' ');
1026 l++;
1027 }
1028 }
1029 str_add_chr[0] = add_char_to_match[n];
1030 printf("%s%s\n", matches[n], str_add_chr);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001031 }
1032}
1033
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001034
1035static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001036{
1037 /* Do TAB completion */
Eric Andersenc470f442003-07-28 09:56:35 +00001038 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001039 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001040 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001041 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001042 free(matches);
1043 matches = (char **) NULL;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001044 free(add_char_to_match);
1045 add_char_to_match = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +00001046 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001047 return;
1048 }
Matt Kraai1f0c4362001-12-20 23:13:26 +00001049 if (! *lastWasTab) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001050
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001051 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001052 int len_found;
1053 char matchBuf[BUFSIZ];
1054 int find_type;
1055 int recalc_pos;
1056
Eric Andersenc470f442003-07-28 09:56:35 +00001057 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001058
1059 /* Make a local copy of the string -- up
1060 * to the position of the cursor */
1061 tmp = strncpy(matchBuf, command_ps, cursor);
1062 tmp[cursor] = 0;
1063
1064 find_type = find_match(matchBuf, &recalc_pos);
1065
1066 /* Free up any memory already allocated */
1067 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001068
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001069#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001070 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001071 * then try completing this word as a username. */
1072
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001073 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001074 username_tab_completion(matchBuf, NULL);
1075 if (!matches)
Mark Whitley4e338752001-01-26 20:42:23 +00001076#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001077 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001078 * in the current working directory that matches. */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001079 exe_n_cwd_tab_completion(matchBuf, find_type);
1080 /* Remove duplicate found and sort */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001081 if(matches) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001082 int i, j, n, srt;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001083 /* bubble */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001084 n = num_matches;
1085 for(i=0; i<(n-1); i++)
1086 for(j=i+1; j<n; j++)
1087 if(matches[i]!=NULL && matches[j]!=NULL) {
1088 srt = strcmp(matches[i], matches[j]);
1089 if(srt == 0) {
1090 free(matches[j]);
1091 matches[j]=0;
1092 } else if(srt > 0) {
1093 tmp1 = matches[i];
1094 matches[i] = matches[j];
1095 matches[j] = tmp1;
1096 srt = add_char_to_match[i];
1097 add_char_to_match[i] = add_char_to_match[j];
1098 add_char_to_match[j] = srt;
1099 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001100 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001101 j = n;
1102 n = 0;
1103 for(i=0; i<j; i++)
1104 if(matches[i]) {
1105 matches[n]=matches[i];
1106 add_char_to_match[n]=add_char_to_match[i];
1107 n++;
1108 }
1109 num_matches = n;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001110 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001111 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001112 if (!matches || num_matches > 1) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001113
Mark Whitley4e338752001-01-26 20:42:23 +00001114 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001115 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +00001116 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001117 /* find minimal match */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001118 tmp1 = bb_xstrdup(matches[0]);
1119 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001120 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001121 if (matches[len_found][(tmp - tmp1)] != *tmp) {
1122 *tmp = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001123 break;
1124 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001125 if (*tmp1 == 0) { /* have unique */
1126 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001127 return;
1128 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001129 tmp = add_quote_for_spec_chars(tmp1, 0);
1130 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +00001131 } else { /* one match */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001132 tmp = add_quote_for_spec_chars(matches[0], add_char_to_match[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001133 /* for next completion current found */
1134 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001135 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001136 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001137 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001138 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001139
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001140 /* before word for match */
1141 command_ps[cursor - recalc_pos] = 0;
1142 /* save tail line */
1143 strcpy(matchBuf, command_ps + cursor);
1144 /* add match */
1145 strcat(command_ps, tmp);
1146 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001147 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001148 /* back to begin word for match */
1149 input_backward(recalc_pos);
1150 /* new pos */
1151 recalc_pos = cursor + len_found;
1152 /* new len */
1153 len = strlen(command_ps);
1154 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001155 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001156 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001157 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001158 } else {
1159 /* Ok -- the last char was a TAB. Since they
1160 * just hit TAB again, print a list of all the
1161 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001162 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001163 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001164
1165 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001166 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001167 showfiles();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001168 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001169 }
1170 }
1171}
Eric Andersenc470f442003-07-28 09:56:35 +00001172#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001173
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001174#if MAX_HISTORY >= 1
1175static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001176{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001177 if(command_ps[0] != 0 || history[cur_history] == 0) {
1178 free(history[cur_history]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001179 history[cur_history] = bb_xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001180 }
1181 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +00001182}
1183
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001184static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001185{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001186 int ch = cur_history;
1187
1188 if (ch < n_history) {
1189 get_previous_history(); /* save the current history line */
1190 return (cur_history = ch+1);
1191 } else {
1192 beep();
1193 return 0;
1194 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001195}
Robert Griebl350d26b2002-12-03 22:45:46 +00001196
Robert Griebl350d26b2002-12-03 22:45:46 +00001197#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
Rob Landleydfba7412006-03-06 20:47:33 +00001198void load_history ( const char *fromfile )
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001199{
Robert Griebl350d26b2002-12-03 22:45:46 +00001200 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001201 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001202
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001203 /* cleanup old */
1204
1205 for(hi = n_history; hi > 0; ) {
1206 hi--;
1207 free ( history [hi] );
Robert Griebl350d26b2002-12-03 22:45:46 +00001208 }
1209
1210 if (( fp = fopen ( fromfile, "r" ))) {
Eric Andersenc470f442003-07-28 09:56:35 +00001211
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001212 for ( hi = 0; hi < MAX_HISTORY; ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001213 char * hl = bb_get_chomped_line_from_file(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001214 int l;
1215
1216 if(!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001217 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001218 l = strlen(hl);
1219 if(l >= BUFSIZ)
1220 hl[BUFSIZ-1] = 0;
1221 if(l == 0 || hl[0] == ' ') {
1222 free(hl);
1223 continue;
1224 }
1225 history [hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001226 }
1227 fclose ( fp );
1228 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001229 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001230}
1231
Rob Landleydfba7412006-03-06 20:47:33 +00001232void save_history ( const char *tofile )
Robert Griebl350d26b2002-12-03 22:45:46 +00001233{
Robert Griebl350d26b2002-12-03 22:45:46 +00001234 FILE *fp = fopen ( tofile, "w" );
Eric Andersenc470f442003-07-28 09:56:35 +00001235
Robert Griebl350d26b2002-12-03 22:45:46 +00001236 if ( fp ) {
1237 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001238
Robert Griebl350d26b2002-12-03 22:45:46 +00001239 for ( i = 0; i < n_history; i++ ) {
Eric Andersen27bb7902003-12-23 20:24:51 +00001240 fprintf(fp, "%s\n", history [i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001241 }
1242 fclose ( fp );
1243 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001244}
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001245#endif
Robert Griebl350d26b2002-12-03 22:45:46 +00001246
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001247#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001248
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001249enum {
1250 ESC = 27,
1251 DEL = 127,
1252};
1253
1254
Erik Andersen6273f652000-03-17 01:12:41 +00001255/*
1256 * This function is used to grab a character buffer
1257 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001258 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001259 * a mini readline).
1260 *
1261 * The following standard commands are not implemented:
1262 * ESC-b -- Move back one word
1263 * ESC-f -- Move forward one word
1264 * ESC-d -- Delete back one word
1265 * ESC-h -- Delete forward one word
1266 * CTL-t -- Transpose two characters
1267 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001268 * Minimalist vi-style command line editing available if configured.
1269 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001270 *
Erik Andersen6273f652000-03-17 01:12:41 +00001271 */
Eric Andersenc470f442003-07-28 09:56:35 +00001272
Paul Fox3f11b1b2005-08-04 19:04:46 +00001273#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
1274static int vi_mode;
1275
1276void setvimode ( int viflag )
1277{
1278 vi_mode = viflag;
1279}
1280
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001281static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001282vi_Word_motion(char *command, int eat)
1283{
1284 while (cursor < len && !isspace(command[cursor]))
1285 input_forward();
1286 if (eat) while (cursor < len && isspace(command[cursor]))
1287 input_forward();
1288}
1289
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001290static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001291vi_word_motion(char *command, int eat)
1292{
1293 if (isalnum(command[cursor]) || command[cursor] == '_') {
1294 while (cursor < len &&
1295 (isalnum(command[cursor+1]) ||
1296 command[cursor+1] == '_'))
1297 input_forward();
1298 } else if (ispunct(command[cursor])) {
1299 while (cursor < len &&
1300 (ispunct(command[cursor+1])))
1301 input_forward();
1302 }
1303
1304 if (cursor < len)
1305 input_forward();
1306
1307 if (eat && cursor < len && isspace(command[cursor]))
1308 while (cursor < len && isspace(command[cursor]))
1309 input_forward();
1310}
1311
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001312static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001313vi_End_motion(char *command)
1314{
1315 input_forward();
1316 while (cursor < len && isspace(command[cursor]))
1317 input_forward();
1318 while (cursor < len-1 && !isspace(command[cursor+1]))
1319 input_forward();
1320}
1321
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001322static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001323vi_end_motion(char *command)
1324{
1325 if (cursor >= len-1)
1326 return;
1327 input_forward();
1328 while (cursor < len-1 && isspace(command[cursor]))
1329 input_forward();
1330 if (cursor >= len-1)
1331 return;
1332 if (isalnum(command[cursor]) || command[cursor] == '_') {
1333 while (cursor < len-1 &&
1334 (isalnum(command[cursor+1]) ||
1335 command[cursor+1] == '_'))
1336 input_forward();
1337 } else if (ispunct(command[cursor])) {
1338 while (cursor < len-1 &&
1339 (ispunct(command[cursor+1])))
1340 input_forward();
1341 }
1342}
1343
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001344static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001345vi_Back_motion(char *command)
1346{
1347 while (cursor > 0 && isspace(command[cursor-1]))
1348 input_backward(1);
1349 while (cursor > 0 && !isspace(command[cursor-1]))
1350 input_backward(1);
1351}
1352
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001353static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001354vi_back_motion(char *command)
1355{
1356 if (cursor <= 0)
1357 return;
1358 input_backward(1);
1359 while (cursor > 0 && isspace(command[cursor]))
1360 input_backward(1);
1361 if (cursor <= 0)
1362 return;
1363 if (isalnum(command[cursor]) || command[cursor] == '_') {
1364 while (cursor > 0 &&
1365 (isalnum(command[cursor-1]) ||
1366 command[cursor-1] == '_'))
1367 input_backward(1);
1368 } else if (ispunct(command[cursor])) {
1369 while (cursor > 0 &&
1370 (ispunct(command[cursor-1])))
1371 input_backward(1);
1372 }
1373}
1374#endif
1375
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001376/*
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001377 * the emacs and vi modes share much of the code in the big
1378 * command loop. commands entered when in vi's command mode (aka
1379 * "escape mode") get an extra bit added to distinguish them --
1380 * this keeps them from being self-inserted. this clutters the
1381 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001382 */
1383
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001384#define vbit 0x100
1385
1386/* leave out the "vi-mode"-only case labels if vi editing isn't
1387 * configured. */
1388#define vi_case(caselabel) USE_FEATURE_COMMAND_EDITING(caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001389
1390/* convert uppercase ascii to equivalent control char, for readability */
1391#define CNTRL(uc_char) ((uc_char) - 0x40)
1392
Eric Andersen044228d2001-07-17 01:12:36 +00001393
1394int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001395{
1396
Erik Andersenc7c634b2000-03-19 05:28:55 +00001397 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001398 int lastWasTab = FALSE;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001399 unsigned char c;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001400 unsigned int ic;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001401#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001402 unsigned int prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001403 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 Fox0f2dd9f2006-03-07 20:26:11 +00001441 ic = c;
1442
Paul Fox3f11b1b2005-08-04 19:04:46 +00001443#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
1444 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001445 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001446 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001447#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001448 switch (ic)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001449 {
Erik Andersenf0657d32000-04-12 17:49:52 +00001450 case '\n':
1451 case '\r':
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001452 vi_case( case '\n'|vbit: )
1453 vi_case( case '\r'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001454 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001455 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001456 break_out = 1;
1457 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001458 case CNTRL('A'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001459 vi_case( case '0'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001460 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001461 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001462 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001463 case CNTRL('B'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001464 vi_case( case 'h'|vbit: )
1465 vi_case( case '\b'|vbit: )
1466 vi_case( case DEL|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001467 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001468 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001469 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001470 case CNTRL('C'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001471 vi_case( case CNTRL('C')|vbit: )
Eric Andersen86349772000-12-18 20:25:50 +00001472 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001473 goto_new_line();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001474#ifndef CONFIG_ASH
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001475 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001476 len = 0;
1477 lastWasTab = FALSE;
1478 put_prompt();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001479#else
1480 len = 0;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001481 break_out = -1; /* to control traps */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001482#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001483 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001484 case CNTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001485 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001486 * if the len=0 and no chars to delete */
1487 if (len == 0) {
Eric Andersencb01bb12004-08-19 18:22:13 +00001488 errno = 0;
Eric Andersened424db2001-04-23 15:28:28 +00001489prepare_to_die:
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001490#if !defined(CONFIG_ASH)
Mark Whitley4e338752001-01-26 20:42:23 +00001491 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001492 goto_new_line();
1493 /* cmdedit_reset_term() called in atexit */
1494 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001495#else
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001496 /* to control stopped jobs */
1497 len = break_out = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001498 break;
1499#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001500 } else {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001501 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001502 }
1503 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001504 case CNTRL('E'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001505 vi_case( case '$'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001506 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001507 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001508 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001509 case CNTRL('F'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001510 vi_case( case 'l'|vbit: )
1511 vi_case( case ' '|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001512 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001513 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001514 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001515 case '\b':
1516 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001517 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001518 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001519 break;
1520 case '\t':
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001521#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001522 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001523#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001524 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001525 case CNTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001526 /* Control-k -- clear to end of line */
Eric Andersen65a07302002-04-13 13:26:49 +00001527 *(command + cursor) = 0;
1528 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001529 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001530 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001531 case CNTRL('L'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001532 vi_case( case CNTRL('L')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001533 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001534 printf("\033[H");
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001535 redraw(0, len-cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001536 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001537#if MAX_HISTORY >= 1
Paul Fox3f11b1b2005-08-04 19:04:46 +00001538 case CNTRL('N'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001539 vi_case( case CNTRL('N')|vbit: )
1540 vi_case( case 'j'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001541 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001542 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001543 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001544 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001545 case CNTRL('P'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001546 vi_case( case CNTRL('P')|vbit: )
1547 vi_case( case 'k'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001548 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001549 if (cur_history > 0) {
1550 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001551 goto rewrite_line;
1552 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001553 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001554 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001555 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001556#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001557 case CNTRL('U'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001558 vi_case( case CNTRL('U')|vbit: )
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001559 /* Control-U -- Clear line before cursor */
1560 if (cursor) {
1561 strcpy(command, command + cursor);
1562 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001563 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001564 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001565 case CNTRL('W'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001566 vi_case( case CNTRL('W')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001567 /* Control-W -- Remove the last word */
1568 while (cursor > 0 && isspace(command[cursor-1]))
1569 input_backspace();
1570 while (cursor > 0 &&!isspace(command[cursor-1]))
1571 input_backspace();
1572 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001573#if CONFIG_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001574 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001575 vi_cmdmode = 0;
1576 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001577 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001578 input_backward(cursor);
1579 vi_cmdmode = 0;
1580 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001581 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001582 input_forward();
1583 vi_cmdmode = 0;
1584 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001585 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001586 input_end();
1587 vi_cmdmode = 0;
1588 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001589 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001590 input_delete(1);
1591 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001592 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001593 if (cursor > 0) {
1594 input_backward(1);
1595 input_delete(1);
1596 }
1597 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001598 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001599 vi_Word_motion(command, 1);
1600 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001601 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001602 vi_word_motion(command, 1);
1603 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001604 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001605 vi_End_motion(command);
1606 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001607 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001608 vi_end_motion(command);
1609 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001610 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001611 vi_Back_motion(command);
1612 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001613 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001614 vi_back_motion(command);
1615 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001616 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001617 vi_cmdmode = 0;
1618 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001619 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001620 goto clear_to_eol;
1621
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001622 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001623 vi_cmdmode = 0;
1624 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001625 case 'd'|vbit:
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001626 {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001627 int nc, sc;
1628 sc = cursor;
1629 prevc = ic;
1630 if (safe_read(0, &c, 1) < 1)
1631 goto prepare_to_die;
1632 if (c == (prevc & 0xff)) {
1633 /* "cc", "dd" */
1634 input_backward(cursor);
1635 goto clear_to_eol;
1636 break;
1637 }
1638 switch(c) {
1639 case 'w':
1640 case 'W':
1641 case 'e':
1642 case 'E':
1643 switch (c) {
1644 case 'w': /* "dw", "cw" */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001645 vi_word_motion(command, vi_cmdmode);
Paul Fox3f11b1b2005-08-04 19:04:46 +00001646 break;
1647 case 'W': /* 'dW', 'cW' */
1648 vi_Word_motion(command, vi_cmdmode);
1649 break;
1650 case 'e': /* 'de', 'ce' */
1651 vi_end_motion(command);
1652 input_forward();
1653 break;
1654 case 'E': /* 'dE', 'cE' */
1655 vi_End_motion(command);
1656 input_forward();
1657 break;
1658 }
1659 nc = cursor;
1660 input_backward(cursor - sc);
1661 while (nc-- > cursor)
1662 input_delete(1);
1663 break;
1664 case 'b': /* "db", "cb" */
1665 case 'B': /* implemented as B */
1666 if (c == 'b')
1667 vi_back_motion(command);
1668 else
1669 vi_Back_motion(command);
1670 while (sc-- > cursor)
1671 input_delete(1);
1672 break;
1673 case ' ': /* "d ", "c " */
1674 input_delete(1);
1675 break;
1676 case '$': /* "d$", "c$" */
1677 clear_to_eol:
1678 while (cursor < len)
1679 input_delete(1);
1680 break;
1681 }
1682 }
1683 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001684 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001685 input_forward();
1686 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001687 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001688 put();
1689 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001690 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001691 if (safe_read(0, &c, 1) < 1)
1692 goto prepare_to_die;
1693 if (c == 0)
1694 beep();
1695 else {
1696 *(command + cursor) = c;
1697 putchar(c);
1698 putchar('\b');
1699 }
1700 break;
1701#endif /* CONFIG_FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001702
1703 case ESC:
1704
Paul Fox3f11b1b2005-08-04 19:04:46 +00001705#if CONFIG_FEATURE_COMMAND_EDITING_VI
1706 if (vi_mode) {
1707 /* ESC: insert mode --> command mode */
1708 vi_cmdmode = 1;
1709 input_backward(1);
1710 break;
1711 }
1712#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001713 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001714 if (safe_read(0, &c, 1) < 1)
1715 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001716 /* different vt100 emulations */
1717 if (c == '[' || c == 'O') {
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001718 vi_case( case '['|vbit: )
1719 vi_case( case 'O'|vbit: )
Eric Andersen7467c8d2001-07-12 20:26:32 +00001720 if (safe_read(0, &c, 1) < 1)
1721 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001722 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001723 if (c >= '1' && c <= '9') {
1724 unsigned char dummy;
1725
1726 if (safe_read(0, &dummy, 1) < 1)
1727 goto prepare_to_die;
1728 if(dummy != '~')
1729 c = 0;
1730 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001731 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001732#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001733 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001734
1735 input_tab(&lastWasTab);
1736 break;
1737#endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001738#if MAX_HISTORY >= 1
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001739 case 'A':
1740 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001741 if (cur_history > 0) {
1742 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001743 goto rewrite_line;
1744 } else {
1745 beep();
1746 }
1747 break;
1748 case 'B':
1749 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001750 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001751 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001752 /* Rewrite the line with the selected history item */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001753rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001754 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001755 len = strlen(strcpy(command, history[cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001756 /* redraw and go to eol (bol, in vi */
1757#if CONFIG_FEATURE_COMMAND_EDITING_VI
1758 redraw(cmdedit_y, vi_mode ? 9999:0);
1759#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001760 redraw(cmdedit_y, 0);
Paul Fox3f11b1b2005-08-04 19:04:46 +00001761#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001762 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001763#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001764 case 'C':
1765 /* Right Arrow -- Move forward one character */
1766 input_forward();
1767 break;
1768 case 'D':
1769 /* Left Arrow -- Move back one character */
1770 input_backward(1);
1771 break;
1772 case '3':
1773 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001774 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001775 break;
1776 case '1':
1777 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001778 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001779 input_backward(cursor);
1780 break;
1781 case '4':
1782 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001783 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001784 input_end();
1785 break;
1786 default:
Glenn L McGrath475820c2004-01-22 12:42:23 +00001787 c = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001788 beep();
1789 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001790 break;
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 */