blob: 944be00ab19c86c6cfee5691fdfa70c98654e3f3 [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;
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000992 printf("%s%s%-*s", matches[n], str_add_chr,
993 acol - strlen(matches[n]), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000994 }
995 str_add_chr[0] = add_char_to_match[n];
996 printf("%s%s\n", matches[n], str_add_chr);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000997 }
998}
999
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001000static int match_compare(const void *a, const void *b)
1001{
1002 return strcmp(*(char**)a, *(char**)b);
1003}
1004
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001005static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001006{
1007 /* Do TAB completion */
Eric Andersenc470f442003-07-28 09:56:35 +00001008 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001009 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001010 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001011 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001012 free(matches);
1013 matches = (char **) NULL;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001014 free(add_char_to_match);
1015 add_char_to_match = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +00001016 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001017 return;
1018 }
Matt Kraai1f0c4362001-12-20 23:13:26 +00001019 if (! *lastWasTab) {
"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);
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001049 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001050 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001051 int i, n = 0;
1052 qsort(matches, num_matches, sizeof(char*), match_compare);
1053 for (i = 0; i < num_matches - 1; ++i) {
1054 if (matches[i] && matches[i+1]) {
1055 if (strcmp(matches[i], matches[i+1]) == 0) {
1056 free(matches[i]);
1057 matches[i] = 0;
1058 } else {
1059 add_char_to_match[n] = add_char_to_match[i];
1060 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +00001061 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001062 }
Denis Vlasenko92758142006-10-03 19:56:34 +00001063 }
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001064 add_char_to_match[n] = add_char_to_match[num_matches-1];
1065 matches[n++] = matches[num_matches-1];
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001066 num_matches = n;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001067 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001068 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001069 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +00001070 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001071 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +00001072 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001073 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001074 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001075 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001076 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001077 if (matches[len_found][(tmp - tmp1)] != *tmp) {
1078 *tmp = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001079 break;
1080 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001081 if (*tmp1 == 0) { /* have unique */
1082 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001083 return;
1084 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001085 tmp = add_quote_for_spec_chars(tmp1, 0);
1086 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +00001087 } else { /* one match */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001088 tmp = add_quote_for_spec_chars(matches[0], add_char_to_match[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001089 /* for next completion current found */
1090 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001091 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001092 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001093 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001094 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001095
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001096 /* before word for match */
1097 command_ps[cursor - recalc_pos] = 0;
1098 /* save tail line */
1099 strcpy(matchBuf, command_ps + cursor);
1100 /* add match */
1101 strcat(command_ps, tmp);
1102 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001103 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001104 /* back to begin word for match */
1105 input_backward(recalc_pos);
1106 /* new pos */
1107 recalc_pos = cursor + len_found;
1108 /* new len */
1109 len = strlen(command_ps);
1110 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001111 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001112 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001113 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001114 } else {
1115 /* Ok -- the last char was a TAB. Since they
1116 * just hit TAB again, print a list of all the
1117 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001118 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001119 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001120
1121 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001122 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001123 showfiles();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001124 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001125 }
1126 }
1127}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001128#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001129
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001130#if MAX_HISTORY > 0
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001131static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001132{
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001133 if (command_ps[0] != 0 || history[cur_history] == 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001134 free(history[cur_history]);
Rob Landleyd921b2e2006-08-03 15:41:12 +00001135 history[cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001136 }
1137 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +00001138}
1139
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001140static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001141{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001142 int ch = cur_history;
1143
1144 if (ch < n_history) {
1145 get_previous_history(); /* save the current history line */
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001146 cur_history = ch + 1;
1147 return cur_history;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001148 } else {
1149 beep();
1150 return 0;
1151 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001152}
Robert Griebl350d26b2002-12-03 22:45:46 +00001153
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001154#if ENABLE_FEATURE_COMMAND_SAVEHISTORY
Rob Landleydfba7412006-03-06 20:47:33 +00001155void load_history ( const char *fromfile )
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001156{
Robert Griebl350d26b2002-12-03 22:45:46 +00001157 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001158 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001159
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001160 /* cleanup old */
1161
1162 for(hi = n_history; hi > 0; ) {
1163 hi--;
1164 free ( history [hi] );
Robert Griebl350d26b2002-12-03 22:45:46 +00001165 }
1166
1167 if (( fp = fopen ( fromfile, "r" ))) {
Eric Andersenc470f442003-07-28 09:56:35 +00001168
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001169 for ( hi = 0; hi < MAX_HISTORY; ) {
Denis Vlasenko2d5ca602006-10-12 22:43:20 +00001170 char * hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001171 int l;
1172
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001173 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001174 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001175 l = strlen(hl);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001176 if (l >= BUFSIZ)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001177 hl[BUFSIZ-1] = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001178 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001179 free(hl);
1180 continue;
1181 }
1182 history [hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001183 }
1184 fclose ( fp );
1185 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001186 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001187}
1188
Rob Landleydfba7412006-03-06 20:47:33 +00001189void save_history ( const char *tofile )
Robert Griebl350d26b2002-12-03 22:45:46 +00001190{
Robert Griebl350d26b2002-12-03 22:45:46 +00001191 FILE *fp = fopen ( tofile, "w" );
Eric Andersenc470f442003-07-28 09:56:35 +00001192
Robert Griebl350d26b2002-12-03 22:45:46 +00001193 if ( fp ) {
1194 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001195
Robert Griebl350d26b2002-12-03 22:45:46 +00001196 for ( i = 0; i < n_history; i++ ) {
Eric Andersen27bb7902003-12-23 20:24:51 +00001197 fprintf(fp, "%s\n", history [i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001198 }
1199 fclose ( fp );
1200 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001201}
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001202#endif
Robert Griebl350d26b2002-12-03 22:45:46 +00001203
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001204#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001205
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001206enum {
1207 ESC = 27,
1208 DEL = 127,
1209};
1210
1211
Erik Andersen6273f652000-03-17 01:12:41 +00001212/*
1213 * This function is used to grab a character buffer
1214 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001215 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001216 * a mini readline).
1217 *
1218 * The following standard commands are not implemented:
1219 * ESC-b -- Move back one word
1220 * ESC-f -- Move forward one word
1221 * ESC-d -- Delete back one word
1222 * ESC-h -- Delete forward one word
1223 * CTL-t -- Transpose two characters
1224 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001225 * Minimalist vi-style command line editing available if configured.
1226 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001227 *
Erik Andersen6273f652000-03-17 01:12:41 +00001228 */
Eric Andersenc470f442003-07-28 09:56:35 +00001229
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001230#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001231static int vi_mode;
1232
1233void setvimode ( int viflag )
1234{
1235 vi_mode = viflag;
1236}
1237
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001238static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001239vi_Word_motion(char *command, int eat)
1240{
1241 while (cursor < len && !isspace(command[cursor]))
1242 input_forward();
1243 if (eat) while (cursor < len && isspace(command[cursor]))
1244 input_forward();
1245}
1246
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001247static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001248vi_word_motion(char *command, int eat)
1249{
1250 if (isalnum(command[cursor]) || command[cursor] == '_') {
1251 while (cursor < len &&
1252 (isalnum(command[cursor+1]) ||
1253 command[cursor+1] == '_'))
1254 input_forward();
1255 } else if (ispunct(command[cursor])) {
1256 while (cursor < len &&
1257 (ispunct(command[cursor+1])))
1258 input_forward();
1259 }
1260
1261 if (cursor < len)
1262 input_forward();
1263
1264 if (eat && cursor < len && isspace(command[cursor]))
1265 while (cursor < len && isspace(command[cursor]))
1266 input_forward();
1267}
1268
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001269static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001270vi_End_motion(char *command)
1271{
1272 input_forward();
1273 while (cursor < len && isspace(command[cursor]))
1274 input_forward();
1275 while (cursor < len-1 && !isspace(command[cursor+1]))
1276 input_forward();
1277}
1278
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001279static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001280vi_end_motion(char *command)
1281{
1282 if (cursor >= len-1)
1283 return;
1284 input_forward();
1285 while (cursor < len-1 && isspace(command[cursor]))
1286 input_forward();
1287 if (cursor >= len-1)
1288 return;
1289 if (isalnum(command[cursor]) || command[cursor] == '_') {
1290 while (cursor < len-1 &&
1291 (isalnum(command[cursor+1]) ||
1292 command[cursor+1] == '_'))
1293 input_forward();
1294 } else if (ispunct(command[cursor])) {
1295 while (cursor < len-1 &&
1296 (ispunct(command[cursor+1])))
1297 input_forward();
1298 }
1299}
1300
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001301static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001302vi_Back_motion(char *command)
1303{
1304 while (cursor > 0 && isspace(command[cursor-1]))
1305 input_backward(1);
1306 while (cursor > 0 && !isspace(command[cursor-1]))
1307 input_backward(1);
1308}
1309
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001310static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001311vi_back_motion(char *command)
1312{
1313 if (cursor <= 0)
1314 return;
1315 input_backward(1);
1316 while (cursor > 0 && isspace(command[cursor]))
1317 input_backward(1);
1318 if (cursor <= 0)
1319 return;
1320 if (isalnum(command[cursor]) || command[cursor] == '_') {
1321 while (cursor > 0 &&
1322 (isalnum(command[cursor-1]) ||
1323 command[cursor-1] == '_'))
1324 input_backward(1);
1325 } else if (ispunct(command[cursor])) {
1326 while (cursor > 0 &&
1327 (ispunct(command[cursor-1])))
1328 input_backward(1);
1329 }
1330}
1331#endif
1332
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001333/*
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001334 * the emacs and vi modes share much of the code in the big
1335 * command loop. commands entered when in vi's command mode (aka
1336 * "escape mode") get an extra bit added to distinguish them --
1337 * this keeps them from being self-inserted. this clutters the
1338 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001339 */
1340
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001341#define vbit 0x100
1342
1343/* leave out the "vi-mode"-only case labels if vi editing isn't
1344 * configured. */
1345#define vi_case(caselabel) USE_FEATURE_COMMAND_EDITING(caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001346
1347/* convert uppercase ascii to equivalent control char, for readability */
1348#define CNTRL(uc_char) ((uc_char) - 0x40)
1349
Eric Andersen044228d2001-07-17 01:12:36 +00001350
1351int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001352{
1353
Erik Andersenc7c634b2000-03-19 05:28:55 +00001354 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001355 int lastWasTab = FALSE;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001356 unsigned char c;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001357 unsigned int ic;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001358#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001359 unsigned int prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001360 int vi_cmdmode = 0;
1361#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001362 /* prepare before init handlers */
Eric Andersenc470f442003-07-28 09:56:35 +00001363 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001364 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001365 command_ps = command;
1366
Eric Andersen34506362001-08-02 05:02:46 +00001367 getTermSettings(0, (void *) &initial_settings);
1368 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1369 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1370 /* Turn off echoing and CTRL-C, so we can trap it */
1371 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Eric Andersen34506362001-08-02 05:02:46 +00001372 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1373 new_settings.c_cc[VMIN] = 1;
1374 new_settings.c_cc[VTIME] = 0;
1375 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001376# ifndef _POSIX_VDISABLE
1377# define _POSIX_VDISABLE '\0'
1378# endif
Eric Andersenc470f442003-07-28 09:56:35 +00001379 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001380 command[0] = 0;
1381
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001382 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001383 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001384
Eric Andersen6faae7d2001-02-16 20:09:17 +00001385 /* Now initialize things */
1386 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001387 /* Print out the command prompt */
1388 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001389
Erik Andersenc7c634b2000-03-19 05:28:55 +00001390 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001391
Eric Andersenc470f442003-07-28 09:56:35 +00001392 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001393
Eric Andersen7467c8d2001-07-12 20:26:32 +00001394 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001395 /* if we can't read input then exit */
1396 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001397
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001398 ic = c;
1399
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001400#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001401 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001402 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001403 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001404#endif
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001405 switch (ic)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001406 {
Erik Andersenf0657d32000-04-12 17:49:52 +00001407 case '\n':
1408 case '\r':
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001409 vi_case( case '\n'|vbit: )
1410 vi_case( case '\r'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001411 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001412 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001413 break_out = 1;
1414 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001415 case CNTRL('A'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001416 vi_case( case '0'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001417 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001418 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001419 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001420 case CNTRL('B'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001421 vi_case( case 'h'|vbit: )
1422 vi_case( case '\b'|vbit: )
1423 vi_case( case DEL|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001424 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001425 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001426 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001427 case CNTRL('C'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001428 vi_case( case CNTRL('C')|vbit: )
Eric Andersen86349772000-12-18 20:25:50 +00001429 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001430 goto_new_line();
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001431#if !ENABLE_ASH
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001432 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001433 len = 0;
1434 lastWasTab = FALSE;
1435 put_prompt();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001436#else
1437 len = 0;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001438 break_out = -1; /* to control traps */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001439#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001440 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001441 case CNTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001442 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001443 * if the len=0 and no chars to delete */
1444 if (len == 0) {
Eric Andersencb01bb12004-08-19 18:22:13 +00001445 errno = 0;
Eric Andersened424db2001-04-23 15:28:28 +00001446prepare_to_die:
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001447#if !ENABLE_ASH
Mark Whitley4e338752001-01-26 20:42:23 +00001448 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001449 goto_new_line();
1450 /* cmdedit_reset_term() called in atexit */
1451 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001452#else
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001453 /* to control stopped jobs */
1454 len = break_out = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001455 break;
1456#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001457 } else {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001458 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001459 }
1460 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001461 case CNTRL('E'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001462 vi_case( case '$'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001463 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001464 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001465 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001466 case CNTRL('F'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001467 vi_case( case 'l'|vbit: )
1468 vi_case( case ' '|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001469 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001470 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001471 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001472 case '\b':
1473 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001474 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001475 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001476 break;
1477 case '\t':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001478#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001479 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001480#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001481 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001482 case CNTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001483 /* Control-k -- clear to end of line */
Eric Andersen65a07302002-04-13 13:26:49 +00001484 *(command + cursor) = 0;
1485 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001486 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001487 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001488 case CNTRL('L'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001489 vi_case( case CNTRL('L')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001490 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001491 printf("\033[H");
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001492 redraw(0, len-cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001493 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001494#if MAX_HISTORY > 0
Paul Fox3f11b1b2005-08-04 19:04:46 +00001495 case CNTRL('N'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001496 vi_case( case CNTRL('N')|vbit: )
1497 vi_case( case 'j'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001498 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001499 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001500 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001501 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001502 case CNTRL('P'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001503 vi_case( case CNTRL('P')|vbit: )
1504 vi_case( case 'k'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001505 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001506 if (cur_history > 0) {
1507 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001508 goto rewrite_line;
1509 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001510 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001511 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001512 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001513#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001514 case CNTRL('U'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001515 vi_case( case CNTRL('U')|vbit: )
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001516 /* Control-U -- Clear line before cursor */
1517 if (cursor) {
1518 strcpy(command, command + cursor);
1519 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001520 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001521 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001522 case CNTRL('W'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001523 vi_case( case CNTRL('W')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001524 /* Control-W -- Remove the last word */
1525 while (cursor > 0 && isspace(command[cursor-1]))
1526 input_backspace();
1527 while (cursor > 0 &&!isspace(command[cursor-1]))
1528 input_backspace();
1529 break;
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001530#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001531 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001532 vi_cmdmode = 0;
1533 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001534 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001535 input_backward(cursor);
1536 vi_cmdmode = 0;
1537 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001538 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001539 input_forward();
1540 vi_cmdmode = 0;
1541 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001542 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001543 input_end();
1544 vi_cmdmode = 0;
1545 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001546 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001547 input_delete(1);
1548 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001549 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001550 if (cursor > 0) {
1551 input_backward(1);
1552 input_delete(1);
1553 }
1554 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001555 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001556 vi_Word_motion(command, 1);
1557 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001558 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001559 vi_word_motion(command, 1);
1560 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001561 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001562 vi_End_motion(command);
1563 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001564 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001565 vi_end_motion(command);
1566 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001567 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001568 vi_Back_motion(command);
1569 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001570 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001571 vi_back_motion(command);
1572 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001573 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001574 vi_cmdmode = 0;
1575 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001576 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001577 goto clear_to_eol;
1578
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001579 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001580 vi_cmdmode = 0;
1581 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001582 case 'd'|vbit:
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001583 {
Denis Vlasenko92758142006-10-03 19:56:34 +00001584 int nc, sc;
1585 sc = cursor;
1586 prevc = ic;
1587 if (safe_read(0, &c, 1) < 1)
1588 goto prepare_to_die;
1589 if (c == (prevc & 0xff)) {
1590 /* "cc", "dd" */
1591 input_backward(cursor);
1592 goto clear_to_eol;
1593 break;
1594 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001595 switch (c) {
Denis Vlasenko92758142006-10-03 19:56:34 +00001596 case 'w':
1597 case 'W':
1598 case 'e':
1599 case 'E':
1600 switch (c) {
1601 case 'w': /* "dw", "cw" */
1602 vi_word_motion(command, vi_cmdmode);
1603 break;
1604 case 'W': /* 'dW', 'cW' */
1605 vi_Word_motion(command, vi_cmdmode);
1606 break;
1607 case 'e': /* 'de', 'ce' */
1608 vi_end_motion(command);
1609 input_forward();
1610 break;
1611 case 'E': /* 'dE', 'cE' */
1612 vi_End_motion(command);
1613 input_forward();
1614 break;
1615 }
1616 nc = cursor;
1617 input_backward(cursor - sc);
1618 while (nc-- > cursor)
1619 input_delete(1);
1620 break;
1621 case 'b': /* "db", "cb" */
1622 case 'B': /* implemented as B */
1623 if (c == 'b')
1624 vi_back_motion(command);
1625 else
1626 vi_Back_motion(command);
1627 while (sc-- > cursor)
1628 input_delete(1);
1629 break;
1630 case ' ': /* "d ", "c " */
1631 input_delete(1);
1632 break;
1633 case '$': /* "d$", "c$" */
1634 clear_to_eol:
1635 while (cursor < len)
1636 input_delete(1);
1637 break;
1638 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001639 }
1640 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001641 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001642 input_forward();
1643 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001644 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001645 put();
1646 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001647 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001648 if (safe_read(0, &c, 1) < 1)
1649 goto prepare_to_die;
1650 if (c == 0)
1651 beep();
1652 else {
1653 *(command + cursor) = c;
1654 putchar(c);
1655 putchar('\b');
1656 }
1657 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001658#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001659
1660 case ESC:
1661
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001662#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001663 if (vi_mode) {
1664 /* ESC: insert mode --> command mode */
1665 vi_cmdmode = 1;
1666 input_backward(1);
1667 break;
1668 }
1669#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001670 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001671 if (safe_read(0, &c, 1) < 1)
1672 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001673 /* different vt100 emulations */
1674 if (c == '[' || c == 'O') {
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001675 vi_case( case '['|vbit: )
1676 vi_case( case 'O'|vbit: )
Eric Andersen7467c8d2001-07-12 20:26:32 +00001677 if (safe_read(0, &c, 1) < 1)
1678 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001679 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001680 if (c >= '1' && c <= '9') {
1681 unsigned char dummy;
1682
1683 if (safe_read(0, &dummy, 1) < 1)
1684 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001685 if (dummy != '~')
Glenn L McGrath475820c2004-01-22 12:42:23 +00001686 c = 0;
1687 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001688 switch (c) {
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001689#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001690 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001691
1692 input_tab(&lastWasTab);
1693 break;
1694#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001695#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001696 case 'A':
1697 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001698 if (cur_history > 0) {
1699 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001700 goto rewrite_line;
1701 } else {
1702 beep();
1703 }
1704 break;
1705 case 'B':
1706 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001707 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001708 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001709 /* Rewrite the line with the selected history item */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001710rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001711 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001712 len = strlen(strcpy(command, history[cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001713 /* redraw and go to eol (bol, in vi */
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001714#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001715 redraw(cmdedit_y, vi_mode ? 9999:0);
1716#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001717 redraw(cmdedit_y, 0);
Paul Fox3f11b1b2005-08-04 19:04:46 +00001718#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001719 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001720#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001721 case 'C':
1722 /* Right Arrow -- Move forward one character */
1723 input_forward();
1724 break;
1725 case 'D':
1726 /* Left Arrow -- Move back one character */
1727 input_backward(1);
1728 break;
1729 case '3':
1730 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001731 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001732 break;
1733 case '1':
1734 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001735 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001736 input_backward(cursor);
1737 break;
1738 case '4':
1739 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001740 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001741 input_end();
1742 break;
1743 default:
Glenn L McGrath475820c2004-01-22 12:42:23 +00001744 c = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001745 beep();
1746 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001747 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001748
Eric Andersenc470f442003-07-28 09:56:35 +00001749 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001750#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001751 /* Control-V -- Add non-printable symbol */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001752 if (c == CNTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001753 if (safe_read(0, &c, 1) < 1)
1754 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001755 if (c == 0) {
1756 beep();
1757 break;
1758 }
1759 } else
1760#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001761 {
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001762#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001763 if (vi_cmdmode) /* don't self-insert */
1764 break;
1765#endif
1766 if (!Isprint(c)) /* Skip non-printable characters */
1767 break;
1768 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001769
Eric Andersenc470f442003-07-28 09:56:35 +00001770 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001771 break;
1772
1773 len++;
1774
Eric Andersenc470f442003-07-28 09:56:35 +00001775 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001776 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001777 *(command + cursor + 1) = 0;
1778 cmdedit_set_out_char(0);
Eric Andersenc470f442003-07-28 09:56:35 +00001779 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001780 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001781
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001782 memmove(command + sc + 1, command + sc, len - sc);
1783 *(command + sc) = c;
1784 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001785 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001786 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001787 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001788 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001789 }
1790
Erik Andersenc7c634b2000-03-19 05:28:55 +00001791 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001792 }
Eric Andersenc470f442003-07-28 09:56:35 +00001793 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001794 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001795
1796 if (c != '\t')
1797 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001798 }
1799
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001800 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001801 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001802
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001803#if MAX_HISTORY > 0
Erik Andersenc7c634b2000-03-19 05:28:55 +00001804 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001805 /* cleanup may be saved current command line */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001806 if (len> 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001807 int i = n_history;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001808
1809 free(history[MAX_HISTORY]);
1810 history[MAX_HISTORY] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001811 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001812 if (i >= MAX_HISTORY) {
1813 free(history[0]);
1814 for(i = 0; i < (MAX_HISTORY-1); i++)
1815 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001816 }
Rob Landleyd921b2e2006-08-03 15:41:12 +00001817 history[i++] = xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001818 cur_history = i;
1819 n_history = i;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001820#if ENABLE_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001821 num_ok_lines++;
1822#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001823 }
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001824#else /* MAX_HISTORY == 0 */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001825#if ENABLE_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001826 if (len > 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001827 num_ok_lines++;
1828 }
1829#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001830#endif /* MAX_HISTORY > 0 */
Eric Andersen27bb7902003-12-23 20:24:51 +00001831 if (break_out > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001832 command[len++] = '\n'; /* set '\n' */
1833 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001834 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001835#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001836 input_tab(0); /* strong free */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001837#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001838#if ENABLE_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001839 free(cmdedit_prompt);
1840#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001841 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001842 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001843}
1844
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001845#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001846
1847
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001848#ifdef TEST
1849
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001850const char *applet_name = "debug stuff usage";
Eric Andersene5dfced2001-04-09 22:48:12 +00001851
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001852#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001853#include <locale.h>
1854#endif
1855
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001856int main(int argc, char **argv)
1857{
1858 char buff[BUFSIZ];
1859 char *prompt =
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001860#if ENABLE_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001861 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001862\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001863\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001864#else
1865 "% ";
1866#endif
1867
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001868#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001869 setlocale(LC_ALL, "");
1870#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001871 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001872 int l;
Eric Andersen27bb7902003-12-23 20:24:51 +00001873 l = cmdedit_read_input(prompt, buff);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001874 if (l > 0 && buff[l-1] == '\n') {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001875 buff[l-1] = 0;
Eric Andersen27bb7902003-12-23 20:24:51 +00001876 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1877 } else {
1878 break;
1879 }
Eric Andersen7467c8d2001-07-12 20:26:32 +00001880 }
Eric Andersen27bb7902003-12-23 20:24:51 +00001881 printf("*** cmdedit_read_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001882 return 0;
1883}
1884
Eric Andersenc470f442003-07-28 09:56:35 +00001885#endif /* TEST */