blob: 884489b4541fc8760954d0b39d74decee89adc5b [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 Andersen5f2c79d2001-02-16 18:36:04 +00003 * Termios command line History and Editting.
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
Eric Andersenbdfd0d72001-10-24 05:00:29 +000046#ifdef CONFIG_LOCALE_SUPPORT
Eric Andersene5dfced2001-04-09 22:48:12 +000047#define Isprint(c) isprint((c))
48#else
49#define Isprint(c) ( (c) >= ' ' && (c) != ((unsigned char)'\233') )
50#endif
51
Glenn L McGrath3b251852004-01-03 12:07:32 +000052#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000053
Eric Andersen27bb7902003-12-23 20:24:51 +000054/* pretect redefined for test */
55#undef CONFIG_FEATURE_COMMAND_EDITING
56#undef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
57#undef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
58#undef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
59#undef CONFIG_FEATURE_CLEAN_UP
60
Eric Andersenbdfd0d72001-10-24 05:00:29 +000061#define CONFIG_FEATURE_COMMAND_EDITING
62#define CONFIG_FEATURE_COMMAND_TAB_COMPLETION
63#define CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
64#define CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
65#define CONFIG_FEATURE_CLEAN_UP
Eric Andersen5f2c79d2001-02-16 18:36:04 +000066
Eric Andersenc470f442003-07-28 09:56:35 +000067#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000068
Eric Andersenbdfd0d72001-10-24 05:00:29 +000069#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5165fbe2001-02-20 06:42:29 +000070#include <dirent.h>
71#include <sys/stat.h>
72#endif
73
Eric Andersenbdfd0d72001-10-24 05:00:29 +000074#ifdef CONFIG_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000075
Eric Andersenbdfd0d72001-10-24 05:00:29 +000076#if defined(CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION) || defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
77#define CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +000078#endif
79
Eric Andersenbdfd0d72001-10-24 05:00:29 +000080#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Glenn L McGrath78b0e372001-06-26 02:06:08 +000081# ifndef TEST
Eric Andersen887ca792002-07-03 23:19:26 +000082# include "pwd_.h"
Glenn L McGrath78b0e372001-06-26 02:06:08 +000083# else
84# include <pwd.h>
85# endif /* TEST */
Eric Andersenc470f442003-07-28 09:56:35 +000086#endif /* advanced FEATURES */
Eric Andersenaf4ac772001-02-01 22:43:49 +000087
88
Eric Andersen5f2c79d2001-02-16 18:36:04 +000089/* Maximum length of the linked list for the command line history */
Robert Griebl350d26b2002-12-03 22:45:46 +000090#ifndef CONFIG_FEATURE_COMMAND_HISTORY
91#define MAX_HISTORY 15
92#else
93#define MAX_HISTORY CONFIG_FEATURE_COMMAND_HISTORY
94#endif
95
Glenn L McGrath062c74f2002-11-27 09:29:49 +000096#if MAX_HISTORY < 1
97#warning cmdedit: You set MAX_HISTORY < 1. The history algorithm switched off.
98#else
99static char *history[MAX_HISTORY+1]; /* history + current */
100/* saved history lines */
101static int n_history;
102/* current pointer to history line */
103static int cur_history;
104#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +0000105
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000106#include <termios.h>
107#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
108#define getTermSettings(fd,argp) tcgetattr(fd, argp);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000109
110/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000111static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000112
113
Mark Whitley4e338752001-01-26 20:42:23 +0000114static
Eric Andersenc470f442003-07-28 09:56:35 +0000115volatile int cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +0000116static
Eric Andersenc470f442003-07-28 09:56:35 +0000117volatile int handlers_sets = 0; /* Set next bites: */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000118
Mark Whitley4e338752001-01-26 20:42:23 +0000119enum {
Eric Andersenc470f442003-07-28 09:56:35 +0000120 SET_ATEXIT = 1, /* when atexit() has been called
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000121 and get euid,uid,gid to fast compare */
Eric Andersen7467c8d2001-07-12 20:26:32 +0000122 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
123 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000124};
Erik Andersen13456d12000-03-16 08:09:57 +0000125
Mark Whitley4e338752001-01-26 20:42:23 +0000126
Eric Andersenc470f442003-07-28 09:56:35 +0000127static int cmdedit_x; /* real x terminal position */
128static int cmdedit_y; /* pseudoreal y terminal position */
129static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000130
Eric Andersenc470f442003-07-28 09:56:35 +0000131static int cursor; /* required global for signal handler */
132static int len; /* --- "" - - "" - -"- --""-- --""--- */
133static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000134static
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000135#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000136 const
137#endif
Eric Andersenc470f442003-07-28 09:56:35 +0000138char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000139
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000140#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000141static char *user_buf = "";
142static char *home_pwd_buf = "";
143static int my_euid;
144#endif
145
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000146#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000147static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000148static int num_ok_lines = 1;
149#endif
150
151
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000152#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000153
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000154#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000155static int my_euid;
156#endif
157
158static int my_uid;
159static int my_gid;
160
Eric Andersenc470f442003-07-28 09:56:35 +0000161#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
162
Mark Whitley4e338752001-01-26 20:42:23 +0000163static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000164
Mark Whitley4e338752001-01-26 20:42:23 +0000165static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000166{
Eric Andersenc470f442003-07-28 09:56:35 +0000167 static sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000168
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000169 /* emulate || signal call */
170 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Eric Andersen8efe9672003-09-15 08:33:45 +0000171 int width = 0;
172 get_terminal_width_height(0, &width, NULL);
173 cmdedit_setwidth(width, nsig == SIGWINCH);
Mark Whitley4e338752001-01-26 20:42:23 +0000174 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000175 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000176
Eric Andersenc470f442003-07-28 09:56:35 +0000177 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000178 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersenc470f442003-07-28 09:56:35 +0000179 else if (nsig == SIGWINCH) /* signaled called handler */
180 signal(SIGWINCH, win_changed); /* set for next call */
181 else /* nsig == 0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000182 /* set previous handler */
Eric Andersenc470f442003-07-28 09:56:35 +0000183 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000184}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000185
186static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000187{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000188 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000189/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000190 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000191 handlers_sets &= ~SET_RESET_TERM;
192 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000193 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000194 /* reset SIGWINCH handler to previous (default) */
195 win_changed(0);
196 handlers_sets &= ~SET_WCHG_HANDLERS;
197 }
198 fflush(stdout);
Erik Andersen13456d12000-03-16 08:09:57 +0000199}
200
Mark Whitley4e338752001-01-26 20:42:23 +0000201
Mark Whitley4e338752001-01-26 20:42:23 +0000202/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000203static void cmdedit_set_out_char(int next_char)
204{
205
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000206 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000207
208 if (c == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000209 c = ' '; /* destroy end char? */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000210#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000211 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000212 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000213 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000214 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000215 c += '@';
216 if (c == 127)
217 c = '?';
218 printf("\033[7m%c\033[0m", c);
219 } else
220#endif
221 putchar(c);
222 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000223 /* terminal is scrolled down */
224 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000225 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000226
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000227 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000228 next_char = ' ';
229 /* destroy "(auto)margin" */
230 putchar(next_char);
231 putchar('\b');
232 }
233 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000234}
235
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000236/* Move to end line. Bonus: rewrite line from cursor */
237static void input_end(void)
238{
239 while (cursor < len)
240 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000241}
242
243/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000244static void goto_new_line(void)
245{
Mark Whitley4e338752001-01-26 20:42:23 +0000246 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000247 if (cmdedit_x)
248 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000249}
250
251
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000252static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000253{
Robert Grieblb2301592002-07-30 23:13:51 +0000254 if ( s )
255 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000256}
Eric Andersen81fe1232003-07-29 06:38:40 +0000257
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000258static inline void beep(void)
259{
260 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000261}
262
Mark Whitley4e338752001-01-26 20:42:23 +0000263/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000264/* special for slow terminal */
265static void input_backward(int num)
266{
267 if (num > cursor)
268 num = cursor;
Eric Andersenc470f442003-07-28 09:56:35 +0000269 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000270
Eric Andersenc470f442003-07-28 09:56:35 +0000271 if (cmdedit_x >= num) { /* no to up line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000272 cmdedit_x -= num;
273 if (num < 4)
274 while (num-- > 0)
275 putchar('\b');
276
277 else
278 printf("\033[%dD", num);
279 } else {
280 int count_y;
281
282 if (cmdedit_x) {
Eric Andersenc470f442003-07-28 09:56:35 +0000283 putchar('\r'); /* back to first terminal pos. */
284 num -= cmdedit_x; /* set previous backward */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000285 }
286 count_y = 1 + num / cmdedit_termw;
287 printf("\033[%dA", count_y);
288 cmdedit_y -= count_y;
289 /* require forward after uping */
290 cmdedit_x = cmdedit_termw * count_y - num;
Eric Andersenc470f442003-07-28 09:56:35 +0000291 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000292 }
Erik Andersen13456d12000-03-16 08:09:57 +0000293}
294
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000295static void put_prompt(void)
296{
297 out1str(cmdedit_prompt);
Eric Andersenc470f442003-07-28 09:56:35 +0000298 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000299 cursor = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000300 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000301}
302
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000303#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000304static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000305{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000306 cmdedit_prompt = prmt_ptr;
307 cmdedit_prmt_len = strlen(prmt_ptr);
308 put_prompt();
309}
310#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000311static void parse_prompt(const char *prmt_ptr)
312{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000313 int prmt_len = 0;
314 int sub_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000315 char flg_not_length = '[';
316 char *prmt_mem_ptr = xcalloc(1, 1);
317 char *pwd_buf = xgetcwd(0);
318 char buf2[PATH_MAX + 1];
319 char buf[2];
320 char c;
321 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000322
Eric Andersen5f265b72001-05-11 16:58:46 +0000323 if (!pwd_buf) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000324 pwd_buf=(char *)bb_msg_unknown;
Eric Andersen5f265b72001-05-11 16:58:46 +0000325 }
326
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000327 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000328 pbuf = buf;
329 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000330 c = *prmt_ptr++;
331 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000332 const char *cp = prmt_ptr;
333 int l;
Eric Andersenc470f442003-07-28 09:56:35 +0000334
Manuel Novoa III cad53642003-03-19 09:13:01 +0000335 c = bb_process_escape_sequence(&prmt_ptr);
Eric Andersene5dfced2001-04-09 22:48:12 +0000336 if(prmt_ptr==cp) {
337 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000338 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000339 c = *prmt_ptr++;
340 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000341#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000342 case 'u':
343 pbuf = user_buf;
344 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000345#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000346 case 'h':
347 pbuf = hostname_buf;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000348 if (pbuf == 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000349 pbuf = xcalloc(256, 1);
350 if (gethostname(pbuf, 255) < 0) {
351 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000352 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000353 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000354
355 if (s)
356 *s = 0;
357 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000358 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000359 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000360 break;
361 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000362 c = my_euid == 0 ? '#' : '$';
363 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000364#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000365 case 'w':
366 pbuf = pwd_buf;
367 l = strlen(home_pwd_buf);
368 if (home_pwd_buf[0] != 0 &&
369 strncmp(home_pwd_buf, pbuf, l) == 0 &&
370 (pbuf[l]=='/' || pbuf[l]=='\0') &&
371 strlen(pwd_buf+l)<PATH_MAX) {
372 pbuf = buf2;
373 *pbuf = '~';
374 strcpy(pbuf+1, pwd_buf+l);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000375 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000376 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000377#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000378 case 'W':
379 pbuf = pwd_buf;
380 cp = strrchr(pbuf,'/');
381 if ( (cp != NULL) && (cp != pbuf) )
382 pbuf += (cp-pbuf)+1;
383 break;
384 case '!':
385 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
386 break;
387 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000388 c = '\033';
389 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000390 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000391 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000392 int h;
393 buf2[l++] = *prmt_ptr;
394 buf2[l] = 0;
395 h = strtol(buf2, &pbuf, 16);
396 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000397 l--;
398 break;
399 }
400 prmt_ptr++;
401 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000402 buf2[l] = 0;
403 c = (char)strtol(buf2, 0, 16);
404 if(c==0)
405 c = '?';
406 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000407 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000408 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000409 if (c == flg_not_length) {
410 flg_not_length = flg_not_length == '[' ? ']' : '[';
411 continue;
412 }
413 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000414 }
Eric Andersenc470f442003-07-28 09:56:35 +0000415 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000416 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000417 if(pbuf == buf)
418 *pbuf = c;
419 prmt_len += strlen(pbuf);
420 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000421 if (flg_not_length == ']')
422 sub_len++;
423 }
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 Andersenf9ff8a72001-03-15 20:51:09 +0000427 cmdedit_prmt_len = prmt_len - sub_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000428 put_prompt();
429}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000430#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000431
432
433/* draw promt, editor line, and clear tail */
434static void redraw(int y, int back_cursor)
435{
Eric Andersenc470f442003-07-28 09:56:35 +0000436 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000437 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000438 putchar('\r');
439 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000440 input_end(); /* rewrite */
441 printf("\033[J"); /* destroy tail after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000442 input_backward(back_cursor);
443}
444
Erik Andersenf0657d32000-04-12 17:49:52 +0000445/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000446static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000447{
Mark Whitley4e338752001-01-26 20:42:23 +0000448 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000449
Mark Whitley4e338752001-01-26 20:42:23 +0000450 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000451 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000452
453 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000454 len--;
Eric Andersenc470f442003-07-28 09:56:35 +0000455 input_end(); /* rewtite new line */
456 cmdedit_set_out_char(0); /* destroy end char */
457 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000458}
459
Mark Whitley4e338752001-01-26 20:42:23 +0000460/* Delete the char in back of the cursor */
461static void input_backspace(void)
462{
463 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000464 input_backward(1);
465 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000466 }
467}
468
469
Erik Andersenf0657d32000-04-12 17:49:52 +0000470/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000471static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000472{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000473 if (cursor < len)
474 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000475}
476
477
Mark Whitley4e338752001-01-26 20:42:23 +0000478static void cmdedit_setwidth(int w, int redraw_flg)
479{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000480 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000481 if (w <= cmdedit_termw) {
482 cmdedit_termw = cmdedit_termw % w;
483 }
Mark Whitley4e338752001-01-26 20:42:23 +0000484 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000485 cmdedit_termw = w;
486
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000487 if (redraw_flg) {
488 /* new y for current cursor */
489 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000490
491 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000492 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
493 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000494 }
Eric Andersenc470f442003-07-28 09:56:35 +0000495 }
Mark Whitley4e338752001-01-26 20:42:23 +0000496}
497
Eric Andersen7467c8d2001-07-12 20:26:32 +0000498static void cmdedit_init(void)
Mark Whitley4e338752001-01-26 20:42:23 +0000499{
Eric Andersen61173a52001-03-19 17:48:55 +0000500 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000501 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
502 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000503 win_changed(-SIGWINCH);
504 handlers_sets |= SET_WCHG_HANDLERS;
505 }
506
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000507 if ((handlers_sets & SET_ATEXIT) == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000508#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000509 struct passwd *entry;
510
511 my_euid = geteuid();
512 entry = getpwuid(my_euid);
513 if (entry) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000514 user_buf = bb_xstrdup(entry->pw_name);
515 home_pwd_buf = bb_xstrdup(entry->pw_dir);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000516 }
517#endif
518
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000519#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000520
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000521#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000522 my_euid = geteuid();
523#endif
524 my_uid = getuid();
525 my_gid = getgid();
Eric Andersenc470f442003-07-28 09:56:35 +0000526#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000527 handlers_sets |= SET_ATEXIT;
Eric Andersenc470f442003-07-28 09:56:35 +0000528 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000529 }
Mark Whitley4e338752001-01-26 20:42:23 +0000530}
Erik Andersenf0657d32000-04-12 17:49:52 +0000531
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000532#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000533
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000534static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000535{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000536 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
537 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
538 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
539 (st->st_mode & S_IXOTH)) return TRUE;
540 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000541}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000542
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000543#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000544
545static char **username_tab_completion(char *ud, int *num_matches)
546{
547 struct passwd *entry;
548 int userlen;
549 char *temp;
550
551
Eric Andersenc470f442003-07-28 09:56:35 +0000552 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000553 userlen = strlen(ud);
554
Eric Andersenc470f442003-07-28 09:56:35 +0000555 if (num_matches == 0) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000556 char *sav_ud = ud - 1;
557 char *home = 0;
558
Eric Andersenc470f442003-07-28 09:56:35 +0000559 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000560 home = home_pwd_buf;
561 } else {
562 /* "~user/..." */
563 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000564 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000565 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000566 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000567 ud = temp;
568 if (entry)
569 home = entry->pw_dir;
570 }
571 if (home) {
572 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000573 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000574
575 /* /home/user/... */
576 sprintf(temp2, "%s%s", home, ud);
577 strcpy(sav_ud, temp2);
578 }
579 }
Eric Andersenc470f442003-07-28 09:56:35 +0000580 return 0; /* void, result save to argument :-) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000581 } else {
582 /* "~[^/]*" */
583 char **matches = (char **) NULL;
584 int nm = 0;
585
586 setpwent();
587
588 while ((entry = getpwent()) != NULL) {
589 /* Null usernames should result in all users as possible completions. */
590 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
591
Eric Andersenc470f442003-07-28 09:56:35 +0000592 bb_xasprintf(&temp, "~%s/", entry->pw_name);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000593 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
594
595 matches[nm++] = temp;
596 }
597 }
598
599 endpwent();
600 (*num_matches) = nm;
601 return (matches);
602 }
603}
Eric Andersenc470f442003-07-28 09:56:35 +0000604#endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000605
606enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000607 FIND_EXE_ONLY = 0,
608 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000609 FIND_FILE_ONLY = 2,
610};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000611
Mark Whitley4e338752001-01-26 20:42:23 +0000612static int path_parse(char ***p, int flags)
613{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000614 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000615 char *tmp;
616 char *pth;
617
Mark Whitley4e338752001-01-26 20:42:23 +0000618 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000619 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
620 /* PATH=<empty> or PATH=:<empty> */
621 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000622 return 1;
623 }
624
625 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000626 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000627
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000628 for (;;) {
Eric Andersenc470f442003-07-28 09:56:35 +0000629 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000630 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000631 if (tmp) {
632 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000633 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000634 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000635 break;
636 }
637
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000638 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000639
640 tmp = pth;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000641 (*p)[0] = bb_xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000642 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000643
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000644 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000645 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000646 if (tmp) {
Eric Andersenc470f442003-07-28 09:56:35 +0000647 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000648 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000649 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000650 } else
651 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000652 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000653 }
654
655 return npth;
656}
657
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000658static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000659{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000660 int l = 0;
661 char *s = xmalloc((strlen(found) + 1) * 2);
662
663 while (*found) {
664 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
665 s[l++] = '\\';
666 s[l++] = *found++;
667 }
668 s[l] = 0;
669 return s;
670}
671
672static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000673 int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000674{
675
676 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000677 DIR *dir;
678 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000679 char dirbuf[BUFSIZ];
680 int nm = *num_matches;
681 struct stat st;
682 char *path1[1];
683 char **paths = path1;
684 int npaths;
685 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000686 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000687 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000688
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000689 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000690
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000691 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000692 /* no dir, if flags==EXE_ONLY - get paths, else "." */
693 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000694 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000695 } else {
696 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000697 /* save for change */
698 strcpy(dirbuf, command);
699 /* set dir only */
700 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000701#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000702 if (dirbuf[0] == '~') /* ~/... or ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000703 username_tab_completion(dirbuf, 0);
704#endif
705 /* "strip" dirname in command */
706 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000707
Mark Whitley4e338752001-01-26 20:42:23 +0000708 paths[0] = dirbuf;
Eric Andersenc470f442003-07-28 09:56:35 +0000709 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000710 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000711
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000712 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000713
Mark Whitley4e338752001-01-26 20:42:23 +0000714 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000715 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000716 continue;
717
718 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000719 char *str_found = next->d_name;
720
Mark Whitley4e338752001-01-26 20:42:23 +0000721 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000722 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000723 continue;
724 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000725 if (*str_found == '.' && *pfind == 0) {
726 if (*paths[i] == '/' && paths[i][1] == 0
Eric Andersenc470f442003-07-28 09:56:35 +0000727 && str_found[1] == 0) str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000728 else
Mark Whitley4e338752001-01-26 20:42:23 +0000729 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000730 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000731 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000732 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000733 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000734 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000735 /* find with dirs ? */
736 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000737 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000738 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000739 /* name is directory */
Eric Andersene5dfced2001-04-09 22:48:12 +0000740 str_found = found;
741 found = concat_path_file(found, "");
742 free(str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000743 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000744 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000745 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000746 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000747 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000748 str_found = add_quote_for_spec_chars(found);
749 if (type == FIND_FILE_ONLY ||
Matt Kraai1f0c4362001-12-20 23:13:26 +0000750 (type == FIND_EXE_ONLY && is_execute(&st)))
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000751 strcat(str_found, " ");
752 }
Mark Whitley4e338752001-01-26 20:42:23 +0000753 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000754 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
755
756 matches[nm++] = str_found;
Eric Andersene5dfced2001-04-09 22:48:12 +0000757cont:
758 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000759 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000760 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000761 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000762 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000763 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000764 free(paths);
765 }
Mark Whitley4e338752001-01-26 20:42:23 +0000766 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000767 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000768}
Erik Andersenf0657d32000-04-12 17:49:52 +0000769
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000770static int match_compare(const void *a, const void *b)
771{
772 return strcmp(*(char **) a, *(char **) b);
773}
774
775
776
777#define QUOT (UCHAR_MAX+1)
778
779#define collapse_pos(is, in) { \
Eric Andersen889a3012002-03-20 14:31:15 +0000780 memcpy(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
781 memcpy(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000782
783static int find_match(char *matchBuf, int *len_with_quotes)
784{
785 int i, j;
786 int command_mode;
787 int c, c2;
788 int int_buf[BUFSIZ + 1];
789 int pos_buf[BUFSIZ + 1];
790
791 /* set to integer dimension characters and own positions */
792 for (i = 0;; i++) {
793 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
794 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000795 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000796 break;
797 } else
798 pos_buf[i] = i;
799 }
800
801 /* mask \+symbol and convert '\t' to ' ' */
802 for (i = j = 0; matchBuf[i]; i++, j++)
803 if (matchBuf[i] == '\\') {
804 collapse_pos(j, j + 1);
805 int_buf[j] |= QUOT;
806 i++;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000807#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000808 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000809 int_buf[j] = ' ' | QUOT;
810#endif
811 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000812#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000813 else if (matchBuf[i] == '\t')
814 int_buf[j] = ' ';
815#endif
816
817 /* mask "symbols" or 'symbols' */
818 c2 = 0;
819 for (i = 0; int_buf[i]; i++) {
820 c = int_buf[i];
821 if (c == '\'' || c == '"') {
822 if (c2 == 0)
823 c2 = c;
824 else {
825 if (c == c2)
826 c2 = 0;
827 else
828 int_buf[i] |= QUOT;
829 }
830 } else if (c2 != 0 && c != '$')
831 int_buf[i] |= QUOT;
832 }
833
834 /* skip commands with arguments if line have commands delimiters */
835 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
836 for (i = 0; int_buf[i]; i++) {
837 c = int_buf[i];
838 c2 = int_buf[i + 1];
839 j = i ? int_buf[i - 1] : -1;
840 command_mode = 0;
841 if (c == ';' || c == '&' || c == '|') {
842 command_mode = 1 + (c == c2);
843 if (c == '&') {
844 if (j == '>' || j == '<')
845 command_mode = 0;
846 } else if (c == '|' && j == '>')
847 command_mode = 0;
848 }
849 if (command_mode) {
850 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000851 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000852 }
853 }
854 /* collapse `command...` */
855 for (i = 0; int_buf[i]; i++)
856 if (int_buf[i] == '`') {
857 for (j = i + 1; int_buf[j]; j++)
858 if (int_buf[j] == '`') {
859 collapse_pos(i, j + 1);
860 j = 0;
861 break;
862 }
863 if (j) {
864 /* not found close ` - command mode, collapse all previous */
865 collapse_pos(0, i + 1);
866 break;
867 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000868 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000869 }
870
871 /* collapse (command...(command...)...) or {command...{command...}...} */
Eric Andersenc470f442003-07-28 09:56:35 +0000872 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000873 c2 = 0;
874 for (i = 0; int_buf[i]; i++)
875 if (int_buf[i] == '(' || int_buf[i] == '{') {
876 if (int_buf[i] == '(')
877 c++;
878 else
879 c2++;
880 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000881 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000882 }
883 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
884 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
885 if (int_buf[i] == ')')
886 c--;
887 else
888 c2--;
889 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000890 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000891 }
892
893 /* skip first not quote space */
894 for (i = 0; int_buf[i]; i++)
895 if (int_buf[i] != ' ')
896 break;
897 if (i)
898 collapse_pos(0, i);
899
900 /* set find mode for completion */
901 command_mode = FIND_EXE_ONLY;
902 for (i = 0; int_buf[i]; i++)
903 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
904 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000905 && matchBuf[pos_buf[0]]=='c'
906 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000907 command_mode = FIND_DIR_ONLY;
908 else {
909 command_mode = FIND_FILE_ONLY;
910 break;
911 }
912 }
913 /* "strlen" */
914 for (i = 0; int_buf[i]; i++);
915 /* find last word */
916 for (--i; i >= 0; i--) {
917 c = int_buf[i];
918 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
919 collapse_pos(0, i + 1);
920 break;
921 }
922 }
923 /* skip first not quoted '\'' or '"' */
924 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
925 /* collapse quote or unquote // or /~ */
Eric Andersenc470f442003-07-28 09:56:35 +0000926 while ((int_buf[i] & ~QUOT) == '/' &&
Mark Whitley7e5291f2001-03-08 19:31:12 +0000927 ((int_buf[i + 1] & ~QUOT) == '/'
928 || (int_buf[i + 1] & ~QUOT) == '~')) {
929 i++;
930 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000931
932 /* set only match and destroy quotes */
933 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000934 for (c = 0; pos_buf[i] >= 0; i++) {
935 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000936 j = pos_buf[i] + 1;
937 }
Eric Andersen4f990532001-05-31 17:15:57 +0000938 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000939 /* old lenght matchBuf with quotes symbols */
940 *len_with_quotes = j ? j - pos_buf[0] : 0;
941
942 return command_mode;
943}
944
Glenn L McGrath4d001292003-01-06 01:11:50 +0000945/*
946 display by column original ideas from ls applet,
947 very optimize by my :)
948*/
949static void showfiles(char **matches, int nfiles)
950{
951 int ncols, row;
952 int column_width = 0;
953 int nrows = nfiles;
954
955 /* find the longest file name- use that as the column width */
956 for (row = 0; row < nrows; row++) {
957 int l = strlen(matches[row]);
958
959 if (column_width < l)
960 column_width = l;
961 }
962 column_width += 2; /* min space for columns */
963 ncols = cmdedit_termw / column_width;
964
965 if (ncols > 1) {
966 nrows /= ncols;
967 if(nfiles % ncols)
968 nrows++; /* round up fractionals */
969 column_width = -column_width; /* for printf("%-Ns", ...); */
970 } else {
971 ncols = 1;
972 }
973 for (row = 0; row < nrows; row++) {
974 int n = row;
975 int nc;
976
977 for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++)
978 printf("%*s", column_width, matches[n]);
979 printf("%s\n", matches[n]);
980 }
981}
982
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000983
984static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000985{
986 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000987 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +0000988 static char **matches;
989
Eric Andersenc470f442003-07-28 09:56:35 +0000990 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +0000991 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000992 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +0000993 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000994 free(matches);
995 matches = (char **) NULL;
996 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000997 return;
998 }
Matt Kraai1f0c4362001-12-20 23:13:26 +0000999 if (! *lastWasTab) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001000
1001 char *tmp;
1002 int len_found;
1003 char matchBuf[BUFSIZ];
1004 int find_type;
1005 int recalc_pos;
1006
Eric Andersenc470f442003-07-28 09:56:35 +00001007 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001008
1009 /* Make a local copy of the string -- up
1010 * to the position of the cursor */
1011 tmp = strncpy(matchBuf, command_ps, cursor);
1012 tmp[cursor] = 0;
1013
1014 find_type = find_match(matchBuf, &recalc_pos);
1015
1016 /* Free up any memory already allocated */
1017 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001018
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001019#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001020 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001021 * then try completing this word as a username. */
1022
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001023 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001024 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001025#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001026 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001027 * in the current working directory that matches. */
1028 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001029 matches =
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001030 exe_n_cwd_tab_completion(matchBuf,
1031 &num_matches, find_type);
1032 /* Remove duplicate found */
1033 if(matches) {
1034 int i, j;
1035 /* bubble */
1036 for(i=0; i<(num_matches-1); i++)
1037 for(j=i+1; j<num_matches; j++)
1038 if(matches[i]!=0 && matches[j]!=0 &&
1039 strcmp(matches[i], matches[j])==0) {
1040 free(matches[j]);
1041 matches[j]=0;
1042 }
1043 j=num_matches;
1044 num_matches = 0;
1045 for(i=0; i<j; i++)
1046 if(matches[i]) {
1047 if(!strcmp(matches[i], "./"))
1048 matches[i][1]=0;
1049 else if(!strcmp(matches[i], "../"))
1050 matches[i][2]=0;
1051 matches[num_matches++]=matches[i];
1052 }
1053 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001054 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001055 if (!matches || num_matches > 1) {
1056 char *tmp1;
1057
Mark Whitley4e338752001-01-26 20:42:23 +00001058 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001059 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +00001060 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001061 /* sort */
1062 qsort(matches, num_matches, sizeof(char *), match_compare);
1063
1064 /* find minimal match */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001065 tmp = bb_xstrdup(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001066 for (tmp1 = tmp; *tmp1; tmp1++)
1067 for (len_found = 1; len_found < num_matches; len_found++)
1068 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1069 *tmp1 = 0;
1070 break;
1071 }
Eric Andersenc470f442003-07-28 09:56:35 +00001072 if (*tmp == 0) { /* have unique */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001073 free(tmp);
1074 return;
1075 }
Eric Andersenc470f442003-07-28 09:56:35 +00001076 } else { /* one match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001077 tmp = matches[0];
1078 /* for next completion current found */
1079 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001080 }
1081
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001082 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001083 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001084 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001085
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001086 /* before word for match */
1087 command_ps[cursor - recalc_pos] = 0;
1088 /* save tail line */
1089 strcpy(matchBuf, command_ps + cursor);
1090 /* add match */
1091 strcat(command_ps, tmp);
1092 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001093 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001094 /* back to begin word for match */
1095 input_backward(recalc_pos);
1096 /* new pos */
1097 recalc_pos = cursor + len_found;
1098 /* new len */
1099 len = strlen(command_ps);
1100 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001101 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001102 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001103 if (tmp != matches[0])
1104 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001105 } else {
1106 /* Ok -- the last char was a TAB. Since they
1107 * just hit TAB again, print a list of all the
1108 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001109 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001110 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001111
1112 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001113 goto_new_line();
Glenn L McGrath4d001292003-01-06 01:11:50 +00001114 showfiles(matches, num_matches);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001115 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001116 }
1117 }
1118}
Eric Andersenc470f442003-07-28 09:56:35 +00001119#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001120
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001121#if MAX_HISTORY >= 1
1122static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001123{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001124 if(command_ps[0] != 0 || history[cur_history] == 0) {
1125 free(history[cur_history]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001126 history[cur_history] = bb_xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001127 }
1128 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +00001129}
1130
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001131static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001132{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001133 int ch = cur_history;
1134
1135 if (ch < n_history) {
1136 get_previous_history(); /* save the current history line */
1137 return (cur_history = ch+1);
1138 } else {
1139 beep();
1140 return 0;
1141 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001142}
Robert Griebl350d26b2002-12-03 22:45:46 +00001143
Robert Griebl350d26b2002-12-03 22:45:46 +00001144#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001145extern void load_history ( const char *fromfile )
1146{
Robert Griebl350d26b2002-12-03 22:45:46 +00001147 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001148 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001149
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001150 /* cleanup old */
1151
1152 for(hi = n_history; hi > 0; ) {
1153 hi--;
1154 free ( history [hi] );
Robert Griebl350d26b2002-12-03 22:45:46 +00001155 }
1156
1157 if (( fp = fopen ( fromfile, "r" ))) {
Eric Andersenc470f442003-07-28 09:56:35 +00001158
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001159 for ( hi = 0; hi < MAX_HISTORY; ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001160 char * hl = bb_get_chomped_line_from_file(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001161 int l;
1162
1163 if(!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001164 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001165 l = strlen(hl);
1166 if(l >= BUFSIZ)
1167 hl[BUFSIZ-1] = 0;
1168 if(l == 0 || hl[0] == ' ') {
1169 free(hl);
1170 continue;
1171 }
1172 history [hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001173 }
1174 fclose ( fp );
1175 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001176 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001177}
1178
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001179extern void save_history ( const char *tofile )
Robert Griebl350d26b2002-12-03 22:45:46 +00001180{
Robert Griebl350d26b2002-12-03 22:45:46 +00001181 FILE *fp = fopen ( tofile, "w" );
Eric Andersenc470f442003-07-28 09:56:35 +00001182
Robert Griebl350d26b2002-12-03 22:45:46 +00001183 if ( fp ) {
1184 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001185
Robert Griebl350d26b2002-12-03 22:45:46 +00001186 for ( i = 0; i < n_history; i++ ) {
Eric Andersen27bb7902003-12-23 20:24:51 +00001187 fprintf(fp, "%s\n", history [i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001188 }
1189 fclose ( fp );
1190 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001191}
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001192#endif
Robert Griebl350d26b2002-12-03 22:45:46 +00001193
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001194#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001195
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001196enum {
1197 ESC = 27,
1198 DEL = 127,
1199};
1200
1201
Erik Andersen6273f652000-03-17 01:12:41 +00001202/*
1203 * This function is used to grab a character buffer
1204 * from the input file descriptor and allows you to
1205 * a string with full command editing (sortof like
1206 * a mini readline).
1207 *
1208 * The following standard commands are not implemented:
1209 * ESC-b -- Move back one word
1210 * ESC-f -- Move forward one word
1211 * ESC-d -- Delete back one word
1212 * ESC-h -- Delete forward one word
1213 * CTL-t -- Transpose two characters
1214 *
1215 * Furthermore, the "vi" command editing keys are not implemented.
1216 *
Erik Andersen6273f652000-03-17 01:12:41 +00001217 */
Eric Andersenc470f442003-07-28 09:56:35 +00001218
Eric Andersen044228d2001-07-17 01:12:36 +00001219
1220int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001221{
1222
Erik Andersenc7c634b2000-03-19 05:28:55 +00001223 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001224 int lastWasTab = FALSE;
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001225 unsigned char c = 0;
Erik Andersen13456d12000-03-16 08:09:57 +00001226
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001227 /* prepare before init handlers */
Eric Andersenc470f442003-07-28 09:56:35 +00001228 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001229 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001230 command_ps = command;
1231
Eric Andersen34506362001-08-02 05:02:46 +00001232 getTermSettings(0, (void *) &initial_settings);
1233 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1234 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1235 /* Turn off echoing and CTRL-C, so we can trap it */
1236 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Eric Andersen34506362001-08-02 05:02:46 +00001237 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1238 new_settings.c_cc[VMIN] = 1;
1239 new_settings.c_cc[VTIME] = 0;
1240 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001241# ifndef _POSIX_VDISABLE
1242# define _POSIX_VDISABLE '\0'
1243# endif
Eric Andersenc470f442003-07-28 09:56:35 +00001244 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001245 command[0] = 0;
1246
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001247 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001248 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001249
Eric Andersen6faae7d2001-02-16 20:09:17 +00001250 /* Now initialize things */
1251 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001252 /* Print out the command prompt */
1253 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001254
Erik Andersenc7c634b2000-03-19 05:28:55 +00001255 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001256
Eric Andersenc470f442003-07-28 09:56:35 +00001257 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001258
Eric Andersen7467c8d2001-07-12 20:26:32 +00001259 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001260 /* if we can't read input then exit */
1261 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001262
Erik Andersen13456d12000-03-16 08:09:57 +00001263 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001264 case '\n':
1265 case '\r':
1266 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001267 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001268 break_out = 1;
1269 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001270 case 1:
1271 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001272 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001273 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001274 case 2:
1275 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001276 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001277 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001278 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001279 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001280 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001281 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001282 len = 0;
1283 lastWasTab = FALSE;
1284 put_prompt();
1285 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001286 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001287 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001288 * if the len=0 and no chars to delete */
1289 if (len == 0) {
Eric Andersened424db2001-04-23 15:28:28 +00001290prepare_to_die:
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001291#if !defined(CONFIG_ASH)
Mark Whitley4e338752001-01-26 20:42:23 +00001292 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001293 goto_new_line();
1294 /* cmdedit_reset_term() called in atexit */
1295 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001296#else
1297 break_out = -1; /* for control stoped jobs */
1298 break;
1299#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001300 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001301 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001302 }
1303 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001304 case 5:
1305 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001306 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001307 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001308 case 6:
1309 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001310 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001311 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001312 case '\b':
1313 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001314 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001315 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001316 break;
1317 case '\t':
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001318#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001319 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001320#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001321 break;
Eric Andersen65a07302002-04-13 13:26:49 +00001322 case 11:
Eric Andersenc470f442003-07-28 09:56:35 +00001323 /* Control-k -- clear to end of line */
Eric Andersen65a07302002-04-13 13:26:49 +00001324 *(command + cursor) = 0;
1325 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001326 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001327 break;
Eric Andersenc470f442003-07-28 09:56:35 +00001328 case 12:
Eric Andersen27bb7902003-12-23 20:24:51 +00001329 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001330 printf("\033[H");
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001331 redraw(0, len-cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001332 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001333#if MAX_HISTORY >= 1
Erik Andersenf0657d32000-04-12 17:49:52 +00001334 case 14:
1335 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001336 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001337 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001338 break;
1339 case 16:
1340 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001341 if (cur_history > 0) {
1342 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001343 goto rewrite_line;
1344 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001345 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001346 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001347 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001348#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001349 case 21:
1350 /* Control-U -- Clear line before cursor */
1351 if (cursor) {
1352 strcpy(command, command + cursor);
1353 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001354 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001355 break;
Eric Andersen27bb7902003-12-23 20:24:51 +00001356 case 23:
1357 /* Control-W -- Remove the last word */
1358 while (cursor > 0 && isspace(command[cursor-1]))
1359 input_backspace();
1360 while (cursor > 0 &&!isspace(command[cursor-1]))
1361 input_backspace();
1362 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001363 case ESC:{
1364 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001365 if (safe_read(0, &c, 1) < 1)
1366 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001367 /* different vt100 emulations */
1368 if (c == '[' || c == 'O') {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001369 if (safe_read(0, &c, 1) < 1)
1370 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001371 }
1372 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001373#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001374 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001375
1376 input_tab(&lastWasTab);
1377 break;
1378#endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001379#if MAX_HISTORY >= 1
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001380 case 'A':
1381 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001382 if (cur_history > 0) {
1383 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001384 goto rewrite_line;
1385 } else {
1386 beep();
1387 }
1388 break;
1389 case 'B':
1390 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001391 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001392 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001393 /* Rewrite the line with the selected history item */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001394rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001395 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001396 len = strlen(strcpy(command, history[cur_history]));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001397 /* redraw and go to end line */
1398 redraw(cmdedit_y, 0);
1399 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001400#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001401 case 'C':
1402 /* Right Arrow -- Move forward one character */
1403 input_forward();
1404 break;
1405 case 'D':
1406 /* Left Arrow -- Move back one character */
1407 input_backward(1);
1408 break;
1409 case '3':
1410 /* Delete */
1411 input_delete();
1412 break;
1413 case '1':
1414 case 'H':
1415 /* Home (Ctrl-A) */
1416 input_backward(cursor);
1417 break;
1418 case '4':
1419 case 'F':
1420 /* End (Ctrl-E) */
1421 input_end();
1422 break;
1423 default:
1424 if (!(c >= '1' && c <= '9'))
1425 c = 0;
1426 beep();
1427 }
1428 if (c >= '1' && c <= '9')
1429 do
Eric Andersen7467c8d2001-07-12 20:26:32 +00001430 if (safe_read(0, &c, 1) < 1)
1431 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001432 while (c != '~');
1433 break;
1434 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001435
Eric Andersenc470f442003-07-28 09:56:35 +00001436 default: /* If it's regular input, do the normal thing */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001437#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001438 /* Control-V -- Add non-printable symbol */
1439 if (c == 22) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001440 if (safe_read(0, &c, 1) < 1)
1441 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001442 if (c == 0) {
1443 beep();
1444 break;
1445 }
1446 } else
1447#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001448 if (!Isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001449 break;
1450
Eric Andersenc470f442003-07-28 09:56:35 +00001451 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001452 break;
1453
1454 len++;
1455
Eric Andersenc470f442003-07-28 09:56:35 +00001456 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001457 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001458 *(command + cursor + 1) = 0;
1459 cmdedit_set_out_char(0);
Eric Andersenc470f442003-07-28 09:56:35 +00001460 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001461 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001462
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001463 memmove(command + sc + 1, command + sc, len - sc);
1464 *(command + sc) = c;
1465 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001466 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001467 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001468 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001469 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001470 }
1471
Erik Andersenc7c634b2000-03-19 05:28:55 +00001472 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001473 }
Eric Andersenc470f442003-07-28 09:56:35 +00001474 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001475 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001476
1477 if (c != '\t')
1478 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001479 }
1480
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001481 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001482 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001483
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001484#if MAX_HISTORY >= 1
Erik Andersenc7c634b2000-03-19 05:28:55 +00001485 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001486 /* cleanup may be saved current command line */
1487 free(history[MAX_HISTORY]);
1488 history[MAX_HISTORY] = 0;
Eric Andersenc470f442003-07-28 09:56:35 +00001489 if (len) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001490 int i = n_history;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001491 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001492 if (i >= MAX_HISTORY) {
1493 free(history[0]);
1494 for(i = 0; i < (MAX_HISTORY-1); i++)
1495 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001496 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001497 history[i++] = bb_xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001498 cur_history = i;
1499 n_history = i;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001500#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001501 num_ok_lines++;
1502#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001503 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001504#else /* MAX_HISTORY < 1 */
1505#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
1506 if (len) { /* no put empty line */
1507 num_ok_lines++;
1508 }
1509#endif
1510#endif /* MAX_HISTORY >= 1 */
Eric Andersen27bb7902003-12-23 20:24:51 +00001511 if (break_out > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001512 command[len++] = '\n'; /* set '\n' */
1513 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001514 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001515#if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersenc470f442003-07-28 09:56:35 +00001516 input_tab(0); /* strong free */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001517#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001518#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001519 free(cmdedit_prompt);
1520#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001521 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001522 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001523}
1524
Eric Andersen7467c8d2001-07-12 20:26:32 +00001525
1526
Eric Andersenc470f442003-07-28 09:56:35 +00001527#endif /* CONFIG_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001528
1529
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001530#ifdef TEST
1531
Manuel Novoa III cad53642003-03-19 09:13:01 +00001532const char *bb_applet_name = "debug stuff usage";
Eric Andersene5dfced2001-04-09 22:48:12 +00001533
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001534#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001535#include <locale.h>
1536#endif
1537
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001538int main(int argc, char **argv)
1539{
1540 char buff[BUFSIZ];
1541 char *prompt =
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001542#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001543 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001544\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001545\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001546#else
1547 "% ";
1548#endif
1549
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001550#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001551 setlocale(LC_ALL, "");
1552#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001553 while(1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001554 int l;
Eric Andersen27bb7902003-12-23 20:24:51 +00001555 l = cmdedit_read_input(prompt, buff);
1556 if(l > 0 && buff[l-1] == '\n') {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001557 buff[l-1] = 0;
Eric Andersen27bb7902003-12-23 20:24:51 +00001558 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1559 } else {
1560 break;
1561 }
Eric Andersen7467c8d2001-07-12 20:26:32 +00001562 }
Eric Andersen27bb7902003-12-23 20:24:51 +00001563 printf("*** cmdedit_read_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001564 return 0;
1565}
1566
Eric Andersenc470f442003-07-28 09:56:35 +00001567#endif /* TEST */