blob: a78642b9dfcf75c89d40345525120aabd5707e41 [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
52#ifndef TEST
53
Eric Andersen5f2c79d2001-02-16 18:36:04 +000054#define D(x)
55
56#else
57
Eric Andersen27bb7902003-12-23 20:24:51 +000058/* pretect redefined for test */
59#undef CONFIG_FEATURE_COMMAND_EDITING
60#undef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
61#undef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
62#undef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
63#undef CONFIG_FEATURE_CLEAN_UP
64
Eric Andersenbdfd0d72001-10-24 05:00:29 +000065#define CONFIG_FEATURE_COMMAND_EDITING
66#define CONFIG_FEATURE_COMMAND_TAB_COMPLETION
67#define CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
68#define CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
69#define CONFIG_FEATURE_CLEAN_UP
Eric Andersen5f2c79d2001-02-16 18:36:04 +000070
Eric Andersen5f2c79d2001-02-16 18:36:04 +000071#define D(x) x
72
Eric Andersenc470f442003-07-28 09:56:35 +000073#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000074
Eric Andersenbdfd0d72001-10-24 05:00:29 +000075#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5165fbe2001-02-20 06:42:29 +000076#include <dirent.h>
77#include <sys/stat.h>
78#endif
79
Eric Andersenbdfd0d72001-10-24 05:00:29 +000080#ifdef CONFIG_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000081
Eric Andersenbdfd0d72001-10-24 05:00:29 +000082#ifndef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
83#undef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +000084#endif
85
Eric Andersenbdfd0d72001-10-24 05:00:29 +000086#if defined(CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION) || defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
87#define CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +000088#endif
89
Eric Andersenbdfd0d72001-10-24 05:00:29 +000090#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Glenn L McGrath78b0e372001-06-26 02:06:08 +000091# ifndef TEST
Eric Andersen887ca792002-07-03 23:19:26 +000092# include "pwd_.h"
Glenn L McGrath78b0e372001-06-26 02:06:08 +000093# else
94# include <pwd.h>
95# endif /* TEST */
Eric Andersenc470f442003-07-28 09:56:35 +000096#endif /* advanced FEATURES */
Eric Andersenaf4ac772001-02-01 22:43:49 +000097
98
Eric Andersen5f2c79d2001-02-16 18:36:04 +000099/* Maximum length of the linked list for the command line history */
Robert Griebl350d26b2002-12-03 22:45:46 +0000100#ifndef CONFIG_FEATURE_COMMAND_HISTORY
101#define MAX_HISTORY 15
102#else
103#define MAX_HISTORY CONFIG_FEATURE_COMMAND_HISTORY
104#endif
105
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000106#if MAX_HISTORY < 1
107#warning cmdedit: You set MAX_HISTORY < 1. The history algorithm switched off.
108#else
109static char *history[MAX_HISTORY+1]; /* history + current */
110/* saved history lines */
111static int n_history;
112/* current pointer to history line */
113static int cur_history;
114#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +0000115
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000116#include <termios.h>
117#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
118#define getTermSettings(fd,argp) tcgetattr(fd, argp);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000119
120/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000121static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000122
123
Mark Whitley4e338752001-01-26 20:42:23 +0000124static
Eric Andersenc470f442003-07-28 09:56:35 +0000125volatile int cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +0000126static
Eric Andersenc470f442003-07-28 09:56:35 +0000127volatile int handlers_sets = 0; /* Set next bites: */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000128
Mark Whitley4e338752001-01-26 20:42:23 +0000129enum {
Eric Andersenc470f442003-07-28 09:56:35 +0000130 SET_ATEXIT = 1, /* when atexit() has been called
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000131 and get euid,uid,gid to fast compare */
Eric Andersen7467c8d2001-07-12 20:26:32 +0000132 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
133 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000134};
Erik Andersen13456d12000-03-16 08:09:57 +0000135
Mark Whitley4e338752001-01-26 20:42:23 +0000136
Eric Andersenc470f442003-07-28 09:56:35 +0000137static int cmdedit_x; /* real x terminal position */
138static int cmdedit_y; /* pseudoreal y terminal position */
139static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000140
Eric Andersenc470f442003-07-28 09:56:35 +0000141static int cursor; /* required global for signal handler */
142static int len; /* --- "" - - "" - -"- --""-- --""--- */
143static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000144static
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000145#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000146 const
147#endif
Eric Andersenc470f442003-07-28 09:56:35 +0000148char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000149
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000150#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000151static char *user_buf = "";
152static char *home_pwd_buf = "";
153static int my_euid;
154#endif
155
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000156#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000157static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000158static int num_ok_lines = 1;
159#endif
160
161
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000162#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000163
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000164#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000165static int my_euid;
166#endif
167
168static int my_uid;
169static int my_gid;
170
Eric Andersenc470f442003-07-28 09:56:35 +0000171#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
172
Mark Whitley4e338752001-01-26 20:42:23 +0000173static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000174
Mark Whitley4e338752001-01-26 20:42:23 +0000175static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000176{
Eric Andersenc470f442003-07-28 09:56:35 +0000177 static sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000178
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000179 /* emulate || signal call */
180 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Eric Andersen8efe9672003-09-15 08:33:45 +0000181 int width = 0;
182 get_terminal_width_height(0, &width, NULL);
183 cmdedit_setwidth(width, nsig == SIGWINCH);
Mark Whitley4e338752001-01-26 20:42:23 +0000184 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000185 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000186
Eric Andersenc470f442003-07-28 09:56:35 +0000187 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000188 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersenc470f442003-07-28 09:56:35 +0000189 else if (nsig == SIGWINCH) /* signaled called handler */
190 signal(SIGWINCH, win_changed); /* set for next call */
191 else /* nsig == 0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000192 /* set previous handler */
Eric Andersenc470f442003-07-28 09:56:35 +0000193 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000194}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000195
196static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000197{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000198 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000199/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000200 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000201 handlers_sets &= ~SET_RESET_TERM;
202 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000203 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000204 /* reset SIGWINCH handler to previous (default) */
205 win_changed(0);
206 handlers_sets &= ~SET_WCHG_HANDLERS;
207 }
208 fflush(stdout);
Erik Andersen13456d12000-03-16 08:09:57 +0000209}
210
Mark Whitley4e338752001-01-26 20:42:23 +0000211
Mark Whitley4e338752001-01-26 20:42:23 +0000212/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000213static void cmdedit_set_out_char(int next_char)
214{
215
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000216 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000217
218 if (c == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000219 c = ' '; /* destroy end char? */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000220#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000221 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000222 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000223 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000224 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000225 c += '@';
226 if (c == 127)
227 c = '?';
228 printf("\033[7m%c\033[0m", c);
229 } else
230#endif
231 putchar(c);
232 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000233 /* terminal is scrolled down */
234 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000235 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000236
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000237 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000238 next_char = ' ';
239 /* destroy "(auto)margin" */
240 putchar(next_char);
241 putchar('\b');
242 }
243 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000244}
245
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000246/* Move to end line. Bonus: rewrite line from cursor */
247static void input_end(void)
248{
249 while (cursor < len)
250 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000251}
252
253/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000254static void goto_new_line(void)
255{
Mark Whitley4e338752001-01-26 20:42:23 +0000256 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000257 if (cmdedit_x)
258 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000259}
260
261
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000262static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000263{
Robert Grieblb2301592002-07-30 23:13:51 +0000264 if ( s )
265 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000266}
Eric Andersen81fe1232003-07-29 06:38:40 +0000267
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000268static inline void beep(void)
269{
270 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000271}
272
Mark Whitley4e338752001-01-26 20:42:23 +0000273/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000274/* special for slow terminal */
275static void input_backward(int num)
276{
277 if (num > cursor)
278 num = cursor;
Eric Andersenc470f442003-07-28 09:56:35 +0000279 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000280
Eric Andersenc470f442003-07-28 09:56:35 +0000281 if (cmdedit_x >= num) { /* no to up line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000282 cmdedit_x -= num;
283 if (num < 4)
284 while (num-- > 0)
285 putchar('\b');
286
287 else
288 printf("\033[%dD", num);
289 } else {
290 int count_y;
291
292 if (cmdedit_x) {
Eric Andersenc470f442003-07-28 09:56:35 +0000293 putchar('\r'); /* back to first terminal pos. */
294 num -= cmdedit_x; /* set previous backward */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000295 }
296 count_y = 1 + num / cmdedit_termw;
297 printf("\033[%dA", count_y);
298 cmdedit_y -= count_y;
299 /* require forward after uping */
300 cmdedit_x = cmdedit_termw * count_y - num;
Eric Andersenc470f442003-07-28 09:56:35 +0000301 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000302 }
Erik Andersen13456d12000-03-16 08:09:57 +0000303}
304
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000305static void put_prompt(void)
306{
307 out1str(cmdedit_prompt);
Eric Andersenc470f442003-07-28 09:56:35 +0000308 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000309 cursor = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000310 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000311}
312
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000313#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000314static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000315{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000316 cmdedit_prompt = prmt_ptr;
317 cmdedit_prmt_len = strlen(prmt_ptr);
318 put_prompt();
319}
320#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000321static void parse_prompt(const char *prmt_ptr)
322{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000323 int prmt_len = 0;
324 int sub_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000325 char flg_not_length = '[';
326 char *prmt_mem_ptr = xcalloc(1, 1);
327 char *pwd_buf = xgetcwd(0);
328 char buf2[PATH_MAX + 1];
329 char buf[2];
330 char c;
331 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000332
Eric Andersen5f265b72001-05-11 16:58:46 +0000333 if (!pwd_buf) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000334 pwd_buf=(char *)bb_msg_unknown;
Eric Andersen5f265b72001-05-11 16:58:46 +0000335 }
336
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000337 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000338 pbuf = buf;
339 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000340 c = *prmt_ptr++;
341 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000342 const char *cp = prmt_ptr;
343 int l;
Eric Andersenc470f442003-07-28 09:56:35 +0000344
Manuel Novoa III cad53642003-03-19 09:13:01 +0000345 c = bb_process_escape_sequence(&prmt_ptr);
Eric Andersene5dfced2001-04-09 22:48:12 +0000346 if(prmt_ptr==cp) {
347 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000348 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000349 c = *prmt_ptr++;
350 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000351#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000352 case 'u':
353 pbuf = user_buf;
354 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000355#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000356 case 'h':
357 pbuf = hostname_buf;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000358 if (pbuf == 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000359 pbuf = xcalloc(256, 1);
360 if (gethostname(pbuf, 255) < 0) {
361 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000362 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000363 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000364
365 if (s)
366 *s = 0;
367 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000368 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000369 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000370 break;
371 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000372 c = my_euid == 0 ? '#' : '$';
373 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000374#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000375 case 'w':
376 pbuf = pwd_buf;
377 l = strlen(home_pwd_buf);
378 if (home_pwd_buf[0] != 0 &&
379 strncmp(home_pwd_buf, pbuf, l) == 0 &&
380 (pbuf[l]=='/' || pbuf[l]=='\0') &&
381 strlen(pwd_buf+l)<PATH_MAX) {
382 pbuf = buf2;
383 *pbuf = '~';
384 strcpy(pbuf+1, pwd_buf+l);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000385 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000386 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000387#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000388 case 'W':
389 pbuf = pwd_buf;
390 cp = strrchr(pbuf,'/');
391 if ( (cp != NULL) && (cp != pbuf) )
392 pbuf += (cp-pbuf)+1;
393 break;
394 case '!':
395 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
396 break;
397 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000398 c = '\033';
399 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000400 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000401 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000402 int h;
403 buf2[l++] = *prmt_ptr;
404 buf2[l] = 0;
405 h = strtol(buf2, &pbuf, 16);
406 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000407 l--;
408 break;
409 }
410 prmt_ptr++;
411 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000412 buf2[l] = 0;
413 c = (char)strtol(buf2, 0, 16);
414 if(c==0)
415 c = '?';
416 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000417 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000418 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000419 if (c == flg_not_length) {
420 flg_not_length = flg_not_length == '[' ? ']' : '[';
421 continue;
422 }
423 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000424 }
Eric Andersenc470f442003-07-28 09:56:35 +0000425 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000426 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000427 if(pbuf == buf)
428 *pbuf = c;
429 prmt_len += strlen(pbuf);
430 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000431 if (flg_not_length == ']')
432 sub_len++;
433 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000434 if(pwd_buf!=(char *)bb_msg_unknown)
Eric Andersen8f697842001-07-02 15:36:57 +0000435 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000436 cmdedit_prompt = prmt_mem_ptr;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000437 cmdedit_prmt_len = prmt_len - sub_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000438 put_prompt();
439}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000440#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000441
442
443/* draw promt, editor line, and clear tail */
444static void redraw(int y, int back_cursor)
445{
Eric Andersenc470f442003-07-28 09:56:35 +0000446 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000447 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000448 putchar('\r');
449 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000450 input_end(); /* rewrite */
451 printf("\033[J"); /* destroy tail after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000452 input_backward(back_cursor);
453}
454
Erik Andersenf0657d32000-04-12 17:49:52 +0000455/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000456static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000457{
Mark Whitley4e338752001-01-26 20:42:23 +0000458 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000459
Mark Whitley4e338752001-01-26 20:42:23 +0000460 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000461 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000462
463 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000464 len--;
Eric Andersenc470f442003-07-28 09:56:35 +0000465 input_end(); /* rewtite new line */
466 cmdedit_set_out_char(0); /* destroy end char */
467 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000468}
469
Mark Whitley4e338752001-01-26 20:42:23 +0000470/* Delete the char in back of the cursor */
471static void input_backspace(void)
472{
473 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000474 input_backward(1);
475 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000476 }
477}
478
479
Erik Andersenf0657d32000-04-12 17:49:52 +0000480/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000481static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000482{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000483 if (cursor < len)
484 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000485}
486
487
Mark Whitley4e338752001-01-26 20:42:23 +0000488static void cmdedit_setwidth(int w, int redraw_flg)
489{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000490 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000491 if (w <= cmdedit_termw) {
492 cmdedit_termw = cmdedit_termw % w;
493 }
Mark Whitley4e338752001-01-26 20:42:23 +0000494 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000495 cmdedit_termw = w;
496
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000497 if (redraw_flg) {
498 /* new y for current cursor */
499 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000500
501 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000502 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
503 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000504 }
Eric Andersenc470f442003-07-28 09:56:35 +0000505 }
Mark Whitley4e338752001-01-26 20:42:23 +0000506}
507
Eric Andersen7467c8d2001-07-12 20:26:32 +0000508static void cmdedit_init(void)
Mark Whitley4e338752001-01-26 20:42:23 +0000509{
Eric Andersen61173a52001-03-19 17:48:55 +0000510 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000511 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
512 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000513 win_changed(-SIGWINCH);
514 handlers_sets |= SET_WCHG_HANDLERS;
515 }
516
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000517 if ((handlers_sets & SET_ATEXIT) == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000518#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000519 struct passwd *entry;
520
521 my_euid = geteuid();
522 entry = getpwuid(my_euid);
523 if (entry) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000524 user_buf = bb_xstrdup(entry->pw_name);
525 home_pwd_buf = bb_xstrdup(entry->pw_dir);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000526 }
527#endif
528
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000529#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000530
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000531#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000532 my_euid = geteuid();
533#endif
534 my_uid = getuid();
535 my_gid = getgid();
Eric Andersenc470f442003-07-28 09:56:35 +0000536#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000537 handlers_sets |= SET_ATEXIT;
Eric Andersenc470f442003-07-28 09:56:35 +0000538 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000539 }
Mark Whitley4e338752001-01-26 20:42:23 +0000540}
Erik Andersenf0657d32000-04-12 17:49:52 +0000541
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000542#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000543
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000544static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000545{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000546 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
547 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
548 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
549 (st->st_mode & S_IXOTH)) return TRUE;
550 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000551}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000552
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000553#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000554
555static char **username_tab_completion(char *ud, int *num_matches)
556{
557 struct passwd *entry;
558 int userlen;
559 char *temp;
560
561
Eric Andersenc470f442003-07-28 09:56:35 +0000562 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000563 userlen = strlen(ud);
564
Eric Andersenc470f442003-07-28 09:56:35 +0000565 if (num_matches == 0) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000566 char *sav_ud = ud - 1;
567 char *home = 0;
568
Eric Andersenc470f442003-07-28 09:56:35 +0000569 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000570 home = home_pwd_buf;
571 } else {
572 /* "~user/..." */
573 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000574 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000575 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000576 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000577 ud = temp;
578 if (entry)
579 home = entry->pw_dir;
580 }
581 if (home) {
582 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000583 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000584
585 /* /home/user/... */
586 sprintf(temp2, "%s%s", home, ud);
587 strcpy(sav_ud, temp2);
588 }
589 }
Eric Andersenc470f442003-07-28 09:56:35 +0000590 return 0; /* void, result save to argument :-) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000591 } else {
592 /* "~[^/]*" */
593 char **matches = (char **) NULL;
594 int nm = 0;
595
596 setpwent();
597
598 while ((entry = getpwent()) != NULL) {
599 /* Null usernames should result in all users as possible completions. */
600 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
601
Eric Andersenc470f442003-07-28 09:56:35 +0000602 bb_xasprintf(&temp, "~%s/", entry->pw_name);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000603 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
604
605 matches[nm++] = temp;
606 }
607 }
608
609 endpwent();
610 (*num_matches) = nm;
611 return (matches);
612 }
613}
Eric Andersenc470f442003-07-28 09:56:35 +0000614#endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000615
616enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000617 FIND_EXE_ONLY = 0,
618 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000619 FIND_FILE_ONLY = 2,
620};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000621
Mark Whitley4e338752001-01-26 20:42:23 +0000622static int path_parse(char ***p, int flags)
623{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000624 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000625 char *tmp;
626 char *pth;
627
Mark Whitley4e338752001-01-26 20:42:23 +0000628 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000629 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
630 /* PATH=<empty> or PATH=:<empty> */
631 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000632 return 1;
633 }
634
635 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000636 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000637
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000638 for (;;) {
Eric Andersenc470f442003-07-28 09:56:35 +0000639 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000640 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000641 if (tmp) {
642 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000643 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000644 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000645 break;
646 }
647
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000648 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000649
650 tmp = pth;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000651 (*p)[0] = bb_xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000652 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000653
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000654 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000655 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000656 if (tmp) {
Eric Andersenc470f442003-07-28 09:56:35 +0000657 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000658 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000659 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000660 } else
661 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000662 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000663 }
664
665 return npth;
666}
667
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000668static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000669{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000670 int l = 0;
671 char *s = xmalloc((strlen(found) + 1) * 2);
672
673 while (*found) {
674 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
675 s[l++] = '\\';
676 s[l++] = *found++;
677 }
678 s[l] = 0;
679 return s;
680}
681
682static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000683 int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000684{
685
686 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000687 DIR *dir;
688 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000689 char dirbuf[BUFSIZ];
690 int nm = *num_matches;
691 struct stat st;
692 char *path1[1];
693 char **paths = path1;
694 int npaths;
695 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000696 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000697 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000698
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000699 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000700
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000701 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000702 /* no dir, if flags==EXE_ONLY - get paths, else "." */
703 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000704 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000705 } else {
706 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000707 /* save for change */
708 strcpy(dirbuf, command);
709 /* set dir only */
710 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000711#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000712 if (dirbuf[0] == '~') /* ~/... or ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000713 username_tab_completion(dirbuf, 0);
714#endif
715 /* "strip" dirname in command */
716 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000717
Mark Whitley4e338752001-01-26 20:42:23 +0000718 paths[0] = dirbuf;
Eric Andersenc470f442003-07-28 09:56:35 +0000719 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000720 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000721
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000722 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000723
Mark Whitley4e338752001-01-26 20:42:23 +0000724 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000725 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000726 continue;
727
728 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000729 char *str_found = next->d_name;
730
Mark Whitley4e338752001-01-26 20:42:23 +0000731 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000732 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000733 continue;
734 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000735 if (*str_found == '.' && *pfind == 0) {
736 if (*paths[i] == '/' && paths[i][1] == 0
Eric Andersenc470f442003-07-28 09:56:35 +0000737 && str_found[1] == 0) str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000738 else
Mark Whitley4e338752001-01-26 20:42:23 +0000739 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000740 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000741 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000742 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000743 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000744 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000745 /* find with dirs ? */
746 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000747 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000748 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000749 /* name is directory */
Eric Andersene5dfced2001-04-09 22:48:12 +0000750 str_found = found;
751 found = concat_path_file(found, "");
752 free(str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000753 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000754 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000755 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000756 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000757 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000758 str_found = add_quote_for_spec_chars(found);
759 if (type == FIND_FILE_ONLY ||
Matt Kraai1f0c4362001-12-20 23:13:26 +0000760 (type == FIND_EXE_ONLY && is_execute(&st)))
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000761 strcat(str_found, " ");
762 }
Mark Whitley4e338752001-01-26 20:42:23 +0000763 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000764 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
765
766 matches[nm++] = str_found;
Eric Andersene5dfced2001-04-09 22:48:12 +0000767cont:
768 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000769 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000770 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000771 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000772 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000773 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000774 free(paths);
775 }
Mark Whitley4e338752001-01-26 20:42:23 +0000776 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000777 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000778}
Erik Andersenf0657d32000-04-12 17:49:52 +0000779
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000780static int match_compare(const void *a, const void *b)
781{
782 return strcmp(*(char **) a, *(char **) b);
783}
784
785
786
787#define QUOT (UCHAR_MAX+1)
788
789#define collapse_pos(is, in) { \
Eric Andersen889a3012002-03-20 14:31:15 +0000790 memcpy(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
791 memcpy(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000792
793static int find_match(char *matchBuf, int *len_with_quotes)
794{
795 int i, j;
796 int command_mode;
797 int c, c2;
798 int int_buf[BUFSIZ + 1];
799 int pos_buf[BUFSIZ + 1];
800
801 /* set to integer dimension characters and own positions */
802 for (i = 0;; i++) {
803 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
804 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000805 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000806 break;
807 } else
808 pos_buf[i] = i;
809 }
810
811 /* mask \+symbol and convert '\t' to ' ' */
812 for (i = j = 0; matchBuf[i]; i++, j++)
813 if (matchBuf[i] == '\\') {
814 collapse_pos(j, j + 1);
815 int_buf[j] |= QUOT;
816 i++;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000817#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000818 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000819 int_buf[j] = ' ' | QUOT;
820#endif
821 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000822#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000823 else if (matchBuf[i] == '\t')
824 int_buf[j] = ' ';
825#endif
826
827 /* mask "symbols" or 'symbols' */
828 c2 = 0;
829 for (i = 0; int_buf[i]; i++) {
830 c = int_buf[i];
831 if (c == '\'' || c == '"') {
832 if (c2 == 0)
833 c2 = c;
834 else {
835 if (c == c2)
836 c2 = 0;
837 else
838 int_buf[i] |= QUOT;
839 }
840 } else if (c2 != 0 && c != '$')
841 int_buf[i] |= QUOT;
842 }
843
844 /* skip commands with arguments if line have commands delimiters */
845 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
846 for (i = 0; int_buf[i]; i++) {
847 c = int_buf[i];
848 c2 = int_buf[i + 1];
849 j = i ? int_buf[i - 1] : -1;
850 command_mode = 0;
851 if (c == ';' || c == '&' || c == '|') {
852 command_mode = 1 + (c == c2);
853 if (c == '&') {
854 if (j == '>' || j == '<')
855 command_mode = 0;
856 } else if (c == '|' && j == '>')
857 command_mode = 0;
858 }
859 if (command_mode) {
860 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000861 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000862 }
863 }
864 /* collapse `command...` */
865 for (i = 0; int_buf[i]; i++)
866 if (int_buf[i] == '`') {
867 for (j = i + 1; int_buf[j]; j++)
868 if (int_buf[j] == '`') {
869 collapse_pos(i, j + 1);
870 j = 0;
871 break;
872 }
873 if (j) {
874 /* not found close ` - command mode, collapse all previous */
875 collapse_pos(0, i + 1);
876 break;
877 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000878 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000879 }
880
881 /* collapse (command...(command...)...) or {command...{command...}...} */
Eric Andersenc470f442003-07-28 09:56:35 +0000882 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000883 c2 = 0;
884 for (i = 0; int_buf[i]; i++)
885 if (int_buf[i] == '(' || int_buf[i] == '{') {
886 if (int_buf[i] == '(')
887 c++;
888 else
889 c2++;
890 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000891 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000892 }
893 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
894 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
895 if (int_buf[i] == ')')
896 c--;
897 else
898 c2--;
899 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000900 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000901 }
902
903 /* skip first not quote space */
904 for (i = 0; int_buf[i]; i++)
905 if (int_buf[i] != ' ')
906 break;
907 if (i)
908 collapse_pos(0, i);
909
910 /* set find mode for completion */
911 command_mode = FIND_EXE_ONLY;
912 for (i = 0; int_buf[i]; i++)
913 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
914 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000915 && matchBuf[pos_buf[0]]=='c'
916 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000917 command_mode = FIND_DIR_ONLY;
918 else {
919 command_mode = FIND_FILE_ONLY;
920 break;
921 }
922 }
923 /* "strlen" */
924 for (i = 0; int_buf[i]; i++);
925 /* find last word */
926 for (--i; i >= 0; i--) {
927 c = int_buf[i];
928 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
929 collapse_pos(0, i + 1);
930 break;
931 }
932 }
933 /* skip first not quoted '\'' or '"' */
934 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
935 /* collapse quote or unquote // or /~ */
Eric Andersenc470f442003-07-28 09:56:35 +0000936 while ((int_buf[i] & ~QUOT) == '/' &&
Mark Whitley7e5291f2001-03-08 19:31:12 +0000937 ((int_buf[i + 1] & ~QUOT) == '/'
938 || (int_buf[i + 1] & ~QUOT) == '~')) {
939 i++;
940 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000941
942 /* set only match and destroy quotes */
943 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000944 for (c = 0; pos_buf[i] >= 0; i++) {
945 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000946 j = pos_buf[i] + 1;
947 }
Eric Andersen4f990532001-05-31 17:15:57 +0000948 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000949 /* old lenght matchBuf with quotes symbols */
950 *len_with_quotes = j ? j - pos_buf[0] : 0;
951
952 return command_mode;
953}
954
Glenn L McGrath4d001292003-01-06 01:11:50 +0000955/*
956 display by column original ideas from ls applet,
957 very optimize by my :)
958*/
959static void showfiles(char **matches, int nfiles)
960{
961 int ncols, row;
962 int column_width = 0;
963 int nrows = nfiles;
964
965 /* find the longest file name- use that as the column width */
966 for (row = 0; row < nrows; row++) {
967 int l = strlen(matches[row]);
968
969 if (column_width < l)
970 column_width = l;
971 }
972 column_width += 2; /* min space for columns */
973 ncols = cmdedit_termw / column_width;
974
975 if (ncols > 1) {
976 nrows /= ncols;
977 if(nfiles % ncols)
978 nrows++; /* round up fractionals */
979 column_width = -column_width; /* for printf("%-Ns", ...); */
980 } else {
981 ncols = 1;
982 }
983 for (row = 0; row < nrows; row++) {
984 int n = row;
985 int nc;
986
987 for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++)
988 printf("%*s", column_width, matches[n]);
989 printf("%s\n", matches[n]);
990 }
991}
992
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000993
994static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000995{
996 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000997 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +0000998 static char **matches;
999
Eric Andersenc470f442003-07-28 09:56:35 +00001000 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001001 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001002 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001003 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001004 free(matches);
1005 matches = (char **) NULL;
1006 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001007 return;
1008 }
Matt Kraai1f0c4362001-12-20 23:13:26 +00001009 if (! *lastWasTab) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001010
1011 char *tmp;
1012 int len_found;
1013 char matchBuf[BUFSIZ];
1014 int find_type;
1015 int recalc_pos;
1016
Eric Andersenc470f442003-07-28 09:56:35 +00001017 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001018
1019 /* Make a local copy of the string -- up
1020 * to the position of the cursor */
1021 tmp = strncpy(matchBuf, command_ps, cursor);
1022 tmp[cursor] = 0;
1023
1024 find_type = find_match(matchBuf, &recalc_pos);
1025
1026 /* Free up any memory already allocated */
1027 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001028
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001029#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001030 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001031 * then try completing this word as a username. */
1032
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001033 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001034 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001035#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001036 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001037 * in the current working directory that matches. */
1038 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001039 matches =
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001040 exe_n_cwd_tab_completion(matchBuf,
1041 &num_matches, find_type);
1042 /* Remove duplicate found */
1043 if(matches) {
1044 int i, j;
1045 /* bubble */
1046 for(i=0; i<(num_matches-1); i++)
1047 for(j=i+1; j<num_matches; j++)
1048 if(matches[i]!=0 && matches[j]!=0 &&
1049 strcmp(matches[i], matches[j])==0) {
1050 free(matches[j]);
1051 matches[j]=0;
1052 }
1053 j=num_matches;
1054 num_matches = 0;
1055 for(i=0; i<j; i++)
1056 if(matches[i]) {
1057 if(!strcmp(matches[i], "./"))
1058 matches[i][1]=0;
1059 else if(!strcmp(matches[i], "../"))
1060 matches[i][2]=0;
1061 matches[num_matches++]=matches[i];
1062 }
1063 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001064 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001065 if (!matches || num_matches > 1) {
1066 char *tmp1;
1067
Mark Whitley4e338752001-01-26 20:42:23 +00001068 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001069 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +00001070 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001071 /* sort */
1072 qsort(matches, num_matches, sizeof(char *), match_compare);
1073
1074 /* find minimal match */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001075 tmp = bb_xstrdup(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001076 for (tmp1 = tmp; *tmp1; tmp1++)
1077 for (len_found = 1; len_found < num_matches; len_found++)
1078 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1079 *tmp1 = 0;
1080 break;
1081 }
Eric Andersenc470f442003-07-28 09:56:35 +00001082 if (*tmp == 0) { /* have unique */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001083 free(tmp);
1084 return;
1085 }
Eric Andersenc470f442003-07-28 09:56:35 +00001086 } else { /* one match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001087 tmp = matches[0];
1088 /* for next completion current found */
1089 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001090 }
1091
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001092 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001093 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001094 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001095
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001096 /* before word for match */
1097 command_ps[cursor - recalc_pos] = 0;
1098 /* save tail line */
1099 strcpy(matchBuf, command_ps + cursor);
1100 /* add match */
1101 strcat(command_ps, tmp);
1102 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001103 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001104 /* back to begin word for match */
1105 input_backward(recalc_pos);
1106 /* new pos */
1107 recalc_pos = cursor + len_found;
1108 /* new len */
1109 len = strlen(command_ps);
1110 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001111 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001112 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001113 if (tmp != matches[0])
1114 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001115 } else {
1116 /* Ok -- the last char was a TAB. Since they
1117 * just hit TAB again, print a list of all the
1118 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001119 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001120 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001121
1122 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001123 goto_new_line();
Glenn L McGrath4d001292003-01-06 01:11:50 +00001124 showfiles(matches, num_matches);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001125 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001126 }
1127 }
1128}
Eric Andersenc470f442003-07-28 09:56:35 +00001129#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001130
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001131#if MAX_HISTORY >= 1
1132static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001133{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001134 if(command_ps[0] != 0 || history[cur_history] == 0) {
1135 free(history[cur_history]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001136 history[cur_history] = bb_xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001137 }
1138 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +00001139}
1140
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001141static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001142{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001143 int ch = cur_history;
1144
1145 if (ch < n_history) {
1146 get_previous_history(); /* save the current history line */
1147 return (cur_history = ch+1);
1148 } else {
1149 beep();
1150 return 0;
1151 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001152}
Robert Griebl350d26b2002-12-03 22:45:46 +00001153
Robert Griebl350d26b2002-12-03 22:45:46 +00001154#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001155extern void load_history ( const char *fromfile )
1156{
Robert Griebl350d26b2002-12-03 22:45:46 +00001157 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001158 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001159
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001160 /* cleanup old */
1161
1162 for(hi = n_history; hi > 0; ) {
1163 hi--;
1164 free ( history [hi] );
Robert Griebl350d26b2002-12-03 22:45:46 +00001165 }
1166
1167 if (( fp = fopen ( fromfile, "r" ))) {
Eric Andersenc470f442003-07-28 09:56:35 +00001168
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001169 for ( hi = 0; hi < MAX_HISTORY; ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001170 char * hl = bb_get_chomped_line_from_file(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001171 int l;
1172
1173 if(!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001174 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001175 l = strlen(hl);
1176 if(l >= BUFSIZ)
1177 hl[BUFSIZ-1] = 0;
1178 if(l == 0 || hl[0] == ' ') {
1179 free(hl);
1180 continue;
1181 }
1182 history [hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001183 }
1184 fclose ( fp );
1185 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001186 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001187}
1188
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001189extern void save_history ( const char *tofile )
Robert Griebl350d26b2002-12-03 22:45:46 +00001190{
Robert Griebl350d26b2002-12-03 22:45:46 +00001191 FILE *fp = fopen ( tofile, "w" );
Eric Andersenc470f442003-07-28 09:56:35 +00001192
Robert Griebl350d26b2002-12-03 22:45:46 +00001193 if ( fp ) {
1194 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001195
Robert Griebl350d26b2002-12-03 22:45:46 +00001196 for ( i = 0; i < n_history; i++ ) {
Eric Andersen27bb7902003-12-23 20:24:51 +00001197 fprintf(fp, "%s\n", history [i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001198 }
1199 fclose ( fp );
1200 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001201}
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001202#endif
Robert Griebl350d26b2002-12-03 22:45:46 +00001203
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001204#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001205
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001206enum {
1207 ESC = 27,
1208 DEL = 127,
1209};
1210
1211
Erik Andersen6273f652000-03-17 01:12:41 +00001212/*
1213 * This function is used to grab a character buffer
1214 * from the input file descriptor and allows you to
1215 * a string with full command editing (sortof like
1216 * a mini readline).
1217 *
1218 * The following standard commands are not implemented:
1219 * ESC-b -- Move back one word
1220 * ESC-f -- Move forward one word
1221 * ESC-d -- Delete back one word
1222 * ESC-h -- Delete forward one word
1223 * CTL-t -- Transpose two characters
1224 *
1225 * Furthermore, the "vi" command editing keys are not implemented.
1226 *
Erik Andersen6273f652000-03-17 01:12:41 +00001227 */
Eric Andersenc470f442003-07-28 09:56:35 +00001228
Eric Andersen044228d2001-07-17 01:12:36 +00001229
1230int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001231{
1232
Erik Andersenc7c634b2000-03-19 05:28:55 +00001233 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001234 int lastWasTab = FALSE;
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001235 unsigned char c = 0;
Erik Andersen13456d12000-03-16 08:09:57 +00001236
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001237 /* prepare before init handlers */
Eric Andersenc470f442003-07-28 09:56:35 +00001238 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001239 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001240 command_ps = command;
1241
Eric Andersen34506362001-08-02 05:02:46 +00001242 getTermSettings(0, (void *) &initial_settings);
1243 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1244 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1245 /* Turn off echoing and CTRL-C, so we can trap it */
1246 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Eric Andersen34506362001-08-02 05:02:46 +00001247 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1248 new_settings.c_cc[VMIN] = 1;
1249 new_settings.c_cc[VTIME] = 0;
1250 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001251# ifndef _POSIX_VDISABLE
1252# define _POSIX_VDISABLE '\0'
1253# endif
Eric Andersenc470f442003-07-28 09:56:35 +00001254 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001255 command[0] = 0;
1256
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001257 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001258 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001259
Eric Andersen6faae7d2001-02-16 20:09:17 +00001260 /* Now initialize things */
1261 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001262 /* Print out the command prompt */
1263 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001264
Erik Andersenc7c634b2000-03-19 05:28:55 +00001265 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001266
Eric Andersenc470f442003-07-28 09:56:35 +00001267 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001268
Eric Andersen7467c8d2001-07-12 20:26:32 +00001269 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001270 /* if we can't read input then exit */
1271 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001272
Erik Andersen13456d12000-03-16 08:09:57 +00001273 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001274 case '\n':
1275 case '\r':
1276 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001277 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001278 break_out = 1;
1279 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001280 case 1:
1281 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001282 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001283 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001284 case 2:
1285 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001286 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001287 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001288 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001289 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001290 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001291 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001292 len = 0;
1293 lastWasTab = FALSE;
1294 put_prompt();
1295 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001296 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001297 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001298 * if the len=0 and no chars to delete */
1299 if (len == 0) {
Eric Andersened424db2001-04-23 15:28:28 +00001300prepare_to_die:
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001301#if !defined(CONFIG_ASH)
Mark Whitley4e338752001-01-26 20:42:23 +00001302 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001303 goto_new_line();
1304 /* cmdedit_reset_term() called in atexit */
1305 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001306#else
1307 break_out = -1; /* for control stoped jobs */
1308 break;
1309#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001310 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001311 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001312 }
1313 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001314 case 5:
1315 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001316 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001317 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001318 case 6:
1319 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001320 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001321 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001322 case '\b':
1323 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001324 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001325 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001326 break;
1327 case '\t':
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001328#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001329 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001330#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001331 break;
Eric Andersen65a07302002-04-13 13:26:49 +00001332 case 11:
Eric Andersenc470f442003-07-28 09:56:35 +00001333 /* Control-k -- clear to end of line */
Eric Andersen65a07302002-04-13 13:26:49 +00001334 *(command + cursor) = 0;
1335 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001336 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001337 break;
Eric Andersenc470f442003-07-28 09:56:35 +00001338 case 12:
Eric Andersen27bb7902003-12-23 20:24:51 +00001339 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001340 printf("\033[H");
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001341 redraw(0, len-cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001342 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001343#if MAX_HISTORY >= 1
Erik Andersenf0657d32000-04-12 17:49:52 +00001344 case 14:
1345 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001346 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001347 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001348 break;
1349 case 16:
1350 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001351 if (cur_history > 0) {
1352 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001353 goto rewrite_line;
1354 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001355 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001356 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001357 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001358#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001359 case 21:
1360 /* Control-U -- Clear line before cursor */
1361 if (cursor) {
1362 strcpy(command, command + cursor);
1363 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001364 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001365 break;
Eric Andersen27bb7902003-12-23 20:24:51 +00001366 case 23:
1367 /* Control-W -- Remove the last word */
1368 while (cursor > 0 && isspace(command[cursor-1]))
1369 input_backspace();
1370 while (cursor > 0 &&!isspace(command[cursor-1]))
1371 input_backspace();
1372 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001373 case ESC:{
1374 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001375 if (safe_read(0, &c, 1) < 1)
1376 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001377 /* different vt100 emulations */
1378 if (c == '[' || c == 'O') {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001379 if (safe_read(0, &c, 1) < 1)
1380 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001381 }
1382 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001383#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001384 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001385
1386 input_tab(&lastWasTab);
1387 break;
1388#endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001389#if MAX_HISTORY >= 1
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001390 case 'A':
1391 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001392 if (cur_history > 0) {
1393 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001394 goto rewrite_line;
1395 } else {
1396 beep();
1397 }
1398 break;
1399 case 'B':
1400 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001401 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001402 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001403 /* Rewrite the line with the selected history item */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001404rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001405 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001406 len = strlen(strcpy(command, history[cur_history]));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001407 /* redraw and go to end line */
1408 redraw(cmdedit_y, 0);
1409 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001410#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001411 case 'C':
1412 /* Right Arrow -- Move forward one character */
1413 input_forward();
1414 break;
1415 case 'D':
1416 /* Left Arrow -- Move back one character */
1417 input_backward(1);
1418 break;
1419 case '3':
1420 /* Delete */
1421 input_delete();
1422 break;
1423 case '1':
1424 case 'H':
1425 /* Home (Ctrl-A) */
1426 input_backward(cursor);
1427 break;
1428 case '4':
1429 case 'F':
1430 /* End (Ctrl-E) */
1431 input_end();
1432 break;
1433 default:
1434 if (!(c >= '1' && c <= '9'))
1435 c = 0;
1436 beep();
1437 }
1438 if (c >= '1' && c <= '9')
1439 do
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 while (c != '~');
1443 break;
1444 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001445
Eric Andersenc470f442003-07-28 09:56:35 +00001446 default: /* If it's regular input, do the normal thing */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001447#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001448 /* Control-V -- Add non-printable symbol */
1449 if (c == 22) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001450 if (safe_read(0, &c, 1) < 1)
1451 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001452 if (c == 0) {
1453 beep();
1454 break;
1455 }
1456 } else
1457#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001458 if (!Isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001459 break;
1460
Eric Andersenc470f442003-07-28 09:56:35 +00001461 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001462 break;
1463
1464 len++;
1465
Eric Andersenc470f442003-07-28 09:56:35 +00001466 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001467 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001468 *(command + cursor + 1) = 0;
1469 cmdedit_set_out_char(0);
Eric Andersenc470f442003-07-28 09:56:35 +00001470 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001471 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001472
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001473 memmove(command + sc + 1, command + sc, len - sc);
1474 *(command + sc) = c;
1475 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001476 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001477 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001478 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001479 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001480 }
1481
Erik Andersenc7c634b2000-03-19 05:28:55 +00001482 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001483 }
Eric Andersenc470f442003-07-28 09:56:35 +00001484 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001485 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001486
1487 if (c != '\t')
1488 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001489 }
1490
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001491 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001492 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001493
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001494#if MAX_HISTORY >= 1
Erik Andersenc7c634b2000-03-19 05:28:55 +00001495 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001496 /* cleanup may be saved current command line */
1497 free(history[MAX_HISTORY]);
1498 history[MAX_HISTORY] = 0;
Eric Andersenc470f442003-07-28 09:56:35 +00001499 if (len) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001500 int i = n_history;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001501 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001502 if (i >= MAX_HISTORY) {
1503 free(history[0]);
1504 for(i = 0; i < (MAX_HISTORY-1); i++)
1505 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001506 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001507 history[i++] = bb_xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001508 cur_history = i;
1509 n_history = i;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001510#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001511 num_ok_lines++;
1512#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001513 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001514#else /* MAX_HISTORY < 1 */
1515#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
1516 if (len) { /* no put empty line */
1517 num_ok_lines++;
1518 }
1519#endif
1520#endif /* MAX_HISTORY >= 1 */
Eric Andersen27bb7902003-12-23 20:24:51 +00001521 if (break_out > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001522 command[len++] = '\n'; /* set '\n' */
1523 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001524 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001525#if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersenc470f442003-07-28 09:56:35 +00001526 input_tab(0); /* strong free */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001527#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001528#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001529 free(cmdedit_prompt);
1530#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001531 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001532 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001533}
1534
Eric Andersen7467c8d2001-07-12 20:26:32 +00001535
1536
Eric Andersenc470f442003-07-28 09:56:35 +00001537#endif /* CONFIG_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001538
1539
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001540#ifdef TEST
1541
Manuel Novoa III cad53642003-03-19 09:13:01 +00001542const char *bb_applet_name = "debug stuff usage";
Eric Andersene5dfced2001-04-09 22:48:12 +00001543
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001544#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001545#include <locale.h>
1546#endif
1547
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001548int main(int argc, char **argv)
1549{
1550 char buff[BUFSIZ];
1551 char *prompt =
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001552#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001553 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001554\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001555\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001556#else
1557 "% ";
1558#endif
1559
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001560#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001561 setlocale(LC_ALL, "");
1562#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001563 while(1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001564 int l;
Eric Andersen27bb7902003-12-23 20:24:51 +00001565 l = cmdedit_read_input(prompt, buff);
1566 if(l > 0 && buff[l-1] == '\n') {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001567 buff[l-1] = 0;
Eric Andersen27bb7902003-12-23 20:24:51 +00001568 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1569 } else {
1570 break;
1571 }
Eric Andersen7467c8d2001-07-12 20:26:32 +00001572 }
Eric Andersen27bb7902003-12-23 20:24:51 +00001573 printf("*** cmdedit_read_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001574 return 0;
1575}
1576
Eric Andersenc470f442003-07-28 09:56:35 +00001577#endif /* TEST */