blob: c0fa1b2552c90643b6560e323371f051835df8e2 [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
Glenn L McGrath67285962004-01-14 09:34:51 +000046#include "../shell/cmdedit.h"
47
Glenn L McGrath475820c2004-01-22 12:42:23 +000048
Eric Andersenbdfd0d72001-10-24 05:00:29 +000049#ifdef CONFIG_LOCALE_SUPPORT
Eric Andersene5dfced2001-04-09 22:48:12 +000050#define Isprint(c) isprint((c))
51#else
52#define Isprint(c) ( (c) >= ' ' && (c) != ((unsigned char)'\233') )
53#endif
54
Glenn L McGrath3b251852004-01-03 12:07:32 +000055#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000056
Eric Andersen27bb7902003-12-23 20:24:51 +000057/* pretect redefined for test */
58#undef CONFIG_FEATURE_COMMAND_EDITING
59#undef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
60#undef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
61#undef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
62#undef CONFIG_FEATURE_CLEAN_UP
63
Eric Andersenbdfd0d72001-10-24 05:00:29 +000064#define CONFIG_FEATURE_COMMAND_EDITING
65#define CONFIG_FEATURE_COMMAND_TAB_COMPLETION
66#define CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
67#define CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
68#define CONFIG_FEATURE_CLEAN_UP
Eric Andersen5f2c79d2001-02-16 18:36:04 +000069
Eric Andersenc470f442003-07-28 09:56:35 +000070#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000071
Eric Andersenbdfd0d72001-10-24 05:00:29 +000072#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5165fbe2001-02-20 06:42:29 +000073#include <dirent.h>
74#include <sys/stat.h>
75#endif
76
Eric Andersenbdfd0d72001-10-24 05:00:29 +000077#ifdef CONFIG_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000078
Eric Andersenbdfd0d72001-10-24 05:00:29 +000079#if defined(CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION) || defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
80#define CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +000081#endif
82
Eric Andersenbdfd0d72001-10-24 05:00:29 +000083#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Glenn L McGrath475820c2004-01-22 12:42:23 +000084#include "pwd_.h"
Eric Andersenc470f442003-07-28 09:56:35 +000085#endif /* advanced FEATURES */
Eric Andersenaf4ac772001-02-01 22:43:49 +000086
87
Eric Andersen5f2c79d2001-02-16 18:36:04 +000088/* Maximum length of the linked list for the command line history */
Robert Griebl350d26b2002-12-03 22:45:46 +000089#ifndef CONFIG_FEATURE_COMMAND_HISTORY
90#define MAX_HISTORY 15
91#else
92#define MAX_HISTORY CONFIG_FEATURE_COMMAND_HISTORY
93#endif
94
Glenn L McGrath062c74f2002-11-27 09:29:49 +000095#if MAX_HISTORY < 1
96#warning cmdedit: You set MAX_HISTORY < 1. The history algorithm switched off.
97#else
98static char *history[MAX_HISTORY+1]; /* history + current */
99/* saved history lines */
100static int n_history;
101/* current pointer to history line */
102static int cur_history;
103#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +0000104
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000105#include <termios.h>
106#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
107#define getTermSettings(fd,argp) tcgetattr(fd, argp);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000108
109/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000110static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000111
112
Mark Whitley4e338752001-01-26 20:42:23 +0000113static
Eric Andersenc470f442003-07-28 09:56:35 +0000114volatile int cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +0000115static
Eric Andersenc470f442003-07-28 09:56:35 +0000116volatile int handlers_sets = 0; /* Set next bites: */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000117
Mark Whitley4e338752001-01-26 20:42:23 +0000118enum {
Eric Andersenc470f442003-07-28 09:56:35 +0000119 SET_ATEXIT = 1, /* when atexit() has been called
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000120 and get euid,uid,gid to fast compare */
Eric Andersen7467c8d2001-07-12 20:26:32 +0000121 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
122 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000123};
Erik Andersen13456d12000-03-16 08:09:57 +0000124
Mark Whitley4e338752001-01-26 20:42:23 +0000125
Eric Andersenc470f442003-07-28 09:56:35 +0000126static int cmdedit_x; /* real x terminal position */
127static int cmdedit_y; /* pseudoreal y terminal position */
128static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000129
Eric Andersenc470f442003-07-28 09:56:35 +0000130static int cursor; /* required global for signal handler */
131static int len; /* --- "" - - "" - -"- --""-- --""--- */
132static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000133static
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000134#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000135 const
136#endif
Eric Andersenc470f442003-07-28 09:56:35 +0000137char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000138
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000139#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000140static char *user_buf = "";
141static char *home_pwd_buf = "";
142static int my_euid;
143#endif
144
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000145#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000146static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000147static int num_ok_lines = 1;
148#endif
149
150
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000151#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000152
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000153#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000154static int my_euid;
155#endif
156
157static int my_uid;
158static int my_gid;
159
Eric Andersenc470f442003-07-28 09:56:35 +0000160#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
161
Mark Whitley4e338752001-01-26 20:42:23 +0000162static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000163
Mark Whitley4e338752001-01-26 20:42:23 +0000164static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000165{
Eric Andersenc470f442003-07-28 09:56:35 +0000166 static sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000167
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000168 /* emulate || signal call */
169 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Eric Andersen8efe9672003-09-15 08:33:45 +0000170 int width = 0;
171 get_terminal_width_height(0, &width, NULL);
172 cmdedit_setwidth(width, nsig == SIGWINCH);
Mark Whitley4e338752001-01-26 20:42:23 +0000173 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000174 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000175
Eric Andersenc470f442003-07-28 09:56:35 +0000176 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000177 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersenc470f442003-07-28 09:56:35 +0000178 else if (nsig == SIGWINCH) /* signaled called handler */
179 signal(SIGWINCH, win_changed); /* set for next call */
180 else /* nsig == 0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000181 /* set previous handler */
Eric Andersenc470f442003-07-28 09:56:35 +0000182 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000183}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000184
185static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000186{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000187 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000188/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000189 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000190 handlers_sets &= ~SET_RESET_TERM;
191 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000192 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000193 /* reset SIGWINCH handler to previous (default) */
194 win_changed(0);
195 handlers_sets &= ~SET_WCHG_HANDLERS;
196 }
197 fflush(stdout);
Erik Andersen13456d12000-03-16 08:09:57 +0000198}
199
Mark Whitley4e338752001-01-26 20:42:23 +0000200
Mark Whitley4e338752001-01-26 20:42:23 +0000201/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000202static void cmdedit_set_out_char(int next_char)
203{
204
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000205 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000206
207 if (c == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000208 c = ' '; /* destroy end char? */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000209#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000210 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000211 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000212 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000213 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000214 c += '@';
215 if (c == 127)
216 c = '?';
217 printf("\033[7m%c\033[0m", c);
218 } else
219#endif
220 putchar(c);
221 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000222 /* terminal is scrolled down */
223 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000224 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000225
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000226 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000227 next_char = ' ';
228 /* destroy "(auto)margin" */
229 putchar(next_char);
230 putchar('\b');
231 }
232 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000233}
234
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000235/* Move to end line. Bonus: rewrite line from cursor */
236static void input_end(void)
237{
238 while (cursor < len)
239 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000240}
241
242/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000243static void goto_new_line(void)
244{
Mark Whitley4e338752001-01-26 20:42:23 +0000245 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000246 if (cmdedit_x)
247 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000248}
249
250
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000251static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000252{
Robert Grieblb2301592002-07-30 23:13:51 +0000253 if ( s )
254 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000255}
Eric Andersen81fe1232003-07-29 06:38:40 +0000256
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000257static inline void beep(void)
258{
259 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000260}
261
Mark Whitley4e338752001-01-26 20:42:23 +0000262/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000263/* special for slow terminal */
264static void input_backward(int num)
265{
266 if (num > cursor)
267 num = cursor;
Eric Andersenc470f442003-07-28 09:56:35 +0000268 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000269
Eric Andersenc470f442003-07-28 09:56:35 +0000270 if (cmdedit_x >= num) { /* no to up line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000271 cmdedit_x -= num;
272 if (num < 4)
273 while (num-- > 0)
274 putchar('\b');
275
276 else
277 printf("\033[%dD", num);
278 } else {
279 int count_y;
280
281 if (cmdedit_x) {
Eric Andersenc470f442003-07-28 09:56:35 +0000282 putchar('\r'); /* back to first terminal pos. */
283 num -= cmdedit_x; /* set previous backward */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000284 }
285 count_y = 1 + num / cmdedit_termw;
286 printf("\033[%dA", count_y);
287 cmdedit_y -= count_y;
288 /* require forward after uping */
289 cmdedit_x = cmdedit_termw * count_y - num;
Eric Andersenc470f442003-07-28 09:56:35 +0000290 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000291 }
Erik Andersen13456d12000-03-16 08:09:57 +0000292}
293
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000294static void put_prompt(void)
295{
296 out1str(cmdedit_prompt);
Eric Andersenc470f442003-07-28 09:56:35 +0000297 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000298 cursor = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000299 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000300}
301
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000302#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000303static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000304{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000305 cmdedit_prompt = prmt_ptr;
306 cmdedit_prmt_len = strlen(prmt_ptr);
307 put_prompt();
308}
309#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000310static void parse_prompt(const char *prmt_ptr)
311{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000312 int prmt_len = 0;
313 int sub_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000314 char flg_not_length = '[';
315 char *prmt_mem_ptr = xcalloc(1, 1);
316 char *pwd_buf = xgetcwd(0);
317 char buf2[PATH_MAX + 1];
318 char buf[2];
319 char c;
320 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000321
Eric Andersen5f265b72001-05-11 16:58:46 +0000322 if (!pwd_buf) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000323 pwd_buf=(char *)bb_msg_unknown;
Eric Andersen5f265b72001-05-11 16:58:46 +0000324 }
325
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000326 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000327 pbuf = buf;
328 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000329 c = *prmt_ptr++;
330 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000331 const char *cp = prmt_ptr;
332 int l;
Eric Andersenc470f442003-07-28 09:56:35 +0000333
Manuel Novoa III cad53642003-03-19 09:13:01 +0000334 c = bb_process_escape_sequence(&prmt_ptr);
Eric Andersene5dfced2001-04-09 22:48:12 +0000335 if(prmt_ptr==cp) {
336 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000337 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000338 c = *prmt_ptr++;
339 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000340#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000341 case 'u':
342 pbuf = user_buf;
343 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000344#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000345 case 'h':
346 pbuf = hostname_buf;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000347 if (pbuf == 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000348 pbuf = xcalloc(256, 1);
349 if (gethostname(pbuf, 255) < 0) {
350 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000351 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000352 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000353
354 if (s)
355 *s = 0;
356 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000357 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000358 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000359 break;
360 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000361 c = my_euid == 0 ? '#' : '$';
362 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000363#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000364 case 'w':
365 pbuf = pwd_buf;
366 l = strlen(home_pwd_buf);
367 if (home_pwd_buf[0] != 0 &&
368 strncmp(home_pwd_buf, pbuf, l) == 0 &&
369 (pbuf[l]=='/' || pbuf[l]=='\0') &&
370 strlen(pwd_buf+l)<PATH_MAX) {
371 pbuf = buf2;
372 *pbuf = '~';
373 strcpy(pbuf+1, pwd_buf+l);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000374 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000375 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000376#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000377 case 'W':
378 pbuf = pwd_buf;
379 cp = strrchr(pbuf,'/');
380 if ( (cp != NULL) && (cp != pbuf) )
381 pbuf += (cp-pbuf)+1;
382 break;
383 case '!':
384 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
385 break;
386 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000387 c = '\033';
388 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000389 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000390 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000391 int h;
392 buf2[l++] = *prmt_ptr;
393 buf2[l] = 0;
394 h = strtol(buf2, &pbuf, 16);
395 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000396 l--;
397 break;
398 }
399 prmt_ptr++;
400 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000401 buf2[l] = 0;
402 c = (char)strtol(buf2, 0, 16);
403 if(c==0)
404 c = '?';
405 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000406 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000407 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000408 if (c == flg_not_length) {
409 flg_not_length = flg_not_length == '[' ? ']' : '[';
410 continue;
411 }
412 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000413 }
Eric Andersenc470f442003-07-28 09:56:35 +0000414 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000415 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000416 if(pbuf == buf)
417 *pbuf = c;
418 prmt_len += strlen(pbuf);
419 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000420 if (flg_not_length == ']')
421 sub_len++;
422 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000423 if(pwd_buf!=(char *)bb_msg_unknown)
Eric Andersen8f697842001-07-02 15:36:57 +0000424 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000425 cmdedit_prompt = prmt_mem_ptr;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000426 cmdedit_prmt_len = prmt_len - sub_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000427 put_prompt();
428}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000429#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430
431
432/* draw promt, editor line, and clear tail */
433static void redraw(int y, int back_cursor)
434{
Eric Andersenc470f442003-07-28 09:56:35 +0000435 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000436 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000437 putchar('\r');
438 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000439 input_end(); /* rewrite */
440 printf("\033[J"); /* destroy tail after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000441 input_backward(back_cursor);
442}
443
Erik Andersenf0657d32000-04-12 17:49:52 +0000444/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000445static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000446{
Mark Whitley4e338752001-01-26 20:42:23 +0000447 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000448
Mark Whitley4e338752001-01-26 20:42:23 +0000449 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000450 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000451
452 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000453 len--;
Eric Andersenc470f442003-07-28 09:56:35 +0000454 input_end(); /* rewtite new line */
455 cmdedit_set_out_char(0); /* destroy end char */
456 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000457}
458
Mark Whitley4e338752001-01-26 20:42:23 +0000459/* Delete the char in back of the cursor */
460static void input_backspace(void)
461{
462 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000463 input_backward(1);
464 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000465 }
466}
467
468
Erik Andersenf0657d32000-04-12 17:49:52 +0000469/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000470static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000471{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000472 if (cursor < len)
473 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000474}
475
476
Mark Whitley4e338752001-01-26 20:42:23 +0000477static void cmdedit_setwidth(int w, int redraw_flg)
478{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000479 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000480 if (w <= cmdedit_termw) {
481 cmdedit_termw = cmdedit_termw % w;
482 }
Mark Whitley4e338752001-01-26 20:42:23 +0000483 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000484 cmdedit_termw = w;
485
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000486 if (redraw_flg) {
487 /* new y for current cursor */
488 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000489
490 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000491 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
492 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000493 }
Eric Andersenc470f442003-07-28 09:56:35 +0000494 }
Mark Whitley4e338752001-01-26 20:42:23 +0000495}
496
Eric Andersen7467c8d2001-07-12 20:26:32 +0000497static void cmdedit_init(void)
Mark Whitley4e338752001-01-26 20:42:23 +0000498{
Eric Andersen61173a52001-03-19 17:48:55 +0000499 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000500 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
501 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000502 win_changed(-SIGWINCH);
503 handlers_sets |= SET_WCHG_HANDLERS;
504 }
505
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000506 if ((handlers_sets & SET_ATEXIT) == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000507#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000508 struct passwd *entry;
509
510 my_euid = geteuid();
511 entry = getpwuid(my_euid);
512 if (entry) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000513 user_buf = bb_xstrdup(entry->pw_name);
514 home_pwd_buf = bb_xstrdup(entry->pw_dir);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000515 }
516#endif
517
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000518#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000519
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000520#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000521 my_euid = geteuid();
522#endif
523 my_uid = getuid();
524 my_gid = getgid();
Eric Andersenc470f442003-07-28 09:56:35 +0000525#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000526 handlers_sets |= SET_ATEXIT;
Eric Andersenc470f442003-07-28 09:56:35 +0000527 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000528 }
Mark Whitley4e338752001-01-26 20:42:23 +0000529}
Erik Andersenf0657d32000-04-12 17:49:52 +0000530
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000531#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000532
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000533static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000534{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000535 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
536 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
537 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
538 (st->st_mode & S_IXOTH)) return TRUE;
539 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000540}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000541
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000542#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000543
544static char **username_tab_completion(char *ud, int *num_matches)
545{
546 struct passwd *entry;
547 int userlen;
548 char *temp;
549
550
Eric Andersenc470f442003-07-28 09:56:35 +0000551 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000552 userlen = strlen(ud);
553
Eric Andersenc470f442003-07-28 09:56:35 +0000554 if (num_matches == 0) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000555 char *sav_ud = ud - 1;
556 char *home = 0;
557
Eric Andersenc470f442003-07-28 09:56:35 +0000558 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000559 home = home_pwd_buf;
560 } else {
561 /* "~user/..." */
562 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000563 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000564 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000565 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000566 ud = temp;
567 if (entry)
568 home = entry->pw_dir;
569 }
570 if (home) {
571 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000572 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000573
574 /* /home/user/... */
575 sprintf(temp2, "%s%s", home, ud);
576 strcpy(sav_ud, temp2);
577 }
578 }
Eric Andersenc470f442003-07-28 09:56:35 +0000579 return 0; /* void, result save to argument :-) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000580 } else {
581 /* "~[^/]*" */
582 char **matches = (char **) NULL;
583 int nm = 0;
584
585 setpwent();
586
587 while ((entry = getpwent()) != NULL) {
588 /* Null usernames should result in all users as possible completions. */
589 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
590
Eric Andersenc470f442003-07-28 09:56:35 +0000591 bb_xasprintf(&temp, "~%s/", entry->pw_name);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000592 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
593
594 matches[nm++] = temp;
595 }
596 }
597
598 endpwent();
599 (*num_matches) = nm;
600 return (matches);
601 }
602}
Eric Andersenc470f442003-07-28 09:56:35 +0000603#endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000604
605enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000606 FIND_EXE_ONLY = 0,
607 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000608 FIND_FILE_ONLY = 2,
609};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000610
Glenn L McGrath67285962004-01-14 09:34:51 +0000611#ifdef CONFIG_ASH
612const char *cmdedit_path_lookup;
613#else
614#define cmdedit_path_lookup getenv("PATH")
615#endif
616
Mark Whitley4e338752001-01-26 20:42:23 +0000617static int path_parse(char ***p, int flags)
618{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000619 int npth;
Glenn L McGrath67285962004-01-14 09:34:51 +0000620 const char *tmp;
621 const char *pth;
Mark Whitley4e338752001-01-26 20:42:23 +0000622
Mark Whitley4e338752001-01-26 20:42:23 +0000623 /* if not setenv PATH variable, to search cur dir "." */
Glenn L McGrath67285962004-01-14 09:34:51 +0000624 if (flags != FIND_EXE_ONLY || (pth = cmdedit_path_lookup) == 0 ||
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000625 /* PATH=<empty> or PATH=:<empty> */
626 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000627 return 1;
628 }
629
630 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000631 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000632
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000633 for (;;) {
Eric Andersenc470f442003-07-28 09:56:35 +0000634 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000635 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000636 if (tmp) {
637 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000638 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000639 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000640 break;
641 }
642
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000643 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000644
645 tmp = pth;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000646 (*p)[0] = bb_xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000647 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000648
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000649 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000650 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000651 if (tmp) {
Eric Andersenc470f442003-07-28 09:56:35 +0000652 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000653 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000654 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000655 } else
656 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000657 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000658 }
659
660 return npth;
661}
662
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000663static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000664{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000665 int l = 0;
666 char *s = xmalloc((strlen(found) + 1) * 2);
667
668 while (*found) {
669 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
670 s[l++] = '\\';
671 s[l++] = *found++;
672 }
673 s[l] = 0;
674 return s;
675}
676
677static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000678 int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000679{
680
681 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000682 DIR *dir;
683 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000684 char dirbuf[BUFSIZ];
685 int nm = *num_matches;
686 struct stat st;
687 char *path1[1];
688 char **paths = path1;
689 int npaths;
690 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000691 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000692 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000693
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000694 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000695
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000696 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000697 /* no dir, if flags==EXE_ONLY - get paths, else "." */
698 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000699 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000700 } else {
701 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000702 /* save for change */
703 strcpy(dirbuf, command);
704 /* set dir only */
705 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000706#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000707 if (dirbuf[0] == '~') /* ~/... or ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000708 username_tab_completion(dirbuf, 0);
709#endif
710 /* "strip" dirname in command */
711 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000712
Mark Whitley4e338752001-01-26 20:42:23 +0000713 paths[0] = dirbuf;
Eric Andersenc470f442003-07-28 09:56:35 +0000714 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000715 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000716
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000717 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000718
Mark Whitley4e338752001-01-26 20:42:23 +0000719 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000720 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000721 continue;
722
723 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000724 char *str_found = next->d_name;
725
Mark Whitley4e338752001-01-26 20:42:23 +0000726 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000727 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000728 continue;
729 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000730 if (*str_found == '.' && *pfind == 0) {
731 if (*paths[i] == '/' && paths[i][1] == 0
Eric Andersenc470f442003-07-28 09:56:35 +0000732 && str_found[1] == 0) str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000733 else
Mark Whitley4e338752001-01-26 20:42:23 +0000734 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000735 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000736 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000737 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000738 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000739 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000740 /* find with dirs ? */
741 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000742 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000743 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000744 /* name is directory */
Eric Andersene5dfced2001-04-09 22:48:12 +0000745 str_found = found;
746 found = concat_path_file(found, "");
747 free(str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000748 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000749 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000750 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000751 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000752 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000753 str_found = add_quote_for_spec_chars(found);
754 if (type == FIND_FILE_ONLY ||
Matt Kraai1f0c4362001-12-20 23:13:26 +0000755 (type == FIND_EXE_ONLY && is_execute(&st)))
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000756 strcat(str_found, " ");
757 }
Mark Whitley4e338752001-01-26 20:42:23 +0000758 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000759 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
760
761 matches[nm++] = str_found;
Eric Andersene5dfced2001-04-09 22:48:12 +0000762cont:
763 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000764 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000765 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000766 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000767 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000768 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000769 free(paths);
770 }
Mark Whitley4e338752001-01-26 20:42:23 +0000771 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000772 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000773}
Erik Andersenf0657d32000-04-12 17:49:52 +0000774
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000775static int match_compare(const void *a, const void *b)
776{
777 return strcmp(*(char **) a, *(char **) b);
778}
779
780
781
782#define QUOT (UCHAR_MAX+1)
783
784#define collapse_pos(is, in) { \
Eric Andersen889a3012002-03-20 14:31:15 +0000785 memcpy(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
786 memcpy(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000787
788static int find_match(char *matchBuf, int *len_with_quotes)
789{
790 int i, j;
791 int command_mode;
792 int c, c2;
793 int int_buf[BUFSIZ + 1];
794 int pos_buf[BUFSIZ + 1];
795
796 /* set to integer dimension characters and own positions */
797 for (i = 0;; i++) {
798 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
799 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000800 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000801 break;
802 } else
803 pos_buf[i] = i;
804 }
805
806 /* mask \+symbol and convert '\t' to ' ' */
807 for (i = j = 0; matchBuf[i]; i++, j++)
808 if (matchBuf[i] == '\\') {
809 collapse_pos(j, j + 1);
810 int_buf[j] |= QUOT;
811 i++;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000812#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000813 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000814 int_buf[j] = ' ' | QUOT;
815#endif
816 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000817#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000818 else if (matchBuf[i] == '\t')
819 int_buf[j] = ' ';
820#endif
821
822 /* mask "symbols" or 'symbols' */
823 c2 = 0;
824 for (i = 0; int_buf[i]; i++) {
825 c = int_buf[i];
826 if (c == '\'' || c == '"') {
827 if (c2 == 0)
828 c2 = c;
829 else {
830 if (c == c2)
831 c2 = 0;
832 else
833 int_buf[i] |= QUOT;
834 }
835 } else if (c2 != 0 && c != '$')
836 int_buf[i] |= QUOT;
837 }
838
839 /* skip commands with arguments if line have commands delimiters */
840 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
841 for (i = 0; int_buf[i]; i++) {
842 c = int_buf[i];
843 c2 = int_buf[i + 1];
844 j = i ? int_buf[i - 1] : -1;
845 command_mode = 0;
846 if (c == ';' || c == '&' || c == '|') {
847 command_mode = 1 + (c == c2);
848 if (c == '&') {
849 if (j == '>' || j == '<')
850 command_mode = 0;
851 } else if (c == '|' && j == '>')
852 command_mode = 0;
853 }
854 if (command_mode) {
855 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000856 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000857 }
858 }
859 /* collapse `command...` */
860 for (i = 0; int_buf[i]; i++)
861 if (int_buf[i] == '`') {
862 for (j = i + 1; int_buf[j]; j++)
863 if (int_buf[j] == '`') {
864 collapse_pos(i, j + 1);
865 j = 0;
866 break;
867 }
868 if (j) {
869 /* not found close ` - command mode, collapse all previous */
870 collapse_pos(0, i + 1);
871 break;
872 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000873 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000874 }
875
876 /* collapse (command...(command...)...) or {command...{command...}...} */
Eric Andersenc470f442003-07-28 09:56:35 +0000877 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000878 c2 = 0;
879 for (i = 0; int_buf[i]; i++)
880 if (int_buf[i] == '(' || int_buf[i] == '{') {
881 if (int_buf[i] == '(')
882 c++;
883 else
884 c2++;
885 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000886 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000887 }
888 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
889 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
890 if (int_buf[i] == ')')
891 c--;
892 else
893 c2--;
894 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000895 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000896 }
897
898 /* skip first not quote space */
899 for (i = 0; int_buf[i]; i++)
900 if (int_buf[i] != ' ')
901 break;
902 if (i)
903 collapse_pos(0, i);
904
905 /* set find mode for completion */
906 command_mode = FIND_EXE_ONLY;
907 for (i = 0; int_buf[i]; i++)
908 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
909 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000910 && matchBuf[pos_buf[0]]=='c'
911 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000912 command_mode = FIND_DIR_ONLY;
913 else {
914 command_mode = FIND_FILE_ONLY;
915 break;
916 }
917 }
918 /* "strlen" */
919 for (i = 0; int_buf[i]; i++);
920 /* find last word */
921 for (--i; i >= 0; i--) {
922 c = int_buf[i];
923 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
924 collapse_pos(0, i + 1);
925 break;
926 }
927 }
928 /* skip first not quoted '\'' or '"' */
929 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
930 /* collapse quote or unquote // or /~ */
Eric Andersenc470f442003-07-28 09:56:35 +0000931 while ((int_buf[i] & ~QUOT) == '/' &&
Mark Whitley7e5291f2001-03-08 19:31:12 +0000932 ((int_buf[i + 1] & ~QUOT) == '/'
933 || (int_buf[i + 1] & ~QUOT) == '~')) {
934 i++;
935 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000936
937 /* set only match and destroy quotes */
938 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000939 for (c = 0; pos_buf[i] >= 0; i++) {
940 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000941 j = pos_buf[i] + 1;
942 }
Eric Andersen4f990532001-05-31 17:15:57 +0000943 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000944 /* old lenght matchBuf with quotes symbols */
945 *len_with_quotes = j ? j - pos_buf[0] : 0;
946
947 return command_mode;
948}
949
Glenn L McGrath4d001292003-01-06 01:11:50 +0000950/*
951 display by column original ideas from ls applet,
952 very optimize by my :)
953*/
954static void showfiles(char **matches, int nfiles)
955{
956 int ncols, row;
957 int column_width = 0;
958 int nrows = nfiles;
959
960 /* find the longest file name- use that as the column width */
961 for (row = 0; row < nrows; row++) {
962 int l = strlen(matches[row]);
963
964 if (column_width < l)
965 column_width = l;
966 }
967 column_width += 2; /* min space for columns */
968 ncols = cmdedit_termw / column_width;
969
970 if (ncols > 1) {
971 nrows /= ncols;
972 if(nfiles % ncols)
973 nrows++; /* round up fractionals */
974 column_width = -column_width; /* for printf("%-Ns", ...); */
975 } else {
976 ncols = 1;
977 }
978 for (row = 0; row < nrows; row++) {
979 int n = row;
980 int nc;
981
982 for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++)
983 printf("%*s", column_width, matches[n]);
984 printf("%s\n", matches[n]);
985 }
986}
987
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000988
989static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000990{
991 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000992 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +0000993 static char **matches;
994
Eric Andersenc470f442003-07-28 09:56:35 +0000995 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +0000996 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000997 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +0000998 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000999 free(matches);
1000 matches = (char **) NULL;
1001 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001002 return;
1003 }
Matt Kraai1f0c4362001-12-20 23:13:26 +00001004 if (! *lastWasTab) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001005
1006 char *tmp;
1007 int len_found;
1008 char matchBuf[BUFSIZ];
1009 int find_type;
1010 int recalc_pos;
1011
Eric Andersenc470f442003-07-28 09:56:35 +00001012 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001013
1014 /* Make a local copy of the string -- up
1015 * to the position of the cursor */
1016 tmp = strncpy(matchBuf, command_ps, cursor);
1017 tmp[cursor] = 0;
1018
1019 find_type = find_match(matchBuf, &recalc_pos);
1020
1021 /* Free up any memory already allocated */
1022 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001023
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001024#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001025 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001026 * then try completing this word as a username. */
1027
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001028 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001029 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001030#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001031 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001032 * in the current working directory that matches. */
1033 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001034 matches =
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001035 exe_n_cwd_tab_completion(matchBuf,
1036 &num_matches, find_type);
1037 /* Remove duplicate found */
1038 if(matches) {
1039 int i, j;
1040 /* bubble */
1041 for(i=0; i<(num_matches-1); i++)
1042 for(j=i+1; j<num_matches; j++)
1043 if(matches[i]!=0 && matches[j]!=0 &&
1044 strcmp(matches[i], matches[j])==0) {
1045 free(matches[j]);
1046 matches[j]=0;
1047 }
1048 j=num_matches;
1049 num_matches = 0;
1050 for(i=0; i<j; i++)
1051 if(matches[i]) {
1052 if(!strcmp(matches[i], "./"))
1053 matches[i][1]=0;
1054 else if(!strcmp(matches[i], "../"))
1055 matches[i][2]=0;
1056 matches[num_matches++]=matches[i];
1057 }
1058 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001059 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001060 if (!matches || num_matches > 1) {
1061 char *tmp1;
1062
Mark Whitley4e338752001-01-26 20:42:23 +00001063 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001064 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +00001065 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001066 /* sort */
1067 qsort(matches, num_matches, sizeof(char *), match_compare);
1068
1069 /* find minimal match */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001070 tmp = bb_xstrdup(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001071 for (tmp1 = tmp; *tmp1; tmp1++)
1072 for (len_found = 1; len_found < num_matches; len_found++)
1073 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1074 *tmp1 = 0;
1075 break;
1076 }
Eric Andersenc470f442003-07-28 09:56:35 +00001077 if (*tmp == 0) { /* have unique */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001078 free(tmp);
1079 return;
1080 }
Eric Andersenc470f442003-07-28 09:56:35 +00001081 } else { /* one match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001082 tmp = matches[0];
1083 /* for next completion current found */
1084 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001085 }
1086
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001087 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001088 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001089 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001090
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001091 /* before word for match */
1092 command_ps[cursor - recalc_pos] = 0;
1093 /* save tail line */
1094 strcpy(matchBuf, command_ps + cursor);
1095 /* add match */
1096 strcat(command_ps, tmp);
1097 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001098 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001099 /* back to begin word for match */
1100 input_backward(recalc_pos);
1101 /* new pos */
1102 recalc_pos = cursor + len_found;
1103 /* new len */
1104 len = strlen(command_ps);
1105 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001106 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001107 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001108 if (tmp != matches[0])
1109 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001110 } else {
1111 /* Ok -- the last char was a TAB. Since they
1112 * just hit TAB again, print a list of all the
1113 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001114 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001115 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001116
1117 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001118 goto_new_line();
Glenn L McGrath4d001292003-01-06 01:11:50 +00001119 showfiles(matches, num_matches);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001120 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001121 }
1122 }
1123}
Eric Andersenc470f442003-07-28 09:56:35 +00001124#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001125
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001126#if MAX_HISTORY >= 1
1127static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001128{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001129 if(command_ps[0] != 0 || history[cur_history] == 0) {
1130 free(history[cur_history]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001131 history[cur_history] = bb_xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001132 }
1133 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +00001134}
1135
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001136static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001137{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001138 int ch = cur_history;
1139
1140 if (ch < n_history) {
1141 get_previous_history(); /* save the current history line */
1142 return (cur_history = ch+1);
1143 } else {
1144 beep();
1145 return 0;
1146 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001147}
Robert Griebl350d26b2002-12-03 22:45:46 +00001148
Robert Griebl350d26b2002-12-03 22:45:46 +00001149#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001150extern void load_history ( const char *fromfile )
1151{
Robert Griebl350d26b2002-12-03 22:45:46 +00001152 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001153 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001154
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001155 /* cleanup old */
1156
1157 for(hi = n_history; hi > 0; ) {
1158 hi--;
1159 free ( history [hi] );
Robert Griebl350d26b2002-12-03 22:45:46 +00001160 }
1161
1162 if (( fp = fopen ( fromfile, "r" ))) {
Eric Andersenc470f442003-07-28 09:56:35 +00001163
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001164 for ( hi = 0; hi < MAX_HISTORY; ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001165 char * hl = bb_get_chomped_line_from_file(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001166 int l;
1167
1168 if(!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001169 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001170 l = strlen(hl);
1171 if(l >= BUFSIZ)
1172 hl[BUFSIZ-1] = 0;
1173 if(l == 0 || hl[0] == ' ') {
1174 free(hl);
1175 continue;
1176 }
1177 history [hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001178 }
1179 fclose ( fp );
1180 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001181 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001182}
1183
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001184extern void save_history ( const char *tofile )
Robert Griebl350d26b2002-12-03 22:45:46 +00001185{
Robert Griebl350d26b2002-12-03 22:45:46 +00001186 FILE *fp = fopen ( tofile, "w" );
Eric Andersenc470f442003-07-28 09:56:35 +00001187
Robert Griebl350d26b2002-12-03 22:45:46 +00001188 if ( fp ) {
1189 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001190
Robert Griebl350d26b2002-12-03 22:45:46 +00001191 for ( i = 0; i < n_history; i++ ) {
Eric Andersen27bb7902003-12-23 20:24:51 +00001192 fprintf(fp, "%s\n", history [i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001193 }
1194 fclose ( fp );
1195 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001196}
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001197#endif
Robert Griebl350d26b2002-12-03 22:45:46 +00001198
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001199#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001200
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001201enum {
1202 ESC = 27,
1203 DEL = 127,
1204};
1205
1206
Erik Andersen6273f652000-03-17 01:12:41 +00001207/*
1208 * This function is used to grab a character buffer
1209 * from the input file descriptor and allows you to
1210 * a string with full command editing (sortof like
1211 * a mini readline).
1212 *
1213 * The following standard commands are not implemented:
1214 * ESC-b -- Move back one word
1215 * ESC-f -- Move forward one word
1216 * ESC-d -- Delete back one word
1217 * ESC-h -- Delete forward one word
1218 * CTL-t -- Transpose two characters
1219 *
1220 * Furthermore, the "vi" command editing keys are not implemented.
1221 *
Erik Andersen6273f652000-03-17 01:12:41 +00001222 */
Eric Andersenc470f442003-07-28 09:56:35 +00001223
Eric Andersen044228d2001-07-17 01:12:36 +00001224
1225int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001226{
1227
Erik Andersenc7c634b2000-03-19 05:28:55 +00001228 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001229 int lastWasTab = FALSE;
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001230 unsigned char c = 0;
Erik Andersen13456d12000-03-16 08:09:57 +00001231
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001232 /* prepare before init handlers */
Eric Andersenc470f442003-07-28 09:56:35 +00001233 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001234 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001235 command_ps = command;
1236
Eric Andersen34506362001-08-02 05:02:46 +00001237 getTermSettings(0, (void *) &initial_settings);
1238 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1239 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1240 /* Turn off echoing and CTRL-C, so we can trap it */
1241 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Eric Andersen34506362001-08-02 05:02:46 +00001242 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1243 new_settings.c_cc[VMIN] = 1;
1244 new_settings.c_cc[VTIME] = 0;
1245 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001246# ifndef _POSIX_VDISABLE
1247# define _POSIX_VDISABLE '\0'
1248# endif
Eric Andersenc470f442003-07-28 09:56:35 +00001249 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001250 command[0] = 0;
1251
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001252 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001253 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001254
Eric Andersen6faae7d2001-02-16 20:09:17 +00001255 /* Now initialize things */
1256 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001257 /* Print out the command prompt */
1258 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001259
Erik Andersenc7c634b2000-03-19 05:28:55 +00001260 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001261
Eric Andersenc470f442003-07-28 09:56:35 +00001262 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001263
Eric Andersen7467c8d2001-07-12 20:26:32 +00001264 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001265 /* if we can't read input then exit */
1266 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001267
Erik Andersen13456d12000-03-16 08:09:57 +00001268 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001269 case '\n':
1270 case '\r':
1271 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001272 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001273 break_out = 1;
1274 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001275 case 1:
1276 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001277 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001278 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001279 case 2:
1280 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001281 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001282 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001283 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001284 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001285 goto_new_line();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001286#ifndef CONFIG_ASH
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001287 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001288 len = 0;
1289 lastWasTab = FALSE;
1290 put_prompt();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001291#else
1292 len = 0;
1293 break_out = -1; /* for control traps */
1294#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001295 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
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001307 len = break_out = -1; /* for control stoped jobs */
Eric Andersen044228d2001-07-17 01:12:36 +00001308 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 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001382 if (c >= '1' && c <= '9') {
1383 unsigned char dummy;
1384
1385 if (safe_read(0, &dummy, 1) < 1)
1386 goto prepare_to_die;
1387 if(dummy != '~')
1388 c = 0;
1389 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001390 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001391#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001392 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001393
1394 input_tab(&lastWasTab);
1395 break;
1396#endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001397#if MAX_HISTORY >= 1
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001398 case 'A':
1399 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001400 if (cur_history > 0) {
1401 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001402 goto rewrite_line;
1403 } else {
1404 beep();
1405 }
1406 break;
1407 case 'B':
1408 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001409 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001410 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001411 /* Rewrite the line with the selected history item */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001412rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001413 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001414 len = strlen(strcpy(command, history[cur_history]));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001415 /* redraw and go to end line */
1416 redraw(cmdedit_y, 0);
1417 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001418#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001419 case 'C':
1420 /* Right Arrow -- Move forward one character */
1421 input_forward();
1422 break;
1423 case 'D':
1424 /* Left Arrow -- Move back one character */
1425 input_backward(1);
1426 break;
1427 case '3':
1428 /* Delete */
1429 input_delete();
1430 break;
1431 case '1':
1432 case 'H':
1433 /* Home (Ctrl-A) */
1434 input_backward(cursor);
1435 break;
1436 case '4':
1437 case 'F':
1438 /* End (Ctrl-E) */
1439 input_end();
1440 break;
1441 default:
Glenn L McGrath475820c2004-01-22 12:42:23 +00001442 c = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001443 beep();
1444 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001445 break;
1446 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001447
Eric Andersenc470f442003-07-28 09:56:35 +00001448 default: /* If it's regular input, do the normal thing */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001449#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001450 /* Control-V -- Add non-printable symbol */
1451 if (c == 22) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001452 if (safe_read(0, &c, 1) < 1)
1453 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001454 if (c == 0) {
1455 beep();
1456 break;
1457 }
1458 } else
1459#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001460 if (!Isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001461 break;
1462
Eric Andersenc470f442003-07-28 09:56:35 +00001463 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001464 break;
1465
1466 len++;
1467
Eric Andersenc470f442003-07-28 09:56:35 +00001468 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001469 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001470 *(command + cursor + 1) = 0;
1471 cmdedit_set_out_char(0);
Eric Andersenc470f442003-07-28 09:56:35 +00001472 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001473 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001474
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001475 memmove(command + sc + 1, command + sc, len - sc);
1476 *(command + sc) = c;
1477 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001478 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001479 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001480 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001481 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001482 }
1483
Erik Andersenc7c634b2000-03-19 05:28:55 +00001484 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001485 }
Eric Andersenc470f442003-07-28 09:56:35 +00001486 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001487 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001488
1489 if (c != '\t')
1490 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001491 }
1492
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001493 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001494 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001495
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001496#if MAX_HISTORY >= 1
Erik Andersenc7c634b2000-03-19 05:28:55 +00001497 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001498 /* cleanup may be saved current command line */
1499 free(history[MAX_HISTORY]);
1500 history[MAX_HISTORY] = 0;
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001501 if (len> 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001502 int i = n_history;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001503 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001504 if (i >= MAX_HISTORY) {
1505 free(history[0]);
1506 for(i = 0; i < (MAX_HISTORY-1); i++)
1507 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001508 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001509 history[i++] = bb_xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001510 cur_history = i;
1511 n_history = i;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001512#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001513 num_ok_lines++;
1514#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001515 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001516#else /* MAX_HISTORY < 1 */
1517#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001518 if (len > 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001519 num_ok_lines++;
1520 }
1521#endif
1522#endif /* MAX_HISTORY >= 1 */
Eric Andersen27bb7902003-12-23 20:24:51 +00001523 if (break_out > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001524 command[len++] = '\n'; /* set '\n' */
1525 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001526 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001527#if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersenc470f442003-07-28 09:56:35 +00001528 input_tab(0); /* strong free */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001529#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001530#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001531 free(cmdedit_prompt);
1532#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001533 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001534 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001535}
1536
Eric Andersen7467c8d2001-07-12 20:26:32 +00001537
1538
Eric Andersenc470f442003-07-28 09:56:35 +00001539#endif /* CONFIG_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001540
1541
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001542#ifdef TEST
1543
Manuel Novoa III cad53642003-03-19 09:13:01 +00001544const char *bb_applet_name = "debug stuff usage";
Eric Andersene5dfced2001-04-09 22:48:12 +00001545
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001546#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001547#include <locale.h>
1548#endif
1549
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001550int main(int argc, char **argv)
1551{
1552 char buff[BUFSIZ];
1553 char *prompt =
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001554#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001555 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001556\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001557\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001558#else
1559 "% ";
1560#endif
1561
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001562#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001563 setlocale(LC_ALL, "");
1564#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001565 while(1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001566 int l;
Eric Andersen27bb7902003-12-23 20:24:51 +00001567 l = cmdedit_read_input(prompt, buff);
1568 if(l > 0 && buff[l-1] == '\n') {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001569 buff[l-1] = 0;
Eric Andersen27bb7902003-12-23 20:24:51 +00001570 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1571 } else {
1572 break;
1573 }
Eric Andersen7467c8d2001-07-12 20:26:32 +00001574 }
Eric Andersen27bb7902003-12-23 20:24:51 +00001575 printf("*** cmdedit_read_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001576 return 0;
1577}
1578
Eric Andersenc470f442003-07-28 09:56:35 +00001579#endif /* TEST */