blob: 187aa545bd01e1d62d6af9142424469443ae74e0 [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 */
16
17/*
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000018 Usage and known bugs:
Erik Andersen13456d12000-03-16 08:09:57 +000019 Terminal key codes are not extensive, and more will probably
20 need to be added. This version was created on Debian GNU/Linux 2.x.
21 Delete, Backspace, Home, End, and the arrow keys were tested
22 to work in an Xterm and console. Ctrl-A also works as Home.
Mark Whitley4e338752001-01-26 20:42:23 +000023 Ctrl-E also works as End.
Erik Andersen13456d12000-03-16 08:09:57 +000024
Eric Andersen5f2c79d2001-02-16 18:36:04 +000025 Small bugs (simple effect):
26 - not true viewing if terminal size (x*y symbols) less
27 size (prompt + editor`s line + 2 symbols)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000028 - not true viewing if length prompt less terminal width
Erik Andersen13456d12000-03-16 08:09:57 +000029 */
30
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000031#include "busybox.h"
Eric Andersencbe31da2001-02-20 06:14:08 +000032#include <sys/ioctl.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000033
"Robert P. J. Day"4eddb422006-07-03 00:46:47 +000034#include "cmdedit.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000035
Glenn L McGrath475820c2004-01-22 12:42:23 +000036
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000037#if ENABLE_LOCALE_SUPPORT
38#define Isprint(c) isprint(c)
Eric Andersene5dfced2001-04-09 22:48:12 +000039#else
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000040#define Isprint(c) ((c) >= ' ' && (c) != ((unsigned char)'\233'))
Eric Andersene5dfced2001-04-09 22:48:12 +000041#endif
42
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000043
44/* FIXME: obsolete CONFIG item? */
45#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
46
47
Glenn L McGrath3b251852004-01-03 12:07:32 +000048#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000049
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000050#define ENABLE_FEATURE_COMMAND_EDITING 0
51#define ENABLE_FEATURE_COMMAND_TAB_COMPLETION 0
52#define ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION 0
53#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
54#define ENABLE_FEATURE_CLEAN_UP 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +000055
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000056#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000057
Eric Andersen5165fbe2001-02-20 06:42:29 +000058
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000059#if ENABLE_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000060
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000061#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION || ENABLE_FEATURE_SH_FANCY_PROMPT
62#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR 1
Eric Andersen5f2c79d2001-02-16 18:36:04 +000063#endif
64
Eric Andersen5f2c79d2001-02-16 18:36:04 +000065/* Maximum length of the linked list for the command line history */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000066#if !ENABLE_FEATURE_COMMAND_HISTORY
Robert Griebl350d26b2002-12-03 22:45:46 +000067#define MAX_HISTORY 15
68#else
Denis Vlasenko9d4533e2006-11-02 22:09:37 +000069#define MAX_HISTORY (CONFIG_FEATURE_COMMAND_HISTORY + 0)
Robert Griebl350d26b2002-12-03 22:45:46 +000070#endif
71
Denis Vlasenko9d4533e2006-11-02 22:09:37 +000072#if MAX_HISTORY > 0
Glenn L McGrath062c74f2002-11-27 09:29:49 +000073static char *history[MAX_HISTORY+1]; /* history + current */
74/* saved history lines */
75static int n_history;
76/* current pointer to history line */
77static int cur_history;
78#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +000079
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000080//#include <termios.h>
Glenn L McGrath78b0e372001-06-26 02:06:08 +000081#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
82#define getTermSettings(fd,argp) tcgetattr(fd, argp);
Erik Andersen1d1d9502000-04-21 01:26:49 +000083
84/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +000085static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000086
87
Mark Whitley4e338752001-01-26 20:42:23 +000088static
Eric Andersenc470f442003-07-28 09:56:35 +000089volatile int cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +000090static
Eric Andersenc470f442003-07-28 09:56:35 +000091volatile int handlers_sets = 0; /* Set next bites: */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000092
Mark Whitley4e338752001-01-26 20:42:23 +000093enum {
Eric Andersenc470f442003-07-28 09:56:35 +000094 SET_ATEXIT = 1, /* when atexit() has been called
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000095 and get euid,uid,gid to fast compare */
Eric Andersen7467c8d2001-07-12 20:26:32 +000096 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
97 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +000098};
Erik Andersen13456d12000-03-16 08:09:57 +000099
Mark Whitley4e338752001-01-26 20:42:23 +0000100
Eric Andersenc470f442003-07-28 09:56:35 +0000101static int cmdedit_x; /* real x terminal position */
102static int cmdedit_y; /* pseudoreal y terminal position */
103static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000104
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000105static int cursor; /* required globals for signal handler */
106static int len; /* --- "" - - "" -- -"- --""-- --""--- */
107static char *command_ps; /* --- "" - - "" -- -"- --""-- --""--- */
108static SKIP_FEATURE_SH_FANCY_PROMPT(const) char *cmdedit_prompt; /* -- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000109
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000110#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000111static char *user_buf = "";
112static char *home_pwd_buf = "";
113static int my_euid;
114#endif
115
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000116#if ENABLE_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000117static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000118static int num_ok_lines = 1;
119#endif
120
121
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000122#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000123
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000124#if !ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000125static int my_euid;
126#endif
127
128static int my_uid;
129static int my_gid;
130
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000131#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Eric Andersenc470f442003-07-28 09:56:35 +0000132
Mark Whitley4e338752001-01-26 20:42:23 +0000133static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000134
Mark Whitley4e338752001-01-26 20:42:23 +0000135static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000136{
Eric Andersenc470f442003-07-28 09:56:35 +0000137 static sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000138
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000139 /* emulate || signal call */
140 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Eric Andersen8efe9672003-09-15 08:33:45 +0000141 int width = 0;
142 get_terminal_width_height(0, &width, NULL);
143 cmdedit_setwidth(width, nsig == SIGWINCH);
Mark Whitley4e338752001-01-26 20:42:23 +0000144 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000145 /* Unix not all standard in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000146
Eric Andersenc470f442003-07-28 09:56:35 +0000147 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000148 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersenc470f442003-07-28 09:56:35 +0000149 else if (nsig == SIGWINCH) /* signaled called handler */
150 signal(SIGWINCH, win_changed); /* set for next call */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000151 else /* nsig == 0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000152 /* set previous handler */
Eric Andersenc470f442003-07-28 09:56:35 +0000153 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000154}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000155
156static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000157{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000158 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000159/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen70060d22004-03-27 10:02:48 +0000160 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000161 handlers_sets &= ~SET_RESET_TERM;
162 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000163 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000164 /* reset SIGWINCH handler to previous (default) */
165 win_changed(0);
166 handlers_sets &= ~SET_WCHG_HANDLERS;
167 }
168 fflush(stdout);
Erik Andersen13456d12000-03-16 08:09:57 +0000169}
170
Mark Whitley4e338752001-01-26 20:42:23 +0000171
Mark Whitley4e338752001-01-26 20:42:23 +0000172/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000173static void cmdedit_set_out_char(int next_char)
174{
175
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000176 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000177
178 if (c == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000179 c = ' '; /* destroy end char? */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000180#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000181 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000182 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000183 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000184 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000185 c += '@';
186 if (c == 127)
187 c = '?';
188 printf("\033[7m%c\033[0m", c);
189 } else
190#endif
Rob Landleyd921b2e2006-08-03 15:41:12 +0000191 if (initial_settings.c_lflag & ECHO) putchar(c);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000192 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000193 /* terminal is scrolled down */
194 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000195 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000196
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000197 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000198 next_char = ' ';
199 /* destroy "(auto)margin" */
200 putchar(next_char);
201 putchar('\b');
202 }
203 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000204}
205
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000206/* Move to end line. Bonus: rewrite line from cursor */
207static void input_end(void)
208{
209 while (cursor < len)
210 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000211}
212
213/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000214static void goto_new_line(void)
215{
Mark Whitley4e338752001-01-26 20:42:23 +0000216 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000217 if (cmdedit_x)
218 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000219}
220
221
Rob Landley88621d72006-08-29 19:41:06 +0000222static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000223{
Robert Grieblb2301592002-07-30 23:13:51 +0000224 if ( s )
225 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000226}
Eric Andersen81fe1232003-07-29 06:38:40 +0000227
Rob Landley88621d72006-08-29 19:41:06 +0000228static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000229{
230 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000231}
232
Eric Andersenaff114c2004-04-14 17:51:38 +0000233/* Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000234/* special for slow terminal */
235static void input_backward(int num)
236{
237 if (num > cursor)
238 num = cursor;
Eric Andersenc470f442003-07-28 09:56:35 +0000239 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000240
Eric Andersenc470f442003-07-28 09:56:35 +0000241 if (cmdedit_x >= num) { /* no to up line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000242 cmdedit_x -= num;
243 if (num < 4)
244 while (num-- > 0)
245 putchar('\b');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000246 else
247 printf("\033[%dD", num);
248 } else {
249 int count_y;
250
251 if (cmdedit_x) {
Eric Andersenc470f442003-07-28 09:56:35 +0000252 putchar('\r'); /* back to first terminal pos. */
253 num -= cmdedit_x; /* set previous backward */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000254 }
255 count_y = 1 + num / cmdedit_termw;
256 printf("\033[%dA", count_y);
257 cmdedit_y -= count_y;
258 /* require forward after uping */
259 cmdedit_x = cmdedit_termw * count_y - num;
Eric Andersenc470f442003-07-28 09:56:35 +0000260 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000261 }
Erik Andersen13456d12000-03-16 08:09:57 +0000262}
263
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000264static void put_prompt(void)
265{
266 out1str(cmdedit_prompt);
Eric Andersenc470f442003-07-28 09:56:35 +0000267 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000268 cursor = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000269 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000270}
271
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000272#if !ENABLE_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000273static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000274{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000275 cmdedit_prompt = prmt_ptr;
276 cmdedit_prmt_len = strlen(prmt_ptr);
277 put_prompt();
278}
279#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000280static void parse_prompt(const char *prmt_ptr)
281{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000282 int prmt_len = 0;
"Vladimir N. Oleynik"f0874802005-09-05 15:46:26 +0000283 size_t cur_prmt_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000284 char flg_not_length = '[';
Rob Landley081e3842006-08-03 20:07:35 +0000285 char *prmt_mem_ptr = xzalloc(1);
Eric Andersene5dfced2001-04-09 22:48:12 +0000286 char *pwd_buf = xgetcwd(0);
287 char buf2[PATH_MAX + 1];
288 char buf[2];
289 char c;
290 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000291
Eric Andersen5f265b72001-05-11 16:58:46 +0000292 if (!pwd_buf) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000293 pwd_buf=(char *)bb_msg_unknown;
Eric Andersen5f265b72001-05-11 16:58:46 +0000294 }
295
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000296 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000297 pbuf = buf;
298 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000299 c = *prmt_ptr++;
300 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000301 const char *cp = prmt_ptr;
302 int l;
Eric Andersenc470f442003-07-28 09:56:35 +0000303
Manuel Novoa III cad53642003-03-19 09:13:01 +0000304 c = bb_process_escape_sequence(&prmt_ptr);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000305 if (prmt_ptr==cp) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000306 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000307 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000308 c = *prmt_ptr++;
309 switch (c) {
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000310#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000311 case 'u':
312 pbuf = user_buf;
313 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000314#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000315 case 'h':
316 pbuf = hostname_buf;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000317 if (pbuf == 0) {
Rob Landley081e3842006-08-03 20:07:35 +0000318 pbuf = xzalloc(256);
Eric Andersene5dfced2001-04-09 22:48:12 +0000319 if (gethostname(pbuf, 255) < 0) {
320 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000321 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000322 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000323
324 if (s)
325 *s = 0;
326 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000327 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000328 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000329 break;
330 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000331 c = my_euid == 0 ? '#' : '$';
332 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000333#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000334 case 'w':
335 pbuf = pwd_buf;
336 l = strlen(home_pwd_buf);
337 if (home_pwd_buf[0] != 0 &&
338 strncmp(home_pwd_buf, pbuf, l) == 0 &&
339 (pbuf[l]=='/' || pbuf[l]=='\0') &&
340 strlen(pwd_buf+l)<PATH_MAX) {
341 pbuf = buf2;
342 *pbuf = '~';
343 strcpy(pbuf+1, pwd_buf+l);
Denis Vlasenko92758142006-10-03 19:56:34 +0000344 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000345 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000346#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000347 case 'W':
348 pbuf = pwd_buf;
349 cp = strrchr(pbuf,'/');
350 if ( (cp != NULL) && (cp != pbuf) )
351 pbuf += (cp-pbuf)+1;
352 break;
353 case '!':
354 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
355 break;
356 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000357 c = '\033';
358 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000359 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000360 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000361 int h;
362 buf2[l++] = *prmt_ptr;
363 buf2[l] = 0;
364 h = strtol(buf2, &pbuf, 16);
365 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000366 l--;
367 break;
368 }
369 prmt_ptr++;
370 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000371 buf2[l] = 0;
372 c = (char)strtol(buf2, 0, 16);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000373 if (c==0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000374 c = '?';
375 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000376 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000377 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000378 if (c == flg_not_length) {
379 flg_not_length = flg_not_length == '[' ? ']' : '[';
380 continue;
381 }
382 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000383 }
Eric Andersenc470f442003-07-28 09:56:35 +0000384 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000385 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000386 if (pbuf == buf)
Eric Andersene5dfced2001-04-09 22:48:12 +0000387 *pbuf = c;
"Vladimir N. Oleynik"f0874802005-09-05 15:46:26 +0000388 cur_prmt_len = strlen(pbuf);
389 prmt_len += cur_prmt_len;
390 if (flg_not_length != ']')
391 cmdedit_prmt_len += cur_prmt_len;
Eric Andersene5dfced2001-04-09 22:48:12 +0000392 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000393 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000394 if (pwd_buf!=(char *)bb_msg_unknown)
Eric Andersen8f697842001-07-02 15:36:57 +0000395 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000396 cmdedit_prompt = prmt_mem_ptr;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000397 put_prompt();
398}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000399#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000400
401
Eric Andersenaff114c2004-04-14 17:51:38 +0000402/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000403static void redraw(int y, int back_cursor)
404{
Eric Andersenc470f442003-07-28 09:56:35 +0000405 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000406 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000407 putchar('\r');
408 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000409 input_end(); /* rewrite */
410 printf("\033[J"); /* destroy tail after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000411 input_backward(back_cursor);
412}
413
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000414#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000415#define DELBUFSIZ 128
416static char *delbuf; /* a (malloced) place to store deleted characters */
417static char *delp;
418static char newdelflag; /* whether delbuf should be reused yet */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000419#endif
420
421/* Delete the char in front of the cursor, optionally saving it
422 * for later putback */
423static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000424{
Mark Whitley4e338752001-01-26 20:42:23 +0000425 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000426
Mark Whitley4e338752001-01-26 20:42:23 +0000427 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000428 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000429
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000430#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000431 if (save) {
432 if (newdelflag) {
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000433 if (!delbuf)
434 delbuf = malloc(DELBUFSIZ);
435 /* safe if malloc fails */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000436 delp = delbuf;
437 newdelflag = 0;
438 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000439 if (delbuf && (delp - delbuf < DELBUFSIZ))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000440 *delp++ = command_ps[j];
441 }
442#endif
443
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000444 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000445 len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000446 input_end(); /* rewrite new line */
Eric Andersenc470f442003-07-28 09:56:35 +0000447 cmdedit_set_out_char(0); /* destroy end char */
448 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000449}
450
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000451#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000452static void put(void)
453{
454 int ocursor, j = delp - delbuf;
455 if (j == 0)
456 return;
457 ocursor = cursor;
458 /* open hole and then fill it */
459 memmove(command_ps + cursor + j, command_ps + cursor, len - cursor + 1);
460 strncpy(command_ps + cursor, delbuf, j);
461 len += j;
462 input_end(); /* rewrite new line */
463 input_backward(cursor-ocursor-j+1); /* at end of new text */
464}
465#endif
466
Mark Whitley4e338752001-01-26 20:42:23 +0000467/* Delete the char in back of the cursor */
468static void input_backspace(void)
469{
470 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000471 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000472 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000473 }
474}
475
476
Eric Andersenaff114c2004-04-14 17:51:38 +0000477/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000478static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000479{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000480 if (cursor < len)
481 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000482}
483
Mark Whitley4e338752001-01-26 20:42:23 +0000484static void cmdedit_setwidth(int w, int redraw_flg)
485{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000486 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000487 if (w <= cmdedit_termw) {
488 cmdedit_termw = cmdedit_termw % w;
489 }
Mark Whitley4e338752001-01-26 20:42:23 +0000490 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000491 cmdedit_termw = w;
492
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000493 if (redraw_flg) {
494 /* new y for current cursor */
495 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000496
497 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000498 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
499 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000500 }
Eric Andersenc470f442003-07-28 09:56:35 +0000501 }
Mark Whitley4e338752001-01-26 20:42:23 +0000502}
503
Eric Andersen7467c8d2001-07-12 20:26:32 +0000504static void cmdedit_init(void)
Mark Whitley4e338752001-01-26 20:42:23 +0000505{
Eric Andersen61173a52001-03-19 17:48:55 +0000506 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000507 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
508 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000509 win_changed(-SIGWINCH);
510 handlers_sets |= SET_WCHG_HANDLERS;
511 }
512
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000513 if ((handlers_sets & SET_ATEXIT) == 0) {
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000514#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000515 struct passwd *entry;
516
517 my_euid = geteuid();
518 entry = getpwuid(my_euid);
519 if (entry) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000520 user_buf = xstrdup(entry->pw_name);
521 home_pwd_buf = xstrdup(entry->pw_dir);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000522 }
523#endif
524
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000525#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000526
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000527#if !ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000528 my_euid = geteuid();
529#endif
530 my_uid = getuid();
531 my_gid = getgid();
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000532#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000533 handlers_sets |= SET_ATEXIT;
Eric Andersenc470f442003-07-28 09:56:35 +0000534 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000535 }
Mark Whitley4e338752001-01-26 20:42:23 +0000536}
Erik Andersenf0657d32000-04-12 17:49:52 +0000537
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000538#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000539
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000540static char **matches;
541static int num_matches;
542static char *add_char_to_match;
543
544static void add_match(char *matched, int add_char)
545{
546 int nm = num_matches;
547 int nm1 = nm + 1;
548
549 matches = xrealloc(matches, nm1 * sizeof(char *));
550 add_char_to_match = xrealloc(add_char_to_match, nm1);
551 matches[nm] = matched;
552 add_char_to_match[nm] = (char)add_char;
553 num_matches++;
554}
555
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000556static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000557{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000558 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
559 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
560 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
561 (st->st_mode & S_IXOTH)) return TRUE;
562 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000563}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000564
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000565#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000566
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000567static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000568{
569 struct passwd *entry;
570 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000571
Eric Andersenc470f442003-07-28 09:56:35 +0000572 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000573 userlen = strlen(ud);
574
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000575 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000576 char *sav_ud = ud - 1;
577 char *home = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000578 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000579
Eric Andersenc470f442003-07-28 09:56:35 +0000580 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000581 home = home_pwd_buf;
582 } else {
583 /* "~user/..." */
584 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000585 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000586 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000587 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000588 ud = temp;
589 if (entry)
590 home = entry->pw_dir;
591 }
592 if (home) {
593 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000594 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000595
596 /* /home/user/... */
597 sprintf(temp2, "%s%s", home, ud);
598 strcpy(sav_ud, temp2);
599 }
600 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000601 } else {
602 /* "~[^/]*" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000603 setpwent();
604
605 while ((entry = getpwent()) != NULL) {
606 /* Null usernames should result in all users as possible completions. */
607 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000608 add_match(xasprintf("~%s", entry->pw_name), '/');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000609 }
610 }
611
612 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000613 }
614}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000615#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000616
617enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000618 FIND_EXE_ONLY = 0,
619 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000620 FIND_FILE_ONLY = 2,
621};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000622
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000623#if ENABLE_ASH
Glenn L McGrath67285962004-01-14 09:34:51 +0000624const char *cmdedit_path_lookup;
625#else
626#define cmdedit_path_lookup getenv("PATH")
627#endif
628
Mark Whitley4e338752001-01-26 20:42:23 +0000629static int path_parse(char ***p, int flags)
630{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000631 int npth;
Glenn L McGrath67285962004-01-14 09:34:51 +0000632 const char *tmp;
633 const char *pth;
Mark Whitley4e338752001-01-26 20:42:23 +0000634
Mark Whitley4e338752001-01-26 20:42:23 +0000635 /* if not setenv PATH variable, to search cur dir "." */
Glenn L McGrath67285962004-01-14 09:34:51 +0000636 if (flags != FIND_EXE_ONLY || (pth = cmdedit_path_lookup) == 0 ||
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000637 /* PATH=<empty> or PATH=:<empty> */
638 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000639 return 1;
640 }
641
642 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000643 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000644
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000645 for (;;) {
Eric Andersenc470f442003-07-28 09:56:35 +0000646 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000647 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000648 if (tmp) {
649 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000650 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000651 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000652 break;
653 }
654
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000655 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000656
657 tmp = pth;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000658 (*p)[0] = xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000659 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000660
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000661 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000662 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000663 if (tmp) {
Eric Andersenc470f442003-07-28 09:56:35 +0000664 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000665 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000666 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000667 } else
668 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000669 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000670 }
671
672 return npth;
673}
674
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000675static char *add_quote_for_spec_chars(char *found, int add)
Erik Andersen6273f652000-03-17 01:12:41 +0000676{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000677 int l = 0;
678 char *s = xmalloc((strlen(found) + 1) * 2);
679
680 while (*found) {
681 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
682 s[l++] = '\\';
683 s[l++] = *found++;
684 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000685 if (add)
Denis Vlasenko92758142006-10-03 19:56:34 +0000686 s[l++] = (char)add;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000687 s[l] = 0;
688 return s;
689}
690
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000691static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000692{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000693 DIR *dir;
694 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000695 char dirbuf[BUFSIZ];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000696 struct stat st;
697 char *path1[1];
698 char **paths = path1;
699 int npaths;
700 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000701 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000702 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000703
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000704 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000705
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000706 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000707 /* no dir, if flags==EXE_ONLY - get paths, else "." */
708 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000709 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000710 } else {
711 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000712 /* save for change */
713 strcpy(dirbuf, command);
714 /* set dir only */
715 dirbuf[(pfind - command) + 1] = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000716#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000717 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000718 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000719#endif
720 /* "strip" dirname in command */
721 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000722
Mark Whitley4e338752001-01-26 20:42:23 +0000723 paths[0] = dirbuf;
Eric Andersenc470f442003-07-28 09:56:35 +0000724 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000725 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000726
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000727 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000728
Mark Whitley4e338752001-01-26 20:42:23 +0000729 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000730 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000731 continue;
732
733 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000734 char *str_found = next->d_name;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000735 int add_chr = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000736
Mark Whitley4e338752001-01-26 20:42:23 +0000737 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000738 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000739 continue;
740 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000741 if (*str_found == '.' && *pfind == 0) {
742 if (*paths[i] == '/' && paths[i][1] == 0
Eric Andersenc470f442003-07-28 09:56:35 +0000743 && str_found[1] == 0) str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000744 else
Mark Whitley4e338752001-01-26 20:42:23 +0000745 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000746 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000747 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000748 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000749 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000750 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000751 /* find with dirs ? */
752 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000753 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000754 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000755 /* name is directory */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000756 char *e = found + strlen(found) - 1;
757
758 add_chr = '/';
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000759 if (*e == '/')
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000760 *e = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +0000761 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000762 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000763 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000764 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000765 if (type == FIND_FILE_ONLY ||
Matt Kraai1f0c4362001-12-20 23:13:26 +0000766 (type == FIND_EXE_ONLY && is_execute(&st)))
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000767 add_chr = ' ';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000768 }
Mark Whitley4e338752001-01-26 20:42:23 +0000769 /* Add it to the list */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000770 add_match(found, add_chr);
771 continue;
Eric Andersene5dfced2001-04-09 22:48:12 +0000772cont:
773 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000774 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000775 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000776 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000777 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000778 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000779 free(paths);
780 }
Erik Andersen6273f652000-03-17 01:12:41 +0000781}
Erik Andersenf0657d32000-04-12 17:49:52 +0000782
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000783
784#define QUOT (UCHAR_MAX+1)
785
786#define collapse_pos(is, in) { \
Paul Fox574fee42005-07-19 20:41:06 +0000787 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
788 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000789
790static int find_match(char *matchBuf, int *len_with_quotes)
791{
792 int i, j;
793 int command_mode;
794 int c, c2;
795 int int_buf[BUFSIZ + 1];
796 int pos_buf[BUFSIZ + 1];
797
798 /* set to integer dimension characters and own positions */
799 for (i = 0;; i++) {
800 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
801 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000802 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000803 break;
804 } else
805 pos_buf[i] = i;
806 }
807
808 /* mask \+symbol and convert '\t' to ' ' */
809 for (i = j = 0; matchBuf[i]; i++, j++)
810 if (matchBuf[i] == '\\') {
811 collapse_pos(j, j + 1);
812 int_buf[j] |= QUOT;
813 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000814#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000815 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000816 int_buf[j] = ' ' | QUOT;
817#endif
818 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000819#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000820 else if (matchBuf[i] == '\t')
821 int_buf[j] = ' ';
822#endif
823
824 /* mask "symbols" or 'symbols' */
825 c2 = 0;
826 for (i = 0; int_buf[i]; i++) {
827 c = int_buf[i];
828 if (c == '\'' || c == '"') {
829 if (c2 == 0)
830 c2 = c;
831 else {
832 if (c == c2)
833 c2 = 0;
834 else
835 int_buf[i] |= QUOT;
836 }
837 } else if (c2 != 0 && c != '$')
838 int_buf[i] |= QUOT;
839 }
840
841 /* skip commands with arguments if line have commands delimiters */
842 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
843 for (i = 0; int_buf[i]; i++) {
844 c = int_buf[i];
845 c2 = int_buf[i + 1];
846 j = i ? int_buf[i - 1] : -1;
847 command_mode = 0;
848 if (c == ';' || c == '&' || c == '|') {
849 command_mode = 1 + (c == c2);
850 if (c == '&') {
851 if (j == '>' || j == '<')
852 command_mode = 0;
853 } else if (c == '|' && j == '>')
854 command_mode = 0;
855 }
856 if (command_mode) {
857 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000858 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000859 }
860 }
861 /* collapse `command...` */
862 for (i = 0; int_buf[i]; i++)
863 if (int_buf[i] == '`') {
864 for (j = i + 1; int_buf[j]; j++)
865 if (int_buf[j] == '`') {
866 collapse_pos(i, j + 1);
867 j = 0;
868 break;
869 }
870 if (j) {
871 /* not found close ` - command mode, collapse all previous */
872 collapse_pos(0, i + 1);
873 break;
874 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000875 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000876 }
877
878 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000879 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000880 c2 = 0;
881 for (i = 0; int_buf[i]; i++)
882 if (int_buf[i] == '(' || int_buf[i] == '{') {
883 if (int_buf[i] == '(')
884 c++;
885 else
886 c2++;
887 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000888 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000889 }
890 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
891 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
892 if (int_buf[i] == ')')
893 c--;
894 else
895 c2--;
896 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000897 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000898 }
899
900 /* skip first not quote space */
901 for (i = 0; int_buf[i]; i++)
902 if (int_buf[i] != ' ')
903 break;
904 if (i)
905 collapse_pos(0, i);
906
907 /* set find mode for completion */
908 command_mode = FIND_EXE_ONLY;
909 for (i = 0; int_buf[i]; i++)
910 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
911 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000912 && matchBuf[pos_buf[0]]=='c'
913 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000914 command_mode = FIND_DIR_ONLY;
915 else {
916 command_mode = FIND_FILE_ONLY;
917 break;
918 }
919 }
920 /* "strlen" */
921 for (i = 0; int_buf[i]; i++);
922 /* find last word */
923 for (--i; i >= 0; i--) {
924 c = int_buf[i];
925 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
926 collapse_pos(0, i + 1);
927 break;
928 }
929 }
930 /* skip first not quoted '\'' or '"' */
931 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
932 /* collapse quote or unquote // or /~ */
Eric Andersenc470f442003-07-28 09:56:35 +0000933 while ((int_buf[i] & ~QUOT) == '/' &&
Mark Whitley7e5291f2001-03-08 19:31:12 +0000934 ((int_buf[i + 1] & ~QUOT) == '/'
935 || (int_buf[i + 1] & ~QUOT) == '~')) {
936 i++;
937 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000938
939 /* set only match and destroy quotes */
940 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000941 for (c = 0; pos_buf[i] >= 0; i++) {
942 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000943 j = pos_buf[i] + 1;
944 }
Eric Andersen4f990532001-05-31 17:15:57 +0000945 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000946 /* old lenght matchBuf with quotes symbols */
947 *len_with_quotes = j ? j - pos_buf[0] : 0;
948
949 return command_mode;
950}
951
Glenn L McGrath4d001292003-01-06 01:11:50 +0000952/*
953 display by column original ideas from ls applet,
954 very optimize by my :)
955*/
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000956static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000957{
958 int ncols, row;
959 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000960 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000961 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000962 char str_add_chr[2];
963 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000964
965 /* find the longest file name- use that as the column width */
966 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000967 l = strlen(matches[row]);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000968 if (add_char_to_match[row])
Denis Vlasenko92758142006-10-03 19:56:34 +0000969 l++;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000970 if (column_width < l)
971 column_width = l;
972 }
973 column_width += 2; /* min space for columns */
974 ncols = cmdedit_termw / column_width;
975
976 if (ncols > 1) {
977 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000978 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000979 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000980 } else {
981 ncols = 1;
982 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000983 str_add_chr[1] = 0;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000984 for (row = 0; row < nrows; row++) {
985 int n = row;
986 int nc;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000987 int acol;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000988
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000989 for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
990 str_add_chr[0] = add_char_to_match[n];
991 acol = str_add_chr[0] ? column_width - 1 : column_width;
992 printf("%s%s", matches[n], str_add_chr);
993 l = strlen(matches[n]);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000994 while (l < acol) {
Denis Vlasenko92758142006-10-03 19:56:34 +0000995 putchar(' ');
996 l++;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000997 }
998 }
999 str_add_chr[0] = add_char_to_match[n];
1000 printf("%s%s\n", matches[n], str_add_chr);
Glenn L McGrath4d001292003-01-06 01:11:50 +00001001 }
1002}
1003
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001004static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001005{
1006 /* Do TAB completion */
Eric Andersenc470f442003-07-28 09:56:35 +00001007 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001008 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001009 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001010 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001011 free(matches);
1012 matches = (char **) NULL;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001013 free(add_char_to_match);
1014 add_char_to_match = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +00001015 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001016 return;
1017 }
Matt Kraai1f0c4362001-12-20 23:13:26 +00001018 if (! *lastWasTab) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001019
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001020 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001021 int len_found;
1022 char matchBuf[BUFSIZ];
1023 int find_type;
1024 int recalc_pos;
1025
Eric Andersenc470f442003-07-28 09:56:35 +00001026 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001027
1028 /* Make a local copy of the string -- up
1029 * to the position of the cursor */
1030 tmp = strncpy(matchBuf, command_ps, cursor);
1031 tmp[cursor] = 0;
1032
1033 find_type = find_match(matchBuf, &recalc_pos);
1034
1035 /* Free up any memory already allocated */
1036 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001037
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001038#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001039 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001040 * then try completing this word as a username. */
1041
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001042 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001043 username_tab_completion(matchBuf, NULL);
1044 if (!matches)
Mark Whitley4e338752001-01-26 20:42:23 +00001045#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001046 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001047 * in the current working directory that matches. */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001048 exe_n_cwd_tab_completion(matchBuf, find_type);
1049 /* Remove duplicate found and sort */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001050 if (matches) {
1051 int i, n;
1052 /* strcmp is int(*f)(const char*, const char*) */
1053 /* qsort wants int(*f)(const void*, const void*) */
1054 /* We cheat here :) */
1055 qsort(matches, num_matches, sizeof(char*), (void*)strcmp);
1056 i = 0;
1057 while (i < num_matches - 1) {
1058 n = i + 1;
1059 if (matches[i] && matches[n]) {
1060 while (n < num_matches
1061 && !strcmp(matches[i], matches[n])) {
1062 free(matches[n]);
1063 matches[n] = 0;
1064 n++;
Denis Vlasenko92758142006-10-03 19:56:34 +00001065 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001066 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001067 i = n;
Denis Vlasenko92758142006-10-03 19:56:34 +00001068 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001069 n = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001070 for(i = 0; i < num_matches; i++)
1071 if (matches[i]) {
1072 matches[n] = matches[i];
1073 add_char_to_match[n] = add_char_to_match[i];
Denis Vlasenko92758142006-10-03 19:56:34 +00001074 n++;
1075 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001076 num_matches = n;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001077 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001078 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001079 if (!matches || num_matches > 1) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001080
Mark Whitley4e338752001-01-26 20:42:23 +00001081 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001082 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +00001083 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001084 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001085 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001086 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001087 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001088 if (matches[len_found][(tmp - tmp1)] != *tmp) {
1089 *tmp = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001090 break;
1091 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001092 if (*tmp1 == 0) { /* have unique */
1093 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001094 return;
1095 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001096 tmp = add_quote_for_spec_chars(tmp1, 0);
1097 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +00001098 } else { /* one match */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001099 tmp = add_quote_for_spec_chars(matches[0], add_char_to_match[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001100 /* for next completion current found */
1101 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001102 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001103 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001104 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001105 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001106
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001107 /* before word for match */
1108 command_ps[cursor - recalc_pos] = 0;
1109 /* save tail line */
1110 strcpy(matchBuf, command_ps + cursor);
1111 /* add match */
1112 strcat(command_ps, tmp);
1113 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001114 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001115 /* back to begin word for match */
1116 input_backward(recalc_pos);
1117 /* new pos */
1118 recalc_pos = cursor + len_found;
1119 /* new len */
1120 len = strlen(command_ps);
1121 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001122 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001123 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001124 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001125 } else {
1126 /* Ok -- the last char was a TAB. Since they
1127 * just hit TAB again, print a list of all the
1128 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001129 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001130 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001131
1132 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001133 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001134 showfiles();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001135 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001136 }
1137 }
1138}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001139#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001140
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001141#if MAX_HISTORY > 0
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001142static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001143{
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001144 if (command_ps[0] != 0 || history[cur_history] == 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001145 free(history[cur_history]);
Rob Landleyd921b2e2006-08-03 15:41:12 +00001146 history[cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001147 }
1148 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +00001149}
1150
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001151static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001152{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001153 int ch = cur_history;
1154
1155 if (ch < n_history) {
1156 get_previous_history(); /* save the current history line */
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001157 cur_history = ch + 1;
1158 return cur_history;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001159 } else {
1160 beep();
1161 return 0;
1162 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001163}
Robert Griebl350d26b2002-12-03 22:45:46 +00001164
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001165#if ENABLE_FEATURE_COMMAND_SAVEHISTORY
Rob Landleydfba7412006-03-06 20:47:33 +00001166void load_history ( const char *fromfile )
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001167{
Robert Griebl350d26b2002-12-03 22:45:46 +00001168 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001169 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001170
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001171 /* cleanup old */
1172
1173 for(hi = n_history; hi > 0; ) {
1174 hi--;
1175 free ( history [hi] );
Robert Griebl350d26b2002-12-03 22:45:46 +00001176 }
1177
1178 if (( fp = fopen ( fromfile, "r" ))) {
Eric Andersenc470f442003-07-28 09:56:35 +00001179
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001180 for ( hi = 0; hi < MAX_HISTORY; ) {
Denis Vlasenko2d5ca602006-10-12 22:43:20 +00001181 char * hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001182 int l;
1183
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001184 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001185 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001186 l = strlen(hl);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001187 if (l >= BUFSIZ)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001188 hl[BUFSIZ-1] = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001189 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001190 free(hl);
1191 continue;
1192 }
1193 history [hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001194 }
1195 fclose ( fp );
1196 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001197 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001198}
1199
Rob Landleydfba7412006-03-06 20:47:33 +00001200void save_history ( const char *tofile )
Robert Griebl350d26b2002-12-03 22:45:46 +00001201{
Robert Griebl350d26b2002-12-03 22:45:46 +00001202 FILE *fp = fopen ( tofile, "w" );
Eric Andersenc470f442003-07-28 09:56:35 +00001203
Robert Griebl350d26b2002-12-03 22:45:46 +00001204 if ( fp ) {
1205 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001206
Robert Griebl350d26b2002-12-03 22:45:46 +00001207 for ( i = 0; i < n_history; i++ ) {
Eric Andersen27bb7902003-12-23 20:24:51 +00001208 fprintf(fp, "%s\n", history [i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001209 }
1210 fclose ( fp );
1211 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001212}
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001213#endif
Robert Griebl350d26b2002-12-03 22:45:46 +00001214
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001215#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001216
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001217enum {
1218 ESC = 27,
1219 DEL = 127,
1220};
1221
1222
Erik Andersen6273f652000-03-17 01:12:41 +00001223/*
1224 * This function is used to grab a character buffer
1225 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001226 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001227 * a mini readline).
1228 *
1229 * The following standard commands are not implemented:
1230 * ESC-b -- Move back one word
1231 * ESC-f -- Move forward one word
1232 * ESC-d -- Delete back one word
1233 * ESC-h -- Delete forward one word
1234 * CTL-t -- Transpose two characters
1235 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001236 * Minimalist vi-style command line editing available if configured.
1237 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001238 *
Erik Andersen6273f652000-03-17 01:12:41 +00001239 */
Eric Andersenc470f442003-07-28 09:56:35 +00001240
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001241#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001242static int vi_mode;
1243
1244void setvimode ( int viflag )
1245{
1246 vi_mode = viflag;
1247}
1248
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001249static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001250vi_Word_motion(char *command, int eat)
1251{
1252 while (cursor < len && !isspace(command[cursor]))
1253 input_forward();
1254 if (eat) while (cursor < len && isspace(command[cursor]))
1255 input_forward();
1256}
1257
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001258static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001259vi_word_motion(char *command, int eat)
1260{
1261 if (isalnum(command[cursor]) || command[cursor] == '_') {
1262 while (cursor < len &&
1263 (isalnum(command[cursor+1]) ||
1264 command[cursor+1] == '_'))
1265 input_forward();
1266 } else if (ispunct(command[cursor])) {
1267 while (cursor < len &&
1268 (ispunct(command[cursor+1])))
1269 input_forward();
1270 }
1271
1272 if (cursor < len)
1273 input_forward();
1274
1275 if (eat && cursor < len && isspace(command[cursor]))
1276 while (cursor < len && isspace(command[cursor]))
1277 input_forward();
1278}
1279
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001280static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001281vi_End_motion(char *command)
1282{
1283 input_forward();
1284 while (cursor < len && isspace(command[cursor]))
1285 input_forward();
1286 while (cursor < len-1 && !isspace(command[cursor+1]))
1287 input_forward();
1288}
1289
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001290static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001291vi_end_motion(char *command)
1292{
1293 if (cursor >= len-1)
1294 return;
1295 input_forward();
1296 while (cursor < len-1 && isspace(command[cursor]))
1297 input_forward();
1298 if (cursor >= len-1)
1299 return;
1300 if (isalnum(command[cursor]) || command[cursor] == '_') {
1301 while (cursor < len-1 &&
1302 (isalnum(command[cursor+1]) ||
1303 command[cursor+1] == '_'))
1304 input_forward();
1305 } else if (ispunct(command[cursor])) {
1306 while (cursor < len-1 &&
1307 (ispunct(command[cursor+1])))
1308 input_forward();
1309 }
1310}
1311
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001312static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001313vi_Back_motion(char *command)
1314{
1315 while (cursor > 0 && isspace(command[cursor-1]))
1316 input_backward(1);
1317 while (cursor > 0 && !isspace(command[cursor-1]))
1318 input_backward(1);
1319}
1320
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001321static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001322vi_back_motion(char *command)
1323{
1324 if (cursor <= 0)
1325 return;
1326 input_backward(1);
1327 while (cursor > 0 && isspace(command[cursor]))
1328 input_backward(1);
1329 if (cursor <= 0)
1330 return;
1331 if (isalnum(command[cursor]) || command[cursor] == '_') {
1332 while (cursor > 0 &&
1333 (isalnum(command[cursor-1]) ||
1334 command[cursor-1] == '_'))
1335 input_backward(1);
1336 } else if (ispunct(command[cursor])) {
1337 while (cursor > 0 &&
1338 (ispunct(command[cursor-1])))
1339 input_backward(1);
1340 }
1341}
1342#endif
1343
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001344/*
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001345 * the emacs and vi modes share much of the code in the big
1346 * command loop. commands entered when in vi's command mode (aka
1347 * "escape mode") get an extra bit added to distinguish them --
1348 * this keeps them from being self-inserted. this clutters the
1349 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001350 */
1351
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001352#define vbit 0x100
1353
1354/* leave out the "vi-mode"-only case labels if vi editing isn't
1355 * configured. */
1356#define vi_case(caselabel) USE_FEATURE_COMMAND_EDITING(caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001357
1358/* convert uppercase ascii to equivalent control char, for readability */
1359#define CNTRL(uc_char) ((uc_char) - 0x40)
1360
Eric Andersen044228d2001-07-17 01:12:36 +00001361
1362int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001363{
1364
Erik Andersenc7c634b2000-03-19 05:28:55 +00001365 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001366 int lastWasTab = FALSE;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001367 unsigned char c;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001368 unsigned int ic;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001369#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001370 unsigned int prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001371 int vi_cmdmode = 0;
1372#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001373 /* prepare before init handlers */
Eric Andersenc470f442003-07-28 09:56:35 +00001374 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001375 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001376 command_ps = command;
1377
Eric Andersen34506362001-08-02 05:02:46 +00001378 getTermSettings(0, (void *) &initial_settings);
1379 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1380 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1381 /* Turn off echoing and CTRL-C, so we can trap it */
1382 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Eric Andersen34506362001-08-02 05:02:46 +00001383 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1384 new_settings.c_cc[VMIN] = 1;
1385 new_settings.c_cc[VTIME] = 0;
1386 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001387# ifndef _POSIX_VDISABLE
1388# define _POSIX_VDISABLE '\0'
1389# endif
Eric Andersenc470f442003-07-28 09:56:35 +00001390 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001391 command[0] = 0;
1392
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001393 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001394 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001395
Eric Andersen6faae7d2001-02-16 20:09:17 +00001396 /* Now initialize things */
1397 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001398 /* Print out the command prompt */
1399 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001400
Erik Andersenc7c634b2000-03-19 05:28:55 +00001401 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001402
Eric Andersenc470f442003-07-28 09:56:35 +00001403 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001404
Eric Andersen7467c8d2001-07-12 20:26:32 +00001405 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001406 /* if we can't read input then exit */
1407 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001408
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001409 ic = c;
1410
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001411#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001412 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001413 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001414 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001415#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001416 switch (ic)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001417 {
Erik Andersenf0657d32000-04-12 17:49:52 +00001418 case '\n':
1419 case '\r':
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001420 vi_case( case '\n'|vbit: )
1421 vi_case( case '\r'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001422 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001423 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001424 break_out = 1;
1425 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001426 case CNTRL('A'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001427 vi_case( case '0'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001428 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001429 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001430 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001431 case CNTRL('B'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001432 vi_case( case 'h'|vbit: )
1433 vi_case( case '\b'|vbit: )
1434 vi_case( case DEL|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001435 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001436 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001437 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001438 case CNTRL('C'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001439 vi_case( case CNTRL('C')|vbit: )
Eric Andersen86349772000-12-18 20:25:50 +00001440 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001441 goto_new_line();
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001442#if !ENABLE_ASH
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001443 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001444 len = 0;
1445 lastWasTab = FALSE;
1446 put_prompt();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001447#else
1448 len = 0;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001449 break_out = -1; /* to control traps */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001450#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001451 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001452 case CNTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001453 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001454 * if the len=0 and no chars to delete */
1455 if (len == 0) {
Eric Andersencb01bb12004-08-19 18:22:13 +00001456 errno = 0;
Eric Andersened424db2001-04-23 15:28:28 +00001457prepare_to_die:
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001458#if !ENABLE_ASH
Mark Whitley4e338752001-01-26 20:42:23 +00001459 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001460 goto_new_line();
1461 /* cmdedit_reset_term() called in atexit */
1462 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001463#else
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001464 /* to control stopped jobs */
1465 len = break_out = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001466 break;
1467#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001468 } else {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001469 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001470 }
1471 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001472 case CNTRL('E'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001473 vi_case( case '$'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001474 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001475 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001476 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001477 case CNTRL('F'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001478 vi_case( case 'l'|vbit: )
1479 vi_case( case ' '|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001480 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001481 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001482 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001483 case '\b':
1484 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001485 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001486 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001487 break;
1488 case '\t':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001489#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001490 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001491#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001492 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001493 case CNTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001494 /* Control-k -- clear to end of line */
Eric Andersen65a07302002-04-13 13:26:49 +00001495 *(command + cursor) = 0;
1496 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001497 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001498 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001499 case CNTRL('L'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001500 vi_case( case CNTRL('L')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001501 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001502 printf("\033[H");
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001503 redraw(0, len-cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001504 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001505#if MAX_HISTORY > 0
Paul Fox3f11b1b2005-08-04 19:04:46 +00001506 case CNTRL('N'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001507 vi_case( case CNTRL('N')|vbit: )
1508 vi_case( case 'j'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001509 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001510 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001511 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001512 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001513 case CNTRL('P'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001514 vi_case( case CNTRL('P')|vbit: )
1515 vi_case( case 'k'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001516 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001517 if (cur_history > 0) {
1518 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001519 goto rewrite_line;
1520 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001521 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001522 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001523 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001524#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001525 case CNTRL('U'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001526 vi_case( case CNTRL('U')|vbit: )
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001527 /* Control-U -- Clear line before cursor */
1528 if (cursor) {
1529 strcpy(command, command + cursor);
1530 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001531 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001532 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001533 case CNTRL('W'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001534 vi_case( case CNTRL('W')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001535 /* Control-W -- Remove the last word */
1536 while (cursor > 0 && isspace(command[cursor-1]))
1537 input_backspace();
1538 while (cursor > 0 &&!isspace(command[cursor-1]))
1539 input_backspace();
1540 break;
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001541#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001542 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001543 vi_cmdmode = 0;
1544 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001545 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001546 input_backward(cursor);
1547 vi_cmdmode = 0;
1548 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001549 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001550 input_forward();
1551 vi_cmdmode = 0;
1552 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001553 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001554 input_end();
1555 vi_cmdmode = 0;
1556 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001557 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001558 input_delete(1);
1559 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001560 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001561 if (cursor > 0) {
1562 input_backward(1);
1563 input_delete(1);
1564 }
1565 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001566 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001567 vi_Word_motion(command, 1);
1568 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001569 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001570 vi_word_motion(command, 1);
1571 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001572 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001573 vi_End_motion(command);
1574 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001575 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001576 vi_end_motion(command);
1577 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001578 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001579 vi_Back_motion(command);
1580 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001581 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001582 vi_back_motion(command);
1583 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001584 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001585 vi_cmdmode = 0;
1586 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001587 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001588 goto clear_to_eol;
1589
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001590 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001591 vi_cmdmode = 0;
1592 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001593 case 'd'|vbit:
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001594 {
Denis Vlasenko92758142006-10-03 19:56:34 +00001595 int nc, sc;
1596 sc = cursor;
1597 prevc = ic;
1598 if (safe_read(0, &c, 1) < 1)
1599 goto prepare_to_die;
1600 if (c == (prevc & 0xff)) {
1601 /* "cc", "dd" */
1602 input_backward(cursor);
1603 goto clear_to_eol;
1604 break;
1605 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001606 switch (c) {
Denis Vlasenko92758142006-10-03 19:56:34 +00001607 case 'w':
1608 case 'W':
1609 case 'e':
1610 case 'E':
1611 switch (c) {
1612 case 'w': /* "dw", "cw" */
1613 vi_word_motion(command, vi_cmdmode);
1614 break;
1615 case 'W': /* 'dW', 'cW' */
1616 vi_Word_motion(command, vi_cmdmode);
1617 break;
1618 case 'e': /* 'de', 'ce' */
1619 vi_end_motion(command);
1620 input_forward();
1621 break;
1622 case 'E': /* 'dE', 'cE' */
1623 vi_End_motion(command);
1624 input_forward();
1625 break;
1626 }
1627 nc = cursor;
1628 input_backward(cursor - sc);
1629 while (nc-- > cursor)
1630 input_delete(1);
1631 break;
1632 case 'b': /* "db", "cb" */
1633 case 'B': /* implemented as B */
1634 if (c == 'b')
1635 vi_back_motion(command);
1636 else
1637 vi_Back_motion(command);
1638 while (sc-- > cursor)
1639 input_delete(1);
1640 break;
1641 case ' ': /* "d ", "c " */
1642 input_delete(1);
1643 break;
1644 case '$': /* "d$", "c$" */
1645 clear_to_eol:
1646 while (cursor < len)
1647 input_delete(1);
1648 break;
1649 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001650 }
1651 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001652 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001653 input_forward();
1654 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001655 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001656 put();
1657 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001658 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001659 if (safe_read(0, &c, 1) < 1)
1660 goto prepare_to_die;
1661 if (c == 0)
1662 beep();
1663 else {
1664 *(command + cursor) = c;
1665 putchar(c);
1666 putchar('\b');
1667 }
1668 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001669#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001670
1671 case ESC:
1672
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001673#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001674 if (vi_mode) {
1675 /* ESC: insert mode --> command mode */
1676 vi_cmdmode = 1;
1677 input_backward(1);
1678 break;
1679 }
1680#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001681 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001682 if (safe_read(0, &c, 1) < 1)
1683 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001684 /* different vt100 emulations */
1685 if (c == '[' || c == 'O') {
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001686 vi_case( case '['|vbit: )
1687 vi_case( case 'O'|vbit: )
Eric Andersen7467c8d2001-07-12 20:26:32 +00001688 if (safe_read(0, &c, 1) < 1)
1689 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001690 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001691 if (c >= '1' && c <= '9') {
1692 unsigned char dummy;
1693
1694 if (safe_read(0, &dummy, 1) < 1)
1695 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001696 if (dummy != '~')
Glenn L McGrath475820c2004-01-22 12:42:23 +00001697 c = 0;
1698 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001699 switch (c) {
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001700#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001701 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001702
1703 input_tab(&lastWasTab);
1704 break;
1705#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001706#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001707 case 'A':
1708 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001709 if (cur_history > 0) {
1710 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001711 goto rewrite_line;
1712 } else {
1713 beep();
1714 }
1715 break;
1716 case 'B':
1717 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001718 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001719 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001720 /* Rewrite the line with the selected history item */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001721rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001722 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001723 len = strlen(strcpy(command, history[cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001724 /* redraw and go to eol (bol, in vi */
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001725#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001726 redraw(cmdedit_y, vi_mode ? 9999:0);
1727#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001728 redraw(cmdedit_y, 0);
Paul Fox3f11b1b2005-08-04 19:04:46 +00001729#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001730 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001731#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001732 case 'C':
1733 /* Right Arrow -- Move forward one character */
1734 input_forward();
1735 break;
1736 case 'D':
1737 /* Left Arrow -- Move back one character */
1738 input_backward(1);
1739 break;
1740 case '3':
1741 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001742 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001743 break;
1744 case '1':
1745 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001746 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001747 input_backward(cursor);
1748 break;
1749 case '4':
1750 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001751 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001752 input_end();
1753 break;
1754 default:
Glenn L McGrath475820c2004-01-22 12:42:23 +00001755 c = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001756 beep();
1757 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001758 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001759
Eric Andersenc470f442003-07-28 09:56:35 +00001760 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001761#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001762 /* Control-V -- Add non-printable symbol */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001763 if (c == CNTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001764 if (safe_read(0, &c, 1) < 1)
1765 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001766 if (c == 0) {
1767 beep();
1768 break;
1769 }
1770 } else
1771#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001772 {
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001773#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001774 if (vi_cmdmode) /* don't self-insert */
1775 break;
1776#endif
1777 if (!Isprint(c)) /* Skip non-printable characters */
1778 break;
1779 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001780
Eric Andersenc470f442003-07-28 09:56:35 +00001781 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001782 break;
1783
1784 len++;
1785
Eric Andersenc470f442003-07-28 09:56:35 +00001786 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001787 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001788 *(command + cursor + 1) = 0;
1789 cmdedit_set_out_char(0);
Eric Andersenc470f442003-07-28 09:56:35 +00001790 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001791 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001792
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001793 memmove(command + sc + 1, command + sc, len - sc);
1794 *(command + sc) = c;
1795 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001796 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001797 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001798 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001799 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001800 }
1801
Erik Andersenc7c634b2000-03-19 05:28:55 +00001802 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001803 }
Eric Andersenc470f442003-07-28 09:56:35 +00001804 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001805 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001806
1807 if (c != '\t')
1808 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001809 }
1810
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001811 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001812 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001813
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001814#if MAX_HISTORY > 0
Erik Andersenc7c634b2000-03-19 05:28:55 +00001815 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001816 /* cleanup may be saved current command line */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001817 if (len> 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001818 int i = n_history;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001819
1820 free(history[MAX_HISTORY]);
1821 history[MAX_HISTORY] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001822 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001823 if (i >= MAX_HISTORY) {
1824 free(history[0]);
1825 for(i = 0; i < (MAX_HISTORY-1); i++)
1826 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001827 }
Rob Landleyd921b2e2006-08-03 15:41:12 +00001828 history[i++] = xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001829 cur_history = i;
1830 n_history = i;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001831#if ENABLE_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001832 num_ok_lines++;
1833#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001834 }
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001835#else /* MAX_HISTORY == 0 */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001836#if ENABLE_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001837 if (len > 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001838 num_ok_lines++;
1839 }
1840#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001841#endif /* MAX_HISTORY > 0 */
Eric Andersen27bb7902003-12-23 20:24:51 +00001842 if (break_out > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001843 command[len++] = '\n'; /* set '\n' */
1844 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001845 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001846#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001847 input_tab(0); /* strong free */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001848#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001849#if ENABLE_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001850 free(cmdedit_prompt);
1851#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001852 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001853 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001854}
1855
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001856#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001857
1858
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001859#ifdef TEST
1860
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001861const char *applet_name = "debug stuff usage";
Eric Andersene5dfced2001-04-09 22:48:12 +00001862
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001863#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001864#include <locale.h>
1865#endif
1866
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001867int main(int argc, char **argv)
1868{
1869 char buff[BUFSIZ];
1870 char *prompt =
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001871#if ENABLE_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001872 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001873\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001874\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001875#else
1876 "% ";
1877#endif
1878
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001879#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001880 setlocale(LC_ALL, "");
1881#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001882 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001883 int l;
Eric Andersen27bb7902003-12-23 20:24:51 +00001884 l = cmdedit_read_input(prompt, buff);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001885 if (l > 0 && buff[l-1] == '\n') {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001886 buff[l-1] = 0;
Eric Andersen27bb7902003-12-23 20:24:51 +00001887 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1888 } else {
1889 break;
1890 }
Eric Andersen7467c8d2001-07-12 20:26:32 +00001891 }
Eric Andersen27bb7902003-12-23 20:24:51 +00001892 printf("*** cmdedit_read_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001893 return 0;
1894}
1895
Eric Andersenc470f442003-07-28 09:56:35 +00001896#endif /* TEST */