blob: 59226aff52f5b040a7736e72f7f89fbf10fd9d22 [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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000034#include "busybox.h"
Eric Andersencbe31da2001-02-20 06:14:08 +000035#include <stdio.h>
36#include <errno.h>
37#include <unistd.h>
38#include <stdlib.h>
39#include <string.h>
40#include <sys/ioctl.h>
41#include <ctype.h>
42#include <signal.h>
43#include <limits.h>
44
"Robert P. J. Day"4eddb422006-07-03 00:46:47 +000045#include "cmdedit.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000046
Glenn L McGrath475820c2004-01-22 12:42:23 +000047
Eric Andersenbdfd0d72001-10-24 05:00:29 +000048#ifdef CONFIG_LOCALE_SUPPORT
Eric Andersene5dfced2001-04-09 22:48:12 +000049#define Isprint(c) isprint((c))
50#else
51#define Isprint(c) ( (c) >= ' ' && (c) != ((unsigned char)'\233') )
52#endif
53
Glenn L McGrath3b251852004-01-03 12:07:32 +000054#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000055
Eric Andersen27bb7902003-12-23 20:24:51 +000056/* pretect redefined for test */
57#undef CONFIG_FEATURE_COMMAND_EDITING
58#undef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
59#undef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
60#undef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
61#undef CONFIG_FEATURE_CLEAN_UP
62
Eric Andersenbdfd0d72001-10-24 05:00:29 +000063#define CONFIG_FEATURE_COMMAND_EDITING
64#define CONFIG_FEATURE_COMMAND_TAB_COMPLETION
65#define CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
66#define CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
67#define CONFIG_FEATURE_CLEAN_UP
Eric Andersen5f2c79d2001-02-16 18:36:04 +000068
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000069#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000070
Eric Andersenbdfd0d72001-10-24 05:00:29 +000071#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5165fbe2001-02-20 06:42:29 +000072#include <dirent.h>
73#include <sys/stat.h>
74#endif
75
Eric Andersenbdfd0d72001-10-24 05:00:29 +000076#ifdef CONFIG_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000077
Eric Andersenbdfd0d72001-10-24 05:00:29 +000078#if defined(CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION) || defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
79#define CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +000080#endif
81
Eric Andersenbdfd0d72001-10-24 05:00:29 +000082#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Glenn L McGrath475820c2004-01-22 12:42:23 +000083#include "pwd_.h"
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000084#endif /* advanced FEATURES */
Eric Andersenaf4ac772001-02-01 22:43:49 +000085
86
Eric Andersen5f2c79d2001-02-16 18:36:04 +000087/* Maximum length of the linked list for the command line history */
Robert Griebl350d26b2002-12-03 22:45:46 +000088#ifndef CONFIG_FEATURE_COMMAND_HISTORY
89#define MAX_HISTORY 15
90#else
91#define MAX_HISTORY CONFIG_FEATURE_COMMAND_HISTORY
92#endif
93
Glenn L McGrath062c74f2002-11-27 09:29:49 +000094#if MAX_HISTORY < 1
95#warning cmdedit: You set MAX_HISTORY < 1. The history algorithm switched off.
96#else
97static char *history[MAX_HISTORY+1]; /* history + current */
98/* saved history lines */
99static int n_history;
100/* current pointer to history line */
101static int cur_history;
102#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +0000103
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000104#include <termios.h>
105#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
106#define getTermSettings(fd,argp) tcgetattr(fd, argp);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000107
108/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000109static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000110
111
Mark Whitley4e338752001-01-26 20:42:23 +0000112static
Eric Andersenc470f442003-07-28 09:56:35 +0000113volatile int cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +0000114static
Eric Andersenc470f442003-07-28 09:56:35 +0000115volatile int handlers_sets = 0; /* Set next bites: */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000116
Mark Whitley4e338752001-01-26 20:42:23 +0000117enum {
Eric Andersenc470f442003-07-28 09:56:35 +0000118 SET_ATEXIT = 1, /* when atexit() has been called
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000119 and get euid,uid,gid to fast compare */
Eric Andersen7467c8d2001-07-12 20:26:32 +0000120 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
121 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000122};
Erik Andersen13456d12000-03-16 08:09:57 +0000123
Mark Whitley4e338752001-01-26 20:42:23 +0000124
Eric Andersenc470f442003-07-28 09:56:35 +0000125static int cmdedit_x; /* real x terminal position */
126static int cmdedit_y; /* pseudoreal y terminal position */
127static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000128
Eric Andersenc470f442003-07-28 09:56:35 +0000129static int cursor; /* required global for signal handler */
130static int len; /* --- "" - - "" - -"- --""-- --""--- */
131static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000132static
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000133#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000134 const
135#endif
Eric Andersenc470f442003-07-28 09:56:35 +0000136char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000137
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000138#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000139static char *user_buf = "";
140static char *home_pwd_buf = "";
141static int my_euid;
142#endif
143
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000144#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000145static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000146static int num_ok_lines = 1;
147#endif
148
149
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000150#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000151
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000152#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000153static int my_euid;
154#endif
155
156static int my_uid;
157static int my_gid;
158
Eric Andersenc470f442003-07-28 09:56:35 +0000159#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
160
Mark Whitley4e338752001-01-26 20:42:23 +0000161static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000162
Mark Whitley4e338752001-01-26 20:42:23 +0000163static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000164{
Eric Andersenc470f442003-07-28 09:56:35 +0000165 static sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000166
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000167 /* emulate || signal call */
168 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Eric Andersen8efe9672003-09-15 08:33:45 +0000169 int width = 0;
170 get_terminal_width_height(0, &width, NULL);
171 cmdedit_setwidth(width, nsig == SIGWINCH);
Mark Whitley4e338752001-01-26 20:42:23 +0000172 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000173 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000174
Eric Andersenc470f442003-07-28 09:56:35 +0000175 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000176 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersenc470f442003-07-28 09:56:35 +0000177 else if (nsig == SIGWINCH) /* signaled called handler */
178 signal(SIGWINCH, win_changed); /* set for next call */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000179 else /* nsig == 0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000180 /* set previous handler */
Eric Andersenc470f442003-07-28 09:56:35 +0000181 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000182}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000183
184static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000185{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000186 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000187/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen70060d22004-03-27 10:02:48 +0000188 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000189 handlers_sets &= ~SET_RESET_TERM;
190 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000191 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000192 /* reset SIGWINCH handler to previous (default) */
193 win_changed(0);
194 handlers_sets &= ~SET_WCHG_HANDLERS;
195 }
196 fflush(stdout);
Erik Andersen13456d12000-03-16 08:09:57 +0000197}
198
Mark Whitley4e338752001-01-26 20:42:23 +0000199
Mark Whitley4e338752001-01-26 20:42:23 +0000200/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000201static void cmdedit_set_out_char(int next_char)
202{
203
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000204 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000205
206 if (c == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000207 c = ' '; /* destroy end char? */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000208#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000209 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000210 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000211 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000212 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000213 c += '@';
214 if (c == 127)
215 c = '?';
216 printf("\033[7m%c\033[0m", c);
217 } else
218#endif
219 putchar(c);
220 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000221 /* terminal is scrolled down */
222 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000223 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000224
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000225 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000226 next_char = ' ';
227 /* destroy "(auto)margin" */
228 putchar(next_char);
229 putchar('\b');
230 }
231 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000232}
233
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000234/* Move to end line. Bonus: rewrite line from cursor */
235static void input_end(void)
236{
237 while (cursor < len)
238 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000239}
240
241/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000242static void goto_new_line(void)
243{
Mark Whitley4e338752001-01-26 20:42:23 +0000244 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000245 if (cmdedit_x)
246 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000247}
248
249
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000250static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000251{
Robert Grieblb2301592002-07-30 23:13:51 +0000252 if ( s )
253 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000254}
Eric Andersen81fe1232003-07-29 06:38:40 +0000255
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000256static inline void beep(void)
257{
258 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000259}
260
Eric Andersenaff114c2004-04-14 17:51:38 +0000261/* Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000262/* special for slow terminal */
263static void input_backward(int num)
264{
265 if (num > cursor)
266 num = cursor;
Eric Andersenc470f442003-07-28 09:56:35 +0000267 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000268
Eric Andersenc470f442003-07-28 09:56:35 +0000269 if (cmdedit_x >= num) { /* no to up line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000270 cmdedit_x -= num;
271 if (num < 4)
272 while (num-- > 0)
273 putchar('\b');
274
275 else
276 printf("\033[%dD", num);
277 } else {
278 int count_y;
279
280 if (cmdedit_x) {
Eric Andersenc470f442003-07-28 09:56:35 +0000281 putchar('\r'); /* back to first terminal pos. */
282 num -= cmdedit_x; /* set previous backward */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000283 }
284 count_y = 1 + num / cmdedit_termw;
285 printf("\033[%dA", count_y);
286 cmdedit_y -= count_y;
287 /* require forward after uping */
288 cmdedit_x = cmdedit_termw * count_y - num;
Eric Andersenc470f442003-07-28 09:56:35 +0000289 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000290 }
Erik Andersen13456d12000-03-16 08:09:57 +0000291}
292
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000293static void put_prompt(void)
294{
295 out1str(cmdedit_prompt);
Eric Andersenc470f442003-07-28 09:56:35 +0000296 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000297 cursor = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000298 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000299}
300
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000301#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000302static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000303{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000304 cmdedit_prompt = prmt_ptr;
305 cmdedit_prmt_len = strlen(prmt_ptr);
306 put_prompt();
307}
308#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000309static void parse_prompt(const char *prmt_ptr)
310{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000311 int prmt_len = 0;
"Vladimir N. Oleynik"f0874802005-09-05 15:46:26 +0000312 size_t cur_prmt_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000313 char flg_not_length = '[';
314 char *prmt_mem_ptr = xcalloc(1, 1);
315 char *pwd_buf = xgetcwd(0);
316 char buf2[PATH_MAX + 1];
317 char buf[2];
318 char c;
319 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000320
Eric Andersen5f265b72001-05-11 16:58:46 +0000321 if (!pwd_buf) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000322 pwd_buf=(char *)bb_msg_unknown;
Eric Andersen5f265b72001-05-11 16:58:46 +0000323 }
324
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000325 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000326 pbuf = buf;
327 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000328 c = *prmt_ptr++;
329 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000330 const char *cp = prmt_ptr;
331 int l;
Eric Andersenc470f442003-07-28 09:56:35 +0000332
Manuel Novoa III cad53642003-03-19 09:13:01 +0000333 c = bb_process_escape_sequence(&prmt_ptr);
Eric Andersene5dfced2001-04-09 22:48:12 +0000334 if(prmt_ptr==cp) {
335 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000336 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000337 c = *prmt_ptr++;
338 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000339#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000340 case 'u':
341 pbuf = user_buf;
342 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000343#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000344 case 'h':
345 pbuf = hostname_buf;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000346 if (pbuf == 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000347 pbuf = xcalloc(256, 1);
348 if (gethostname(pbuf, 255) < 0) {
349 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000350 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000351 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000352
353 if (s)
354 *s = 0;
355 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000356 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000357 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000358 break;
359 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000360 c = my_euid == 0 ? '#' : '$';
361 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000362#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000363 case 'w':
364 pbuf = pwd_buf;
365 l = strlen(home_pwd_buf);
366 if (home_pwd_buf[0] != 0 &&
367 strncmp(home_pwd_buf, pbuf, l) == 0 &&
368 (pbuf[l]=='/' || pbuf[l]=='\0') &&
369 strlen(pwd_buf+l)<PATH_MAX) {
370 pbuf = buf2;
371 *pbuf = '~';
372 strcpy(pbuf+1, pwd_buf+l);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000373 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000374 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000375#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000376 case 'W':
377 pbuf = pwd_buf;
378 cp = strrchr(pbuf,'/');
379 if ( (cp != NULL) && (cp != pbuf) )
380 pbuf += (cp-pbuf)+1;
381 break;
382 case '!':
383 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
384 break;
385 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000386 c = '\033';
387 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000388 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000389 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000390 int h;
391 buf2[l++] = *prmt_ptr;
392 buf2[l] = 0;
393 h = strtol(buf2, &pbuf, 16);
394 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000395 l--;
396 break;
397 }
398 prmt_ptr++;
399 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000400 buf2[l] = 0;
401 c = (char)strtol(buf2, 0, 16);
402 if(c==0)
403 c = '?';
404 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000405 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000406 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000407 if (c == flg_not_length) {
408 flg_not_length = flg_not_length == '[' ? ']' : '[';
409 continue;
410 }
411 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000412 }
Eric Andersenc470f442003-07-28 09:56:35 +0000413 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000414 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000415 if(pbuf == buf)
416 *pbuf = c;
"Vladimir N. Oleynik"f0874802005-09-05 15:46:26 +0000417 cur_prmt_len = strlen(pbuf);
418 prmt_len += cur_prmt_len;
419 if (flg_not_length != ']')
420 cmdedit_prmt_len += cur_prmt_len;
Eric Andersene5dfced2001-04-09 22:48:12 +0000421 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000422 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000423 if(pwd_buf!=(char *)bb_msg_unknown)
Eric Andersen8f697842001-07-02 15:36:57 +0000424 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000425 cmdedit_prompt = prmt_mem_ptr;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000426 put_prompt();
427}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000428#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000429
430
Eric Andersenaff114c2004-04-14 17:51:38 +0000431/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000432static void redraw(int y, int back_cursor)
433{
Eric Andersenc470f442003-07-28 09:56:35 +0000434 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000435 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000436 putchar('\r');
437 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000438 input_end(); /* rewrite */
439 printf("\033[J"); /* destroy tail after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000440 input_backward(back_cursor);
441}
442
Paul Fox3f11b1b2005-08-04 19:04:46 +0000443#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000444#define DELBUFSIZ 128
445static char *delbuf; /* a (malloced) place to store deleted characters */
446static char *delp;
447static char 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) {
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000462 if (!delbuf)
463 delbuf = malloc(DELBUFSIZ);
464 /* safe if malloc fails */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000465 delp = delbuf;
466 newdelflag = 0;
467 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000468 if (delbuf && (delp - delbuf < DELBUFSIZ))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000469 *delp++ = command_ps[j];
470 }
471#endif
472
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000473 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000474 len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000475 input_end(); /* rewrite new line */
Eric Andersenc470f442003-07-28 09:56:35 +0000476 cmdedit_set_out_char(0); /* destroy end char */
477 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000478}
479
Paul Fox3f11b1b2005-08-04 19:04:46 +0000480#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
481static void put(void)
482{
483 int ocursor, j = delp - delbuf;
484 if (j == 0)
485 return;
486 ocursor = cursor;
487 /* open hole and then fill it */
488 memmove(command_ps + cursor + j, command_ps + cursor, len - cursor + 1);
489 strncpy(command_ps + cursor, delbuf, j);
490 len += j;
491 input_end(); /* rewrite new line */
492 input_backward(cursor-ocursor-j+1); /* at end of new text */
493}
494#endif
495
Mark Whitley4e338752001-01-26 20:42:23 +0000496/* Delete the char in back of the cursor */
497static void input_backspace(void)
498{
499 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000500 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000501 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000502 }
503}
504
505
Eric Andersenaff114c2004-04-14 17:51:38 +0000506/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000507static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000508{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000509 if (cursor < len)
510 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000511}
512
Mark Whitley4e338752001-01-26 20:42:23 +0000513static void cmdedit_setwidth(int w, int redraw_flg)
514{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000515 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000516 if (w <= cmdedit_termw) {
517 cmdedit_termw = cmdedit_termw % w;
518 }
Mark Whitley4e338752001-01-26 20:42:23 +0000519 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000520 cmdedit_termw = w;
521
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000522 if (redraw_flg) {
523 /* new y for current cursor */
524 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000525
526 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000527 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
528 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000529 }
Eric Andersenc470f442003-07-28 09:56:35 +0000530 }
Mark Whitley4e338752001-01-26 20:42:23 +0000531}
532
Eric Andersen7467c8d2001-07-12 20:26:32 +0000533static void cmdedit_init(void)
Mark Whitley4e338752001-01-26 20:42:23 +0000534{
Eric Andersen61173a52001-03-19 17:48:55 +0000535 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000536 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
537 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000538 win_changed(-SIGWINCH);
539 handlers_sets |= SET_WCHG_HANDLERS;
540 }
541
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000542 if ((handlers_sets & SET_ATEXIT) == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000543#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000544 struct passwd *entry;
545
546 my_euid = geteuid();
547 entry = getpwuid(my_euid);
548 if (entry) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000549 user_buf = bb_xstrdup(entry->pw_name);
550 home_pwd_buf = bb_xstrdup(entry->pw_dir);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000551 }
552#endif
553
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000554#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000555
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000556#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000557 my_euid = geteuid();
558#endif
559 my_uid = getuid();
560 my_gid = getgid();
Eric Andersenc470f442003-07-28 09:56:35 +0000561#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000562 handlers_sets |= SET_ATEXIT;
Eric Andersenc470f442003-07-28 09:56:35 +0000563 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000564 }
Mark Whitley4e338752001-01-26 20:42:23 +0000565}
Erik Andersenf0657d32000-04-12 17:49:52 +0000566
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000567#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000568
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000569static char **matches;
570static int num_matches;
571static char *add_char_to_match;
572
573static void add_match(char *matched, int add_char)
574{
575 int nm = num_matches;
576 int nm1 = nm + 1;
577
578 matches = xrealloc(matches, nm1 * sizeof(char *));
579 add_char_to_match = xrealloc(add_char_to_match, nm1);
580 matches[nm] = matched;
581 add_char_to_match[nm] = (char)add_char;
582 num_matches++;
583}
584
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000585static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000586{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000587 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
588 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
589 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
590 (st->st_mode & S_IXOTH)) return TRUE;
591 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000592}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000593
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000594#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000595
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000596static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000597{
598 struct passwd *entry;
599 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000600
Eric Andersenc470f442003-07-28 09:56:35 +0000601 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000602 userlen = strlen(ud);
603
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000604 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000605 char *sav_ud = ud - 1;
606 char *home = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000607 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000608
Eric Andersenc470f442003-07-28 09:56:35 +0000609 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000610 home = home_pwd_buf;
611 } else {
612 /* "~user/..." */
613 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000614 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000615 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000616 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000617 ud = temp;
618 if (entry)
619 home = entry->pw_dir;
620 }
621 if (home) {
622 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000623 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000624
625 /* /home/user/... */
626 sprintf(temp2, "%s%s", home, ud);
627 strcpy(sav_ud, temp2);
628 }
629 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000630 } else {
631 /* "~[^/]*" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000632 setpwent();
633
634 while ((entry = getpwent()) != NULL) {
635 /* Null usernames should result in all users as possible completions. */
636 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000637 add_match(bb_xasprintf("~%s", entry->pw_name), '/');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000638 }
639 }
640
641 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000642 }
643}
Eric Andersenc470f442003-07-28 09:56:35 +0000644#endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000645
646enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000647 FIND_EXE_ONLY = 0,
648 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000649 FIND_FILE_ONLY = 2,
650};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000651
Glenn L McGrath67285962004-01-14 09:34:51 +0000652#ifdef CONFIG_ASH
653const char *cmdedit_path_lookup;
654#else
655#define cmdedit_path_lookup getenv("PATH")
656#endif
657
Mark Whitley4e338752001-01-26 20:42:23 +0000658static int path_parse(char ***p, int flags)
659{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000660 int npth;
Glenn L McGrath67285962004-01-14 09:34:51 +0000661 const char *tmp;
662 const char *pth;
Mark Whitley4e338752001-01-26 20:42:23 +0000663
Mark Whitley4e338752001-01-26 20:42:23 +0000664 /* if not setenv PATH variable, to search cur dir "." */
Glenn L McGrath67285962004-01-14 09:34:51 +0000665 if (flags != FIND_EXE_ONLY || (pth = cmdedit_path_lookup) == 0 ||
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000666 /* PATH=<empty> or PATH=:<empty> */
667 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000668 return 1;
669 }
670
671 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000672 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000673
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000674 for (;;) {
Eric Andersenc470f442003-07-28 09:56:35 +0000675 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000676 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000677 if (tmp) {
678 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000679 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000680 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000681 break;
682 }
683
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000684 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000685
686 tmp = pth;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000687 (*p)[0] = bb_xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000688 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000689
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000690 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000691 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000692 if (tmp) {
Eric Andersenc470f442003-07-28 09:56:35 +0000693 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000694 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000695 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000696 } else
697 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000698 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000699 }
700
701 return npth;
702}
703
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000704static char *add_quote_for_spec_chars(char *found, int add)
Erik Andersen6273f652000-03-17 01:12:41 +0000705{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000706 int l = 0;
707 char *s = xmalloc((strlen(found) + 1) * 2);
708
709 while (*found) {
710 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
711 s[l++] = '\\';
712 s[l++] = *found++;
713 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000714 if(add)
715 s[l++] = (char)add;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000716 s[l] = 0;
717 return s;
718}
719
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000720static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000721{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000722 DIR *dir;
723 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000724 char dirbuf[BUFSIZ];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000725 struct stat st;
726 char *path1[1];
727 char **paths = path1;
728 int npaths;
729 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000730 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000731 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000732
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000733 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000734
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000735 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000736 /* no dir, if flags==EXE_ONLY - get paths, else "." */
737 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000738 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000739 } else {
740 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000741 /* save for change */
742 strcpy(dirbuf, command);
743 /* set dir only */
744 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000745#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000746 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000747 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000748#endif
749 /* "strip" dirname in command */
750 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000751
Mark Whitley4e338752001-01-26 20:42:23 +0000752 paths[0] = dirbuf;
Eric Andersenc470f442003-07-28 09:56:35 +0000753 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000754 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000755
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000756 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000757
Mark Whitley4e338752001-01-26 20:42:23 +0000758 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000759 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000760 continue;
761
762 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000763 char *str_found = next->d_name;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000764 int add_chr = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000765
Mark Whitley4e338752001-01-26 20:42:23 +0000766 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000767 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000768 continue;
769 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000770 if (*str_found == '.' && *pfind == 0) {
771 if (*paths[i] == '/' && paths[i][1] == 0
Eric Andersenc470f442003-07-28 09:56:35 +0000772 && str_found[1] == 0) str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000773 else
Mark Whitley4e338752001-01-26 20:42:23 +0000774 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000775 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000776 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000777 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000778 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000779 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000780 /* find with dirs ? */
781 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000782 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000783 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000784 /* name is directory */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000785 char *e = found + strlen(found) - 1;
786
787 add_chr = '/';
788 if(*e == '/')
789 *e = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +0000790 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000791 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000792 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000793 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000794 if (type == FIND_FILE_ONLY ||
Matt Kraai1f0c4362001-12-20 23:13:26 +0000795 (type == FIND_EXE_ONLY && is_execute(&st)))
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000796 add_chr = ' ';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000797 }
Mark Whitley4e338752001-01-26 20:42:23 +0000798 /* Add it to the list */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000799 add_match(found, add_chr);
800 continue;
Eric Andersene5dfced2001-04-09 22:48:12 +0000801cont:
802 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000803 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000804 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000805 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000806 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000807 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000808 free(paths);
809 }
Erik Andersen6273f652000-03-17 01:12:41 +0000810}
Erik Andersenf0657d32000-04-12 17:49:52 +0000811
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000812
813#define QUOT (UCHAR_MAX+1)
814
815#define collapse_pos(is, in) { \
Paul Fox574fee42005-07-19 20:41:06 +0000816 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
817 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000818
819static int find_match(char *matchBuf, int *len_with_quotes)
820{
821 int i, j;
822 int command_mode;
823 int c, c2;
824 int int_buf[BUFSIZ + 1];
825 int pos_buf[BUFSIZ + 1];
826
827 /* set to integer dimension characters and own positions */
828 for (i = 0;; i++) {
829 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
830 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000831 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000832 break;
833 } else
834 pos_buf[i] = i;
835 }
836
837 /* mask \+symbol and convert '\t' to ' ' */
838 for (i = j = 0; matchBuf[i]; i++, j++)
839 if (matchBuf[i] == '\\') {
840 collapse_pos(j, j + 1);
841 int_buf[j] |= QUOT;
842 i++;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000843#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000844 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000845 int_buf[j] = ' ' | QUOT;
846#endif
847 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000848#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000849 else if (matchBuf[i] == '\t')
850 int_buf[j] = ' ';
851#endif
852
853 /* mask "symbols" or 'symbols' */
854 c2 = 0;
855 for (i = 0; int_buf[i]; i++) {
856 c = int_buf[i];
857 if (c == '\'' || c == '"') {
858 if (c2 == 0)
859 c2 = c;
860 else {
861 if (c == c2)
862 c2 = 0;
863 else
864 int_buf[i] |= QUOT;
865 }
866 } else if (c2 != 0 && c != '$')
867 int_buf[i] |= QUOT;
868 }
869
870 /* skip commands with arguments if line have commands delimiters */
871 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
872 for (i = 0; int_buf[i]; i++) {
873 c = int_buf[i];
874 c2 = int_buf[i + 1];
875 j = i ? int_buf[i - 1] : -1;
876 command_mode = 0;
877 if (c == ';' || c == '&' || c == '|') {
878 command_mode = 1 + (c == c2);
879 if (c == '&') {
880 if (j == '>' || j == '<')
881 command_mode = 0;
882 } else if (c == '|' && j == '>')
883 command_mode = 0;
884 }
885 if (command_mode) {
886 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000887 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000888 }
889 }
890 /* collapse `command...` */
891 for (i = 0; int_buf[i]; i++)
892 if (int_buf[i] == '`') {
893 for (j = i + 1; int_buf[j]; j++)
894 if (int_buf[j] == '`') {
895 collapse_pos(i, j + 1);
896 j = 0;
897 break;
898 }
899 if (j) {
900 /* not found close ` - command mode, collapse all previous */
901 collapse_pos(0, i + 1);
902 break;
903 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000904 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000905 }
906
907 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000908 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000909 c2 = 0;
910 for (i = 0; int_buf[i]; i++)
911 if (int_buf[i] == '(' || int_buf[i] == '{') {
912 if (int_buf[i] == '(')
913 c++;
914 else
915 c2++;
916 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000917 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000918 }
919 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
920 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
921 if (int_buf[i] == ')')
922 c--;
923 else
924 c2--;
925 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000926 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000927 }
928
929 /* skip first not quote space */
930 for (i = 0; int_buf[i]; i++)
931 if (int_buf[i] != ' ')
932 break;
933 if (i)
934 collapse_pos(0, i);
935
936 /* set find mode for completion */
937 command_mode = FIND_EXE_ONLY;
938 for (i = 0; int_buf[i]; i++)
939 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
940 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000941 && matchBuf[pos_buf[0]]=='c'
942 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000943 command_mode = FIND_DIR_ONLY;
944 else {
945 command_mode = FIND_FILE_ONLY;
946 break;
947 }
948 }
949 /* "strlen" */
950 for (i = 0; int_buf[i]; i++);
951 /* find last word */
952 for (--i; i >= 0; i--) {
953 c = int_buf[i];
954 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
955 collapse_pos(0, i + 1);
956 break;
957 }
958 }
959 /* skip first not quoted '\'' or '"' */
960 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
961 /* collapse quote or unquote // or /~ */
Eric Andersenc470f442003-07-28 09:56:35 +0000962 while ((int_buf[i] & ~QUOT) == '/' &&
Mark Whitley7e5291f2001-03-08 19:31:12 +0000963 ((int_buf[i + 1] & ~QUOT) == '/'
964 || (int_buf[i + 1] & ~QUOT) == '~')) {
965 i++;
966 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000967
968 /* set only match and destroy quotes */
969 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000970 for (c = 0; pos_buf[i] >= 0; i++) {
971 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000972 j = pos_buf[i] + 1;
973 }
Eric Andersen4f990532001-05-31 17:15:57 +0000974 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000975 /* old lenght matchBuf with quotes symbols */
976 *len_with_quotes = j ? j - pos_buf[0] : 0;
977
978 return command_mode;
979}
980
Glenn L McGrath4d001292003-01-06 01:11:50 +0000981/*
982 display by column original ideas from ls applet,
983 very optimize by my :)
984*/
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000985static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000986{
987 int ncols, row;
988 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000989 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000990 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000991 char str_add_chr[2];
992 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000993
994 /* find the longest file name- use that as the column width */
995 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000996 l = strlen(matches[row]);
997 if(add_char_to_match[row])
998 l++;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000999 if (column_width < l)
1000 column_width = l;
1001 }
1002 column_width += 2; /* min space for columns */
1003 ncols = cmdedit_termw / column_width;
1004
1005 if (ncols > 1) {
1006 nrows /= ncols;
1007 if(nfiles % ncols)
1008 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001009 } else {
1010 ncols = 1;
1011 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001012 str_add_chr[1] = 0;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001013 for (row = 0; row < nrows; row++) {
1014 int n = row;
1015 int nc;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001016 int acol;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001017
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001018 for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
1019 str_add_chr[0] = add_char_to_match[n];
1020 acol = str_add_chr[0] ? column_width - 1 : column_width;
1021 printf("%s%s", matches[n], str_add_chr);
1022 l = strlen(matches[n]);
1023 while(l < acol) {
1024 putchar(' ');
1025 l++;
1026 }
1027 }
1028 str_add_chr[0] = add_char_to_match[n];
1029 printf("%s%s\n", matches[n], str_add_chr);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001030 }
1031}
1032
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001033
1034static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001035{
1036 /* Do TAB completion */
Eric Andersenc470f442003-07-28 09:56:35 +00001037 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001038 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001039 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001040 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001041 free(matches);
1042 matches = (char **) NULL;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001043 free(add_char_to_match);
1044 add_char_to_match = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +00001045 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001046 return;
1047 }
Matt Kraai1f0c4362001-12-20 23:13:26 +00001048 if (! *lastWasTab) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001049
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001050 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001051 int len_found;
1052 char matchBuf[BUFSIZ];
1053 int find_type;
1054 int recalc_pos;
1055
Eric Andersenc470f442003-07-28 09:56:35 +00001056 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001057
1058 /* Make a local copy of the string -- up
1059 * to the position of the cursor */
1060 tmp = strncpy(matchBuf, command_ps, cursor);
1061 tmp[cursor] = 0;
1062
1063 find_type = find_match(matchBuf, &recalc_pos);
1064
1065 /* Free up any memory already allocated */
1066 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001067
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001068#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001069 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001070 * then try completing this word as a username. */
1071
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001072 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001073 username_tab_completion(matchBuf, NULL);
1074 if (!matches)
Mark Whitley4e338752001-01-26 20:42:23 +00001075#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001076 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001077 * in the current working directory that matches. */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001078 exe_n_cwd_tab_completion(matchBuf, find_type);
1079 /* Remove duplicate found and sort */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001080 if(matches) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001081 int i, j, n, srt;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001082 /* bubble */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001083 n = num_matches;
1084 for(i=0; i<(n-1); i++)
1085 for(j=i+1; j<n; j++)
1086 if(matches[i]!=NULL && matches[j]!=NULL) {
1087 srt = strcmp(matches[i], matches[j]);
1088 if(srt == 0) {
1089 free(matches[j]);
1090 matches[j]=0;
1091 } else if(srt > 0) {
1092 tmp1 = matches[i];
1093 matches[i] = matches[j];
1094 matches[j] = tmp1;
1095 srt = add_char_to_match[i];
1096 add_char_to_match[i] = add_char_to_match[j];
1097 add_char_to_match[j] = srt;
1098 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001099 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001100 j = n;
1101 n = 0;
1102 for(i=0; i<j; i++)
1103 if(matches[i]) {
1104 matches[n]=matches[i];
1105 add_char_to_match[n]=add_char_to_match[i];
1106 n++;
1107 }
1108 num_matches = n;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001109 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001110 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001111 if (!matches || num_matches > 1) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001112
Mark Whitley4e338752001-01-26 20:42:23 +00001113 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001114 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +00001115 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001116 /* find minimal match */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001117 tmp1 = bb_xstrdup(matches[0]);
1118 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001119 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001120 if (matches[len_found][(tmp - tmp1)] != *tmp) {
1121 *tmp = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001122 break;
1123 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001124 if (*tmp1 == 0) { /* have unique */
1125 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001126 return;
1127 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001128 tmp = add_quote_for_spec_chars(tmp1, 0);
1129 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +00001130 } else { /* one match */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001131 tmp = add_quote_for_spec_chars(matches[0], add_char_to_match[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001132 /* for next completion current found */
1133 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001134 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001135 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001136 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001137 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001138
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001139 /* before word for match */
1140 command_ps[cursor - recalc_pos] = 0;
1141 /* save tail line */
1142 strcpy(matchBuf, command_ps + cursor);
1143 /* add match */
1144 strcat(command_ps, tmp);
1145 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001146 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001147 /* back to begin word for match */
1148 input_backward(recalc_pos);
1149 /* new pos */
1150 recalc_pos = cursor + len_found;
1151 /* new len */
1152 len = strlen(command_ps);
1153 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001154 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001155 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001156 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001157 } else {
1158 /* Ok -- the last char was a TAB. Since they
1159 * just hit TAB again, print a list of all the
1160 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001161 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001162 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001163
1164 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001165 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001166 showfiles();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001167 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001168 }
1169 }
1170}
Eric Andersenc470f442003-07-28 09:56:35 +00001171#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001172
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001173#if MAX_HISTORY >= 1
1174static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001175{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001176 if(command_ps[0] != 0 || history[cur_history] == 0) {
1177 free(history[cur_history]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001178 history[cur_history] = bb_xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001179 }
1180 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +00001181}
1182
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001183static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001184{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001185 int ch = cur_history;
1186
1187 if (ch < n_history) {
1188 get_previous_history(); /* save the current history line */
1189 return (cur_history = ch+1);
1190 } else {
1191 beep();
1192 return 0;
1193 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001194}
Robert Griebl350d26b2002-12-03 22:45:46 +00001195
Robert Griebl350d26b2002-12-03 22:45:46 +00001196#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
Rob Landleydfba7412006-03-06 20:47:33 +00001197void load_history ( const char *fromfile )
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001198{
Robert Griebl350d26b2002-12-03 22:45:46 +00001199 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001200 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001201
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001202 /* cleanup old */
1203
1204 for(hi = n_history; hi > 0; ) {
1205 hi--;
1206 free ( history [hi] );
Robert Griebl350d26b2002-12-03 22:45:46 +00001207 }
1208
1209 if (( fp = fopen ( fromfile, "r" ))) {
Eric Andersenc470f442003-07-28 09:56:35 +00001210
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001211 for ( hi = 0; hi < MAX_HISTORY; ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001212 char * hl = bb_get_chomped_line_from_file(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001213 int l;
1214
1215 if(!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001216 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001217 l = strlen(hl);
1218 if(l >= BUFSIZ)
1219 hl[BUFSIZ-1] = 0;
1220 if(l == 0 || hl[0] == ' ') {
1221 free(hl);
1222 continue;
1223 }
1224 history [hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001225 }
1226 fclose ( fp );
1227 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001228 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001229}
1230
Rob Landleydfba7412006-03-06 20:47:33 +00001231void save_history ( const char *tofile )
Robert Griebl350d26b2002-12-03 22:45:46 +00001232{
Robert Griebl350d26b2002-12-03 22:45:46 +00001233 FILE *fp = fopen ( tofile, "w" );
Eric Andersenc470f442003-07-28 09:56:35 +00001234
Robert Griebl350d26b2002-12-03 22:45:46 +00001235 if ( fp ) {
1236 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001237
Robert Griebl350d26b2002-12-03 22:45:46 +00001238 for ( i = 0; i < n_history; i++ ) {
Eric Andersen27bb7902003-12-23 20:24:51 +00001239 fprintf(fp, "%s\n", history [i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001240 }
1241 fclose ( fp );
1242 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001243}
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001244#endif
Robert Griebl350d26b2002-12-03 22:45:46 +00001245
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001246#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001247
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001248enum {
1249 ESC = 27,
1250 DEL = 127,
1251};
1252
1253
Erik Andersen6273f652000-03-17 01:12:41 +00001254/*
1255 * This function is used to grab a character buffer
1256 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001257 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001258 * a mini readline).
1259 *
1260 * The following standard commands are not implemented:
1261 * ESC-b -- Move back one word
1262 * ESC-f -- Move forward one word
1263 * ESC-d -- Delete back one word
1264 * ESC-h -- Delete forward one word
1265 * CTL-t -- Transpose two characters
1266 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001267 * Minimalist vi-style command line editing available if configured.
1268 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001269 *
Erik Andersen6273f652000-03-17 01:12:41 +00001270 */
Eric Andersenc470f442003-07-28 09:56:35 +00001271
Paul Fox3f11b1b2005-08-04 19:04:46 +00001272#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
1273static int vi_mode;
1274
1275void setvimode ( int viflag )
1276{
1277 vi_mode = viflag;
1278}
1279
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001280static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001281vi_Word_motion(char *command, int eat)
1282{
1283 while (cursor < len && !isspace(command[cursor]))
1284 input_forward();
1285 if (eat) while (cursor < len && isspace(command[cursor]))
1286 input_forward();
1287}
1288
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001289static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001290vi_word_motion(char *command, int eat)
1291{
1292 if (isalnum(command[cursor]) || command[cursor] == '_') {
1293 while (cursor < len &&
1294 (isalnum(command[cursor+1]) ||
1295 command[cursor+1] == '_'))
1296 input_forward();
1297 } else if (ispunct(command[cursor])) {
1298 while (cursor < len &&
1299 (ispunct(command[cursor+1])))
1300 input_forward();
1301 }
1302
1303 if (cursor < len)
1304 input_forward();
1305
1306 if (eat && cursor < len && isspace(command[cursor]))
1307 while (cursor < len && isspace(command[cursor]))
1308 input_forward();
1309}
1310
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001311static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001312vi_End_motion(char *command)
1313{
1314 input_forward();
1315 while (cursor < len && isspace(command[cursor]))
1316 input_forward();
1317 while (cursor < len-1 && !isspace(command[cursor+1]))
1318 input_forward();
1319}
1320
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001321static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001322vi_end_motion(char *command)
1323{
1324 if (cursor >= len-1)
1325 return;
1326 input_forward();
1327 while (cursor < len-1 && isspace(command[cursor]))
1328 input_forward();
1329 if (cursor >= len-1)
1330 return;
1331 if (isalnum(command[cursor]) || command[cursor] == '_') {
1332 while (cursor < len-1 &&
1333 (isalnum(command[cursor+1]) ||
1334 command[cursor+1] == '_'))
1335 input_forward();
1336 } else if (ispunct(command[cursor])) {
1337 while (cursor < len-1 &&
1338 (ispunct(command[cursor+1])))
1339 input_forward();
1340 }
1341}
1342
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001343static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001344vi_Back_motion(char *command)
1345{
1346 while (cursor > 0 && isspace(command[cursor-1]))
1347 input_backward(1);
1348 while (cursor > 0 && !isspace(command[cursor-1]))
1349 input_backward(1);
1350}
1351
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001352static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001353vi_back_motion(char *command)
1354{
1355 if (cursor <= 0)
1356 return;
1357 input_backward(1);
1358 while (cursor > 0 && isspace(command[cursor]))
1359 input_backward(1);
1360 if (cursor <= 0)
1361 return;
1362 if (isalnum(command[cursor]) || command[cursor] == '_') {
1363 while (cursor > 0 &&
1364 (isalnum(command[cursor-1]) ||
1365 command[cursor-1] == '_'))
1366 input_backward(1);
1367 } else if (ispunct(command[cursor])) {
1368 while (cursor > 0 &&
1369 (ispunct(command[cursor-1])))
1370 input_backward(1);
1371 }
1372}
1373#endif
1374
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001375/*
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001376 * the emacs and vi modes share much of the code in the big
1377 * command loop. commands entered when in vi's command mode (aka
1378 * "escape mode") get an extra bit added to distinguish them --
1379 * this keeps them from being self-inserted. this clutters the
1380 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001381 */
1382
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001383#define vbit 0x100
1384
1385/* leave out the "vi-mode"-only case labels if vi editing isn't
1386 * configured. */
1387#define vi_case(caselabel) USE_FEATURE_COMMAND_EDITING(caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001388
1389/* convert uppercase ascii to equivalent control char, for readability */
1390#define CNTRL(uc_char) ((uc_char) - 0x40)
1391
Eric Andersen044228d2001-07-17 01:12:36 +00001392
1393int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001394{
1395
Erik Andersenc7c634b2000-03-19 05:28:55 +00001396 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001397 int lastWasTab = FALSE;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001398 unsigned char c;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001399 unsigned int ic;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001400#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001401 unsigned int prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001402 int vi_cmdmode = 0;
1403#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001404 /* prepare before init handlers */
Eric Andersenc470f442003-07-28 09:56:35 +00001405 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001406 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001407 command_ps = command;
1408
Eric Andersen34506362001-08-02 05:02:46 +00001409 getTermSettings(0, (void *) &initial_settings);
1410 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1411 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1412 /* Turn off echoing and CTRL-C, so we can trap it */
1413 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Eric Andersen34506362001-08-02 05:02:46 +00001414 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1415 new_settings.c_cc[VMIN] = 1;
1416 new_settings.c_cc[VTIME] = 0;
1417 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001418# ifndef _POSIX_VDISABLE
1419# define _POSIX_VDISABLE '\0'
1420# endif
Eric Andersenc470f442003-07-28 09:56:35 +00001421 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001422 command[0] = 0;
1423
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001424 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001425 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001426
Eric Andersen6faae7d2001-02-16 20:09:17 +00001427 /* Now initialize things */
1428 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001429 /* Print out the command prompt */
1430 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001431
Erik Andersenc7c634b2000-03-19 05:28:55 +00001432 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001433
Eric Andersenc470f442003-07-28 09:56:35 +00001434 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001435
Eric Andersen7467c8d2001-07-12 20:26:32 +00001436 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001437 /* if we can't read input then exit */
1438 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001439
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001440 ic = c;
1441
Paul Fox3f11b1b2005-08-04 19:04:46 +00001442#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
1443 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001444 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001445 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001446#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001447 switch (ic)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001448 {
Erik Andersenf0657d32000-04-12 17:49:52 +00001449 case '\n':
1450 case '\r':
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001451 vi_case( case '\n'|vbit: )
1452 vi_case( case '\r'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001453 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001454 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001455 break_out = 1;
1456 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001457 case CNTRL('A'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001458 vi_case( case '0'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001459 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001460 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001461 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001462 case CNTRL('B'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001463 vi_case( case 'h'|vbit: )
1464 vi_case( case '\b'|vbit: )
1465 vi_case( case DEL|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001466 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001467 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001468 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001469 case CNTRL('C'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001470 vi_case( case CNTRL('C')|vbit: )
Eric Andersen86349772000-12-18 20:25:50 +00001471 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001472 goto_new_line();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001473#ifndef CONFIG_ASH
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001474 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001475 len = 0;
1476 lastWasTab = FALSE;
1477 put_prompt();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001478#else
1479 len = 0;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001480 break_out = -1; /* to control traps */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001481#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001482 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001483 case CNTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001484 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001485 * if the len=0 and no chars to delete */
1486 if (len == 0) {
Eric Andersencb01bb12004-08-19 18:22:13 +00001487 errno = 0;
Eric Andersened424db2001-04-23 15:28:28 +00001488prepare_to_die:
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001489#if !defined(CONFIG_ASH)
Mark Whitley4e338752001-01-26 20:42:23 +00001490 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001491 goto_new_line();
1492 /* cmdedit_reset_term() called in atexit */
1493 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001494#else
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001495 /* to control stopped jobs */
1496 len = break_out = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001497 break;
1498#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001499 } else {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001500 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001501 }
1502 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001503 case CNTRL('E'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001504 vi_case( case '$'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001505 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001506 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001507 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001508 case CNTRL('F'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001509 vi_case( case 'l'|vbit: )
1510 vi_case( case ' '|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001511 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001512 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001513 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001514 case '\b':
1515 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001516 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001517 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001518 break;
1519 case '\t':
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001520#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001521 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001522#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001523 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001524 case CNTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001525 /* Control-k -- clear to end of line */
Eric Andersen65a07302002-04-13 13:26:49 +00001526 *(command + cursor) = 0;
1527 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001528 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001529 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001530 case CNTRL('L'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001531 vi_case( case CNTRL('L')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001532 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001533 printf("\033[H");
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001534 redraw(0, len-cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001535 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001536#if MAX_HISTORY >= 1
Paul Fox3f11b1b2005-08-04 19:04:46 +00001537 case CNTRL('N'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001538 vi_case( case CNTRL('N')|vbit: )
1539 vi_case( case 'j'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001540 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001541 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001542 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001543 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001544 case CNTRL('P'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001545 vi_case( case CNTRL('P')|vbit: )
1546 vi_case( case 'k'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001547 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001548 if (cur_history > 0) {
1549 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001550 goto rewrite_line;
1551 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001552 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001553 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001554 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001555#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001556 case CNTRL('U'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001557 vi_case( case CNTRL('U')|vbit: )
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001558 /* Control-U -- Clear line before cursor */
1559 if (cursor) {
1560 strcpy(command, command + cursor);
1561 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001562 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001563 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001564 case CNTRL('W'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001565 vi_case( case CNTRL('W')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001566 /* Control-W -- Remove the last word */
1567 while (cursor > 0 && isspace(command[cursor-1]))
1568 input_backspace();
1569 while (cursor > 0 &&!isspace(command[cursor-1]))
1570 input_backspace();
1571 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001572#if CONFIG_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001573 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001574 vi_cmdmode = 0;
1575 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001576 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001577 input_backward(cursor);
1578 vi_cmdmode = 0;
1579 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001580 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001581 input_forward();
1582 vi_cmdmode = 0;
1583 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001584 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001585 input_end();
1586 vi_cmdmode = 0;
1587 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001588 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001589 input_delete(1);
1590 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001591 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001592 if (cursor > 0) {
1593 input_backward(1);
1594 input_delete(1);
1595 }
1596 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001597 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001598 vi_Word_motion(command, 1);
1599 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001600 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001601 vi_word_motion(command, 1);
1602 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001603 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001604 vi_End_motion(command);
1605 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001606 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001607 vi_end_motion(command);
1608 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001609 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001610 vi_Back_motion(command);
1611 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001612 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001613 vi_back_motion(command);
1614 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001615 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001616 vi_cmdmode = 0;
1617 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001618 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001619 goto clear_to_eol;
1620
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001621 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001622 vi_cmdmode = 0;
1623 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001624 case 'd'|vbit:
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001625 {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001626 int nc, sc;
1627 sc = cursor;
1628 prevc = ic;
1629 if (safe_read(0, &c, 1) < 1)
1630 goto prepare_to_die;
1631 if (c == (prevc & 0xff)) {
1632 /* "cc", "dd" */
1633 input_backward(cursor);
1634 goto clear_to_eol;
1635 break;
1636 }
1637 switch(c) {
1638 case 'w':
1639 case 'W':
1640 case 'e':
1641 case 'E':
1642 switch (c) {
1643 case 'w': /* "dw", "cw" */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001644 vi_word_motion(command, vi_cmdmode);
Paul Fox3f11b1b2005-08-04 19:04:46 +00001645 break;
1646 case 'W': /* 'dW', 'cW' */
1647 vi_Word_motion(command, vi_cmdmode);
1648 break;
1649 case 'e': /* 'de', 'ce' */
1650 vi_end_motion(command);
1651 input_forward();
1652 break;
1653 case 'E': /* 'dE', 'cE' */
1654 vi_End_motion(command);
1655 input_forward();
1656 break;
1657 }
1658 nc = cursor;
1659 input_backward(cursor - sc);
1660 while (nc-- > cursor)
1661 input_delete(1);
1662 break;
1663 case 'b': /* "db", "cb" */
1664 case 'B': /* implemented as B */
1665 if (c == 'b')
1666 vi_back_motion(command);
1667 else
1668 vi_Back_motion(command);
1669 while (sc-- > cursor)
1670 input_delete(1);
1671 break;
1672 case ' ': /* "d ", "c " */
1673 input_delete(1);
1674 break;
1675 case '$': /* "d$", "c$" */
1676 clear_to_eol:
1677 while (cursor < len)
1678 input_delete(1);
1679 break;
1680 }
1681 }
1682 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001683 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001684 input_forward();
1685 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001686 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001687 put();
1688 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001689 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001690 if (safe_read(0, &c, 1) < 1)
1691 goto prepare_to_die;
1692 if (c == 0)
1693 beep();
1694 else {
1695 *(command + cursor) = c;
1696 putchar(c);
1697 putchar('\b');
1698 }
1699 break;
1700#endif /* CONFIG_FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001701
1702 case ESC:
1703
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 Fox0f2dd9f2006-03-07 20:26:11 +00001717 vi_case( case '['|vbit: )
1718 vi_case( case 'O'|vbit: )
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;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001790
Eric Andersenc470f442003-07-28 09:56:35 +00001791 default: /* If it's regular input, do the normal thing */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001792#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001793 /* Control-V -- Add non-printable symbol */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001794 if (c == CNTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001795 if (safe_read(0, &c, 1) < 1)
1796 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001797 if (c == 0) {
1798 beep();
1799 break;
1800 }
1801 } else
1802#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001803 {
1804#if CONFIG_FEATURE_COMMAND_EDITING_VI
1805 if (vi_cmdmode) /* don't self-insert */
1806 break;
1807#endif
1808 if (!Isprint(c)) /* Skip non-printable characters */
1809 break;
1810 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001811
Eric Andersenc470f442003-07-28 09:56:35 +00001812 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001813 break;
1814
1815 len++;
1816
Eric Andersenc470f442003-07-28 09:56:35 +00001817 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001818 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001819 *(command + cursor + 1) = 0;
1820 cmdedit_set_out_char(0);
Eric Andersenc470f442003-07-28 09:56:35 +00001821 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001822 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001823
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001824 memmove(command + sc + 1, command + sc, len - sc);
1825 *(command + sc) = c;
1826 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001827 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001828 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001829 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001830 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001831 }
1832
Erik Andersenc7c634b2000-03-19 05:28:55 +00001833 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001834 }
Eric Andersenc470f442003-07-28 09:56:35 +00001835 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001836 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001837
1838 if (c != '\t')
1839 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001840 }
1841
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001842 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001843 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001844
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001845#if MAX_HISTORY >= 1
Erik Andersenc7c634b2000-03-19 05:28:55 +00001846 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001847 /* cleanup may be saved current command line */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001848 if (len> 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001849 int i = n_history;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001850
1851 free(history[MAX_HISTORY]);
1852 history[MAX_HISTORY] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001853 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001854 if (i >= MAX_HISTORY) {
1855 free(history[0]);
1856 for(i = 0; i < (MAX_HISTORY-1); i++)
1857 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001858 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001859 history[i++] = bb_xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001860 cur_history = i;
1861 n_history = i;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001862#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001863 num_ok_lines++;
1864#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001865 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001866#else /* MAX_HISTORY < 1 */
1867#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001868 if (len > 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001869 num_ok_lines++;
1870 }
1871#endif
1872#endif /* MAX_HISTORY >= 1 */
Eric Andersen27bb7902003-12-23 20:24:51 +00001873 if (break_out > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001874 command[len++] = '\n'; /* set '\n' */
1875 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001876 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001877#if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersenc470f442003-07-28 09:56:35 +00001878 input_tab(0); /* strong free */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001879#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001880#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001881 free(cmdedit_prompt);
1882#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001883 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001884 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001885}
1886
Eric Andersen7467c8d2001-07-12 20:26:32 +00001887
1888
Eric Andersenc470f442003-07-28 09:56:35 +00001889#endif /* CONFIG_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001890
1891
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001892#ifdef TEST
1893
Manuel Novoa III cad53642003-03-19 09:13:01 +00001894const char *bb_applet_name = "debug stuff usage";
Eric Andersene5dfced2001-04-09 22:48:12 +00001895
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001896#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001897#include <locale.h>
1898#endif
1899
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001900int main(int argc, char **argv)
1901{
1902 char buff[BUFSIZ];
1903 char *prompt =
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001904#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001905 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001906\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001907\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001908#else
1909 "% ";
1910#endif
1911
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001912#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001913 setlocale(LC_ALL, "");
1914#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001915 while(1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001916 int l;
Eric Andersen27bb7902003-12-23 20:24:51 +00001917 l = cmdedit_read_input(prompt, buff);
1918 if(l > 0 && buff[l-1] == '\n') {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001919 buff[l-1] = 0;
Eric Andersen27bb7902003-12-23 20:24:51 +00001920 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1921 } else {
1922 break;
1923 }
Eric Andersen7467c8d2001-07-12 20:26:32 +00001924 }
Eric Andersen27bb7902003-12-23 20:24:51 +00001925 printf("*** cmdedit_read_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001926 return 0;
1927}
1928
Eric Andersenc470f442003-07-28 09:56:35 +00001929#endif /* TEST */