blob: 16825089d5e4ef5a4b222bf8a4b862a563498e3c [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 Andersen81fe1232003-07-29 06:38:40 +00005 * Copyright (c) 1986-2003 may safely be consumed by a BSD or GPL license.
Eric Andersen7467c8d2001-07-12 20:26:32 +00006 * Written by: Vladimir Oleynik <dzo@simtreas.ru>
Eric Andersen5f2c79d2001-02-16 18:36:04 +00007 *
8 * Used ideas:
9 * Adam Rogoyski <rogoyski@cs.utexas.edu>
10 * Dave Cinege <dcinege@psychosis.com>
11 * Jakub Jelinek (c) 1995
Eric Andersen81fe1232003-07-29 06:38:40 +000012 * Erik Andersen <andersen@codepoet.org> (Majorly adjusted for busybox)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000013 *
Erik Andersen13456d12000-03-16 08:09:57 +000014 * This code is 'as is' with no warranty.
Erik Andersen13456d12000-03-16 08:09:57 +000015 *
Erik Andersen13456d12000-03-16 08:09:57 +000016 *
17 */
18
19/*
20 Usage and Known bugs:
21 Terminal key codes are not extensive, and more will probably
22 need to be added. This version was created on Debian GNU/Linux 2.x.
23 Delete, Backspace, Home, End, and the arrow keys were tested
24 to work in an Xterm and console. Ctrl-A also works as Home.
Mark Whitley4e338752001-01-26 20:42:23 +000025 Ctrl-E also works as End.
Erik Andersen13456d12000-03-16 08:09:57 +000026
Eric Andersen5f2c79d2001-02-16 18:36:04 +000027 Small bugs (simple effect):
28 - not true viewing if terminal size (x*y symbols) less
29 size (prompt + editor`s line + 2 symbols)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000030 - not true viewing if length prompt less terminal width
Erik Andersen13456d12000-03-16 08:09:57 +000031 */
32
Mark Whitley4e338752001-01-26 20:42:23 +000033
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
Eric Andersenc470f442003-07-28 09:56:35 +000066#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000067
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 Andersenc470f442003-07-28 09:56:35 +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 Andersenc470f442003-07-28 09:56:35 +0000118volatile int cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +0000119static
Eric Andersenc470f442003-07-28 09:56:35 +0000120volatile int handlers_sets = 0; /* Set next bites: */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000121
Mark Whitley4e338752001-01-26 20:42:23 +0000122enum {
Eric Andersenc470f442003-07-28 09:56:35 +0000123 SET_ATEXIT = 1, /* when atexit() has been called
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000124 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 Andersenc470f442003-07-28 09:56:35 +0000130static int cmdedit_x; /* real x terminal position */
131static int cmdedit_y; /* pseudoreal y terminal position */
132static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000133
Eric Andersenc470f442003-07-28 09:56:35 +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 Andersenc470f442003-07-28 09:56:35 +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 Andersenc470f442003-07-28 09:56:35 +0000164#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
165
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{
Eric Andersenc470f442003-07-28 09:56:35 +0000170 static sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000171
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000172 /* emulate || signal call */
173 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Eric Andersen8efe9672003-09-15 08:33:45 +0000174 int width = 0;
175 get_terminal_width_height(0, &width, NULL);
176 cmdedit_setwidth(width, nsig == SIGWINCH);
Mark Whitley4e338752001-01-26 20:42:23 +0000177 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000178 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000179
Eric Andersenc470f442003-07-28 09:56:35 +0000180 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000181 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersenc470f442003-07-28 09:56:35 +0000182 else if (nsig == SIGWINCH) /* signaled called handler */
183 signal(SIGWINCH, win_changed); /* set for next call */
184 else /* nsig == 0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000185 /* set previous handler */
Eric Andersenc470f442003-07-28 09:56:35 +0000186 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000187}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000188
189static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000190{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000191 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000192/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000193 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000194 handlers_sets &= ~SET_RESET_TERM;
195 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000196 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000197 /* reset SIGWINCH handler to previous (default) */
198 win_changed(0);
199 handlers_sets &= ~SET_WCHG_HANDLERS;
200 }
201 fflush(stdout);
Erik Andersen13456d12000-03-16 08:09:57 +0000202}
203
Mark Whitley4e338752001-01-26 20:42:23 +0000204
Mark Whitley4e338752001-01-26 20:42:23 +0000205/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000206static void cmdedit_set_out_char(int next_char)
207{
208
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000209 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000210
211 if (c == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000212 c = ' '; /* destroy end char? */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000213#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000214 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000215 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000216 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000217 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000218 c += '@';
219 if (c == 127)
220 c = '?';
221 printf("\033[7m%c\033[0m", c);
222 } else
223#endif
224 putchar(c);
225 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000226 /* terminal is scrolled down */
227 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000228 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000229
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000230 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000231 next_char = ' ';
232 /* destroy "(auto)margin" */
233 putchar(next_char);
234 putchar('\b');
235 }
236 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000237}
238
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000239/* Move to end line. Bonus: rewrite line from cursor */
240static void input_end(void)
241{
242 while (cursor < len)
243 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000244}
245
246/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000247static void goto_new_line(void)
248{
Mark Whitley4e338752001-01-26 20:42:23 +0000249 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000250 if (cmdedit_x)
251 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000252}
253
254
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000255static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000256{
Robert Grieblb2301592002-07-30 23:13:51 +0000257 if ( s )
258 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000259}
Eric Andersen81fe1232003-07-29 06:38:40 +0000260
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000261static inline void beep(void)
262{
263 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000264}
265
Mark Whitley4e338752001-01-26 20:42:23 +0000266/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000267/* special for slow terminal */
268static void input_backward(int num)
269{
270 if (num > cursor)
271 num = cursor;
Eric Andersenc470f442003-07-28 09:56:35 +0000272 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000273
Eric Andersenc470f442003-07-28 09:56:35 +0000274 if (cmdedit_x >= num) { /* no to up line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000275 cmdedit_x -= num;
276 if (num < 4)
277 while (num-- > 0)
278 putchar('\b');
279
280 else
281 printf("\033[%dD", num);
282 } else {
283 int count_y;
284
285 if (cmdedit_x) {
Eric Andersenc470f442003-07-28 09:56:35 +0000286 putchar('\r'); /* back to first terminal pos. */
287 num -= cmdedit_x; /* set previous backward */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000288 }
289 count_y = 1 + num / cmdedit_termw;
290 printf("\033[%dA", count_y);
291 cmdedit_y -= count_y;
292 /* require forward after uping */
293 cmdedit_x = cmdedit_termw * count_y - num;
Eric Andersenc470f442003-07-28 09:56:35 +0000294 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000295 }
Erik Andersen13456d12000-03-16 08:09:57 +0000296}
297
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000298static void put_prompt(void)
299{
300 out1str(cmdedit_prompt);
Eric Andersenc470f442003-07-28 09:56:35 +0000301 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000302 cursor = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000303 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000304}
305
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000306#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000307static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000308{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000309 cmdedit_prompt = prmt_ptr;
310 cmdedit_prmt_len = strlen(prmt_ptr);
311 put_prompt();
312}
313#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000314static void parse_prompt(const char *prmt_ptr)
315{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000316 int prmt_len = 0;
317 int sub_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000318 char flg_not_length = '[';
319 char *prmt_mem_ptr = xcalloc(1, 1);
320 char *pwd_buf = xgetcwd(0);
321 char buf2[PATH_MAX + 1];
322 char buf[2];
323 char c;
324 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000325
Eric Andersen5f265b72001-05-11 16:58:46 +0000326 if (!pwd_buf) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000327 pwd_buf=(char *)bb_msg_unknown;
Eric Andersen5f265b72001-05-11 16:58:46 +0000328 }
329
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000330 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000331 pbuf = buf;
332 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000333 c = *prmt_ptr++;
334 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000335 const char *cp = prmt_ptr;
336 int l;
Eric Andersenc470f442003-07-28 09:56:35 +0000337
Manuel Novoa III cad53642003-03-19 09:13:01 +0000338 c = bb_process_escape_sequence(&prmt_ptr);
Eric Andersene5dfced2001-04-09 22:48:12 +0000339 if(prmt_ptr==cp) {
340 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000341 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000342 c = *prmt_ptr++;
343 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000344#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000345 case 'u':
346 pbuf = user_buf;
347 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000348#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000349 case 'h':
350 pbuf = hostname_buf;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000351 if (pbuf == 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000352 pbuf = xcalloc(256, 1);
353 if (gethostname(pbuf, 255) < 0) {
354 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000355 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000356 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000357
358 if (s)
359 *s = 0;
360 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000361 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000362 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000363 break;
364 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000365 c = my_euid == 0 ? '#' : '$';
366 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000367#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000368 case 'w':
369 pbuf = pwd_buf;
370 l = strlen(home_pwd_buf);
371 if (home_pwd_buf[0] != 0 &&
372 strncmp(home_pwd_buf, pbuf, l) == 0 &&
373 (pbuf[l]=='/' || pbuf[l]=='\0') &&
374 strlen(pwd_buf+l)<PATH_MAX) {
375 pbuf = buf2;
376 *pbuf = '~';
377 strcpy(pbuf+1, pwd_buf+l);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000378 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000379 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000380#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000381 case 'W':
382 pbuf = pwd_buf;
383 cp = strrchr(pbuf,'/');
384 if ( (cp != NULL) && (cp != pbuf) )
385 pbuf += (cp-pbuf)+1;
386 break;
387 case '!':
388 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
389 break;
390 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000391 c = '\033';
392 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000393 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000394 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000395 int h;
396 buf2[l++] = *prmt_ptr;
397 buf2[l] = 0;
398 h = strtol(buf2, &pbuf, 16);
399 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000400 l--;
401 break;
402 }
403 prmt_ptr++;
404 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000405 buf2[l] = 0;
406 c = (char)strtol(buf2, 0, 16);
407 if(c==0)
408 c = '?';
409 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000410 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000411 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000412 if (c == flg_not_length) {
413 flg_not_length = flg_not_length == '[' ? ']' : '[';
414 continue;
415 }
416 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000417 }
Eric Andersenc470f442003-07-28 09:56:35 +0000418 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000419 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000420 if(pbuf == buf)
421 *pbuf = c;
422 prmt_len += strlen(pbuf);
423 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000424 if (flg_not_length == ']')
425 sub_len++;
426 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000427 if(pwd_buf!=(char *)bb_msg_unknown)
Eric Andersen8f697842001-07-02 15:36:57 +0000428 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000429 cmdedit_prompt = prmt_mem_ptr;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000430 cmdedit_prmt_len = prmt_len - sub_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000431 put_prompt();
432}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000433#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000434
435
436/* draw promt, editor line, and clear tail */
437static void redraw(int y, int back_cursor)
438{
Eric Andersenc470f442003-07-28 09:56:35 +0000439 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000440 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000441 putchar('\r');
442 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000443 input_end(); /* rewrite */
444 printf("\033[J"); /* destroy tail after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000445 input_backward(back_cursor);
446}
447
Erik Andersenf0657d32000-04-12 17:49:52 +0000448/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000449static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000450{
Mark Whitley4e338752001-01-26 20:42:23 +0000451 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000452
Mark Whitley4e338752001-01-26 20:42:23 +0000453 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000454 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000455
456 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000457 len--;
Eric Andersenc470f442003-07-28 09:56:35 +0000458 input_end(); /* rewtite new line */
459 cmdedit_set_out_char(0); /* destroy end char */
460 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000461}
462
Mark Whitley4e338752001-01-26 20:42:23 +0000463/* Delete the char in back of the cursor */
464static void input_backspace(void)
465{
466 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000467 input_backward(1);
468 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000469 }
470}
471
472
Erik Andersenf0657d32000-04-12 17:49:52 +0000473/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000474static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000475{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000476 if (cursor < len)
477 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000478}
479
480
Mark Whitley4e338752001-01-26 20:42:23 +0000481static void cmdedit_setwidth(int w, int redraw_flg)
482{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000483 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000484 if (w <= cmdedit_termw) {
485 cmdedit_termw = cmdedit_termw % w;
486 }
Mark Whitley4e338752001-01-26 20:42:23 +0000487 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000488 cmdedit_termw = w;
489
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000490 if (redraw_flg) {
491 /* new y for current cursor */
492 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000493
494 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000495 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
496 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000497 }
Eric Andersenc470f442003-07-28 09:56:35 +0000498 }
Mark Whitley4e338752001-01-26 20:42:23 +0000499}
500
Eric Andersen7467c8d2001-07-12 20:26:32 +0000501static void cmdedit_init(void)
Mark Whitley4e338752001-01-26 20:42:23 +0000502{
Eric Andersen61173a52001-03-19 17:48:55 +0000503 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000504 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
505 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000506 win_changed(-SIGWINCH);
507 handlers_sets |= SET_WCHG_HANDLERS;
508 }
509
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000510 if ((handlers_sets & SET_ATEXIT) == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000511#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000512 struct passwd *entry;
513
514 my_euid = geteuid();
515 entry = getpwuid(my_euid);
516 if (entry) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000517 user_buf = bb_xstrdup(entry->pw_name);
518 home_pwd_buf = bb_xstrdup(entry->pw_dir);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000519 }
520#endif
521
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000522#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000523
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000524#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000525 my_euid = geteuid();
526#endif
527 my_uid = getuid();
528 my_gid = getgid();
Eric Andersenc470f442003-07-28 09:56:35 +0000529#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000530 handlers_sets |= SET_ATEXIT;
Eric Andersenc470f442003-07-28 09:56:35 +0000531 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000532 }
Mark Whitley4e338752001-01-26 20:42:23 +0000533}
Erik Andersenf0657d32000-04-12 17:49:52 +0000534
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000535#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000536
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000537static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000538{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000539 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
540 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
541 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
542 (st->st_mode & S_IXOTH)) return TRUE;
543 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000544}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000545
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000546#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000547
548static char **username_tab_completion(char *ud, int *num_matches)
549{
550 struct passwd *entry;
551 int userlen;
552 char *temp;
553
554
Eric Andersenc470f442003-07-28 09:56:35 +0000555 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000556 userlen = strlen(ud);
557
Eric Andersenc470f442003-07-28 09:56:35 +0000558 if (num_matches == 0) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000559 char *sav_ud = ud - 1;
560 char *home = 0;
561
Eric Andersenc470f442003-07-28 09:56:35 +0000562 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000563 home = home_pwd_buf;
564 } else {
565 /* "~user/..." */
566 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000567 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000568 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000569 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000570 ud = temp;
571 if (entry)
572 home = entry->pw_dir;
573 }
574 if (home) {
575 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000576 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000577
578 /* /home/user/... */
579 sprintf(temp2, "%s%s", home, ud);
580 strcpy(sav_ud, temp2);
581 }
582 }
Eric Andersenc470f442003-07-28 09:56:35 +0000583 return 0; /* void, result save to argument :-) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000584 } else {
585 /* "~[^/]*" */
586 char **matches = (char **) NULL;
587 int nm = 0;
588
589 setpwent();
590
591 while ((entry = getpwent()) != NULL) {
592 /* Null usernames should result in all users as possible completions. */
593 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
594
Eric Andersenc470f442003-07-28 09:56:35 +0000595 bb_xasprintf(&temp, "~%s/", entry->pw_name);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000596 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
597
598 matches[nm++] = temp;
599 }
600 }
601
602 endpwent();
603 (*num_matches) = nm;
604 return (matches);
605 }
606}
Eric Andersenc470f442003-07-28 09:56:35 +0000607#endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000608
609enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000610 FIND_EXE_ONLY = 0,
611 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000612 FIND_FILE_ONLY = 2,
613};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000614
Mark Whitley4e338752001-01-26 20:42:23 +0000615static int path_parse(char ***p, int flags)
616{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000617 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000618 char *tmp;
619 char *pth;
620
Mark Whitley4e338752001-01-26 20:42:23 +0000621 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000622 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
623 /* PATH=<empty> or PATH=:<empty> */
624 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000625 return 1;
626 }
627
628 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000629 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000630
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000631 for (;;) {
Eric Andersenc470f442003-07-28 09:56:35 +0000632 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000633 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000634 if (tmp) {
635 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000636 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000637 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000638 break;
639 }
640
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000641 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000642
643 tmp = pth;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000644 (*p)[0] = bb_xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000645 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000646
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000647 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000648 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000649 if (tmp) {
Eric Andersenc470f442003-07-28 09:56:35 +0000650 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000651 if (*++tmp == 0)
Eric Andersenc470f442003-07-28 09:56:35 +0000652 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000653 } else
654 break;
Eric Andersenc470f442003-07-28 09:56:35 +0000655 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000656 }
657
658 return npth;
659}
660
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000661static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000662{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000663 int l = 0;
664 char *s = xmalloc((strlen(found) + 1) * 2);
665
666 while (*found) {
667 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
668 s[l++] = '\\';
669 s[l++] = *found++;
670 }
671 s[l] = 0;
672 return s;
673}
674
675static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000676 int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000677{
678
679 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000680 DIR *dir;
681 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000682 char dirbuf[BUFSIZ];
683 int nm = *num_matches;
684 struct stat st;
685 char *path1[1];
686 char **paths = path1;
687 int npaths;
688 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000689 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000690 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000691
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000692 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000693
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000694 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000695 /* no dir, if flags==EXE_ONLY - get paths, else "." */
696 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000697 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000698 } else {
699 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000700 /* save for change */
701 strcpy(dirbuf, command);
702 /* set dir only */
703 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000704#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000705 if (dirbuf[0] == '~') /* ~/... or ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000706 username_tab_completion(dirbuf, 0);
707#endif
708 /* "strip" dirname in command */
709 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000710
Mark Whitley4e338752001-01-26 20:42:23 +0000711 paths[0] = dirbuf;
Eric Andersenc470f442003-07-28 09:56:35 +0000712 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000713 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000714
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000715 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000716
Mark Whitley4e338752001-01-26 20:42:23 +0000717 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000718 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000719 continue;
720
721 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000722 char *str_found = next->d_name;
723
Mark Whitley4e338752001-01-26 20:42:23 +0000724 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000725 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000726 continue;
727 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000728 if (*str_found == '.' && *pfind == 0) {
729 if (*paths[i] == '/' && paths[i][1] == 0
Eric Andersenc470f442003-07-28 09:56:35 +0000730 && str_found[1] == 0) str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000731 else
Mark Whitley4e338752001-01-26 20:42:23 +0000732 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000733 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000734 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000735 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000736 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000737 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000738 /* find with dirs ? */
739 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000740 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000741 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000742 /* name is directory */
Eric Andersene5dfced2001-04-09 22:48:12 +0000743 str_found = found;
744 found = concat_path_file(found, "");
745 free(str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000746 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000747 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000748 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000749 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000750 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000751 str_found = add_quote_for_spec_chars(found);
752 if (type == FIND_FILE_ONLY ||
Matt Kraai1f0c4362001-12-20 23:13:26 +0000753 (type == FIND_EXE_ONLY && is_execute(&st)))
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000754 strcat(str_found, " ");
755 }
Mark Whitley4e338752001-01-26 20:42:23 +0000756 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000757 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
758
759 matches[nm++] = str_found;
Eric Andersene5dfced2001-04-09 22:48:12 +0000760cont:
761 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000762 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000763 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000764 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000765 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000766 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000767 free(paths);
768 }
Mark Whitley4e338752001-01-26 20:42:23 +0000769 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000770 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000771}
Erik Andersenf0657d32000-04-12 17:49:52 +0000772
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000773static int match_compare(const void *a, const void *b)
774{
775 return strcmp(*(char **) a, *(char **) b);
776}
777
778
779
780#define QUOT (UCHAR_MAX+1)
781
782#define collapse_pos(is, in) { \
Eric Andersen889a3012002-03-20 14:31:15 +0000783 memcpy(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
784 memcpy(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000785
786static int find_match(char *matchBuf, int *len_with_quotes)
787{
788 int i, j;
789 int command_mode;
790 int c, c2;
791 int int_buf[BUFSIZ + 1];
792 int pos_buf[BUFSIZ + 1];
793
794 /* set to integer dimension characters and own positions */
795 for (i = 0;; i++) {
796 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
797 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000798 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000799 break;
800 } else
801 pos_buf[i] = i;
802 }
803
804 /* mask \+symbol and convert '\t' to ' ' */
805 for (i = j = 0; matchBuf[i]; i++, j++)
806 if (matchBuf[i] == '\\') {
807 collapse_pos(j, j + 1);
808 int_buf[j] |= QUOT;
809 i++;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000810#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000811 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000812 int_buf[j] = ' ' | QUOT;
813#endif
814 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000815#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000816 else if (matchBuf[i] == '\t')
817 int_buf[j] = ' ';
818#endif
819
820 /* mask "symbols" or 'symbols' */
821 c2 = 0;
822 for (i = 0; int_buf[i]; i++) {
823 c = int_buf[i];
824 if (c == '\'' || c == '"') {
825 if (c2 == 0)
826 c2 = c;
827 else {
828 if (c == c2)
829 c2 = 0;
830 else
831 int_buf[i] |= QUOT;
832 }
833 } else if (c2 != 0 && c != '$')
834 int_buf[i] |= QUOT;
835 }
836
837 /* skip commands with arguments if line have commands delimiters */
838 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
839 for (i = 0; int_buf[i]; i++) {
840 c = int_buf[i];
841 c2 = int_buf[i + 1];
842 j = i ? int_buf[i - 1] : -1;
843 command_mode = 0;
844 if (c == ';' || c == '&' || c == '|') {
845 command_mode = 1 + (c == c2);
846 if (c == '&') {
847 if (j == '>' || j == '<')
848 command_mode = 0;
849 } else if (c == '|' && j == '>')
850 command_mode = 0;
851 }
852 if (command_mode) {
853 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000854 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000855 }
856 }
857 /* collapse `command...` */
858 for (i = 0; int_buf[i]; i++)
859 if (int_buf[i] == '`') {
860 for (j = i + 1; int_buf[j]; j++)
861 if (int_buf[j] == '`') {
862 collapse_pos(i, j + 1);
863 j = 0;
864 break;
865 }
866 if (j) {
867 /* not found close ` - command mode, collapse all previous */
868 collapse_pos(0, i + 1);
869 break;
870 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000871 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000872 }
873
874 /* collapse (command...(command...)...) or {command...{command...}...} */
Eric Andersenc470f442003-07-28 09:56:35 +0000875 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000876 c2 = 0;
877 for (i = 0; int_buf[i]; i++)
878 if (int_buf[i] == '(' || int_buf[i] == '{') {
879 if (int_buf[i] == '(')
880 c++;
881 else
882 c2++;
883 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000884 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000885 }
886 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
887 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
888 if (int_buf[i] == ')')
889 c--;
890 else
891 c2--;
892 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000893 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000894 }
895
896 /* skip first not quote space */
897 for (i = 0; int_buf[i]; i++)
898 if (int_buf[i] != ' ')
899 break;
900 if (i)
901 collapse_pos(0, i);
902
903 /* set find mode for completion */
904 command_mode = FIND_EXE_ONLY;
905 for (i = 0; int_buf[i]; i++)
906 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
907 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000908 && matchBuf[pos_buf[0]]=='c'
909 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000910 command_mode = FIND_DIR_ONLY;
911 else {
912 command_mode = FIND_FILE_ONLY;
913 break;
914 }
915 }
916 /* "strlen" */
917 for (i = 0; int_buf[i]; i++);
918 /* find last word */
919 for (--i; i >= 0; i--) {
920 c = int_buf[i];
921 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
922 collapse_pos(0, i + 1);
923 break;
924 }
925 }
926 /* skip first not quoted '\'' or '"' */
927 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
928 /* collapse quote or unquote // or /~ */
Eric Andersenc470f442003-07-28 09:56:35 +0000929 while ((int_buf[i] & ~QUOT) == '/' &&
Mark Whitley7e5291f2001-03-08 19:31:12 +0000930 ((int_buf[i + 1] & ~QUOT) == '/'
931 || (int_buf[i + 1] & ~QUOT) == '~')) {
932 i++;
933 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000934
935 /* set only match and destroy quotes */
936 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000937 for (c = 0; pos_buf[i] >= 0; i++) {
938 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000939 j = pos_buf[i] + 1;
940 }
Eric Andersen4f990532001-05-31 17:15:57 +0000941 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000942 /* old lenght matchBuf with quotes symbols */
943 *len_with_quotes = j ? j - pos_buf[0] : 0;
944
945 return command_mode;
946}
947
Glenn L McGrath4d001292003-01-06 01:11:50 +0000948/*
949 display by column original ideas from ls applet,
950 very optimize by my :)
951*/
952static void showfiles(char **matches, int nfiles)
953{
954 int ncols, row;
955 int column_width = 0;
956 int nrows = nfiles;
957
958 /* find the longest file name- use that as the column width */
959 for (row = 0; row < nrows; row++) {
960 int l = strlen(matches[row]);
961
962 if (column_width < l)
963 column_width = l;
964 }
965 column_width += 2; /* min space for columns */
966 ncols = cmdedit_termw / column_width;
967
968 if (ncols > 1) {
969 nrows /= ncols;
970 if(nfiles % ncols)
971 nrows++; /* round up fractionals */
972 column_width = -column_width; /* for printf("%-Ns", ...); */
973 } else {
974 ncols = 1;
975 }
976 for (row = 0; row < nrows; row++) {
977 int n = row;
978 int nc;
979
980 for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++)
981 printf("%*s", column_width, matches[n]);
982 printf("%s\n", matches[n]);
983 }
984}
985
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000986
987static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000988{
989 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000990 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +0000991 static char **matches;
992
Eric Andersenc470f442003-07-28 09:56:35 +0000993 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +0000994 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000995 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +0000996 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000997 free(matches);
998 matches = (char **) NULL;
999 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001000 return;
1001 }
Matt Kraai1f0c4362001-12-20 23:13:26 +00001002 if (! *lastWasTab) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001003
1004 char *tmp;
1005 int len_found;
1006 char matchBuf[BUFSIZ];
1007 int find_type;
1008 int recalc_pos;
1009
Eric Andersenc470f442003-07-28 09:56:35 +00001010 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001011
1012 /* Make a local copy of the string -- up
1013 * to the position of the cursor */
1014 tmp = strncpy(matchBuf, command_ps, cursor);
1015 tmp[cursor] = 0;
1016
1017 find_type = find_match(matchBuf, &recalc_pos);
1018
1019 /* Free up any memory already allocated */
1020 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001021
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001022#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001023 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001024 * then try completing this word as a username. */
1025
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001026 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001027 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001028#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001029 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001030 * in the current working directory that matches. */
1031 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001032 matches =
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001033 exe_n_cwd_tab_completion(matchBuf,
1034 &num_matches, find_type);
1035 /* Remove duplicate found */
1036 if(matches) {
1037 int i, j;
1038 /* bubble */
1039 for(i=0; i<(num_matches-1); i++)
1040 for(j=i+1; j<num_matches; j++)
1041 if(matches[i]!=0 && matches[j]!=0 &&
1042 strcmp(matches[i], matches[j])==0) {
1043 free(matches[j]);
1044 matches[j]=0;
1045 }
1046 j=num_matches;
1047 num_matches = 0;
1048 for(i=0; i<j; i++)
1049 if(matches[i]) {
1050 if(!strcmp(matches[i], "./"))
1051 matches[i][1]=0;
1052 else if(!strcmp(matches[i], "../"))
1053 matches[i][2]=0;
1054 matches[num_matches++]=matches[i];
1055 }
1056 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001057 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001058 if (!matches || num_matches > 1) {
1059 char *tmp1;
1060
Mark Whitley4e338752001-01-26 20:42:23 +00001061 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001062 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +00001063 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001064 /* sort */
1065 qsort(matches, num_matches, sizeof(char *), match_compare);
1066
1067 /* find minimal match */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001068 tmp = bb_xstrdup(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001069 for (tmp1 = tmp; *tmp1; tmp1++)
1070 for (len_found = 1; len_found < num_matches; len_found++)
1071 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1072 *tmp1 = 0;
1073 break;
1074 }
Eric Andersenc470f442003-07-28 09:56:35 +00001075 if (*tmp == 0) { /* have unique */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001076 free(tmp);
1077 return;
1078 }
Eric Andersenc470f442003-07-28 09:56:35 +00001079 } else { /* one match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001080 tmp = matches[0];
1081 /* for next completion current found */
1082 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001083 }
1084
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001085 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001086 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001087 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001088
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001089 /* before word for match */
1090 command_ps[cursor - recalc_pos] = 0;
1091 /* save tail line */
1092 strcpy(matchBuf, command_ps + cursor);
1093 /* add match */
1094 strcat(command_ps, tmp);
1095 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001096 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001097 /* back to begin word for match */
1098 input_backward(recalc_pos);
1099 /* new pos */
1100 recalc_pos = cursor + len_found;
1101 /* new len */
1102 len = strlen(command_ps);
1103 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001104 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001105 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001106 if (tmp != matches[0])
1107 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();
Glenn L McGrath4d001292003-01-06 01:11:50 +00001117 showfiles(matches, num_matches);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001118 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001119 }
1120 }
1121}
Eric Andersenc470f442003-07-28 09:56:35 +00001122#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001123
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001124#if MAX_HISTORY >= 1
1125static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001126{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001127 if(command_ps[0] != 0 || history[cur_history] == 0) {
1128 free(history[cur_history]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001129 history[cur_history] = bb_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 */
1140 return (cur_history = ch+1);
1141 } else {
1142 beep();
1143 return 0;
1144 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001145}
Robert Griebl350d26b2002-12-03 22:45:46 +00001146
Robert Griebl350d26b2002-12-03 22:45:46 +00001147#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001148extern void load_history ( const char *fromfile )
1149{
Robert Griebl350d26b2002-12-03 22:45:46 +00001150 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001151 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001152
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001153 /* cleanup old */
1154
1155 for(hi = n_history; hi > 0; ) {
1156 hi--;
1157 free ( history [hi] );
Robert Griebl350d26b2002-12-03 22:45:46 +00001158 }
1159
1160 if (( fp = fopen ( fromfile, "r" ))) {
Eric Andersenc470f442003-07-28 09:56:35 +00001161
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001162 for ( hi = 0; hi < MAX_HISTORY; ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001163 char * hl = bb_get_chomped_line_from_file(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001164 int l;
1165
1166 if(!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001167 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001168 l = strlen(hl);
1169 if(l >= BUFSIZ)
1170 hl[BUFSIZ-1] = 0;
1171 if(l == 0 || hl[0] == ' ') {
1172 free(hl);
1173 continue;
1174 }
1175 history [hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001176 }
1177 fclose ( fp );
1178 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001179 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001180}
1181
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001182extern void save_history ( const char *tofile )
Robert Griebl350d26b2002-12-03 22:45:46 +00001183{
Robert Griebl350d26b2002-12-03 22:45:46 +00001184 FILE *fp = fopen ( tofile, "w" );
Eric Andersenc470f442003-07-28 09:56:35 +00001185
Robert Griebl350d26b2002-12-03 22:45:46 +00001186 if ( fp ) {
1187 int i;
Eric Andersenc470f442003-07-28 09:56:35 +00001188
Robert Griebl350d26b2002-12-03 22:45:46 +00001189 for ( i = 0; i < n_history; i++ ) {
1190 fputs ( history [i], fp );
1191 fputc ( '\n', fp );
1192 }
1193 fclose ( fp );
1194 }
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
1209 * a string with full command editing (sortof like
1210 * 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 *
1219 * Furthermore, the "vi" command editing keys are not implemented.
1220 *
Erik Andersen6273f652000-03-17 01:12:41 +00001221 */
Eric Andersenc470f442003-07-28 09:56:35 +00001222
Eric Andersen044228d2001-07-17 01:12:36 +00001223
1224int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001225{
1226
Erik Andersenc7c634b2000-03-19 05:28:55 +00001227 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001228 int lastWasTab = FALSE;
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001229 unsigned char c = 0;
Erik Andersen13456d12000-03-16 08:09:57 +00001230
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001231 /* prepare before init handlers */
Eric Andersenc470f442003-07-28 09:56:35 +00001232 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001233 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001234 command_ps = command;
1235
Eric Andersen34506362001-08-02 05:02:46 +00001236 getTermSettings(0, (void *) &initial_settings);
1237 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1238 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1239 /* Turn off echoing and CTRL-C, so we can trap it */
1240 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001241#ifndef linux
Eric Andersen34506362001-08-02 05:02:46 +00001242 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1243 new_settings.c_cc[VMIN] = 1;
1244 new_settings.c_cc[VTIME] = 0;
1245 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001246# ifndef _POSIX_VDISABLE
1247# define _POSIX_VDISABLE '\0'
1248# endif
Eric Andersenc470f442003-07-28 09:56:35 +00001249 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001250#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001251 command[0] = 0;
1252
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001253 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001254 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001255
Eric Andersen6faae7d2001-02-16 20:09:17 +00001256 /* Now initialize things */
1257 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001258 /* Print out the command prompt */
1259 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001260
Erik Andersenc7c634b2000-03-19 05:28:55 +00001261 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001262
Eric Andersenc470f442003-07-28 09:56:35 +00001263 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001264
Eric Andersen7467c8d2001-07-12 20:26:32 +00001265 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001266 /* if we can't read input then exit */
1267 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001268
Erik Andersen13456d12000-03-16 08:09:57 +00001269 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001270 case '\n':
1271 case '\r':
1272 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001273 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001274 break_out = 1;
1275 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001276 case 1:
1277 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001278 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001279 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001280 case 2:
1281 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001282 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001283 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001284 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001285 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001286 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001287 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001288 len = 0;
Eric Andersenc470f442003-07-28 09:56:35 +00001289#if !defined(CONFIG_ASH)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001290 lastWasTab = FALSE;
1291 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +00001292#else
1293 break_out = 2;
1294#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001295 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001296 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001297 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001298 * if the len=0 and no chars to delete */
1299 if (len == 0) {
Eric Andersened424db2001-04-23 15:28:28 +00001300prepare_to_die:
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001301#if !defined(CONFIG_ASH)
Mark Whitley4e338752001-01-26 20:42:23 +00001302 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001303 goto_new_line();
1304 /* cmdedit_reset_term() called in atexit */
1305 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001306#else
1307 break_out = -1; /* for control stoped jobs */
1308 break;
1309#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001310 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001311 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001312 }
1313 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001314 case 5:
1315 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001316 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001317 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001318 case 6:
1319 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001320 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001321 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001322 case '\b':
1323 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001324 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001325 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001326 break;
1327 case '\t':
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001328#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001329 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001330#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001331 break;
Eric Andersen65a07302002-04-13 13:26:49 +00001332 case 11:
Eric Andersenc470f442003-07-28 09:56:35 +00001333 /* Control-k -- clear to end of line */
Eric Andersen65a07302002-04-13 13:26:49 +00001334 *(command + cursor) = 0;
1335 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001336 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001337 break;
Eric Andersenc470f442003-07-28 09:56:35 +00001338 case 12:
Eric Andersen65a07302002-04-13 13:26:49 +00001339 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001340 printf("\033[H");
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001341 redraw(0, len-cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001342 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001343#if MAX_HISTORY >= 1
Erik Andersenf0657d32000-04-12 17:49:52 +00001344 case 14:
1345 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001346 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001347 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001348 break;
1349 case 16:
1350 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001351 if (cur_history > 0) {
1352 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001353 goto rewrite_line;
1354 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001355 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001356 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001357 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001358#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001359 case 21:
1360 /* Control-U -- Clear line before cursor */
1361 if (cursor) {
1362 strcpy(command, command + cursor);
1363 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001364 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001365 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001366 case ESC:{
1367 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001368 if (safe_read(0, &c, 1) < 1)
1369 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001370 /* different vt100 emulations */
1371 if (c == '[' || c == 'O') {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001372 if (safe_read(0, &c, 1) < 1)
1373 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001374 }
1375 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001376#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001377 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001378
1379 input_tab(&lastWasTab);
1380 break;
1381#endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001382#if MAX_HISTORY >= 1
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001383 case 'A':
1384 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001385 if (cur_history > 0) {
1386 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001387 goto rewrite_line;
1388 } else {
1389 beep();
1390 }
1391 break;
1392 case 'B':
1393 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001394 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001395 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001396 /* Rewrite the line with the selected history item */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001397rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001398 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001399 len = strlen(strcpy(command, history[cur_history]));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001400 /* redraw and go to end line */
1401 redraw(cmdedit_y, 0);
1402 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001403#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001404 case 'C':
1405 /* Right Arrow -- Move forward one character */
1406 input_forward();
1407 break;
1408 case 'D':
1409 /* Left Arrow -- Move back one character */
1410 input_backward(1);
1411 break;
1412 case '3':
1413 /* Delete */
1414 input_delete();
1415 break;
1416 case '1':
1417 case 'H':
1418 /* Home (Ctrl-A) */
1419 input_backward(cursor);
1420 break;
1421 case '4':
1422 case 'F':
1423 /* End (Ctrl-E) */
1424 input_end();
1425 break;
1426 default:
1427 if (!(c >= '1' && c <= '9'))
1428 c = 0;
1429 beep();
1430 }
1431 if (c >= '1' && c <= '9')
1432 do
Eric Andersen7467c8d2001-07-12 20:26:32 +00001433 if (safe_read(0, &c, 1) < 1)
1434 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001435 while (c != '~');
1436 break;
1437 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001438
Eric Andersenc470f442003-07-28 09:56:35 +00001439 default: /* If it's regular input, do the normal thing */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001440#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001441 /* Control-V -- Add non-printable symbol */
1442 if (c == 22) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001443 if (safe_read(0, &c, 1) < 1)
1444 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001445 if (c == 0) {
1446 beep();
1447 break;
1448 }
1449 } else
1450#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001451 if (!Isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001452 break;
1453
Eric Andersenc470f442003-07-28 09:56:35 +00001454 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001455 break;
1456
1457 len++;
1458
Eric Andersenc470f442003-07-28 09:56:35 +00001459 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001460 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001461 *(command + cursor + 1) = 0;
1462 cmdedit_set_out_char(0);
Eric Andersenc470f442003-07-28 09:56:35 +00001463 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001464 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001465
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001466 memmove(command + sc + 1, command + sc, len - sc);
1467 *(command + sc) = c;
1468 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001469 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001470 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001471 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001472 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001473 }
1474
Erik Andersenc7c634b2000-03-19 05:28:55 +00001475 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001476 }
Eric Andersenc470f442003-07-28 09:56:35 +00001477 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001478 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001479
1480 if (c != '\t')
1481 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001482 }
1483
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001484 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001485 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001486
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001487#if MAX_HISTORY >= 1
Erik Andersenc7c634b2000-03-19 05:28:55 +00001488 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001489 /* cleanup may be saved current command line */
1490 free(history[MAX_HISTORY]);
1491 history[MAX_HISTORY] = 0;
Eric Andersenc470f442003-07-28 09:56:35 +00001492 if (len) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001493 int i = n_history;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001494 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001495 if (i >= MAX_HISTORY) {
1496 free(history[0]);
1497 for(i = 0; i < (MAX_HISTORY-1); i++)
1498 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001499 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001500 history[i++] = bb_xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001501 cur_history = i;
1502 n_history = i;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001503#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001504 num_ok_lines++;
1505#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001506 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001507#else /* MAX_HISTORY < 1 */
1508#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
1509 if (len) { /* no put empty line */
1510 num_ok_lines++;
1511 }
1512#endif
1513#endif /* MAX_HISTORY >= 1 */
Eric Andersenc470f442003-07-28 09:56:35 +00001514 if(break_out == 1) {
1515 command[len++] = '\n'; /* set '\n' */
1516 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001517 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001518#if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersenc470f442003-07-28 09:56:35 +00001519 input_tab(0); /* strong free */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001520#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001521#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001522 free(cmdedit_prompt);
1523#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001524 cmdedit_reset_term();
Eric Andersenc470f442003-07-28 09:56:35 +00001525#if !defined(CONFIG_ASH)
Eric Andersen044228d2001-07-17 01:12:36 +00001526 return len;
Eric Andersenc470f442003-07-28 09:56:35 +00001527#else
1528 return break_out < 0 ? break_out : len;
1529#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001530}
1531
Eric Andersen7467c8d2001-07-12 20:26:32 +00001532
1533
Eric Andersenc470f442003-07-28 09:56:35 +00001534#endif /* CONFIG_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001535
1536
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001537#ifdef TEST
1538
Manuel Novoa III cad53642003-03-19 09:13:01 +00001539const char *bb_applet_name = "debug stuff usage";
Eric Andersene5dfced2001-04-09 22:48:12 +00001540const char *memory_exhausted = "Memory exhausted";
1541
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001542#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001543#include <locale.h>
1544#endif
1545
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001546int main(int argc, char **argv)
1547{
1548 char buff[BUFSIZ];
1549 char *prompt =
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001550#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001551 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001552\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001553\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001554#else
1555 "% ";
1556#endif
1557
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001558#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001559 setlocale(LC_ALL, "");
1560#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001561 while(1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001562 int l;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001563 cmdedit_read_input(prompt, buff);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001564 l = strlen(buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001565 if(l==0)
1566 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001567 if(l > 0 && buff[l-1] == '\n')
1568 buff[l-1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001569 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001570 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001571 printf("*** cmdedit_read_input() detect ^C\n");
1572 return 0;
1573}
1574
Eric Andersenc470f442003-07-28 09:56:35 +00001575#endif /* TEST */