blob: dda6f69584084cd5e491ff97c25627872dafe3b1 [file] [log] [blame]
Erik Andersenc7c634b2000-03-19 05:28:55 +00001/* vi: set sw=4 ts=4: */
Erik Andersen13456d12000-03-16 08:09:57 +00002/*
Eric Andersenaff114c2004-04-14 17:51:38 +00003 * Termios command line History and Editing.
Erik Andersen13456d12000-03-16 08:09:57 +00004 *
Eric Andersen81fe1232003-07-29 06:38:40 +00005 * Copyright (c) 1986-2003 may safely be consumed by a BSD or GPL license.
Eric Andersen7467c8d2001-07-12 20:26:32 +00006 * Written by: Vladimir Oleynik <dzo@simtreas.ru>
Eric Andersen5f2c79d2001-02-16 18:36:04 +00007 *
8 * Used ideas:
9 * Adam Rogoyski <rogoyski@cs.utexas.edu>
10 * Dave Cinege <dcinege@psychosis.com>
11 * Jakub Jelinek (c) 1995
Eric Andersen81fe1232003-07-29 06:38:40 +000012 * Erik Andersen <andersen@codepoet.org> (Majorly adjusted for busybox)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000013 *
Erik Andersen13456d12000-03-16 08:09:57 +000014 * This code is 'as is' with no warranty.
Erik Andersen13456d12000-03-16 08:09:57 +000015 *
Erik Andersen13456d12000-03-16 08:09:57 +000016 *
17 */
18
19/*
20 Usage and Known bugs:
21 Terminal key codes are not extensive, and more will probably
22 need to be added. This version was created on Debian GNU/Linux 2.x.
23 Delete, Backspace, Home, End, and the arrow keys were tested
24 to work in an Xterm and console. Ctrl-A also works as Home.
Mark Whitley4e338752001-01-26 20:42:23 +000025 Ctrl-E also works as End.
Erik Andersen13456d12000-03-16 08:09:57 +000026
Eric Andersen5f2c79d2001-02-16 18:36:04 +000027 Small bugs (simple effect):
28 - not true viewing if terminal size (x*y symbols) less
29 size (prompt + editor`s line + 2 symbols)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000030 - not true viewing if length prompt less terminal width
Erik Andersen13456d12000-03-16 08:09:57 +000031 */
32
Mark Whitley4e338752001-01-26 20:42:23 +000033
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000034#include "busybox.h"
Eric Andersencbe31da2001-02-20 06:14:08 +000035#include <stdio.h>
36#include <errno.h>
37#include <unistd.h>
38#include <stdlib.h>
39#include <string.h>
40#include <sys/ioctl.h>
41#include <ctype.h>
42#include <signal.h>
43#include <limits.h>
44
"Robert P. J. Day"4eddb422006-07-03 00:46:47 +000045#include "cmdedit.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000046
Glenn L McGrath475820c2004-01-22 12:42:23 +000047
Eric Andersenbdfd0d72001-10-24 05:00:29 +000048#ifdef CONFIG_LOCALE_SUPPORT
Eric Andersene5dfced2001-04-09 22:48:12 +000049#define Isprint(c) isprint((c))
50#else
51#define Isprint(c) ( (c) >= ' ' && (c) != ((unsigned char)'\233') )
52#endif
53
Glenn L McGrath3b251852004-01-03 12:07:32 +000054#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000055
Eric Andersen27bb7902003-12-23 20:24:51 +000056/* pretect redefined for test */
57#undef CONFIG_FEATURE_COMMAND_EDITING
58#undef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
59#undef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
60#undef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
61#undef CONFIG_FEATURE_CLEAN_UP
62
Eric Andersenbdfd0d72001-10-24 05:00:29 +000063#define CONFIG_FEATURE_COMMAND_EDITING
64#define CONFIG_FEATURE_COMMAND_TAB_COMPLETION
65#define CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
66#define CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
67#define CONFIG_FEATURE_CLEAN_UP
Eric Andersen5f2c79d2001-02-16 18:36:04 +000068
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000069#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000070
Eric Andersenbdfd0d72001-10-24 05:00:29 +000071#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5165fbe2001-02-20 06:42:29 +000072#include <dirent.h>
73#include <sys/stat.h>
74#endif
75
Eric Andersenbdfd0d72001-10-24 05:00:29 +000076#ifdef CONFIG_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000077
Eric Andersenbdfd0d72001-10-24 05:00:29 +000078#if defined(CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION) || defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
79#define CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +000080#endif
81
Eric Andersen5f2c79d2001-02-16 18:36:04 +000082/* Maximum length of the linked list for the command line history */
Robert Griebl350d26b2002-12-03 22:45:46 +000083#ifndef CONFIG_FEATURE_COMMAND_HISTORY
84#define MAX_HISTORY 15
85#else
Denis Vlasenko9d4533e2006-11-02 22:09:37 +000086#define MAX_HISTORY (CONFIG_FEATURE_COMMAND_HISTORY + 0)
Robert Griebl350d26b2002-12-03 22:45:46 +000087#endif
88
Denis Vlasenko9d4533e2006-11-02 22:09:37 +000089#if MAX_HISTORY > 0
Glenn L McGrath062c74f2002-11-27 09:29:49 +000090static char *history[MAX_HISTORY+1]; /* history + current */
91/* saved history lines */
92static int n_history;
93/* current pointer to history line */
94static int cur_history;
95#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +000096
Glenn L McGrath78b0e372001-06-26 02:06:08 +000097#include <termios.h>
98#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
99#define getTermSettings(fd,argp) tcgetattr(fd, argp);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000100
101/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000102static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000103
104
Mark Whitley4e338752001-01-26 20:42:23 +0000105static
Eric Andersenc470f442003-07-28 09:56:35 +0000106volatile int cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +0000107static
Eric Andersenc470f442003-07-28 09:56:35 +0000108volatile int handlers_sets = 0; /* Set next bites: */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000109
Mark Whitley4e338752001-01-26 20:42:23 +0000110enum {
Eric Andersenc470f442003-07-28 09:56:35 +0000111 SET_ATEXIT = 1, /* when atexit() has been called
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000112 and get euid,uid,gid to fast compare */
Eric Andersen7467c8d2001-07-12 20:26:32 +0000113 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
114 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000115};
Erik Andersen13456d12000-03-16 08:09:57 +0000116
Mark Whitley4e338752001-01-26 20:42:23 +0000117
Eric Andersenc470f442003-07-28 09:56:35 +0000118static int cmdedit_x; /* real x terminal position */
119static int cmdedit_y; /* pseudoreal y terminal position */
120static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000121
Eric Andersenc470f442003-07-28 09:56:35 +0000122static int cursor; /* required global for signal handler */
123static int len; /* --- "" - - "" - -"- --""-- --""--- */
124static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000125static
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000126#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000127 const
128#endif
Eric Andersenc470f442003-07-28 09:56:35 +0000129char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000130
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000131#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000132static char *user_buf = "";
133static char *home_pwd_buf = "";
134static int my_euid;
135#endif
136
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000137#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000138static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000139static int num_ok_lines = 1;
140#endif
141
142
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000143#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000144
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000145#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000146static int my_euid;
147#endif
148
149static int my_uid;
150static int my_gid;
151
Eric Andersenc470f442003-07-28 09:56:35 +0000152#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
153
Mark Whitley4e338752001-01-26 20:42:23 +0000154static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000155
Mark Whitley4e338752001-01-26 20:42:23 +0000156static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000157{
Eric Andersenc470f442003-07-28 09:56:35 +0000158 static sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000159
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000160 /* emulate || signal call */
161 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Eric Andersen8efe9672003-09-15 08:33:45 +0000162 int width = 0;
163 get_terminal_width_height(0, &width, NULL);
164 cmdedit_setwidth(width, nsig == SIGWINCH);
Mark Whitley4e338752001-01-26 20:42:23 +0000165 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000166 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000167
Eric Andersenc470f442003-07-28 09:56:35 +0000168 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000169 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersenc470f442003-07-28 09:56:35 +0000170 else if (nsig == SIGWINCH) /* signaled called handler */
171 signal(SIGWINCH, win_changed); /* set for next call */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000172 else /* nsig == 0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000173 /* set previous handler */
Eric Andersenc470f442003-07-28 09:56:35 +0000174 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000175}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000176
177static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000178{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000179 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000180/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen70060d22004-03-27 10:02:48 +0000181 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000182 handlers_sets &= ~SET_RESET_TERM;
183 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000184 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000185 /* reset SIGWINCH handler to previous (default) */
186 win_changed(0);
187 handlers_sets &= ~SET_WCHG_HANDLERS;
188 }
189 fflush(stdout);
Erik Andersen13456d12000-03-16 08:09:57 +0000190}
191
Mark Whitley4e338752001-01-26 20:42:23 +0000192
Mark Whitley4e338752001-01-26 20:42:23 +0000193/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000194static void cmdedit_set_out_char(int next_char)
195{
196
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000197 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000198
199 if (c == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000200 c = ' '; /* destroy end char? */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000201#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000202 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000203 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000204 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000205 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000206 c += '@';
207 if (c == 127)
208 c = '?';
209 printf("\033[7m%c\033[0m", c);
210 } else
211#endif
Rob Landleyd921b2e2006-08-03 15:41:12 +0000212 if (initial_settings.c_lflag & ECHO) putchar(c);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000213 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000214 /* terminal is scrolled down */
215 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000216 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000217
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000218 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000219 next_char = ' ';
220 /* destroy "(auto)margin" */
221 putchar(next_char);
222 putchar('\b');
223 }
224 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000225}
226
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000227/* Move to end line. Bonus: rewrite line from cursor */
228static void input_end(void)
229{
230 while (cursor < len)
231 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000232}
233
234/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000235static void goto_new_line(void)
236{
Mark Whitley4e338752001-01-26 20:42:23 +0000237 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000238 if (cmdedit_x)
239 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000240}
241
242
Rob Landley88621d72006-08-29 19:41:06 +0000243static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000244{
Robert Grieblb2301592002-07-30 23:13:51 +0000245 if ( s )
246 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000247}
Eric Andersen81fe1232003-07-29 06:38:40 +0000248
Rob Landley88621d72006-08-29 19:41:06 +0000249static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000250{
251 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000252}
253
Eric Andersenaff114c2004-04-14 17:51:38 +0000254/* Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000255/* special for slow terminal */
256static void input_backward(int num)
257{
258 if (num > cursor)
259 num = cursor;
Eric Andersenc470f442003-07-28 09:56:35 +0000260 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000261
Eric Andersenc470f442003-07-28 09:56:35 +0000262 if (cmdedit_x >= num) { /* no to up line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000263 cmdedit_x -= num;
264 if (num < 4)
265 while (num-- > 0)
266 putchar('\b');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000267 else
268 printf("\033[%dD", num);
269 } else {
270 int count_y;
271
272 if (cmdedit_x) {
Eric Andersenc470f442003-07-28 09:56:35 +0000273 putchar('\r'); /* back to first terminal pos. */
274 num -= cmdedit_x; /* set previous backward */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000275 }
276 count_y = 1 + num / cmdedit_termw;
277 printf("\033[%dA", count_y);
278 cmdedit_y -= count_y;
279 /* require forward after uping */
280 cmdedit_x = cmdedit_termw * count_y - num;
Eric Andersenc470f442003-07-28 09:56:35 +0000281 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000282 }
Erik Andersen13456d12000-03-16 08:09:57 +0000283}
284
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000285static void put_prompt(void)
286{
287 out1str(cmdedit_prompt);
Eric Andersenc470f442003-07-28 09:56:35 +0000288 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000289 cursor = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000290 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000291}
292
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000293#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000294static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000295{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000296 cmdedit_prompt = prmt_ptr;
297 cmdedit_prmt_len = strlen(prmt_ptr);
298 put_prompt();
299}
300#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000301static void parse_prompt(const char *prmt_ptr)
302{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000303 int prmt_len = 0;
"Vladimir N. Oleynik"f0874802005-09-05 15:46:26 +0000304 size_t cur_prmt_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000305 char flg_not_length = '[';
Rob Landley081e3842006-08-03 20:07:35 +0000306 char *prmt_mem_ptr = xzalloc(1);
Eric Andersene5dfced2001-04-09 22:48:12 +0000307 char *pwd_buf = xgetcwd(0);
308 char buf2[PATH_MAX + 1];
309 char buf[2];
310 char c;
311 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000312
Eric Andersen5f265b72001-05-11 16:58:46 +0000313 if (!pwd_buf) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000314 pwd_buf=(char *)bb_msg_unknown;
Eric Andersen5f265b72001-05-11 16:58:46 +0000315 }
316
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000317 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000318 pbuf = buf;
319 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000320 c = *prmt_ptr++;
321 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000322 const char *cp = prmt_ptr;
323 int l;
Eric Andersenc470f442003-07-28 09:56:35 +0000324
Manuel Novoa III cad53642003-03-19 09:13:01 +0000325 c = bb_process_escape_sequence(&prmt_ptr);
Eric Andersene5dfced2001-04-09 22:48:12 +0000326 if(prmt_ptr==cp) {
327 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000328 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000329 c = *prmt_ptr++;
330 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000331#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000332 case 'u':
333 pbuf = user_buf;
334 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000335#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000336 case 'h':
337 pbuf = hostname_buf;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000338 if (pbuf == 0) {
Rob Landley081e3842006-08-03 20:07:35 +0000339 pbuf = xzalloc(256);
Eric Andersene5dfced2001-04-09 22:48:12 +0000340 if (gethostname(pbuf, 255) < 0) {
341 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000342 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000343 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000344
345 if (s)
346 *s = 0;
347 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000348 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000349 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000350 break;
351 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000352 c = my_euid == 0 ? '#' : '$';
353 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000354#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000355 case 'w':
356 pbuf = pwd_buf;
357 l = strlen(home_pwd_buf);
358 if (home_pwd_buf[0] != 0 &&
359 strncmp(home_pwd_buf, pbuf, l) == 0 &&
360 (pbuf[l]=='/' || pbuf[l]=='\0') &&
361 strlen(pwd_buf+l)<PATH_MAX) {
362 pbuf = buf2;
363 *pbuf = '~';
364 strcpy(pbuf+1, pwd_buf+l);
Denis Vlasenko92758142006-10-03 19:56:34 +0000365 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000366 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000367#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000368 case 'W':
369 pbuf = pwd_buf;
370 cp = strrchr(pbuf,'/');
371 if ( (cp != NULL) && (cp != pbuf) )
372 pbuf += (cp-pbuf)+1;
373 break;
374 case '!':
375 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
376 break;
377 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000378 c = '\033';
379 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000380 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000381 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000382 int h;
383 buf2[l++] = *prmt_ptr;
384 buf2[l] = 0;
385 h = strtol(buf2, &pbuf, 16);
386 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000387 l--;
388 break;
389 }
390 prmt_ptr++;
391 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000392 buf2[l] = 0;
393 c = (char)strtol(buf2, 0, 16);
394 if(c==0)
395 c = '?';
396 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000397 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000398 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000399 if (c == flg_not_length) {
400 flg_not_length = flg_not_length == '[' ? ']' : '[';
401 continue;
402 }
403 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000404 }
Eric Andersenc470f442003-07-28 09:56:35 +0000405 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000406 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000407 if(pbuf == buf)
408 *pbuf = c;
"Vladimir N. Oleynik"f0874802005-09-05 15:46:26 +0000409 cur_prmt_len = strlen(pbuf);
410 prmt_len += cur_prmt_len;
411 if (flg_not_length != ']')
412 cmdedit_prmt_len += cur_prmt_len;
Eric Andersene5dfced2001-04-09 22:48:12 +0000413 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000414 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000415 if(pwd_buf!=(char *)bb_msg_unknown)
Eric Andersen8f697842001-07-02 15:36:57 +0000416 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000417 cmdedit_prompt = prmt_mem_ptr;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000418 put_prompt();
419}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000420#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000421
422
Eric Andersenaff114c2004-04-14 17:51:38 +0000423/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000424static void redraw(int y, int back_cursor)
425{
Eric Andersenc470f442003-07-28 09:56:35 +0000426 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000427 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000428 putchar('\r');
429 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000430 input_end(); /* rewrite */
431 printf("\033[J"); /* destroy tail after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000432 input_backward(back_cursor);
433}
434
Paul Fox3f11b1b2005-08-04 19:04:46 +0000435#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000436#define DELBUFSIZ 128
437static char *delbuf; /* a (malloced) place to store deleted characters */
438static char *delp;
439static char newdelflag; /* whether delbuf should be reused yet */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000440#endif
441
442/* Delete the char in front of the cursor, optionally saving it
443 * for later putback */
444static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000445{
Mark Whitley4e338752001-01-26 20:42:23 +0000446 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000447
Mark Whitley4e338752001-01-26 20:42:23 +0000448 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000449 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000450
Paul Fox3f11b1b2005-08-04 19:04:46 +0000451#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
452 if (save) {
453 if (newdelflag) {
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000454 if (!delbuf)
455 delbuf = malloc(DELBUFSIZ);
456 /* safe if malloc fails */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000457 delp = delbuf;
458 newdelflag = 0;
459 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000460 if (delbuf && (delp - delbuf < DELBUFSIZ))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000461 *delp++ = command_ps[j];
462 }
463#endif
464
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000465 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000466 len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000467 input_end(); /* rewrite new line */
Eric Andersenc470f442003-07-28 09:56:35 +0000468 cmdedit_set_out_char(0); /* destroy end char */
469 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000470}
471
Paul Fox3f11b1b2005-08-04 19:04:46 +0000472#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
473static void put(void)
474{
475 int ocursor, j = delp - delbuf;
476 if (j == 0)
477 return;
478 ocursor = cursor;
479 /* open hole and then fill it */
480 memmove(command_ps + cursor + j, command_ps + cursor, len - cursor + 1);
481 strncpy(command_ps + cursor, delbuf, j);
482 len += j;
483 input_end(); /* rewrite new line */
484 input_backward(cursor-ocursor-j+1); /* at end of new text */
485}
486#endif
487
Mark Whitley4e338752001-01-26 20:42:23 +0000488/* Delete the char in back of the cursor */
489static void input_backspace(void)
490{
491 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000492 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000493 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000494 }
495}
496
497
Eric Andersenaff114c2004-04-14 17:51:38 +0000498/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000499static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000500{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000501 if (cursor < len)
502 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000503}
504
Mark Whitley4e338752001-01-26 20:42:23 +0000505static void cmdedit_setwidth(int w, int redraw_flg)
506{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000507 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000508 if (w <= cmdedit_termw) {
509 cmdedit_termw = cmdedit_termw % w;
510 }
Mark Whitley4e338752001-01-26 20:42:23 +0000511 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000512 cmdedit_termw = w;
513
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000514 if (redraw_flg) {
515 /* new y for current cursor */
516 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000517
518 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000519 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
520 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000521 }
Eric Andersenc470f442003-07-28 09:56:35 +0000522 }
Mark Whitley4e338752001-01-26 20:42:23 +0000523}
524
Eric Andersen7467c8d2001-07-12 20:26:32 +0000525static void cmdedit_init(void)
Mark Whitley4e338752001-01-26 20:42:23 +0000526{
Eric Andersen61173a52001-03-19 17:48:55 +0000527 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000528 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
529 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000530 win_changed(-SIGWINCH);
531 handlers_sets |= SET_WCHG_HANDLERS;
532 }
533
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000534 if ((handlers_sets & SET_ATEXIT) == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000535#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000536 struct passwd *entry;
537
538 my_euid = geteuid();
539 entry = getpwuid(my_euid);
540 if (entry) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000541 user_buf = xstrdup(entry->pw_name);
542 home_pwd_buf = xstrdup(entry->pw_dir);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000543 }
544#endif
545
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000546#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000547
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000548#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000549 my_euid = geteuid();
550#endif
551 my_uid = getuid();
552 my_gid = getgid();
Eric Andersenc470f442003-07-28 09:56:35 +0000553#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000554 handlers_sets |= SET_ATEXIT;
Eric Andersenc470f442003-07-28 09:56:35 +0000555 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000556 }
Mark Whitley4e338752001-01-26 20:42:23 +0000557}
Erik Andersenf0657d32000-04-12 17:49:52 +0000558
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000559#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000560
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000561static char **matches;
562static int num_matches;
563static char *add_char_to_match;
564
565static void add_match(char *matched, int add_char)
566{
567 int nm = num_matches;
568 int nm1 = nm + 1;
569
570 matches = xrealloc(matches, nm1 * sizeof(char *));
571 add_char_to_match = xrealloc(add_char_to_match, nm1);
572 matches[nm] = matched;
573 add_char_to_match[nm] = (char)add_char;
574 num_matches++;
575}
576
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000577static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000578{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000579 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
580 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
581 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
582 (st->st_mode & S_IXOTH)) return TRUE;
583 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000584}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000585
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000586#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000587
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000588static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000589{
590 struct passwd *entry;
591 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000592
Eric Andersenc470f442003-07-28 09:56:35 +0000593 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000594 userlen = strlen(ud);
595
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000596 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000597 char *sav_ud = ud - 1;
598 char *home = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000599 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000600
Eric Andersenc470f442003-07-28 09:56:35 +0000601 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000602 home = home_pwd_buf;
603 } else {
604 /* "~user/..." */
605 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000606 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000607 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000608 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000609 ud = temp;
610 if (entry)
611 home = entry->pw_dir;
612 }
613 if (home) {
614 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000615 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000616
617 /* /home/user/... */
618 sprintf(temp2, "%s%s", home, ud);
619 strcpy(sav_ud, temp2);
620 }
621 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000622 } else {
623 /* "~[^/]*" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000624 setpwent();
625
626 while ((entry = getpwent()) != NULL) {
627 /* Null usernames should result in all users as possible completions. */
628 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000629 add_match(xasprintf("~%s", entry->pw_name), '/');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000630 }
631 }
632
633 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000634 }
635}
Eric Andersenc470f442003-07-28 09:56:35 +0000636#endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000637
638enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000639 FIND_EXE_ONLY = 0,
640 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000641 FIND_FILE_ONLY = 2,
642};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000643
Glenn L McGrath67285962004-01-14 09:34:51 +0000644#ifdef CONFIG_ASH
645const char *cmdedit_path_lookup;
646#else
647#define cmdedit_path_lookup getenv("PATH")
648#endif
649
Mark Whitley4e338752001-01-26 20:42:23 +0000650static int path_parse(char ***p, int flags)
651{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000652 int npth;
Glenn L McGrath67285962004-01-14 09:34:51 +0000653 const char *tmp;
654 const char *pth;
Mark Whitley4e338752001-01-26 20:42:23 +0000655
Mark Whitley4e338752001-01-26 20:42:23 +0000656 /* if not setenv PATH variable, to search cur dir "." */
Glenn L McGrath67285962004-01-14 09:34:51 +0000657 if (flags != FIND_EXE_ONLY || (pth = cmdedit_path_lookup) == 0 ||
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000658 /* PATH=<empty> or PATH=:<empty> */
659 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000660 return 1;
661 }
662
663 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000664 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000665
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000666 for (;;) {
Eric Andersenc470f442003-07-28 09:56:35 +0000667 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000668 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000669 if (tmp) {
670 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000671 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000672 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000673 break;
674 }
675
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000676 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000677
678 tmp = pth;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000679 (*p)[0] = xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000680 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000681
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000682 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000683 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000684 if (tmp) {
Eric Andersenc470f442003-07-28 09:56:35 +0000685 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000686 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000687 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000688 } else
689 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000690 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000691 }
692
693 return npth;
694}
695
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000696static char *add_quote_for_spec_chars(char *found, int add)
Erik Andersen6273f652000-03-17 01:12:41 +0000697{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000698 int l = 0;
699 char *s = xmalloc((strlen(found) + 1) * 2);
700
701 while (*found) {
702 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
703 s[l++] = '\\';
704 s[l++] = *found++;
705 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000706 if(add)
Denis Vlasenko92758142006-10-03 19:56:34 +0000707 s[l++] = (char)add;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000708 s[l] = 0;
709 return s;
710}
711
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000712static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000713{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000714 DIR *dir;
715 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000716 char dirbuf[BUFSIZ];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000717 struct stat st;
718 char *path1[1];
719 char **paths = path1;
720 int npaths;
721 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000722 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000723 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000724
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000725 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000726
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000727 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000728 /* no dir, if flags==EXE_ONLY - get paths, else "." */
729 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000730 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000731 } else {
732 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000733 /* save for change */
734 strcpy(dirbuf, command);
735 /* set dir only */
736 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000737#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000738 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000739 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000740#endif
741 /* "strip" dirname in command */
742 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000743
Mark Whitley4e338752001-01-26 20:42:23 +0000744 paths[0] = dirbuf;
Eric Andersenc470f442003-07-28 09:56:35 +0000745 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000746 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000747
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000748 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000749
Mark Whitley4e338752001-01-26 20:42:23 +0000750 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000751 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000752 continue;
753
754 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000755 char *str_found = next->d_name;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000756 int add_chr = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000757
Mark Whitley4e338752001-01-26 20:42:23 +0000758 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000759 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000760 continue;
761 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000762 if (*str_found == '.' && *pfind == 0) {
763 if (*paths[i] == '/' && paths[i][1] == 0
Eric Andersenc470f442003-07-28 09:56:35 +0000764 && str_found[1] == 0) str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000765 else
Mark Whitley4e338752001-01-26 20:42:23 +0000766 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000767 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000768 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000769 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000770 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000771 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000772 /* find with dirs ? */
773 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000774 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000775 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000776 /* name is directory */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000777 char *e = found + strlen(found) - 1;
778
779 add_chr = '/';
780 if(*e == '/')
781 *e = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +0000782 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000783 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000784 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000785 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000786 if (type == FIND_FILE_ONLY ||
Matt Kraai1f0c4362001-12-20 23:13:26 +0000787 (type == FIND_EXE_ONLY && is_execute(&st)))
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000788 add_chr = ' ';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000789 }
Mark Whitley4e338752001-01-26 20:42:23 +0000790 /* Add it to the list */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000791 add_match(found, add_chr);
792 continue;
Eric Andersene5dfced2001-04-09 22:48:12 +0000793cont:
794 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000795 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000796 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000797 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000798 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000799 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000800 free(paths);
801 }
Erik Andersen6273f652000-03-17 01:12:41 +0000802}
Erik Andersenf0657d32000-04-12 17:49:52 +0000803
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000804
805#define QUOT (UCHAR_MAX+1)
806
807#define collapse_pos(is, in) { \
Paul Fox574fee42005-07-19 20:41:06 +0000808 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
809 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000810
811static int find_match(char *matchBuf, int *len_with_quotes)
812{
813 int i, j;
814 int command_mode;
815 int c, c2;
816 int int_buf[BUFSIZ + 1];
817 int pos_buf[BUFSIZ + 1];
818
819 /* set to integer dimension characters and own positions */
820 for (i = 0;; i++) {
821 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
822 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000823 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000824 break;
825 } else
826 pos_buf[i] = i;
827 }
828
829 /* mask \+symbol and convert '\t' to ' ' */
830 for (i = j = 0; matchBuf[i]; i++, j++)
831 if (matchBuf[i] == '\\') {
832 collapse_pos(j, j + 1);
833 int_buf[j] |= QUOT;
834 i++;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000835#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000836 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000837 int_buf[j] = ' ' | QUOT;
838#endif
839 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000840#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000841 else if (matchBuf[i] == '\t')
842 int_buf[j] = ' ';
843#endif
844
845 /* mask "symbols" or 'symbols' */
846 c2 = 0;
847 for (i = 0; int_buf[i]; i++) {
848 c = int_buf[i];
849 if (c == '\'' || c == '"') {
850 if (c2 == 0)
851 c2 = c;
852 else {
853 if (c == c2)
854 c2 = 0;
855 else
856 int_buf[i] |= QUOT;
857 }
858 } else if (c2 != 0 && c != '$')
859 int_buf[i] |= QUOT;
860 }
861
862 /* skip commands with arguments if line have commands delimiters */
863 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
864 for (i = 0; int_buf[i]; i++) {
865 c = int_buf[i];
866 c2 = int_buf[i + 1];
867 j = i ? int_buf[i - 1] : -1;
868 command_mode = 0;
869 if (c == ';' || c == '&' || c == '|') {
870 command_mode = 1 + (c == c2);
871 if (c == '&') {
872 if (j == '>' || j == '<')
873 command_mode = 0;
874 } else if (c == '|' && j == '>')
875 command_mode = 0;
876 }
877 if (command_mode) {
878 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000879 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000880 }
881 }
882 /* collapse `command...` */
883 for (i = 0; int_buf[i]; i++)
884 if (int_buf[i] == '`') {
885 for (j = i + 1; int_buf[j]; j++)
886 if (int_buf[j] == '`') {
887 collapse_pos(i, j + 1);
888 j = 0;
889 break;
890 }
891 if (j) {
892 /* not found close ` - command mode, collapse all previous */
893 collapse_pos(0, i + 1);
894 break;
895 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000896 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000897 }
898
899 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000900 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000901 c2 = 0;
902 for (i = 0; int_buf[i]; i++)
903 if (int_buf[i] == '(' || int_buf[i] == '{') {
904 if (int_buf[i] == '(')
905 c++;
906 else
907 c2++;
908 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000909 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000910 }
911 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
912 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
913 if (int_buf[i] == ')')
914 c--;
915 else
916 c2--;
917 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000918 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000919 }
920
921 /* skip first not quote space */
922 for (i = 0; int_buf[i]; i++)
923 if (int_buf[i] != ' ')
924 break;
925 if (i)
926 collapse_pos(0, i);
927
928 /* set find mode for completion */
929 command_mode = FIND_EXE_ONLY;
930 for (i = 0; int_buf[i]; i++)
931 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
932 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000933 && matchBuf[pos_buf[0]]=='c'
934 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000935 command_mode = FIND_DIR_ONLY;
936 else {
937 command_mode = FIND_FILE_ONLY;
938 break;
939 }
940 }
941 /* "strlen" */
942 for (i = 0; int_buf[i]; i++);
943 /* find last word */
944 for (--i; i >= 0; i--) {
945 c = int_buf[i];
946 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
947 collapse_pos(0, i + 1);
948 break;
949 }
950 }
951 /* skip first not quoted '\'' or '"' */
952 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
953 /* collapse quote or unquote // or /~ */
Eric Andersenc470f442003-07-28 09:56:35 +0000954 while ((int_buf[i] & ~QUOT) == '/' &&
Mark Whitley7e5291f2001-03-08 19:31:12 +0000955 ((int_buf[i + 1] & ~QUOT) == '/'
956 || (int_buf[i + 1] & ~QUOT) == '~')) {
957 i++;
958 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000959
960 /* set only match and destroy quotes */
961 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000962 for (c = 0; pos_buf[i] >= 0; i++) {
963 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000964 j = pos_buf[i] + 1;
965 }
Eric Andersen4f990532001-05-31 17:15:57 +0000966 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000967 /* old lenght matchBuf with quotes symbols */
968 *len_with_quotes = j ? j - pos_buf[0] : 0;
969
970 return command_mode;
971}
972
Glenn L McGrath4d001292003-01-06 01:11:50 +0000973/*
974 display by column original ideas from ls applet,
975 very optimize by my :)
976*/
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000977static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000978{
979 int ncols, row;
980 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000981 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000982 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000983 char str_add_chr[2];
984 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000985
986 /* find the longest file name- use that as the column width */
987 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000988 l = strlen(matches[row]);
989 if(add_char_to_match[row])
Denis Vlasenko92758142006-10-03 19:56:34 +0000990 l++;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000991 if (column_width < l)
992 column_width = l;
993 }
994 column_width += 2; /* min space for columns */
995 ncols = cmdedit_termw / column_width;
996
997 if (ncols > 1) {
998 nrows /= ncols;
999 if(nfiles % ncols)
1000 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +00001001 } else {
1002 ncols = 1;
1003 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001004 str_add_chr[1] = 0;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001005 for (row = 0; row < nrows; row++) {
1006 int n = row;
1007 int nc;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001008 int acol;
Glenn L McGrath4d001292003-01-06 01:11:50 +00001009
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001010 for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
1011 str_add_chr[0] = add_char_to_match[n];
1012 acol = str_add_chr[0] ? column_width - 1 : column_width;
1013 printf("%s%s", matches[n], str_add_chr);
1014 l = strlen(matches[n]);
1015 while(l < acol) {
Denis Vlasenko92758142006-10-03 19:56:34 +00001016 putchar(' ');
1017 l++;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001018 }
1019 }
1020 str_add_chr[0] = add_char_to_match[n];
1021 printf("%s%s\n", matches[n], str_add_chr);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001022 }
1023}
1024
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001025
1026static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001027{
1028 /* Do TAB completion */
Eric Andersenc470f442003-07-28 09:56:35 +00001029 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001030 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001031 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001032 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001033 free(matches);
1034 matches = (char **) NULL;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001035 free(add_char_to_match);
1036 add_char_to_match = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +00001037 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001038 return;
1039 }
Matt Kraai1f0c4362001-12-20 23:13:26 +00001040 if (! *lastWasTab) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001041
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001042 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001043 int len_found;
1044 char matchBuf[BUFSIZ];
1045 int find_type;
1046 int recalc_pos;
1047
Eric Andersenc470f442003-07-28 09:56:35 +00001048 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001049
1050 /* Make a local copy of the string -- up
1051 * to the position of the cursor */
1052 tmp = strncpy(matchBuf, command_ps, cursor);
1053 tmp[cursor] = 0;
1054
1055 find_type = find_match(matchBuf, &recalc_pos);
1056
1057 /* Free up any memory already allocated */
1058 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001059
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001060#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001061 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001062 * then try completing this word as a username. */
1063
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001064 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001065 username_tab_completion(matchBuf, NULL);
1066 if (!matches)
Mark Whitley4e338752001-01-26 20:42:23 +00001067#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001068 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001069 * in the current working directory that matches. */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001070 exe_n_cwd_tab_completion(matchBuf, find_type);
1071 /* Remove duplicate found and sort */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001072 if(matches) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001073 int i, j, n, srt;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001074 /* bubble */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001075 n = num_matches;
Denis Vlasenko92758142006-10-03 19:56:34 +00001076 for(i=0; i<(n-1); i++) {
1077 for(j=i+1; j<n; j++) {
1078 if(matches[i]!=NULL && matches[j]!=NULL) {
1079 srt = strcmp(matches[i], matches[j]);
1080 if(srt == 0) {
1081 free(matches[j]);
1082 matches[j]=0;
1083 } else if(srt > 0) {
1084 tmp1 = matches[i];
1085 matches[i] = matches[j];
1086 matches[j] = tmp1;
1087 srt = add_char_to_match[i];
1088 add_char_to_match[i] = add_char_to_match[j];
1089 add_char_to_match[j] = srt;
1090 }
1091 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001092 }
Denis Vlasenko92758142006-10-03 19:56:34 +00001093 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001094 j = n;
1095 n = 0;
1096 for(i=0; i<j; i++)
Denis Vlasenko92758142006-10-03 19:56:34 +00001097 if(matches[i]) {
1098 matches[n]=matches[i];
1099 add_char_to_match[n]=add_char_to_match[i];
1100 n++;
1101 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001102 num_matches = n;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001103 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001104 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001105 if (!matches || num_matches > 1) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001106
Mark Whitley4e338752001-01-26 20:42:23 +00001107 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001108 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +00001109 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001110 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001111 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001112 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001113 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001114 if (matches[len_found][(tmp - tmp1)] != *tmp) {
1115 *tmp = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001116 break;
1117 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001118 if (*tmp1 == 0) { /* have unique */
1119 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001120 return;
1121 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001122 tmp = add_quote_for_spec_chars(tmp1, 0);
1123 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +00001124 } else { /* one match */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001125 tmp = add_quote_for_spec_chars(matches[0], add_char_to_match[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001126 /* for next completion current found */
1127 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001128 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001129 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001130 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001131 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001132
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001133 /* before word for match */
1134 command_ps[cursor - recalc_pos] = 0;
1135 /* save tail line */
1136 strcpy(matchBuf, command_ps + cursor);
1137 /* add match */
1138 strcat(command_ps, tmp);
1139 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001140 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001141 /* back to begin word for match */
1142 input_backward(recalc_pos);
1143 /* new pos */
1144 recalc_pos = cursor + len_found;
1145 /* new len */
1146 len = strlen(command_ps);
1147 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001148 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001149 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001150 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001151 } else {
1152 /* Ok -- the last char was a TAB. Since they
1153 * just hit TAB again, print a list of all the
1154 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001155 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001156 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001157
1158 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001159 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001160 showfiles();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001161 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001162 }
1163 }
1164}
Eric Andersenc470f442003-07-28 09:56:35 +00001165#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001166
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001167#if MAX_HISTORY > 0
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001168static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001169{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001170 if(command_ps[0] != 0 || history[cur_history] == 0) {
1171 free(history[cur_history]);
Rob Landleyd921b2e2006-08-03 15:41:12 +00001172 history[cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001173 }
1174 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +00001175}
1176
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001177static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001178{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001179 int ch = cur_history;
1180
1181 if (ch < n_history) {
1182 get_previous_history(); /* save the current history line */
1183 return (cur_history = ch+1);
1184 } else {
1185 beep();
1186 return 0;
1187 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001188}
Robert Griebl350d26b2002-12-03 22:45:46 +00001189
Robert Griebl350d26b2002-12-03 22:45:46 +00001190#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
Rob Landleydfba7412006-03-06 20:47:33 +00001191void load_history ( const char *fromfile )
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001192{
Robert Griebl350d26b2002-12-03 22:45:46 +00001193 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001194 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001195
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001196 /* cleanup old */
1197
1198 for(hi = n_history; hi > 0; ) {
1199 hi--;
1200 free ( history [hi] );
Robert Griebl350d26b2002-12-03 22:45:46 +00001201 }
1202
1203 if (( fp = fopen ( fromfile, "r" ))) {
Eric Andersenc470f442003-07-28 09:56:35 +00001204
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001205 for ( hi = 0; hi < MAX_HISTORY; ) {
Denis Vlasenko2d5ca602006-10-12 22:43:20 +00001206 char * hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001207 int l;
1208
1209 if(!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001210 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001211 l = strlen(hl);
1212 if(l >= BUFSIZ)
1213 hl[BUFSIZ-1] = 0;
1214 if(l == 0 || hl[0] == ' ') {
1215 free(hl);
1216 continue;
1217 }
1218 history [hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001219 }
1220 fclose ( fp );
1221 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001222 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001223}
1224
Rob Landleydfba7412006-03-06 20:47:33 +00001225void save_history ( const char *tofile )
Robert Griebl350d26b2002-12-03 22:45:46 +00001226{
Robert Griebl350d26b2002-12-03 22:45:46 +00001227 FILE *fp = fopen ( tofile, "w" );
Eric Andersenc470f442003-07-28 09:56:35 +00001228
Robert Griebl350d26b2002-12-03 22:45:46 +00001229 if ( fp ) {
1230 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001231
Robert Griebl350d26b2002-12-03 22:45:46 +00001232 for ( i = 0; i < n_history; i++ ) {
Eric Andersen27bb7902003-12-23 20:24:51 +00001233 fprintf(fp, "%s\n", history [i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001234 }
1235 fclose ( fp );
1236 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001237}
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001238#endif
Robert Griebl350d26b2002-12-03 22:45:46 +00001239
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001240#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001241
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001242enum {
1243 ESC = 27,
1244 DEL = 127,
1245};
1246
1247
Erik Andersen6273f652000-03-17 01:12:41 +00001248/*
1249 * This function is used to grab a character buffer
1250 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001251 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001252 * a mini readline).
1253 *
1254 * The following standard commands are not implemented:
1255 * ESC-b -- Move back one word
1256 * ESC-f -- Move forward one word
1257 * ESC-d -- Delete back one word
1258 * ESC-h -- Delete forward one word
1259 * CTL-t -- Transpose two characters
1260 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001261 * Minimalist vi-style command line editing available if configured.
1262 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001263 *
Erik Andersen6273f652000-03-17 01:12:41 +00001264 */
Eric Andersenc470f442003-07-28 09:56:35 +00001265
Paul Fox3f11b1b2005-08-04 19:04:46 +00001266#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
1267static int vi_mode;
1268
1269void setvimode ( int viflag )
1270{
1271 vi_mode = viflag;
1272}
1273
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001274static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001275vi_Word_motion(char *command, int eat)
1276{
1277 while (cursor < len && !isspace(command[cursor]))
1278 input_forward();
1279 if (eat) while (cursor < len && isspace(command[cursor]))
1280 input_forward();
1281}
1282
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001283static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001284vi_word_motion(char *command, int eat)
1285{
1286 if (isalnum(command[cursor]) || command[cursor] == '_') {
1287 while (cursor < len &&
1288 (isalnum(command[cursor+1]) ||
1289 command[cursor+1] == '_'))
1290 input_forward();
1291 } else if (ispunct(command[cursor])) {
1292 while (cursor < len &&
1293 (ispunct(command[cursor+1])))
1294 input_forward();
1295 }
1296
1297 if (cursor < len)
1298 input_forward();
1299
1300 if (eat && cursor < len && isspace(command[cursor]))
1301 while (cursor < len && isspace(command[cursor]))
1302 input_forward();
1303}
1304
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001305static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001306vi_End_motion(char *command)
1307{
1308 input_forward();
1309 while (cursor < len && isspace(command[cursor]))
1310 input_forward();
1311 while (cursor < len-1 && !isspace(command[cursor+1]))
1312 input_forward();
1313}
1314
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001315static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001316vi_end_motion(char *command)
1317{
1318 if (cursor >= len-1)
1319 return;
1320 input_forward();
1321 while (cursor < len-1 && isspace(command[cursor]))
1322 input_forward();
1323 if (cursor >= len-1)
1324 return;
1325 if (isalnum(command[cursor]) || command[cursor] == '_') {
1326 while (cursor < len-1 &&
1327 (isalnum(command[cursor+1]) ||
1328 command[cursor+1] == '_'))
1329 input_forward();
1330 } else if (ispunct(command[cursor])) {
1331 while (cursor < len-1 &&
1332 (ispunct(command[cursor+1])))
1333 input_forward();
1334 }
1335}
1336
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001337static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001338vi_Back_motion(char *command)
1339{
1340 while (cursor > 0 && isspace(command[cursor-1]))
1341 input_backward(1);
1342 while (cursor > 0 && !isspace(command[cursor-1]))
1343 input_backward(1);
1344}
1345
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001346static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001347vi_back_motion(char *command)
1348{
1349 if (cursor <= 0)
1350 return;
1351 input_backward(1);
1352 while (cursor > 0 && isspace(command[cursor]))
1353 input_backward(1);
1354 if (cursor <= 0)
1355 return;
1356 if (isalnum(command[cursor]) || command[cursor] == '_') {
1357 while (cursor > 0 &&
1358 (isalnum(command[cursor-1]) ||
1359 command[cursor-1] == '_'))
1360 input_backward(1);
1361 } else if (ispunct(command[cursor])) {
1362 while (cursor > 0 &&
1363 (ispunct(command[cursor-1])))
1364 input_backward(1);
1365 }
1366}
1367#endif
1368
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001369/*
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001370 * the emacs and vi modes share much of the code in the big
1371 * command loop. commands entered when in vi's command mode (aka
1372 * "escape mode") get an extra bit added to distinguish them --
1373 * this keeps them from being self-inserted. this clutters the
1374 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001375 */
1376
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001377#define vbit 0x100
1378
1379/* leave out the "vi-mode"-only case labels if vi editing isn't
1380 * configured. */
1381#define vi_case(caselabel) USE_FEATURE_COMMAND_EDITING(caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001382
1383/* convert uppercase ascii to equivalent control char, for readability */
1384#define CNTRL(uc_char) ((uc_char) - 0x40)
1385
Eric Andersen044228d2001-07-17 01:12:36 +00001386
1387int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001388{
1389
Erik Andersenc7c634b2000-03-19 05:28:55 +00001390 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001391 int lastWasTab = FALSE;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001392 unsigned char c;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001393 unsigned int ic;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001394#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001395 unsigned int prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001396 int vi_cmdmode = 0;
1397#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001398 /* prepare before init handlers */
Eric Andersenc470f442003-07-28 09:56:35 +00001399 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001400 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001401 command_ps = command;
1402
Eric Andersen34506362001-08-02 05:02:46 +00001403 getTermSettings(0, (void *) &initial_settings);
1404 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1405 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1406 /* Turn off echoing and CTRL-C, so we can trap it */
1407 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Eric Andersen34506362001-08-02 05:02:46 +00001408 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1409 new_settings.c_cc[VMIN] = 1;
1410 new_settings.c_cc[VTIME] = 0;
1411 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001412# ifndef _POSIX_VDISABLE
1413# define _POSIX_VDISABLE '\0'
1414# endif
Eric Andersenc470f442003-07-28 09:56:35 +00001415 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001416 command[0] = 0;
1417
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001418 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001419 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001420
Eric Andersen6faae7d2001-02-16 20:09:17 +00001421 /* Now initialize things */
1422 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001423 /* Print out the command prompt */
1424 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001425
Erik Andersenc7c634b2000-03-19 05:28:55 +00001426 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001427
Eric Andersenc470f442003-07-28 09:56:35 +00001428 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001429
Eric Andersen7467c8d2001-07-12 20:26:32 +00001430 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001431 /* if we can't read input then exit */
1432 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001433
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001434 ic = c;
1435
Paul Fox3f11b1b2005-08-04 19:04:46 +00001436#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
1437 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001438 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001439 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001440#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001441 switch (ic)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001442 {
Erik Andersenf0657d32000-04-12 17:49:52 +00001443 case '\n':
1444 case '\r':
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001445 vi_case( case '\n'|vbit: )
1446 vi_case( case '\r'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001447 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001448 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001449 break_out = 1;
1450 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001451 case CNTRL('A'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001452 vi_case( case '0'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001453 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001454 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001455 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001456 case CNTRL('B'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001457 vi_case( case 'h'|vbit: )
1458 vi_case( case '\b'|vbit: )
1459 vi_case( case DEL|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001460 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001461 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001462 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001463 case CNTRL('C'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001464 vi_case( case CNTRL('C')|vbit: )
Eric Andersen86349772000-12-18 20:25:50 +00001465 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001466 goto_new_line();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001467#ifndef CONFIG_ASH
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001468 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001469 len = 0;
1470 lastWasTab = FALSE;
1471 put_prompt();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001472#else
1473 len = 0;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001474 break_out = -1; /* to control traps */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001475#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001476 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001477 case CNTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001478 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001479 * if the len=0 and no chars to delete */
1480 if (len == 0) {
Eric Andersencb01bb12004-08-19 18:22:13 +00001481 errno = 0;
Eric Andersened424db2001-04-23 15:28:28 +00001482prepare_to_die:
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001483#if !defined(CONFIG_ASH)
Mark Whitley4e338752001-01-26 20:42:23 +00001484 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001485 goto_new_line();
1486 /* cmdedit_reset_term() called in atexit */
1487 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001488#else
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001489 /* to control stopped jobs */
1490 len = break_out = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001491 break;
1492#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001493 } else {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001494 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001495 }
1496 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001497 case CNTRL('E'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001498 vi_case( case '$'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001499 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001500 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001501 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001502 case CNTRL('F'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001503 vi_case( case 'l'|vbit: )
1504 vi_case( case ' '|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001505 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001506 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001507 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001508 case '\b':
1509 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001510 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001511 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001512 break;
1513 case '\t':
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001514#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001515 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001516#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001517 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001518 case CNTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001519 /* Control-k -- clear to end of line */
Eric Andersen65a07302002-04-13 13:26:49 +00001520 *(command + cursor) = 0;
1521 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001522 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001523 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001524 case CNTRL('L'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001525 vi_case( case CNTRL('L')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001526 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001527 printf("\033[H");
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001528 redraw(0, len-cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001529 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001530#if MAX_HISTORY > 0
Paul Fox3f11b1b2005-08-04 19:04:46 +00001531 case CNTRL('N'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001532 vi_case( case CNTRL('N')|vbit: )
1533 vi_case( case 'j'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001534 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001535 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001536 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001537 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001538 case CNTRL('P'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001539 vi_case( case CNTRL('P')|vbit: )
1540 vi_case( case 'k'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001541 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001542 if (cur_history > 0) {
1543 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001544 goto rewrite_line;
1545 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001546 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001547 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001548 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001549#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001550 case CNTRL('U'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001551 vi_case( case CNTRL('U')|vbit: )
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001552 /* Control-U -- Clear line before cursor */
1553 if (cursor) {
1554 strcpy(command, command + cursor);
1555 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001556 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001557 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001558 case CNTRL('W'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001559 vi_case( case CNTRL('W')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001560 /* Control-W -- Remove the last word */
1561 while (cursor > 0 && isspace(command[cursor-1]))
1562 input_backspace();
1563 while (cursor > 0 &&!isspace(command[cursor-1]))
1564 input_backspace();
1565 break;
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001566#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001567 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001568 vi_cmdmode = 0;
1569 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001570 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001571 input_backward(cursor);
1572 vi_cmdmode = 0;
1573 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001574 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001575 input_forward();
1576 vi_cmdmode = 0;
1577 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001578 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001579 input_end();
1580 vi_cmdmode = 0;
1581 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001582 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001583 input_delete(1);
1584 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001585 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001586 if (cursor > 0) {
1587 input_backward(1);
1588 input_delete(1);
1589 }
1590 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001591 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001592 vi_Word_motion(command, 1);
1593 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001594 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001595 vi_word_motion(command, 1);
1596 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001597 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001598 vi_End_motion(command);
1599 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001600 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001601 vi_end_motion(command);
1602 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001603 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001604 vi_Back_motion(command);
1605 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001606 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001607 vi_back_motion(command);
1608 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001609 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001610 vi_cmdmode = 0;
1611 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001612 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001613 goto clear_to_eol;
1614
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001615 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001616 vi_cmdmode = 0;
1617 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001618 case 'd'|vbit:
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001619 {
Denis Vlasenko92758142006-10-03 19:56:34 +00001620 int nc, sc;
1621 sc = cursor;
1622 prevc = ic;
1623 if (safe_read(0, &c, 1) < 1)
1624 goto prepare_to_die;
1625 if (c == (prevc & 0xff)) {
1626 /* "cc", "dd" */
1627 input_backward(cursor);
1628 goto clear_to_eol;
1629 break;
1630 }
1631 switch(c) {
1632 case 'w':
1633 case 'W':
1634 case 'e':
1635 case 'E':
1636 switch (c) {
1637 case 'w': /* "dw", "cw" */
1638 vi_word_motion(command, vi_cmdmode);
1639 break;
1640 case 'W': /* 'dW', 'cW' */
1641 vi_Word_motion(command, vi_cmdmode);
1642 break;
1643 case 'e': /* 'de', 'ce' */
1644 vi_end_motion(command);
1645 input_forward();
1646 break;
1647 case 'E': /* 'dE', 'cE' */
1648 vi_End_motion(command);
1649 input_forward();
1650 break;
1651 }
1652 nc = cursor;
1653 input_backward(cursor - sc);
1654 while (nc-- > cursor)
1655 input_delete(1);
1656 break;
1657 case 'b': /* "db", "cb" */
1658 case 'B': /* implemented as B */
1659 if (c == 'b')
1660 vi_back_motion(command);
1661 else
1662 vi_Back_motion(command);
1663 while (sc-- > cursor)
1664 input_delete(1);
1665 break;
1666 case ' ': /* "d ", "c " */
1667 input_delete(1);
1668 break;
1669 case '$': /* "d$", "c$" */
1670 clear_to_eol:
1671 while (cursor < len)
1672 input_delete(1);
1673 break;
1674 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001675 }
1676 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001677 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001678 input_forward();
1679 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001680 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001681 put();
1682 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001683 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001684 if (safe_read(0, &c, 1) < 1)
1685 goto prepare_to_die;
1686 if (c == 0)
1687 beep();
1688 else {
1689 *(command + cursor) = c;
1690 putchar(c);
1691 putchar('\b');
1692 }
1693 break;
1694#endif /* CONFIG_FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001695
1696 case ESC:
1697
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001698#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001699 if (vi_mode) {
1700 /* ESC: insert mode --> command mode */
1701 vi_cmdmode = 1;
1702 input_backward(1);
1703 break;
1704 }
1705#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001706 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001707 if (safe_read(0, &c, 1) < 1)
1708 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001709 /* different vt100 emulations */
1710 if (c == '[' || c == 'O') {
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001711 vi_case( case '['|vbit: )
1712 vi_case( case 'O'|vbit: )
Eric Andersen7467c8d2001-07-12 20:26:32 +00001713 if (safe_read(0, &c, 1) < 1)
1714 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001715 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001716 if (c >= '1' && c <= '9') {
1717 unsigned char dummy;
1718
1719 if (safe_read(0, &dummy, 1) < 1)
1720 goto prepare_to_die;
1721 if(dummy != '~')
1722 c = 0;
1723 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001724 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001725#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001726 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001727
1728 input_tab(&lastWasTab);
1729 break;
1730#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001731#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001732 case 'A':
1733 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001734 if (cur_history > 0) {
1735 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001736 goto rewrite_line;
1737 } else {
1738 beep();
1739 }
1740 break;
1741 case 'B':
1742 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001743 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001744 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001745 /* Rewrite the line with the selected history item */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001746rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001747 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001748 len = strlen(strcpy(command, history[cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001749 /* redraw and go to eol (bol, in vi */
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001750#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001751 redraw(cmdedit_y, vi_mode ? 9999:0);
1752#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001753 redraw(cmdedit_y, 0);
Paul Fox3f11b1b2005-08-04 19:04:46 +00001754#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001755 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001756#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001757 case 'C':
1758 /* Right Arrow -- Move forward one character */
1759 input_forward();
1760 break;
1761 case 'D':
1762 /* Left Arrow -- Move back one character */
1763 input_backward(1);
1764 break;
1765 case '3':
1766 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001767 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001768 break;
1769 case '1':
1770 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001771 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001772 input_backward(cursor);
1773 break;
1774 case '4':
1775 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001776 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001777 input_end();
1778 break;
1779 default:
Glenn L McGrath475820c2004-01-22 12:42:23 +00001780 c = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001781 beep();
1782 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001783 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001784
Eric Andersenc470f442003-07-28 09:56:35 +00001785 default: /* If it's regular input, do the normal thing */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001786#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001787 /* Control-V -- Add non-printable symbol */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001788 if (c == CNTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001789 if (safe_read(0, &c, 1) < 1)
1790 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001791 if (c == 0) {
1792 beep();
1793 break;
1794 }
1795 } else
1796#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001797 {
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001798#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001799 if (vi_cmdmode) /* don't self-insert */
1800 break;
1801#endif
1802 if (!Isprint(c)) /* Skip non-printable characters */
1803 break;
1804 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001805
Eric Andersenc470f442003-07-28 09:56:35 +00001806 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001807 break;
1808
1809 len++;
1810
Eric Andersenc470f442003-07-28 09:56:35 +00001811 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001812 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001813 *(command + cursor + 1) = 0;
1814 cmdedit_set_out_char(0);
Eric Andersenc470f442003-07-28 09:56:35 +00001815 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001816 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001817
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001818 memmove(command + sc + 1, command + sc, len - sc);
1819 *(command + sc) = c;
1820 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001821 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001822 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001823 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001824 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001825 }
1826
Erik Andersenc7c634b2000-03-19 05:28:55 +00001827 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001828 }
Eric Andersenc470f442003-07-28 09:56:35 +00001829 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001830 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001831
1832 if (c != '\t')
1833 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001834 }
1835
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001836 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001837 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001838
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001839#if MAX_HISTORY > 0
Erik Andersenc7c634b2000-03-19 05:28:55 +00001840 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001841 /* cleanup may be saved current command line */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001842 if (len> 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001843 int i = n_history;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001844
1845 free(history[MAX_HISTORY]);
1846 history[MAX_HISTORY] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001847 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001848 if (i >= MAX_HISTORY) {
1849 free(history[0]);
1850 for(i = 0; i < (MAX_HISTORY-1); i++)
1851 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001852 }
Rob Landleyd921b2e2006-08-03 15:41:12 +00001853 history[i++] = xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001854 cur_history = i;
1855 n_history = i;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001856#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001857 num_ok_lines++;
1858#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001859 }
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001860#else /* MAX_HISTORY == 0 */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001861#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001862 if (len > 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001863 num_ok_lines++;
1864 }
1865#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001866#endif /* MAX_HISTORY > 0 */
Eric Andersen27bb7902003-12-23 20:24:51 +00001867 if (break_out > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001868 command[len++] = '\n'; /* set '\n' */
1869 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001870 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001871#if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersenc470f442003-07-28 09:56:35 +00001872 input_tab(0); /* strong free */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001873#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001874#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001875 free(cmdedit_prompt);
1876#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001877 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001878 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001879}
1880
Eric Andersen7467c8d2001-07-12 20:26:32 +00001881
1882
Eric Andersenc470f442003-07-28 09:56:35 +00001883#endif /* CONFIG_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001884
1885
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001886#ifdef TEST
1887
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001888const char *applet_name = "debug stuff usage";
Eric Andersene5dfced2001-04-09 22:48:12 +00001889
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001890#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001891#include <locale.h>
1892#endif
1893
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001894int main(int argc, char **argv)
1895{
1896 char buff[BUFSIZ];
1897 char *prompt =
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001898#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001899 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001900\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001901\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001902#else
1903 "% ";
1904#endif
1905
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001906#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001907 setlocale(LC_ALL, "");
1908#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001909 while(1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001910 int l;
Eric Andersen27bb7902003-12-23 20:24:51 +00001911 l = cmdedit_read_input(prompt, buff);
1912 if(l > 0 && buff[l-1] == '\n') {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001913 buff[l-1] = 0;
Eric Andersen27bb7902003-12-23 20:24:51 +00001914 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1915 } else {
1916 break;
1917 }
Eric Andersen7467c8d2001-07-12 20:26:32 +00001918 }
Eric Andersen27bb7902003-12-23 20:24:51 +00001919 printf("*** cmdedit_read_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001920 return 0;
1921}
1922
Eric Andersenc470f442003-07-28 09:56:35 +00001923#endif /* TEST */