blob: bc9cc5ef405a9b38e2c7195f32a2b8d4c5e64c07 [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 Vlasenko7e46cf72006-12-23 01:21:55 +000061#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \
62(ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION || ENABLE_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000063
Eric Andersen5f2c79d2001-02-16 18:36:04 +000064/* Maximum length of the linked list for the command line history */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000065#if !ENABLE_FEATURE_COMMAND_HISTORY
Robert Griebl350d26b2002-12-03 22:45:46 +000066#define MAX_HISTORY 15
67#else
Denis Vlasenko9d4533e2006-11-02 22:09:37 +000068#define MAX_HISTORY (CONFIG_FEATURE_COMMAND_HISTORY + 0)
Robert Griebl350d26b2002-12-03 22:45:46 +000069#endif
70
Denis Vlasenko9d4533e2006-11-02 22:09:37 +000071#if MAX_HISTORY > 0
Glenn L McGrath062c74f2002-11-27 09:29:49 +000072static char *history[MAX_HISTORY+1]; /* history + current */
73/* saved history lines */
74static int n_history;
75/* current pointer to history line */
76static int cur_history;
77#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +000078
Denis Vlasenko0a8a7742006-12-21 22:27:10 +000079#define setTermSettings(fd,argp) tcsetattr(fd, TCSANOW, argp)
Glenn L McGrath78b0e372001-06-26 02:06:08 +000080#define getTermSettings(fd,argp) tcgetattr(fd, argp);
Erik Andersen1d1d9502000-04-21 01:26:49 +000081
82/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +000083static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000084
85
Mark Whitley4e338752001-01-26 20:42:23 +000086static
Eric Andersenc470f442003-07-28 09:56:35 +000087volatile int cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +000088static
Eric Andersenc470f442003-07-28 09:56:35 +000089volatile int handlers_sets = 0; /* Set next bites: */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000090
Mark Whitley4e338752001-01-26 20:42:23 +000091enum {
Eric Andersenc470f442003-07-28 09:56:35 +000092 SET_ATEXIT = 1, /* when atexit() has been called
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000093 and get euid,uid,gid to fast compare */
Eric Andersen7467c8d2001-07-12 20:26:32 +000094 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
95 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +000096};
Erik Andersen13456d12000-03-16 08:09:57 +000097
Mark Whitley4e338752001-01-26 20:42:23 +000098
Eric Andersenc470f442003-07-28 09:56:35 +000099static int cmdedit_x; /* real x terminal position */
100static int cmdedit_y; /* pseudoreal y terminal position */
101static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000102
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000103static int cursor; /* required globals for signal handler */
104static int len; /* --- "" - - "" -- -"- --""-- --""--- */
105static char *command_ps; /* --- "" - - "" -- -"- --""-- --""--- */
106static SKIP_FEATURE_SH_FANCY_PROMPT(const) char *cmdedit_prompt; /* -- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000107
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000108#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000109static char *user_buf = "";
110static char *home_pwd_buf = "";
111static int my_euid;
112#endif
113
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000114#if ENABLE_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000115static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000116static int num_ok_lines = 1;
117#endif
118
119
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000120#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000121
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000122#if !ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000123static int my_euid;
124#endif
125
126static int my_uid;
127static int my_gid;
128
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000129#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Eric Andersenc470f442003-07-28 09:56:35 +0000130
Mark Whitley4e338752001-01-26 20:42:23 +0000131static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000132
Mark Whitley4e338752001-01-26 20:42:23 +0000133static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000134{
Eric Andersenc470f442003-07-28 09:56:35 +0000135 static sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000136
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000137 /* emulate || signal call */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000138 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Eric Andersen8efe9672003-09-15 08:33:45 +0000139 int width = 0;
140 get_terminal_width_height(0, &width, NULL);
141 cmdedit_setwidth(width, nsig == SIGWINCH);
Mark Whitley4e338752001-01-26 20:42:23 +0000142 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000143 /* Unix not all standard in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000144
Eric Andersenc470f442003-07-28 09:56:35 +0000145 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000146 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersenc470f442003-07-28 09:56:35 +0000147 else if (nsig == SIGWINCH) /* signaled called handler */
148 signal(SIGWINCH, win_changed); /* set for next call */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000149 else /* nsig == 0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000150 /* set previous handler */
Eric Andersenc470f442003-07-28 09:56:35 +0000151 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000152}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000153
154static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000155{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000156 if (handlers_sets & SET_RESET_TERM) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000157/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen70060d22004-03-27 10:02:48 +0000158 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000159 handlers_sets &= ~SET_RESET_TERM;
160 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000161 if (handlers_sets & SET_WCHG_HANDLERS) {
Mark Whitley4e338752001-01-26 20:42:23 +0000162 /* reset SIGWINCH handler to previous (default) */
163 win_changed(0);
164 handlers_sets &= ~SET_WCHG_HANDLERS;
165 }
166 fflush(stdout);
Erik Andersen13456d12000-03-16 08:09:57 +0000167}
168
Mark Whitley4e338752001-01-26 20:42:23 +0000169
Mark Whitley4e338752001-01-26 20:42:23 +0000170/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000171static void cmdedit_set_out_char(int next_char)
172{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000173 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000174
175 if (c == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000176 c = ' '; /* destroy end char? */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000177#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000178 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000179 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000180 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000181 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000182 c += '@';
183 if (c == 127)
184 c = '?';
185 printf("\033[7m%c\033[0m", c);
186 } else
187#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000188 {
189 if (initial_settings.c_lflag & ECHO)
190 putchar(c);
191 }
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{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000224 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000225 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;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000258 /* require forward after uping */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000259 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;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +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);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000287 char buf2[PATH_MAX + 1];
288 char buf[2];
289 char c;
Eric Andersene5dfced2001-04-09 22:48:12 +0000290 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000291
Eric Andersen5f265b72001-05-11 16:58:46 +0000292 if (!pwd_buf) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +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) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000297 pbuf = buf;
Eric Andersene5dfced2001-04-09 22:48:12 +0000298 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 Vlasenko0a8a7742006-12-21 22:27:10 +0000305 if (prmt_ptr == cp) {
306 if (*cp == 0)
307 break;
308 c = *prmt_ptr++;
309 switch (c) {
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000310#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000311 case 'u':
312 pbuf = user_buf;
313 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000314#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000315 case 'h':
316 pbuf = hostname_buf;
317 if (pbuf == 0) {
318 pbuf = xzalloc(256);
319 if (gethostname(pbuf, 255) < 0) {
320 strcpy(pbuf, "?");
321 } else {
322 char *s = strchr(pbuf, '.');
323 if (s)
324 *s = 0;
325 }
326 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000327 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000328 break;
329 case '$':
330 c = (my_euid == 0 ? '#' : '$');
331 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000332#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000333 case 'w':
334 pbuf = pwd_buf;
335 l = strlen(home_pwd_buf);
336 if (home_pwd_buf[0] != 0
337 && strncmp(home_pwd_buf, pbuf, l) == 0
338 && (pbuf[l]=='/' || pbuf[l]=='\0')
339 && strlen(pwd_buf+l)<PATH_MAX
340 ) {
341 pbuf = buf2;
342 *pbuf = '~';
343 strcpy(pbuf+1, pwd_buf+l);
344 }
345 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000346#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +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 */
357 c = '\033';
358 break;
359 case 'x': case 'X':
360 for (l = 0; l < 3;) {
361 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) {
366 l--;
367 break;
368 }
369 prmt_ptr++;
370 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000371 buf2[l] = 0;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000372 c = (char)strtol(buf2, 0, 16);
373 if (c == 0)
374 c = '?';
375 pbuf = buf;
376 break;
377 case '[': case ']':
378 if (c == flg_not_length) {
379 flg_not_length = flg_not_length == '[' ? ']' : '[';
380 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000381 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000382 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +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 */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000463 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000464}
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;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000507 if (!(handlers_sets & SET_WCHG_HANDLERS)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000508 /* 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
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000513 if (!(handlers_sets & SET_ATEXIT)) {
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;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000542
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000543static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000544{
545 int nm = num_matches;
546 int nm1 = nm + 1;
547
548 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000549 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000550 num_matches++;
551}
552
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000553/*
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000554static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000555{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000556 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
557 || (my_uid == st->st_uid && (st->st_mode & S_IXUSR))
558 || (my_gid == st->st_gid && (st->st_mode & S_IXGRP))
559 || (st->st_mode & S_IXOTH)
560 ) {
561 return TRUE;
562 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000563 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000564}
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000565*/
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000566
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000567#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000568
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000569static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000570{
571 struct passwd *entry;
572 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000573
Eric Andersenc470f442003-07-28 09:56:35 +0000574 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000575 userlen = strlen(ud);
576
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000577 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000578 char *sav_ud = ud - 1;
579 char *home = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000580 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000581
Eric Andersenc470f442003-07-28 09:56:35 +0000582 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000583 home = home_pwd_buf;
584 } else {
585 /* "~user/..." */
586 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000587 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000588 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000589 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000590 ud = temp;
591 if (entry)
592 home = entry->pw_dir;
593 }
594 if (home) {
595 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000596 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000597
598 /* /home/user/... */
599 sprintf(temp2, "%s%s", home, ud);
600 strcpy(sav_ud, temp2);
601 }
602 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000603 } else {
604 /* "~[^/]*" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000605 setpwent();
606
607 while ((entry = getpwent()) != NULL) {
608 /* Null usernames should result in all users as possible completions. */
609 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000610 add_match(xasprintf("~%s/", entry->pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000611 }
612 }
613
614 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000615 }
616}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000617#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000618
619enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000620 FIND_EXE_ONLY = 0,
621 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000622 FIND_FILE_ONLY = 2,
623};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000624
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000625#if ENABLE_ASH
Glenn L McGrath67285962004-01-14 09:34:51 +0000626const char *cmdedit_path_lookup;
627#else
628#define cmdedit_path_lookup getenv("PATH")
629#endif
630
Mark Whitley4e338752001-01-26 20:42:23 +0000631static int path_parse(char ***p, int flags)
632{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000633 int npth;
Glenn L McGrath67285962004-01-14 09:34:51 +0000634 const char *tmp;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000635 const char *pth = cmdedit_path_lookup;
Mark Whitley4e338752001-01-26 20:42:23 +0000636
Mark Whitley4e338752001-01-26 20:42:23 +0000637 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000638 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000639 return 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000640 /* PATH=<empty> or PATH=:<empty> */
641 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
642 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000643
644 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000645 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000646
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000647 while (1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000648 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000649 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000650 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000651 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000652 if (*++tmp == 0)
653 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000654 }
655
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000656 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000657
658 tmp = pth;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000659 (*p)[0] = xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000660 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000661
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000662 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000663 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000664 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000665 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000666 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
667 if (*++tmp == 0)
668 break; /* :<empty> */
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
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000675static char *add_quote_for_spec_chars(char *found)
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 }
685 s[l] = 0;
686 return s;
687}
688
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000689static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000690{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000691 DIR *dir;
692 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000693 char dirbuf[BUFSIZ];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000694 struct stat st;
695 char *path1[1];
696 char **paths = path1;
697 int npaths;
698 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000699 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000700 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000701
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000702 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000703
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000704 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000705 /* no dir, if flags==EXE_ONLY - get paths, else "." */
706 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000707 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000708 } else {
709 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000710 /* save for change */
711 strcpy(dirbuf, command);
712 /* set dir only */
713 dirbuf[(pfind - command) + 1] = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000714#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000715 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000716 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000717#endif
718 /* "strip" dirname in command */
719 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000720
Mark Whitley4e338752001-01-26 20:42:23 +0000721 paths[0] = dirbuf;
Eric Andersenc470f442003-07-28 09:56:35 +0000722 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000723 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000724
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000725 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000726
Mark Whitley4e338752001-01-26 20:42:23 +0000727 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000728 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000729 continue;
730
731 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000732 int len1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000733 char *str_found = next->d_name;
734
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000735 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000736 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000737 continue;
738 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000739 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000740 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000741 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000742 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000743 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000744 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000745 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000746 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000747 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000748 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000749 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000750 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000751
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000752 len1 = strlen(found);
753 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000754 found[len1] = '\0';
755 found[len1+1] = '\0';
756
Mark Whitley4e338752001-01-26 20:42:23 +0000757 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000758 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000759 if (found[len1-1] != '/') {
760 found[len1] = '/';
761 }
Mark Whitley4e338752001-01-26 20:42:23 +0000762 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000763 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000764 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000765 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000766 }
Mark Whitley4e338752001-01-26 20:42:23 +0000767 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000768 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000769 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000770 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000771 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000772 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000773 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000774 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000775 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000776 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000777 free(paths);
778 }
Erik Andersen6273f652000-03-17 01:12:41 +0000779}
Erik Andersenf0657d32000-04-12 17:49:52 +0000780
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000781
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000782#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000783
784#define collapse_pos(is, in) { \
Paul Fox574fee42005-07-19 20:41:06 +0000785 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
786 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000787
788static int find_match(char *matchBuf, int *len_with_quotes)
789{
790 int i, j;
791 int command_mode;
792 int c, c2;
793 int int_buf[BUFSIZ + 1];
794 int pos_buf[BUFSIZ + 1];
795
796 /* set to integer dimension characters and own positions */
797 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000798 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000799 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000800 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000801 break;
802 } else
803 pos_buf[i] = i;
804 }
805
806 /* mask \+symbol and convert '\t' to ' ' */
807 for (i = j = 0; matchBuf[i]; i++, j++)
808 if (matchBuf[i] == '\\') {
809 collapse_pos(j, j + 1);
810 int_buf[j] |= QUOT;
811 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000812#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000813 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000814 int_buf[j] = ' ' | QUOT;
815#endif
816 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000817#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000818 else if (matchBuf[i] == '\t')
819 int_buf[j] = ' ';
820#endif
821
822 /* mask "symbols" or 'symbols' */
823 c2 = 0;
824 for (i = 0; int_buf[i]; i++) {
825 c = int_buf[i];
826 if (c == '\'' || c == '"') {
827 if (c2 == 0)
828 c2 = c;
829 else {
830 if (c == c2)
831 c2 = 0;
832 else
833 int_buf[i] |= QUOT;
834 }
835 } else if (c2 != 0 && c != '$')
836 int_buf[i] |= QUOT;
837 }
838
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000839 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000840 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
841 for (i = 0; int_buf[i]; i++) {
842 c = int_buf[i];
843 c2 = int_buf[i + 1];
844 j = i ? int_buf[i - 1] : -1;
845 command_mode = 0;
846 if (c == ';' || c == '&' || c == '|') {
847 command_mode = 1 + (c == c2);
848 if (c == '&') {
849 if (j == '>' || j == '<')
850 command_mode = 0;
851 } else if (c == '|' && j == '>')
852 command_mode = 0;
853 }
854 if (command_mode) {
855 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000856 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000857 }
858 }
859 /* collapse `command...` */
860 for (i = 0; int_buf[i]; i++)
861 if (int_buf[i] == '`') {
862 for (j = i + 1; int_buf[j]; j++)
863 if (int_buf[j] == '`') {
864 collapse_pos(i, j + 1);
865 j = 0;
866 break;
867 }
868 if (j) {
869 /* not found close ` - command mode, collapse all previous */
870 collapse_pos(0, i + 1);
871 break;
872 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000873 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000874 }
875
876 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000877 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000878 c2 = 0;
879 for (i = 0; int_buf[i]; i++)
880 if (int_buf[i] == '(' || int_buf[i] == '{') {
881 if (int_buf[i] == '(')
882 c++;
883 else
884 c2++;
885 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000886 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000887 }
888 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
889 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
890 if (int_buf[i] == ')')
891 c--;
892 else
893 c2--;
894 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000895 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000896 }
897
898 /* skip first not quote space */
899 for (i = 0; int_buf[i]; i++)
900 if (int_buf[i] != ' ')
901 break;
902 if (i)
903 collapse_pos(0, i);
904
905 /* set find mode for completion */
906 command_mode = FIND_EXE_ONLY;
907 for (i = 0; int_buf[i]; i++)
908 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
909 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000910 && matchBuf[pos_buf[0]]=='c'
911 && matchBuf[pos_buf[1]]=='d'
912 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000913 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000914 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000915 command_mode = FIND_FILE_ONLY;
916 break;
917 }
918 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000919 for (i = 0; int_buf[i]; i++)
920 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000921 /* find last word */
922 for (--i; i >= 0; i--) {
923 c = int_buf[i];
924 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
925 collapse_pos(0, i + 1);
926 break;
927 }
928 }
929 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000930 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
931 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000932 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000933 while ((int_buf[i] & ~QUOT) == '/'
934 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
935 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000936 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 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000963
964 /* find the longest file name- use that as the column width */
965 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000966 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000967 if (column_width < l)
968 column_width = l;
969 }
970 column_width += 2; /* min space for columns */
971 ncols = cmdedit_termw / column_width;
972
973 if (ncols > 1) {
974 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000975 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000976 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000977 } else {
978 ncols = 1;
979 }
980 for (row = 0; row < nrows; row++) {
981 int n = row;
982 int nc;
983
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000984 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000985 printf("%s%-*s", matches[n],
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000986 column_width - strlen(matches[n]), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000987 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000988 printf("%s\n", matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000989 }
990}
991
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000992static int match_compare(const void *a, const void *b)
993{
994 return strcmp(*(char**)a, *(char**)b);
995}
996
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000997static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000998{
999 /* Do TAB completion */
Eric Andersenc470f442003-07-28 09:56:35 +00001000 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001001 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001002 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001003 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001004 free(matches);
1005 matches = (char **) NULL;
1006 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001007 return;
1008 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001009 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001010 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001011 int len_found;
1012 char matchBuf[BUFSIZ];
1013 int find_type;
1014 int recalc_pos;
1015
Eric Andersenc470f442003-07-28 09:56:35 +00001016 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001017
1018 /* Make a local copy of the string -- up
1019 * to the position of the cursor */
1020 tmp = strncpy(matchBuf, command_ps, cursor);
1021 tmp[cursor] = 0;
1022
1023 find_type = find_match(matchBuf, &recalc_pos);
1024
1025 /* Free up any memory already allocated */
1026 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001027
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001028#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001029 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001030 * then try completing this word as a username. */
1031
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001032 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001033 username_tab_completion(matchBuf, NULL);
1034 if (!matches)
Mark Whitley4e338752001-01-26 20:42:23 +00001035#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001036 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001037 * in the current working directory that matches. */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001038 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001039 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001040 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001041 int i, n = 0;
1042 qsort(matches, num_matches, sizeof(char*), match_compare);
1043 for (i = 0; i < num_matches - 1; ++i) {
1044 if (matches[i] && matches[i+1]) {
1045 if (strcmp(matches[i], matches[i+1]) == 0) {
1046 free(matches[i]);
1047 matches[i] = 0;
1048 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001049 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +00001050 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001051 }
Denis Vlasenko92758142006-10-03 19:56:34 +00001052 }
Denis Vlasenkof58906b2006-12-19 19:30:37 +00001053 matches[n++] = matches[num_matches-1];
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001054 num_matches = n;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001055 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001056 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001057 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +00001058 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001059 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +00001060 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001061 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001062 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001063 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001064 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001065 if (matches[len_found][(tmp - tmp1)] != *tmp) {
1066 *tmp = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001067 break;
1068 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001069 if (*tmp1 == 0) { /* have unique */
1070 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001071 return;
1072 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001073 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001074 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +00001075 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001076 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001077 /* for next completion current found */
1078 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +00001079
1080 len_found = strlen(tmp);
1081 if (tmp[len_found-1] != '/') {
1082 tmp[len_found] = ' ';
1083 tmp[len_found+1] = '\0';
1084 }
Mark Whitley4e338752001-01-26 20:42:23 +00001085 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001086 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001087 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001088 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001089
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001090 /* before word for match */
1091 command_ps[cursor - recalc_pos] = 0;
1092 /* save tail line */
1093 strcpy(matchBuf, command_ps + cursor);
1094 /* add match */
1095 strcat(command_ps, tmp);
1096 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001097 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001098 /* back to begin word for match */
1099 input_backward(recalc_pos);
1100 /* new pos */
1101 recalc_pos = cursor + len_found;
1102 /* new len */
1103 len = strlen(command_ps);
1104 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001105 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001106 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001107 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001108 } else {
1109 /* Ok -- the last char was a TAB. Since they
1110 * just hit TAB again, print a list of all the
1111 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001112 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001113 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001114
1115 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001116 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +00001117 showfiles();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001118 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001119 }
1120 }
1121}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001122#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001123
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001124#if MAX_HISTORY > 0
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001125static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001126{
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001127 if (command_ps[0] != 0 || history[cur_history] == 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001128 free(history[cur_history]);
Rob Landleyd921b2e2006-08-03 15:41:12 +00001129 history[cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001130 }
1131 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +00001132}
1133
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001134static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001135{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001136 int ch = cur_history;
1137
1138 if (ch < n_history) {
1139 get_previous_history(); /* save the current history line */
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001140 cur_history = ch + 1;
1141 return cur_history;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001142 } else {
1143 beep();
1144 return 0;
1145 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001146}
Robert Griebl350d26b2002-12-03 22:45:46 +00001147
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001148#if ENABLE_FEATURE_COMMAND_SAVEHISTORY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001149void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001150{
Robert Griebl350d26b2002-12-03 22:45:46 +00001151 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001152 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001153
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001154 /* cleanup old */
1155
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001156 for (hi = n_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001157 hi--;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001158 free(history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001159 }
1160
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001161 fp = fopen(fromfile, "r");
1162 if (fp) {
1163 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko2d5ca602006-10-12 22:43:20 +00001164 char * hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001165 int l;
1166
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001167 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001168 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001169 l = strlen(hl);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001170 if (l >= BUFSIZ)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001171 hl[BUFSIZ-1] = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001172 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001173 free(hl);
1174 continue;
1175 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001176 history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001177 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001178 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001179 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001180 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001181}
1182
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001183void save_history (const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +00001184{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001185 FILE *fp = fopen(tofile, "w");
Eric Andersenc470f442003-07-28 09:56:35 +00001186
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001187 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +00001188 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001189
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001190 for (i = 0; i < n_history; i++) {
1191 fprintf(fp, "%s\n", history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +00001192 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001193 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +00001194 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001195}
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001196#endif
Robert Griebl350d26b2002-12-03 22:45:46 +00001197
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001198#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001199
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001200enum {
1201 ESC = 27,
1202 DEL = 127,
1203};
1204
1205
Erik Andersen6273f652000-03-17 01:12:41 +00001206/*
1207 * This function is used to grab a character buffer
1208 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001209 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001210 * a mini readline).
1211 *
1212 * The following standard commands are not implemented:
1213 * ESC-b -- Move back one word
1214 * ESC-f -- Move forward one word
1215 * ESC-d -- Delete back one word
1216 * ESC-h -- Delete forward one word
1217 * CTL-t -- Transpose two characters
1218 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001219 * Minimalist vi-style command line editing available if configured.
1220 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001221 *
Erik Andersen6273f652000-03-17 01:12:41 +00001222 */
Eric Andersenc470f442003-07-28 09:56:35 +00001223
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001224#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001225static int vi_mode;
1226
1227void setvimode ( int viflag )
1228{
1229 vi_mode = viflag;
1230}
1231
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001232static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001233vi_Word_motion(char *command, int eat)
1234{
1235 while (cursor < len && !isspace(command[cursor]))
1236 input_forward();
1237 if (eat) while (cursor < len && isspace(command[cursor]))
1238 input_forward();
1239}
1240
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001241static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001242vi_word_motion(char *command, int eat)
1243{
1244 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001245 while (cursor < len
1246 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001247 input_forward();
1248 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001249 while (cursor < len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001250 input_forward();
1251 }
1252
1253 if (cursor < len)
1254 input_forward();
1255
1256 if (eat && cursor < len && isspace(command[cursor]))
1257 while (cursor < len && isspace(command[cursor]))
1258 input_forward();
1259}
1260
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001261static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001262vi_End_motion(char *command)
1263{
1264 input_forward();
1265 while (cursor < len && isspace(command[cursor]))
1266 input_forward();
1267 while (cursor < len-1 && !isspace(command[cursor+1]))
1268 input_forward();
1269}
1270
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001271static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001272vi_end_motion(char *command)
1273{
1274 if (cursor >= len-1)
1275 return;
1276 input_forward();
1277 while (cursor < len-1 && isspace(command[cursor]))
1278 input_forward();
1279 if (cursor >= len-1)
1280 return;
1281 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001282 while (cursor < len-1
1283 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1284 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001285 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001286 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001287 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001288 while (cursor < len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001289 input_forward();
1290 }
1291}
1292
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001293static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001294vi_Back_motion(char *command)
1295{
1296 while (cursor > 0 && isspace(command[cursor-1]))
1297 input_backward(1);
1298 while (cursor > 0 && !isspace(command[cursor-1]))
1299 input_backward(1);
1300}
1301
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001302static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001303vi_back_motion(char *command)
1304{
1305 if (cursor <= 0)
1306 return;
1307 input_backward(1);
1308 while (cursor > 0 && isspace(command[cursor]))
1309 input_backward(1);
1310 if (cursor <= 0)
1311 return;
1312 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001313 while (cursor > 0
1314 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1315 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001316 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001317 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001318 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001319 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001320 input_backward(1);
1321 }
1322}
1323#endif
1324
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001325/*
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001326 * the emacs and vi modes share much of the code in the big
1327 * command loop. commands entered when in vi's command mode (aka
1328 * "escape mode") get an extra bit added to distinguish them --
1329 * this keeps them from being self-inserted. this clutters the
1330 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001331 */
1332
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001333#define vbit 0x100
1334
1335/* leave out the "vi-mode"-only case labels if vi editing isn't
1336 * configured. */
1337#define vi_case(caselabel) USE_FEATURE_COMMAND_EDITING(caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001338
1339/* convert uppercase ascii to equivalent control char, for readability */
1340#define CNTRL(uc_char) ((uc_char) - 0x40)
1341
Eric Andersen044228d2001-07-17 01:12:36 +00001342
1343int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001344{
Erik Andersenc7c634b2000-03-19 05:28:55 +00001345 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001346 int lastWasTab = FALSE;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001347 unsigned char c;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001348 unsigned int ic;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001349#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001350 unsigned int prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001351 int vi_cmdmode = 0;
1352#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001353 /* prepare before init handlers */
Eric Andersenc470f442003-07-28 09:56:35 +00001354 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001355 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001356 command_ps = command;
1357
Eric Andersen34506362001-08-02 05:02:46 +00001358 getTermSettings(0, (void *) &initial_settings);
1359 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1360 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1361 /* Turn off echoing and CTRL-C, so we can trap it */
1362 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Eric Andersen34506362001-08-02 05:02:46 +00001363 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1364 new_settings.c_cc[VMIN] = 1;
1365 new_settings.c_cc[VTIME] = 0;
1366 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001367# ifndef _POSIX_VDISABLE
1368# define _POSIX_VDISABLE '\0'
1369# endif
Eric Andersenc470f442003-07-28 09:56:35 +00001370 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001371 command[0] = 0;
1372
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001373 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001374 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001375
Eric Andersen6faae7d2001-02-16 20:09:17 +00001376 /* Now initialize things */
1377 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001378 /* Print out the command prompt */
1379 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001380
Erik Andersenc7c634b2000-03-19 05:28:55 +00001381 while (1) {
Eric Andersenc470f442003-07-28 09:56:35 +00001382 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001383
Eric Andersen7467c8d2001-07-12 20:26:32 +00001384 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001385 /* if we can't read input then exit */
1386 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001387
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001388 ic = c;
1389
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001390#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001391 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001392 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001393 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001394#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001395 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001396 case '\n':
1397 case '\r':
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001398 vi_case( case '\n'|vbit: )
1399 vi_case( case '\r'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001400 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001401 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001402 break_out = 1;
1403 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001404 case CNTRL('A'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001405 vi_case( case '0'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001406 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001407 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001408 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001409 case CNTRL('B'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001410 vi_case( case 'h'|vbit: )
1411 vi_case( case '\b'|vbit: )
1412 vi_case( case DEL|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001413 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001414 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001415 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001416 case CNTRL('C'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001417 vi_case( case CNTRL('C')|vbit: )
Eric Andersen86349772000-12-18 20:25:50 +00001418 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001419 goto_new_line();
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001420#if !ENABLE_ASH
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001421 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001422 len = 0;
1423 lastWasTab = FALSE;
1424 put_prompt();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001425#else
1426 len = 0;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001427 break_out = -1; /* to control traps */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001428#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001429 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001430 case CNTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001431 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001432 * if the len=0 and no chars to delete */
1433 if (len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001434 errno = 0;
1435 prepare_to_die:
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001436#if !ENABLE_ASH
Mark Whitley4e338752001-01-26 20:42:23 +00001437 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001438 goto_new_line();
1439 /* cmdedit_reset_term() called in atexit */
1440 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001441#else
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001442 /* to control stopped jobs */
1443 len = break_out = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001444 break;
1445#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001446 } else {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001447 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001448 }
1449 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001450 case CNTRL('E'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001451 vi_case( case '$'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001452 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001453 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001454 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001455 case CNTRL('F'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001456 vi_case( case 'l'|vbit: )
1457 vi_case( case ' '|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001458 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001459 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001460 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001461 case '\b':
1462 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001463 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001464 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001465 break;
1466 case '\t':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001467#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001468 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001469#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001470 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001471 case CNTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001472 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001473 command[cursor] = 0;
Eric Andersen65a07302002-04-13 13:26:49 +00001474 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001475 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001476 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001477 case CNTRL('L'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001478 vi_case( case CNTRL('L')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001479 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001480 printf("\033[H");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001481 redraw(0, len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001482 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001483#if MAX_HISTORY > 0
Paul Fox3f11b1b2005-08-04 19:04:46 +00001484 case CNTRL('N'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001485 vi_case( case CNTRL('N')|vbit: )
1486 vi_case( case 'j'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001487 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001488 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001489 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001490 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001491 case CNTRL('P'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001492 vi_case( case CNTRL('P')|vbit: )
1493 vi_case( case 'k'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001494 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001495 if (cur_history > 0) {
1496 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001497 goto rewrite_line;
1498 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001499 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001500 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001501 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001502#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001503 case CNTRL('U'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001504 vi_case( case CNTRL('U')|vbit: )
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001505 /* Control-U -- Clear line before cursor */
1506 if (cursor) {
1507 strcpy(command, command + cursor);
1508 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001509 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001510 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001511 case CNTRL('W'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001512 vi_case( case CNTRL('W')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001513 /* Control-W -- Remove the last word */
1514 while (cursor > 0 && isspace(command[cursor-1]))
1515 input_backspace();
1516 while (cursor > 0 &&!isspace(command[cursor-1]))
1517 input_backspace();
1518 break;
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001519#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001520 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001521 vi_cmdmode = 0;
1522 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001523 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001524 input_backward(cursor);
1525 vi_cmdmode = 0;
1526 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001527 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001528 input_forward();
1529 vi_cmdmode = 0;
1530 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001531 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001532 input_end();
1533 vi_cmdmode = 0;
1534 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001535 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001536 input_delete(1);
1537 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001538 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001539 if (cursor > 0) {
1540 input_backward(1);
1541 input_delete(1);
1542 }
1543 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001544 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001545 vi_Word_motion(command, 1);
1546 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001547 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001548 vi_word_motion(command, 1);
1549 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001550 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001551 vi_End_motion(command);
1552 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001553 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001554 vi_end_motion(command);
1555 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001556 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001557 vi_Back_motion(command);
1558 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001559 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001560 vi_back_motion(command);
1561 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001562 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001563 vi_cmdmode = 0;
1564 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001565 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001566 goto clear_to_eol;
1567
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001568 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001569 vi_cmdmode = 0;
1570 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001571 case 'd'|vbit: {
1572 int nc, sc;
1573 sc = cursor;
1574 prevc = ic;
1575 if (safe_read(0, &c, 1) < 1)
1576 goto prepare_to_die;
1577 if (c == (prevc & 0xff)) {
1578 /* "cc", "dd" */
1579 input_backward(cursor);
1580 goto clear_to_eol;
1581 break;
1582 }
1583 switch (c) {
1584 case 'w':
1585 case 'W':
1586 case 'e':
1587 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001588 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001589 case 'w': /* "dw", "cw" */
1590 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001591 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001592 case 'W': /* 'dW', 'cW' */
1593 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001594 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001595 case 'e': /* 'de', 'ce' */
1596 vi_end_motion(command);
1597 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001598 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001599 case 'E': /* 'dE', 'cE' */
1600 vi_End_motion(command);
1601 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001602 break;
1603 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001604 nc = cursor;
1605 input_backward(cursor - sc);
1606 while (nc-- > cursor)
1607 input_delete(1);
1608 break;
1609 case 'b': /* "db", "cb" */
1610 case 'B': /* implemented as B */
1611 if (c == 'b')
1612 vi_back_motion(command);
1613 else
1614 vi_Back_motion(command);
1615 while (sc-- > cursor)
1616 input_delete(1);
1617 break;
1618 case ' ': /* "d ", "c " */
1619 input_delete(1);
1620 break;
1621 case '$': /* "d$", "c$" */
1622 clear_to_eol:
1623 while (cursor < len)
1624 input_delete(1);
1625 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001626 }
1627 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001628 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001629 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001630 input_forward();
1631 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001632 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001633 put();
1634 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001635 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001636 if (safe_read(0, &c, 1) < 1)
1637 goto prepare_to_die;
1638 if (c == 0)
1639 beep();
1640 else {
1641 *(command + cursor) = c;
1642 putchar(c);
1643 putchar('\b');
1644 }
1645 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001646#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001647
1648 case ESC:
1649
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001650#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001651 if (vi_mode) {
1652 /* ESC: insert mode --> command mode */
1653 vi_cmdmode = 1;
1654 input_backward(1);
1655 break;
1656 }
1657#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001658 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001659 if (safe_read(0, &c, 1) < 1)
1660 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001661 /* different vt100 emulations */
1662 if (c == '[' || c == 'O') {
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001663 vi_case( case '['|vbit: )
1664 vi_case( case 'O'|vbit: )
Eric Andersen7467c8d2001-07-12 20:26:32 +00001665 if (safe_read(0, &c, 1) < 1)
1666 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001667 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001668 if (c >= '1' && c <= '9') {
1669 unsigned char dummy;
1670
1671 if (safe_read(0, &dummy, 1) < 1)
1672 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001673 if (dummy != '~')
Glenn L McGrath475820c2004-01-22 12:42:23 +00001674 c = 0;
1675 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001676 switch (c) {
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001677#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001678 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001679
1680 input_tab(&lastWasTab);
1681 break;
1682#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001683#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001684 case 'A':
1685 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001686 if (cur_history > 0) {
1687 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001688 goto rewrite_line;
1689 } else {
1690 beep();
1691 }
1692 break;
1693 case 'B':
1694 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001695 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001696 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001697 /* Rewrite the line with the selected history item */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001698rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001699 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001700 len = strlen(strcpy(command, history[cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001701 /* redraw and go to eol (bol, in vi */
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001702#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001703 redraw(cmdedit_y, vi_mode ? 9999:0);
1704#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001705 redraw(cmdedit_y, 0);
Paul Fox3f11b1b2005-08-04 19:04:46 +00001706#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001707 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001708#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001709 case 'C':
1710 /* Right Arrow -- Move forward one character */
1711 input_forward();
1712 break;
1713 case 'D':
1714 /* Left Arrow -- Move back one character */
1715 input_backward(1);
1716 break;
1717 case '3':
1718 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001719 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001720 break;
1721 case '1':
1722 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001723 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001724 input_backward(cursor);
1725 break;
1726 case '4':
1727 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001728 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001729 input_end();
1730 break;
1731 default:
Glenn L McGrath475820c2004-01-22 12:42:23 +00001732 c = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001733 beep();
1734 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001735 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001736
Eric Andersenc470f442003-07-28 09:56:35 +00001737 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001738#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001739 /* Control-V -- Add non-printable symbol */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001740 if (c == CNTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001741 if (safe_read(0, &c, 1) < 1)
1742 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001743 if (c == 0) {
1744 beep();
1745 break;
1746 }
1747 } else
1748#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001749 {
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001750#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001751 if (vi_cmdmode) /* don't self-insert */
1752 break;
1753#endif
1754 if (!Isprint(c)) /* Skip non-printable characters */
1755 break;
1756 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001757
Eric Andersenc470f442003-07-28 09:56:35 +00001758 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001759 break;
1760
1761 len++;
1762
Eric Andersenc470f442003-07-28 09:56:35 +00001763 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001764 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001765 *(command + cursor + 1) = 0;
1766 cmdedit_set_out_char(0);
Eric Andersenc470f442003-07-28 09:56:35 +00001767 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001768 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001769
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001770 memmove(command + sc + 1, command + sc, len - sc);
1771 *(command + sc) = c;
1772 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001773 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001774 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001775 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001776 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001777 }
1778
Erik Andersenc7c634b2000-03-19 05:28:55 +00001779 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001780 }
Eric Andersenc470f442003-07-28 09:56:35 +00001781 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001782 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001783
1784 if (c != '\t')
1785 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001786 }
1787
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001788 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001789 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001790
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001791#if MAX_HISTORY > 0
Erik Andersenc7c634b2000-03-19 05:28:55 +00001792 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001793 /* cleanup may be saved current command line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001794 if (len > 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001795 int i = n_history;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001796
1797 free(history[MAX_HISTORY]);
1798 history[MAX_HISTORY] = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001799 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001800 if (i >= MAX_HISTORY) {
1801 free(history[0]);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001802 for(i = 0; i < MAX_HISTORY-1; i++)
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001803 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001804 }
Rob Landleyd921b2e2006-08-03 15:41:12 +00001805 history[i++] = xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001806 cur_history = i;
1807 n_history = i;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001808#if ENABLE_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001809 num_ok_lines++;
1810#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001811 }
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001812#else /* MAX_HISTORY == 0 */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001813#if ENABLE_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001814 if (len > 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001815 num_ok_lines++;
1816 }
1817#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001818#endif /* MAX_HISTORY > 0 */
Eric Andersen27bb7902003-12-23 20:24:51 +00001819 if (break_out > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +00001820 command[len++] = '\n'; /* set '\n' */
1821 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001822 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001823#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001824 input_tab(0); /* strong free */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001825#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001826#if ENABLE_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001827 free(cmdedit_prompt);
1828#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001829 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001830 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001831}
1832
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001833#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001834
1835
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001836#ifdef TEST
1837
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001838const char *applet_name = "debug stuff usage";
Eric Andersene5dfced2001-04-09 22:48:12 +00001839
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001840#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001841#include <locale.h>
1842#endif
1843
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001844int main(int argc, char **argv)
1845{
1846 char buff[BUFSIZ];
1847 char *prompt =
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001848#if ENABLE_FEATURE_SH_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001849 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1850 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1851 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001852#else
1853 "% ";
1854#endif
1855
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001856#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001857 setlocale(LC_ALL, "");
1858#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001859 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001860 int l;
Eric Andersen27bb7902003-12-23 20:24:51 +00001861 l = cmdedit_read_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001862 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001863 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001864 buff[l-1] = 0;
1865 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001866 }
Eric Andersen27bb7902003-12-23 20:24:51 +00001867 printf("*** cmdedit_read_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001868 return 0;
1869}
1870
Eric Andersenc470f442003-07-28 09:56:35 +00001871#endif /* TEST */