blob: 843f73fefc443696037cc42de52993353a2c171c [file] [log] [blame]
Erik Andersenc7c634b2000-03-19 05:28:55 +00001/* vi: set sw=4 ts=4: */
Erik Andersen13456d12000-03-16 08:09:57 +00002/*
Eric Andersen5f2c79d2001-02-16 18:36:04 +00003 * Termios command line History and Editting.
Erik Andersen13456d12000-03-16 08:09:57 +00004 *
Eric Andersen5f2c79d2001-02-16 18:36:04 +00005 * Copyright (c) 1986-2001 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 Andersencb81e642003-07-14 21:21:08 +000012 * Erik Andersen <andersen@codepoet.org> (Majorly adjusted for busybox)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000013 *
Erik Andersen13456d12000-03-16 08:09:57 +000014 * This code is 'as is' with no warranty.
Erik Andersen13456d12000-03-16 08:09:57 +000015 *
Erik Andersen13456d12000-03-16 08:09:57 +000016 *
17 */
18
19/*
20 Usage and Known bugs:
21 Terminal key codes are not extensive, and more will probably
22 need to be added. This version was created on Debian GNU/Linux 2.x.
23 Delete, Backspace, Home, End, and the arrow keys were tested
24 to work in an Xterm and console. Ctrl-A also works as Home.
Mark Whitley4e338752001-01-26 20:42:23 +000025 Ctrl-E also works as End.
Erik Andersen13456d12000-03-16 08:09:57 +000026
Eric Andersen5f2c79d2001-02-16 18:36:04 +000027 Small bugs (simple effect):
28 - not true viewing if terminal size (x*y symbols) less
29 size (prompt + editor`s line + 2 symbols)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000030 - not true viewing if length prompt less terminal width
Erik Andersen13456d12000-03-16 08:09:57 +000031 */
32
Mark Whitley4e338752001-01-26 20:42:23 +000033
Eric Andersencbe31da2001-02-20 06:14:08 +000034#include <stdio.h>
35#include <errno.h>
36#include <unistd.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/ioctl.h>
40#include <ctype.h>
41#include <signal.h>
42#include <limits.h>
43
Eric Andersen3570a342000-09-25 21:45:58 +000044#include "busybox.h"
Mark Whitley4e338752001-01-26 20:42:23 +000045
Eric Andersenbdfd0d72001-10-24 05:00:29 +000046#ifdef CONFIG_LOCALE_SUPPORT
Eric Andersene5dfced2001-04-09 22:48:12 +000047#define Isprint(c) isprint((c))
48#else
49#define Isprint(c) ( (c) >= ' ' && (c) != ((unsigned char)'\233') )
50#endif
51
52#ifndef TEST
53
Eric Andersen5f2c79d2001-02-16 18:36:04 +000054#define D(x)
55
56#else
57
Eric Andersenbdfd0d72001-10-24 05:00:29 +000058#define CONFIG_FEATURE_COMMAND_EDITING
59#define CONFIG_FEATURE_COMMAND_TAB_COMPLETION
60#define CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
61#define CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
62#define CONFIG_FEATURE_CLEAN_UP
Eric Andersen5f2c79d2001-02-16 18:36:04 +000063
Eric Andersen5f2c79d2001-02-16 18:36:04 +000064#define D(x) x
65
66#endif /* TEST */
67
Eric Andersenbdfd0d72001-10-24 05:00:29 +000068#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5165fbe2001-02-20 06:42:29 +000069#include <dirent.h>
70#include <sys/stat.h>
71#endif
72
Eric Andersenbdfd0d72001-10-24 05:00:29 +000073#ifdef CONFIG_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000074
Eric Andersenbdfd0d72001-10-24 05:00:29 +000075#ifndef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
76#undef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +000077#endif
78
Eric Andersenbdfd0d72001-10-24 05:00:29 +000079#if defined(CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION) || defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
80#define CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +000081#endif
82
Eric Andersenbdfd0d72001-10-24 05:00:29 +000083#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Glenn L McGrath78b0e372001-06-26 02:06:08 +000084# ifndef TEST
Eric Andersen887ca792002-07-03 23:19:26 +000085# include "pwd_.h"
Glenn L McGrath78b0e372001-06-26 02:06:08 +000086# else
87# include <pwd.h>
88# endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000089#endif /* advanced FEATURES */
Eric Andersenaf4ac772001-02-01 22:43:49 +000090
91
Eric Andersen5f2c79d2001-02-16 18:36:04 +000092/* Maximum length of the linked list for the command line history */
Robert Griebl350d26b2002-12-03 22:45:46 +000093#ifndef CONFIG_FEATURE_COMMAND_HISTORY
94#define MAX_HISTORY 15
95#else
96#define MAX_HISTORY CONFIG_FEATURE_COMMAND_HISTORY
97#endif
98
Glenn L McGrath062c74f2002-11-27 09:29:49 +000099#if MAX_HISTORY < 1
100#warning cmdedit: You set MAX_HISTORY < 1. The history algorithm switched off.
101#else
102static char *history[MAX_HISTORY+1]; /* history + current */
103/* saved history lines */
104static int n_history;
105/* current pointer to history line */
106static int cur_history;
107#endif
Erik Andersen1d1d9502000-04-21 01:26:49 +0000108
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000109#include <termios.h>
110#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
111#define getTermSettings(fd,argp) tcgetattr(fd, argp);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000112
113/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000114static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000115
116
Mark Whitley4e338752001-01-26 20:42:23 +0000117static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000118volatile int cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +0000119static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000120volatile int handlers_sets = 0; /* Set next bites: */
121
Mark Whitley4e338752001-01-26 20:42:23 +0000122enum {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000123 SET_ATEXIT = 1, /* when atexit() has been called
124 and get euid,uid,gid to fast compare */
Eric Andersen7467c8d2001-07-12 20:26:32 +0000125 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
126 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000127};
Erik Andersen13456d12000-03-16 08:09:57 +0000128
Mark Whitley4e338752001-01-26 20:42:23 +0000129
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000130static int cmdedit_x; /* real x terminal position */
131static int cmdedit_y; /* pseudoreal y terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000132static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000133
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000134static int cursor; /* required global for signal handler */
135static int len; /* --- "" - - "" - -"- --""-- --""--- */
136static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000137static
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000138#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000139 const
140#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000141char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000142
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000143#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000144static char *user_buf = "";
145static char *home_pwd_buf = "";
146static int my_euid;
147#endif
148
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000149#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000150static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000151static int num_ok_lines = 1;
152#endif
153
154
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000155#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000156
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000157#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000158static int my_euid;
159#endif
160
161static int my_uid;
162static int my_gid;
163
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000164#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000165
Mark Whitley4e338752001-01-26 20:42:23 +0000166static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000167
Mark Whitley4e338752001-01-26 20:42:23 +0000168static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000169{
170 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen8d79ce82001-07-22 23:00:15 +0000171 static sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000172
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000173 /* emulate || signal call */
174 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Mark Whitley4e338752001-01-26 20:42:23 +0000175 ioctl(0, TIOCGWINSZ, &win);
176 if (win.ws_col > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000177 cmdedit_setwidth(win.ws_col, nsig == SIGWINCH);
178 }
Mark Whitley4e338752001-01-26 20:42:23 +0000179 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000180 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000181
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000182 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000183 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000184 else if (nsig == SIGWINCH) /* signaled called handler */
185 signal(SIGWINCH, win_changed); /* set for next call */
186 else /* nsig == 0 */
187 /* set previous handler */
188 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000189}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000190
191static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000192{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000193 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000194/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000195 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000196 handlers_sets &= ~SET_RESET_TERM;
197 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000198 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000199 /* reset SIGWINCH handler to previous (default) */
200 win_changed(0);
201 handlers_sets &= ~SET_WCHG_HANDLERS;
202 }
203 fflush(stdout);
Erik Andersen13456d12000-03-16 08:09:57 +0000204}
205
Mark Whitley4e338752001-01-26 20:42:23 +0000206
Mark Whitley4e338752001-01-26 20:42:23 +0000207/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000208static void cmdedit_set_out_char(int next_char)
209{
210
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000211 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000212
213 if (c == 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000214 c = ' '; /* destroy end char? */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000215#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersene5dfced2001-04-09 22:48:12 +0000216 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000217 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000218 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000219 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000220 c += '@';
221 if (c == 127)
222 c = '?';
223 printf("\033[7m%c\033[0m", c);
224 } else
225#endif
226 putchar(c);
227 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000228 /* terminal is scrolled down */
229 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000230 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000231
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000232 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000233 next_char = ' ';
234 /* destroy "(auto)margin" */
235 putchar(next_char);
236 putchar('\b');
237 }
238 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000239}
240
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000241/* Move to end line. Bonus: rewrite line from cursor */
242static void input_end(void)
243{
244 while (cursor < len)
245 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000246}
247
248/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000249static void goto_new_line(void)
250{
Mark Whitley4e338752001-01-26 20:42:23 +0000251 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000252 if (cmdedit_x)
253 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000254}
255
256
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000257static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000258{
Robert Grieblb2301592002-07-30 23:13:51 +0000259 if ( s )
260 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000261}
262static inline void beep(void)
263{
264 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000265}
266
Mark Whitley4e338752001-01-26 20:42:23 +0000267/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000268/* special for slow terminal */
269static void input_backward(int num)
270{
271 if (num > cursor)
272 num = cursor;
Eric Andersene5dfced2001-04-09 22:48:12 +0000273 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000274
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000275 if (cmdedit_x >= num) { /* no to up line */
276 cmdedit_x -= num;
277 if (num < 4)
278 while (num-- > 0)
279 putchar('\b');
280
281 else
282 printf("\033[%dD", num);
283 } else {
284 int count_y;
285
286 if (cmdedit_x) {
287 putchar('\r'); /* back to first terminal pos. */
288 num -= cmdedit_x; /* set previous backward */
289 }
290 count_y = 1 + num / cmdedit_termw;
291 printf("\033[%dA", count_y);
292 cmdedit_y -= count_y;
293 /* require forward after uping */
294 cmdedit_x = cmdedit_termw * count_y - num;
295 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000296 }
Erik Andersen13456d12000-03-16 08:09:57 +0000297}
298
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000299static void put_prompt(void)
300{
301 out1str(cmdedit_prompt);
302 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
303 cursor = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000304 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000305}
306
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000307#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000308static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000309{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000310 cmdedit_prompt = prmt_ptr;
311 cmdedit_prmt_len = strlen(prmt_ptr);
312 put_prompt();
313}
314#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000315static void parse_prompt(const char *prmt_ptr)
316{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000317 int prmt_len = 0;
318 int sub_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000319 char flg_not_length = '[';
320 char *prmt_mem_ptr = xcalloc(1, 1);
321 char *pwd_buf = xgetcwd(0);
322 char buf2[PATH_MAX + 1];
323 char buf[2];
324 char c;
325 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000326
Eric Andersen5f265b72001-05-11 16:58:46 +0000327 if (!pwd_buf) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000328 pwd_buf=(char *)bb_msg_unknown;
Eric Andersen5f265b72001-05-11 16:58:46 +0000329 }
330
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000331 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000332 pbuf = buf;
333 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000334 c = *prmt_ptr++;
335 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000336 const char *cp = prmt_ptr;
337 int l;
338
Manuel Novoa III cad53642003-03-19 09:13:01 +0000339 c = bb_process_escape_sequence(&prmt_ptr);
Eric Andersene5dfced2001-04-09 22:48:12 +0000340 if(prmt_ptr==cp) {
341 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000342 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000343 c = *prmt_ptr++;
344 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000345#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000346 case 'u':
347 pbuf = user_buf;
348 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000349#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000350 case 'h':
351 pbuf = hostname_buf;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000352 if (pbuf == 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000353 pbuf = xcalloc(256, 1);
354 if (gethostname(pbuf, 255) < 0) {
355 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000356 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000357 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000358
359 if (s)
360 *s = 0;
361 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000362 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000363 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000364 break;
365 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000366 c = my_euid == 0 ? '#' : '$';
367 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000368#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000369 case 'w':
370 pbuf = pwd_buf;
371 l = strlen(home_pwd_buf);
372 if (home_pwd_buf[0] != 0 &&
373 strncmp(home_pwd_buf, pbuf, l) == 0 &&
374 (pbuf[l]=='/' || pbuf[l]=='\0') &&
375 strlen(pwd_buf+l)<PATH_MAX) {
376 pbuf = buf2;
377 *pbuf = '~';
378 strcpy(pbuf+1, pwd_buf+l);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000379 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000380 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000381#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000382 case 'W':
383 pbuf = pwd_buf;
384 cp = strrchr(pbuf,'/');
385 if ( (cp != NULL) && (cp != pbuf) )
386 pbuf += (cp-pbuf)+1;
387 break;
388 case '!':
389 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
390 break;
391 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000392 c = '\033';
393 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000394 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000395 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000396 int h;
397 buf2[l++] = *prmt_ptr;
398 buf2[l] = 0;
399 h = strtol(buf2, &pbuf, 16);
400 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000401 l--;
402 break;
403 }
404 prmt_ptr++;
405 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000406 buf2[l] = 0;
407 c = (char)strtol(buf2, 0, 16);
408 if(c==0)
409 c = '?';
410 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000411 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000412 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000413 if (c == flg_not_length) {
414 flg_not_length = flg_not_length == '[' ? ']' : '[';
415 continue;
416 }
417 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000418 }
419 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000420 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000421 if(pbuf == buf)
422 *pbuf = c;
423 prmt_len += strlen(pbuf);
424 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000425 if (flg_not_length == ']')
426 sub_len++;
427 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000428 if(pwd_buf!=(char *)bb_msg_unknown)
Eric Andersen8f697842001-07-02 15:36:57 +0000429 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000430 cmdedit_prompt = prmt_mem_ptr;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000431 cmdedit_prmt_len = prmt_len - sub_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000432 put_prompt();
433}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000434#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000435
436
437/* draw promt, editor line, and clear tail */
438static void redraw(int y, int back_cursor)
439{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000440 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000441 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000442 putchar('\r');
443 put_prompt();
444 input_end(); /* rewrite */
445 printf("\033[J"); /* destroy tail after cursor */
446 input_backward(back_cursor);
447}
448
Erik Andersenf0657d32000-04-12 17:49:52 +0000449/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000450static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000451{
Mark Whitley4e338752001-01-26 20:42:23 +0000452 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000453
Mark Whitley4e338752001-01-26 20:42:23 +0000454 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000455 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000456
457 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000458 len--;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000459 input_end(); /* rewtite new line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000460 cmdedit_set_out_char(0); /* destroy end char */
461 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000462}
463
Mark Whitley4e338752001-01-26 20:42:23 +0000464/* Delete the char in back of the cursor */
465static void input_backspace(void)
466{
467 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000468 input_backward(1);
469 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000470 }
471}
472
473
Erik Andersenf0657d32000-04-12 17:49:52 +0000474/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000475static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000476{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000477 if (cursor < len)
478 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000479}
480
481
Mark Whitley4e338752001-01-26 20:42:23 +0000482static void cmdedit_setwidth(int w, int redraw_flg)
483{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000484 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000485 if (w <= cmdedit_termw) {
486 cmdedit_termw = cmdedit_termw % w;
487 }
Mark Whitley4e338752001-01-26 20:42:23 +0000488 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000489 cmdedit_termw = w;
490
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000491 if (redraw_flg) {
492 /* new y for current cursor */
493 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000494
495 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000496 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
497 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000498 }
Eric Andersen6faae7d2001-02-16 20:09:17 +0000499 }
Mark Whitley4e338752001-01-26 20:42:23 +0000500}
501
Eric Andersen7467c8d2001-07-12 20:26:32 +0000502static void cmdedit_init(void)
Mark Whitley4e338752001-01-26 20:42:23 +0000503{
Eric Andersen61173a52001-03-19 17:48:55 +0000504 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000505 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
506 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000507 win_changed(-SIGWINCH);
508 handlers_sets |= SET_WCHG_HANDLERS;
509 }
510
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000511 if ((handlers_sets & SET_ATEXIT) == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000512#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000513 struct passwd *entry;
514
515 my_euid = geteuid();
516 entry = getpwuid(my_euid);
517 if (entry) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000518 user_buf = bb_xstrdup(entry->pw_name);
519 home_pwd_buf = bb_xstrdup(entry->pw_dir);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000520 }
521#endif
522
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000523#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000524
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000525#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000526 my_euid = geteuid();
527#endif
528 my_uid = getuid();
529 my_gid = getgid();
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000530#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000531 handlers_sets |= SET_ATEXIT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000532 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000533 }
Mark Whitley4e338752001-01-26 20:42:23 +0000534}
Erik Andersenf0657d32000-04-12 17:49:52 +0000535
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000536#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000537
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000538static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000539{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000540 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
541 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
542 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
543 (st->st_mode & S_IXOTH)) return TRUE;
544 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000545}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000546
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000547#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000548
549static char **username_tab_completion(char *ud, int *num_matches)
550{
551 struct passwd *entry;
552 int userlen;
553 char *temp;
554
555
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000556 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000557 userlen = strlen(ud);
558
559 if (num_matches == 0) { /* "~/..." or "~user/..." */
560 char *sav_ud = ud - 1;
561 char *home = 0;
562
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000563 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000564 home = home_pwd_buf;
565 } else {
566 /* "~user/..." */
567 temp = strchr(ud, '/');
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000568 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000569 entry = getpwnam(ud);
570 *temp = '/'; /* restore ~user/... */
571 ud = temp;
572 if (entry)
573 home = entry->pw_dir;
574 }
575 if (home) {
576 if ((userlen + strlen(home) + 1) < BUFSIZ) {
577 char temp2[BUFSIZ]; /* argument size */
578
579 /* /home/user/... */
580 sprintf(temp2, "%s%s", home, ud);
581 strcpy(sav_ud, temp2);
582 }
583 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000584 return 0; /* void, result save to argument :-) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000585 } else {
586 /* "~[^/]*" */
587 char **matches = (char **) NULL;
588 int nm = 0;
589
590 setpwent();
591
592 while ((entry = getpwent()) != NULL) {
593 /* Null usernames should result in all users as possible completions. */
594 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
595
Manuel Novoa III cad53642003-03-19 09:13:01 +0000596 bb_xasprintf(&temp, "~%s/", entry->pw_name);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000597 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
598
599 matches[nm++] = temp;
600 }
601 }
602
603 endpwent();
604 (*num_matches) = nm;
605 return (matches);
606 }
607}
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000608#endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000609
610enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000611 FIND_EXE_ONLY = 0,
612 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000613 FIND_FILE_ONLY = 2,
614};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000615
Mark Whitley4e338752001-01-26 20:42:23 +0000616static int path_parse(char ***p, int flags)
617{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000618 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000619 char *tmp;
620 char *pth;
621
Mark Whitley4e338752001-01-26 20:42:23 +0000622 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000623 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
624 /* PATH=<empty> or PATH=:<empty> */
625 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000626 return 1;
627 }
628
629 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000630 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000631
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000632 for (;;) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000633 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000634 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000635 if (tmp) {
636 if (*++tmp == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000637 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000638 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000639 break;
640 }
641
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000642 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000643
644 tmp = pth;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000645 (*p)[0] = bb_xstrdup(tmp);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000646 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000647
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000648 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000649 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000650 if (tmp) {
651 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
652 if (*++tmp == 0)
653 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000654 } else
655 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000656 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000657 }
658
659 return npth;
660}
661
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000662static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000663{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000664 int l = 0;
665 char *s = xmalloc((strlen(found) + 1) * 2);
666
667 while (*found) {
668 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
669 s[l++] = '\\';
670 s[l++] = *found++;
671 }
672 s[l] = 0;
673 return s;
674}
675
676static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000677 int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000678{
679
680 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000681 DIR *dir;
682 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000683 char dirbuf[BUFSIZ];
684 int nm = *num_matches;
685 struct stat st;
686 char *path1[1];
687 char **paths = path1;
688 int npaths;
689 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000690 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000691 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000692
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000693 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000694
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000695 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000696 /* no dir, if flags==EXE_ONLY - get paths, else "." */
697 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000698 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000699 } else {
700 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000701 /* save for change */
702 strcpy(dirbuf, command);
703 /* set dir only */
704 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000705#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000706 if (dirbuf[0] == '~') /* ~/... or ~user/... */
707 username_tab_completion(dirbuf, 0);
708#endif
709 /* "strip" dirname in command */
710 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000711
Mark Whitley4e338752001-01-26 20:42:23 +0000712 paths[0] = dirbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000713 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000714 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000715
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000716 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000717
Mark Whitley4e338752001-01-26 20:42:23 +0000718 dir = opendir(paths[i]);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000719 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000720 continue;
721
722 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000723 char *str_found = next->d_name;
724
Mark Whitley4e338752001-01-26 20:42:23 +0000725 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000726 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000727 continue;
728 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000729 if (*str_found == '.' && *pfind == 0) {
730 if (*paths[i] == '/' && paths[i][1] == 0
731 && str_found[1] == 0) str_found = ""; /* only "/" */
732 else
Mark Whitley4e338752001-01-26 20:42:23 +0000733 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000734 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000735 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000736 /* hmm, remover in progress? */
Eric Andersene5dfced2001-04-09 22:48:12 +0000737 if (stat(found, &st) < 0)
738 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000739 /* find with dirs ? */
740 if (paths[i] != dirbuf)
741 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000742 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000743 /* name is directory */
Eric Andersene5dfced2001-04-09 22:48:12 +0000744 str_found = found;
745 found = concat_path_file(found, "");
746 free(str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000747 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000748 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000749 /* not put found file if search only dirs for cd */
Eric Andersene5dfced2001-04-09 22:48:12 +0000750 if (type == FIND_DIR_ONLY)
751 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000752 str_found = add_quote_for_spec_chars(found);
753 if (type == FIND_FILE_ONLY ||
Matt Kraai1f0c4362001-12-20 23:13:26 +0000754 (type == FIND_EXE_ONLY && is_execute(&st)))
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000755 strcat(str_found, " ");
756 }
Mark Whitley4e338752001-01-26 20:42:23 +0000757 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000758 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
759
760 matches[nm++] = str_found;
Eric Andersene5dfced2001-04-09 22:48:12 +0000761cont:
762 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000763 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000764 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000765 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000766 if (paths != path1) {
767 free(paths[0]); /* allocated memory only in first member */
768 free(paths);
769 }
Mark Whitley4e338752001-01-26 20:42:23 +0000770 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000771 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000772}
Erik Andersenf0657d32000-04-12 17:49:52 +0000773
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000774static int match_compare(const void *a, const void *b)
775{
776 return strcmp(*(char **) a, *(char **) b);
777}
778
779
780
781#define QUOT (UCHAR_MAX+1)
782
783#define collapse_pos(is, in) { \
Eric Andersen889a3012002-03-20 14:31:15 +0000784 memcpy(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
785 memcpy(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000786
787static int find_match(char *matchBuf, int *len_with_quotes)
788{
789 int i, j;
790 int command_mode;
791 int c, c2;
792 int int_buf[BUFSIZ + 1];
793 int pos_buf[BUFSIZ + 1];
794
795 /* set to integer dimension characters and own positions */
796 for (i = 0;; i++) {
797 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
798 if (int_buf[i] == 0) {
799 pos_buf[i] = -1; /* indicator end line */
800 break;
801 } else
802 pos_buf[i] = i;
803 }
804
805 /* mask \+symbol and convert '\t' to ' ' */
806 for (i = j = 0; matchBuf[i]; i++, j++)
807 if (matchBuf[i] == '\\') {
808 collapse_pos(j, j + 1);
809 int_buf[j] |= QUOT;
810 i++;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000811#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000812 if (matchBuf[i] == '\t') /* algorithm equivalent */
813 int_buf[j] = ' ' | QUOT;
814#endif
815 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000816#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000817 else if (matchBuf[i] == '\t')
818 int_buf[j] = ' ';
819#endif
820
821 /* mask "symbols" or 'symbols' */
822 c2 = 0;
823 for (i = 0; int_buf[i]; i++) {
824 c = int_buf[i];
825 if (c == '\'' || c == '"') {
826 if (c2 == 0)
827 c2 = c;
828 else {
829 if (c == c2)
830 c2 = 0;
831 else
832 int_buf[i] |= QUOT;
833 }
834 } else if (c2 != 0 && c != '$')
835 int_buf[i] |= QUOT;
836 }
837
838 /* skip commands with arguments if line have commands delimiters */
839 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
840 for (i = 0; int_buf[i]; i++) {
841 c = int_buf[i];
842 c2 = int_buf[i + 1];
843 j = i ? int_buf[i - 1] : -1;
844 command_mode = 0;
845 if (c == ';' || c == '&' || c == '|') {
846 command_mode = 1 + (c == c2);
847 if (c == '&') {
848 if (j == '>' || j == '<')
849 command_mode = 0;
850 } else if (c == '|' && j == '>')
851 command_mode = 0;
852 }
853 if (command_mode) {
854 collapse_pos(0, i + command_mode);
855 i = -1; /* hack incremet */
856 }
857 }
858 /* collapse `command...` */
859 for (i = 0; int_buf[i]; i++)
860 if (int_buf[i] == '`') {
861 for (j = i + 1; int_buf[j]; j++)
862 if (int_buf[j] == '`') {
863 collapse_pos(i, j + 1);
864 j = 0;
865 break;
866 }
867 if (j) {
868 /* not found close ` - command mode, collapse all previous */
869 collapse_pos(0, i + 1);
870 break;
871 } else
872 i--; /* hack incremet */
873 }
874
875 /* collapse (command...(command...)...) or {command...{command...}...} */
876 c = 0; /* "recursive" level */
877 c2 = 0;
878 for (i = 0; int_buf[i]; i++)
879 if (int_buf[i] == '(' || int_buf[i] == '{') {
880 if (int_buf[i] == '(')
881 c++;
882 else
883 c2++;
884 collapse_pos(0, i + 1);
885 i = -1; /* hack incremet */
886 }
887 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
888 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
889 if (int_buf[i] == ')')
890 c--;
891 else
892 c2--;
893 collapse_pos(0, i + 1);
894 i = -1; /* hack incremet */
895 }
896
897 /* skip first not quote space */
898 for (i = 0; int_buf[i]; i++)
899 if (int_buf[i] != ' ')
900 break;
901 if (i)
902 collapse_pos(0, i);
903
904 /* set find mode for completion */
905 command_mode = FIND_EXE_ONLY;
906 for (i = 0; int_buf[i]; i++)
907 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
908 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000909 && matchBuf[pos_buf[0]]=='c'
910 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000911 command_mode = FIND_DIR_ONLY;
912 else {
913 command_mode = FIND_FILE_ONLY;
914 break;
915 }
916 }
917 /* "strlen" */
918 for (i = 0; int_buf[i]; i++);
919 /* find last word */
920 for (--i; i >= 0; i--) {
921 c = int_buf[i];
922 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
923 collapse_pos(0, i + 1);
924 break;
925 }
926 }
927 /* skip first not quoted '\'' or '"' */
928 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
929 /* collapse quote or unquote // or /~ */
Mark Whitley7e5291f2001-03-08 19:31:12 +0000930 while ((int_buf[i] & ~QUOT) == '/' &&
931 ((int_buf[i + 1] & ~QUOT) == '/'
932 || (int_buf[i + 1] & ~QUOT) == '~')) {
933 i++;
934 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000935
936 /* set only match and destroy quotes */
937 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000938 for (c = 0; pos_buf[i] >= 0; i++) {
939 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000940 j = pos_buf[i] + 1;
941 }
Eric Andersen4f990532001-05-31 17:15:57 +0000942 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000943 /* old lenght matchBuf with quotes symbols */
944 *len_with_quotes = j ? j - pos_buf[0] : 0;
945
946 return command_mode;
947}
948
Glenn L McGrath4d001292003-01-06 01:11:50 +0000949/*
950 display by column original ideas from ls applet,
951 very optimize by my :)
952*/
953static void showfiles(char **matches, int nfiles)
954{
955 int ncols, row;
956 int column_width = 0;
957 int nrows = nfiles;
958
959 /* find the longest file name- use that as the column width */
960 for (row = 0; row < nrows; row++) {
961 int l = strlen(matches[row]);
962
963 if (column_width < l)
964 column_width = l;
965 }
966 column_width += 2; /* min space for columns */
967 ncols = cmdedit_termw / column_width;
968
969 if (ncols > 1) {
970 nrows /= ncols;
971 if(nfiles % ncols)
972 nrows++; /* round up fractionals */
973 column_width = -column_width; /* for printf("%-Ns", ...); */
974 } else {
975 ncols = 1;
976 }
977 for (row = 0; row < nrows; row++) {
978 int n = row;
979 int nc;
980
981 for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++)
982 printf("%*s", column_width, matches[n]);
983 printf("%s\n", matches[n]);
984 }
985}
986
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000987
988static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000989{
990 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000991 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +0000992 static char **matches;
993
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000994 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +0000995 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000996 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +0000997 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000998 free(matches);
999 matches = (char **) NULL;
1000 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001001 return;
1002 }
Matt Kraai1f0c4362001-12-20 23:13:26 +00001003 if (! *lastWasTab) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001004
1005 char *tmp;
1006 int len_found;
1007 char matchBuf[BUFSIZ];
1008 int find_type;
1009 int recalc_pos;
1010
1011 *lastWasTab = TRUE; /* flop trigger */
1012
1013 /* Make a local copy of the string -- up
1014 * to the position of the cursor */
1015 tmp = strncpy(matchBuf, command_ps, cursor);
1016 tmp[cursor] = 0;
1017
1018 find_type = find_match(matchBuf, &recalc_pos);
1019
1020 /* Free up any memory already allocated */
1021 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001022
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001023#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001024 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001025 * then try completing this word as a username. */
1026
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001027 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001028 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001029#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001030 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001031 * in the current working directory that matches. */
1032 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001033 matches =
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001034 exe_n_cwd_tab_completion(matchBuf,
1035 &num_matches, find_type);
1036 /* Remove duplicate found */
1037 if(matches) {
1038 int i, j;
1039 /* bubble */
1040 for(i=0; i<(num_matches-1); i++)
1041 for(j=i+1; j<num_matches; j++)
1042 if(matches[i]!=0 && matches[j]!=0 &&
1043 strcmp(matches[i], matches[j])==0) {
1044 free(matches[j]);
1045 matches[j]=0;
1046 }
1047 j=num_matches;
1048 num_matches = 0;
1049 for(i=0; i<j; i++)
1050 if(matches[i]) {
1051 if(!strcmp(matches[i], "./"))
1052 matches[i][1]=0;
1053 else if(!strcmp(matches[i], "../"))
1054 matches[i][2]=0;
1055 matches[num_matches++]=matches[i];
1056 }
1057 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001058 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001059 if (!matches || num_matches > 1) {
1060 char *tmp1;
1061
Mark Whitley4e338752001-01-26 20:42:23 +00001062 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001063 if (!matches)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001064 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001065 /* sort */
1066 qsort(matches, num_matches, sizeof(char *), match_compare);
1067
1068 /* find minimal match */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001069 tmp = bb_xstrdup(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001070 for (tmp1 = tmp; *tmp1; tmp1++)
1071 for (len_found = 1; len_found < num_matches; len_found++)
1072 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1073 *tmp1 = 0;
1074 break;
1075 }
1076 if (*tmp == 0) { /* have unique */
1077 free(tmp);
1078 return;
1079 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001080 } else { /* one match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001081 tmp = matches[0];
1082 /* for next completion current found */
1083 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001084 }
1085
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 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001107 if (tmp != matches[0])
1108 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001109 } else {
1110 /* Ok -- the last char was a TAB. Since they
1111 * just hit TAB again, print a list of all the
1112 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001113 if (matches && num_matches > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001114 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001115
1116 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001117 goto_new_line();
Glenn L McGrath4d001292003-01-06 01:11:50 +00001118 showfiles(matches, num_matches);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001119 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001120 }
1121 }
1122}
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001123#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001124
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001125#if MAX_HISTORY >= 1
1126static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001127{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001128 if(command_ps[0] != 0 || history[cur_history] == 0) {
1129 free(history[cur_history]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001130 history[cur_history] = bb_xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001131 }
1132 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +00001133}
1134
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001135static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001136{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001137 int ch = cur_history;
1138
1139 if (ch < n_history) {
1140 get_previous_history(); /* save the current history line */
1141 return (cur_history = ch+1);
1142 } else {
1143 beep();
1144 return 0;
1145 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001146}
Robert Griebl350d26b2002-12-03 22:45:46 +00001147
Robert Griebl350d26b2002-12-03 22:45:46 +00001148#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001149extern void load_history ( const char *fromfile )
1150{
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
1156 for(hi = n_history; hi > 0; ) {
1157 hi--;
1158 free ( history [hi] );
Robert Griebl350d26b2002-12-03 22:45:46 +00001159 }
1160
1161 if (( fp = fopen ( fromfile, "r" ))) {
Robert Griebl350d26b2002-12-03 22:45:46 +00001162
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001163 for ( hi = 0; hi < MAX_HISTORY; ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001164 char * hl = bb_get_chomped_line_from_file(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001165 int l;
1166
1167 if(!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001168 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001169 l = strlen(hl);
1170 if(l >= BUFSIZ)
1171 hl[BUFSIZ-1] = 0;
1172 if(l == 0 || hl[0] == ' ') {
1173 free(hl);
1174 continue;
1175 }
1176 history [hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001177 }
1178 fclose ( fp );
1179 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001180 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001181}
1182
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001183extern void save_history ( const char *tofile )
Robert Griebl350d26b2002-12-03 22:45:46 +00001184{
Robert Griebl350d26b2002-12-03 22:45:46 +00001185 FILE *fp = fopen ( tofile, "w" );
1186
1187 if ( fp ) {
1188 int i;
1189
1190 for ( i = 0; i < n_history; i++ ) {
1191 fputs ( history [i], fp );
1192 fputc ( '\n', fp );
1193 }
1194 fclose ( fp );
1195 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001196}
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001197#endif
Robert Griebl350d26b2002-12-03 22:45:46 +00001198
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001199#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001200
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001201enum {
1202 ESC = 27,
1203 DEL = 127,
1204};
1205
1206
Erik Andersen6273f652000-03-17 01:12:41 +00001207/*
1208 * This function is used to grab a character buffer
1209 * from the input file descriptor and allows you to
1210 * a string with full command editing (sortof like
1211 * a mini readline).
1212 *
1213 * The following standard commands are not implemented:
1214 * ESC-b -- Move back one word
1215 * ESC-f -- Move forward one word
1216 * ESC-d -- Delete back one word
1217 * ESC-h -- Delete forward one word
1218 * CTL-t -- Transpose two characters
1219 *
1220 * Furthermore, the "vi" command editing keys are not implemented.
1221 *
Erik Andersen6273f652000-03-17 01:12:41 +00001222 */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001223
Eric Andersen044228d2001-07-17 01:12:36 +00001224
1225int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001226{
1227
Erik Andersenc7c634b2000-03-19 05:28:55 +00001228 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001229 int lastWasTab = FALSE;
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001230 unsigned char c = 0;
Erik Andersen13456d12000-03-16 08:09:57 +00001231
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001232 /* prepare before init handlers */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001233 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001234 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001235 command_ps = command;
1236
Eric Andersen34506362001-08-02 05:02:46 +00001237 getTermSettings(0, (void *) &initial_settings);
1238 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1239 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1240 /* Turn off echoing and CTRL-C, so we can trap it */
1241 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001242#ifndef linux
Eric Andersen34506362001-08-02 05:02:46 +00001243 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1244 new_settings.c_cc[VMIN] = 1;
1245 new_settings.c_cc[VTIME] = 0;
1246 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001247# ifndef _POSIX_VDISABLE
1248# define _POSIX_VDISABLE '\0'
1249# endif
Eric Andersen34506362001-08-02 05:02:46 +00001250 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001251#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001252 command[0] = 0;
1253
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001254 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001255 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001256
Eric Andersen6faae7d2001-02-16 20:09:17 +00001257 /* Now initialize things */
1258 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001259 /* Print out the command prompt */
1260 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001261
Erik Andersenc7c634b2000-03-19 05:28:55 +00001262 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001263
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001264 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001265
Eric Andersen7467c8d2001-07-12 20:26:32 +00001266 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001267 /* if we can't read input then exit */
1268 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001269
Erik Andersen13456d12000-03-16 08:09:57 +00001270 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001271 case '\n':
1272 case '\r':
1273 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001274 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001275 break_out = 1;
1276 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001277 case 1:
1278 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001279 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001280 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001281 case 2:
1282 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001283 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001284 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001285 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001286 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001287 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001288 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001289 len = 0;
1290 lastWasTab = FALSE;
1291 put_prompt();
1292 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001293 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001294 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001295 * if the len=0 and no chars to delete */
1296 if (len == 0) {
Eric Andersened424db2001-04-23 15:28:28 +00001297prepare_to_die:
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001298#if !defined(CONFIG_ASH)
Mark Whitley4e338752001-01-26 20:42:23 +00001299 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001300 goto_new_line();
1301 /* cmdedit_reset_term() called in atexit */
1302 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001303#else
1304 break_out = -1; /* for control stoped jobs */
1305 break;
1306#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001307 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001308 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001309 }
1310 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001311 case 5:
1312 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001313 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001314 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001315 case 6:
1316 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001317 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001318 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001319 case '\b':
1320 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001321 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001322 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001323 break;
1324 case '\t':
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001325#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001326 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001327#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001328 break;
Eric Andersen65a07302002-04-13 13:26:49 +00001329 case 11:
1330 /* Control-k -- clear to end of line */
1331 *(command + cursor) = 0;
1332 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001333 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001334 break;
1335 case 12:
Eric Andersen65a07302002-04-13 13:26:49 +00001336 /* Control-l -- clear screen */
Eric Andersen65a07302002-04-13 13:26:49 +00001337 printf("\033[H");
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001338 redraw(0, len-cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001339 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001340#if MAX_HISTORY >= 1
Erik Andersenf0657d32000-04-12 17:49:52 +00001341 case 14:
1342 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001343 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001344 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001345 break;
1346 case 16:
1347 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001348 if (cur_history > 0) {
1349 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001350 goto rewrite_line;
1351 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001352 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001353 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001354 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001355#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001356 case 21:
1357 /* Control-U -- Clear line before cursor */
1358 if (cursor) {
1359 strcpy(command, command + cursor);
1360 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001361 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001362 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001363 case ESC:{
1364 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001365 if (safe_read(0, &c, 1) < 1)
1366 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001367 /* different vt100 emulations */
1368 if (c == '[' || c == 'O') {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001369 if (safe_read(0, &c, 1) < 1)
1370 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001371 }
1372 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001373#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001374 case '\t': /* Alt-Tab */
1375
1376 input_tab(&lastWasTab);
1377 break;
1378#endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001379#if MAX_HISTORY >= 1
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001380 case 'A':
1381 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001382 if (cur_history > 0) {
1383 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001384 goto rewrite_line;
1385 } else {
1386 beep();
1387 }
1388 break;
1389 case 'B':
1390 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001391 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001392 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001393 /* Rewrite the line with the selected history item */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001394rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001395 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001396 len = strlen(strcpy(command, history[cur_history]));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001397 /* redraw and go to end line */
1398 redraw(cmdedit_y, 0);
1399 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001400#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001401 case 'C':
1402 /* Right Arrow -- Move forward one character */
1403 input_forward();
1404 break;
1405 case 'D':
1406 /* Left Arrow -- Move back one character */
1407 input_backward(1);
1408 break;
1409 case '3':
1410 /* Delete */
1411 input_delete();
1412 break;
1413 case '1':
1414 case 'H':
1415 /* Home (Ctrl-A) */
1416 input_backward(cursor);
1417 break;
1418 case '4':
1419 case 'F':
1420 /* End (Ctrl-E) */
1421 input_end();
1422 break;
1423 default:
1424 if (!(c >= '1' && c <= '9'))
1425 c = 0;
1426 beep();
1427 }
1428 if (c >= '1' && c <= '9')
1429 do
Eric Andersen7467c8d2001-07-12 20:26:32 +00001430 if (safe_read(0, &c, 1) < 1)
1431 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001432 while (c != '~');
1433 break;
1434 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001435
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001436 default: /* If it's regular input, do the normal thing */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001437#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001438 /* Control-V -- Add non-printable symbol */
1439 if (c == 22) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001440 if (safe_read(0, &c, 1) < 1)
1441 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001442 if (c == 0) {
1443 beep();
1444 break;
1445 }
1446 } else
1447#endif
Eric Andersene5dfced2001-04-09 22:48:12 +00001448 if (!Isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001449 break;
1450
1451 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
1452 break;
1453
1454 len++;
1455
1456 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001457 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001458 *(command + cursor + 1) = 0;
1459 cmdedit_set_out_char(0);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001460 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001461 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001462
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001463 memmove(command + sc + 1, command + sc, len - sc);
1464 *(command + sc) = c;
1465 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001466 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001467 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001468 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001469 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001470 }
1471
Erik Andersenc7c634b2000-03-19 05:28:55 +00001472 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001473 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001474 if (break_out) /* Enter is the command terminator, no more input. */
1475 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001476
1477 if (c != '\t')
1478 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001479 }
1480
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001481 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001482 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001483
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001484#if MAX_HISTORY >= 1
Erik Andersenc7c634b2000-03-19 05:28:55 +00001485 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001486 /* cleanup may be saved current command line */
1487 free(history[MAX_HISTORY]);
1488 history[MAX_HISTORY] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001489 if (len) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001490 int i = n_history;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001491 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001492 if (i >= MAX_HISTORY) {
1493 free(history[0]);
1494 for(i = 0; i < (MAX_HISTORY-1); i++)
1495 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001496 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001497 history[i++] = bb_xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001498 cur_history = i;
1499 n_history = i;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001500#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001501 num_ok_lines++;
1502#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001503 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001504#else /* MAX_HISTORY < 1 */
1505#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
1506 if (len) { /* no put empty line */
1507 num_ok_lines++;
1508 }
1509#endif
1510#endif /* MAX_HISTORY >= 1 */
Eric Andersen044228d2001-07-17 01:12:36 +00001511 if(break_out>0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001512 command[len++] = '\n'; /* set '\n' */
1513 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001514 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001515#if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001516 input_tab(0); /* strong free */
1517#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001518#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001519 free(cmdedit_prompt);
1520#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001521 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001522 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001523}
1524
Eric Andersen7467c8d2001-07-12 20:26:32 +00001525
1526
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001527#endif /* CONFIG_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001528
1529
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001530#ifdef TEST
1531
Manuel Novoa III cad53642003-03-19 09:13:01 +00001532const char *bb_applet_name = "debug stuff usage";
Eric Andersene5dfced2001-04-09 22:48:12 +00001533const char *memory_exhausted = "Memory exhausted";
1534
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001535#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001536#include <locale.h>
1537#endif
1538
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001539int main(int argc, char **argv)
1540{
1541 char buff[BUFSIZ];
1542 char *prompt =
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001543#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001544 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001545\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001546\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001547#else
1548 "% ";
1549#endif
1550
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001551#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001552 setlocale(LC_ALL, "");
1553#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001554 while(1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001555 int l;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001556 cmdedit_read_input(prompt, buff);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001557 l = strlen(buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001558 if(l==0)
1559 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001560 if(l > 0 && buff[l-1] == '\n')
1561 buff[l-1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001562 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001563 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001564 printf("*** cmdedit_read_input() detect ^C\n");
1565 return 0;
1566}
1567
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001568#endif /* TEST */