blob: 4f124d6475989e4e0bc804e35c8669000455ae6f [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.
6 * Written by: Vladimir Oleynik <vodz@usa.net>
7 *
8 * Used ideas:
9 * Adam Rogoyski <rogoyski@cs.utexas.edu>
10 * Dave Cinege <dcinege@psychosis.com>
11 * Jakub Jelinek (c) 1995
12 * Erik Andersen <andersee@debian.org> (Majorly adjusted for busybox)
13 *
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 Andersene5dfced2001-04-09 22:48:12 +000046#ifdef BB_LOCALE_SUPPORT
47#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 Andersenb3d6e2d2001-03-13 22:57:56 +000058#define BB_FEATURE_COMMAND_EDITING
59#define BB_FEATURE_COMMAND_TAB_COMPLETION
60#define BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen94456f52001-02-18 20:26:48 +000061#define BB_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen94456f52001-02-18 20:26:48 +000062#define BB_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 Andersenb3d6e2d2001-03-13 22:57:56 +000068#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5165fbe2001-02-20 06:42:29 +000069#include <dirent.h>
70#include <sys/stat.h>
71#endif
72
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000073#ifdef BB_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000074
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000075#ifndef BB_FEATURE_COMMAND_TAB_COMPLETION
76#undef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +000077#endif
78
Eric Andersen004015e2001-05-21 20:30:51 +000079#if defined(BB_FEATURE_COMMAND_USERNAME_COMPLETION) || defined(BB_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000080#define BB_FEATURE_GETUSERNAME_AND_HOMEDIR
81#endif
82
Eric Andersen5f2c79d2001-02-16 18:36:04 +000083#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
Glenn L McGrath78b0e372001-06-26 02:06:08 +000084# ifndef TEST
85# include "pwd_grp/pwd.h"
86# 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
93struct history {
94 char *s;
95 struct history *p;
96 struct history *n;
Mark Whitley59ab0252001-01-23 22:30:04 +000097};
98
Eric Andersen5f2c79d2001-02-16 18:36:04 +000099/* Maximum length of the linked list for the command line history */
100static const int MAX_HISTORY = 15;
Erik Andersen13456d12000-03-16 08:09:57 +0000101
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000102/* First element in command line list */
103static struct history *his_front = NULL;
104
105/* Last element in command line list */
106static struct history *his_end = NULL;
107
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 */
119static int history_counter = 0; /* Number of commands in history list */
Mark Whitley4e338752001-01-26 20:42:23 +0000120static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000121volatile int handlers_sets = 0; /* Set next bites: */
122
Mark Whitley4e338752001-01-26 20:42:23 +0000123enum {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000124 SET_ATEXIT = 1, /* when atexit() has been called
125 and get euid,uid,gid to fast compare */
126 SET_TERM_HANDLERS = 2, /* set many terminates signal handlers */
127 SET_WCHG_HANDLERS = 4, /* winchg signal handler */
128 SET_RESET_TERM = 8, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000129};
Erik Andersen13456d12000-03-16 08:09:57 +0000130
Mark Whitley4e338752001-01-26 20:42:23 +0000131
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000132static int cmdedit_x; /* real x terminal position */
133static int cmdedit_y; /* pseudoreal y terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000134static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000135
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000136static int cursor; /* required global for signal handler */
137static int len; /* --- "" - - "" - -"- --""-- --""--- */
138static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000139static
Eric Andersen004015e2001-05-21 20:30:51 +0000140#ifndef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000141 const
142#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000143char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000144
145/* Link into lash to reset context to 0 on ^C and such */
Eric Andersen86349772000-12-18 20:25:50 +0000146extern unsigned int shell_context;
147
Erik Andersen13456d12000-03-16 08:09:57 +0000148
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000149#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
150static char *user_buf = "";
151static char *home_pwd_buf = "";
152static int my_euid;
153#endif
154
Eric Andersen004015e2001-05-21 20:30:51 +0000155#ifdef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000156static char *hostname_buf = "";
157static int num_ok_lines = 1;
158#endif
159
160
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000161#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000162
163#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
164static int my_euid;
165#endif
166
167static int my_uid;
168static int my_gid;
169
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000170#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000171
Erik Andersen13456d12000-03-16 08:09:57 +0000172
Mark Whitley4e338752001-01-26 20:42:23 +0000173static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000174
Mark Whitley4e338752001-01-26 20:42:23 +0000175static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000176{
177 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000178 static __sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000179
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000180 /* emulate || signal call */
181 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Mark Whitley4e338752001-01-26 20:42:23 +0000182 ioctl(0, TIOCGWINSZ, &win);
183 if (win.ws_col > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000184 cmdedit_setwidth(win.ws_col, nsig == SIGWINCH);
185 }
Mark Whitley4e338752001-01-26 20:42:23 +0000186 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000187 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000188
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000189 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000190 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000191 else if (nsig == SIGWINCH) /* signaled called handler */
192 signal(SIGWINCH, win_changed); /* set for next call */
193 else /* nsig == 0 */
194 /* set previous handler */
195 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000196}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000197
198static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000199{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000200 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000201/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000202 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000203 handlers_sets &= ~SET_RESET_TERM;
204 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000205 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000206 /* reset SIGWINCH handler to previous (default) */
207 win_changed(0);
208 handlers_sets &= ~SET_WCHG_HANDLERS;
209 }
210 fflush(stdout);
Eric Andersenb040d4f2000-07-25 18:01:20 +0000211#ifdef BB_FEATURE_CLEAN_UP
212 if (his_front) {
213 struct history *n;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000214
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000215 while (his_front != his_end) {
Eric Andersenb040d4f2000-07-25 18:01:20 +0000216 n = his_front->n;
217 free(his_front->s);
218 free(his_front);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000219 his_front = n;
Eric Andersenb040d4f2000-07-25 18:01:20 +0000220 }
221 }
222#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000223}
224
Mark Whitley4e338752001-01-26 20:42:23 +0000225
Mark Whitley4e338752001-01-26 20:42:23 +0000226/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000227static void cmdedit_set_out_char(int next_char)
228{
229
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000230 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000231
232 if (c == 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000233 c = ' '; /* destroy end char? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000234#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersene5dfced2001-04-09 22:48:12 +0000235 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000236 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000237 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000238 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000239 c += '@';
240 if (c == 127)
241 c = '?';
242 printf("\033[7m%c\033[0m", c);
243 } else
244#endif
245 putchar(c);
246 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000247 /* terminal is scrolled down */
248 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000249 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000250
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000251 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000252 next_char = ' ';
253 /* destroy "(auto)margin" */
254 putchar(next_char);
255 putchar('\b');
256 }
257 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000258}
259
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000260/* Move to end line. Bonus: rewrite line from cursor */
261static void input_end(void)
262{
263 while (cursor < len)
264 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000265}
266
267/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000268static void goto_new_line(void)
269{
Mark Whitley4e338752001-01-26 20:42:23 +0000270 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000271 if (cmdedit_x)
272 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000273}
274
275
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000276static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000277{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000278 fputs(s, stdout);
279}
280static inline void beep(void)
281{
282 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000283}
284
Mark Whitley4e338752001-01-26 20:42:23 +0000285/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000286/* special for slow terminal */
287static void input_backward(int num)
288{
289 if (num > cursor)
290 num = cursor;
Eric Andersene5dfced2001-04-09 22:48:12 +0000291 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000292
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000293 if (cmdedit_x >= num) { /* no to up line */
294 cmdedit_x -= num;
295 if (num < 4)
296 while (num-- > 0)
297 putchar('\b');
298
299 else
300 printf("\033[%dD", num);
301 } else {
302 int count_y;
303
304 if (cmdedit_x) {
305 putchar('\r'); /* back to first terminal pos. */
306 num -= cmdedit_x; /* set previous backward */
307 }
308 count_y = 1 + num / cmdedit_termw;
309 printf("\033[%dA", count_y);
310 cmdedit_y -= count_y;
311 /* require forward after uping */
312 cmdedit_x = cmdedit_termw * count_y - num;
313 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000314 }
Erik Andersen13456d12000-03-16 08:09:57 +0000315}
316
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000317static void put_prompt(void)
318{
319 out1str(cmdedit_prompt);
320 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
321 cursor = 0;
322}
323
Eric Andersen004015e2001-05-21 20:30:51 +0000324#ifndef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000325static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000326{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000327 cmdedit_prompt = prmt_ptr;
328 cmdedit_prmt_len = strlen(prmt_ptr);
329 put_prompt();
330}
331#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000332static void parse_prompt(const char *prmt_ptr)
333{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000334 int prmt_len = 0;
335 int sub_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000336 char flg_not_length = '[';
337 char *prmt_mem_ptr = xcalloc(1, 1);
338 char *pwd_buf = xgetcwd(0);
339 char buf2[PATH_MAX + 1];
340 char buf[2];
341 char c;
342 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000343
Eric Andersen5f265b72001-05-11 16:58:46 +0000344 if (!pwd_buf) {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000345 pwd_buf=(char *)unknown;
Eric Andersen5f265b72001-05-11 16:58:46 +0000346 }
347
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000348 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000349 pbuf = buf;
350 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000351 c = *prmt_ptr++;
352 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000353 const char *cp = prmt_ptr;
354 int l;
355
356 c = process_escape_sequence(&prmt_ptr);
357 if(prmt_ptr==cp) {
358 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000359 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000360 c = *prmt_ptr++;
361 switch (c) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000362#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000363 case 'u':
364 pbuf = user_buf;
365 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000366#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000367 case 'h':
368 pbuf = hostname_buf;
369 if (*pbuf == 0) {
370 pbuf = xcalloc(256, 1);
371 if (gethostname(pbuf, 255) < 0) {
372 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000373 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000374 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000375
376 if (s)
377 *s = 0;
378 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000379 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000380 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000381 break;
382 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000383 c = my_euid == 0 ? '#' : '$';
384 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000385#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000386 case 'w':
387 pbuf = pwd_buf;
388 l = strlen(home_pwd_buf);
389 if (home_pwd_buf[0] != 0 &&
390 strncmp(home_pwd_buf, pbuf, l) == 0 &&
391 (pbuf[l]=='/' || pbuf[l]=='\0') &&
392 strlen(pwd_buf+l)<PATH_MAX) {
393 pbuf = buf2;
394 *pbuf = '~';
395 strcpy(pbuf+1, pwd_buf+l);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000396 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000397 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000398#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000399 case 'W':
400 pbuf = pwd_buf;
401 cp = strrchr(pbuf,'/');
402 if ( (cp != NULL) && (cp != pbuf) )
403 pbuf += (cp-pbuf)+1;
404 break;
405 case '!':
406 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
407 break;
408 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000409 c = '\033';
410 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000411 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000412 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000413 int h;
414 buf2[l++] = *prmt_ptr;
415 buf2[l] = 0;
416 h = strtol(buf2, &pbuf, 16);
417 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000418 l--;
419 break;
420 }
421 prmt_ptr++;
422 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000423 buf2[l] = 0;
424 c = (char)strtol(buf2, 0, 16);
425 if(c==0)
426 c = '?';
427 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000428 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000429 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430 if (c == flg_not_length) {
431 flg_not_length = flg_not_length == '[' ? ']' : '[';
432 continue;
433 }
434 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000435 }
436 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000437 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000438 if(pbuf == buf)
439 *pbuf = c;
440 prmt_len += strlen(pbuf);
441 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000442 if (flg_not_length == ']')
443 sub_len++;
444 }
Eric Andersen8f697842001-07-02 15:36:57 +0000445 if(pwd_buf!=(char *)unknown)
446 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000447 cmdedit_prompt = prmt_mem_ptr;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000448 cmdedit_prmt_len = prmt_len - sub_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000449 put_prompt();
450}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000451#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000452
453
454/* draw promt, editor line, and clear tail */
455static void redraw(int y, int back_cursor)
456{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000457 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000458 printf("\033[%dA", y);
459 cmdedit_y = 0; /* new quasireal y */
460 putchar('\r');
461 put_prompt();
462 input_end(); /* rewrite */
463 printf("\033[J"); /* destroy tail after cursor */
464 input_backward(back_cursor);
465}
466
Erik Andersenf0657d32000-04-12 17:49:52 +0000467/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000468static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000469{
Mark Whitley4e338752001-01-26 20:42:23 +0000470 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000471
Mark Whitley4e338752001-01-26 20:42:23 +0000472 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000473 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000474
475 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000476 len--;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000477 input_end(); /* rewtite new line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000478 cmdedit_set_out_char(0); /* destroy end char */
479 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000480}
481
Mark Whitley4e338752001-01-26 20:42:23 +0000482/* Delete the char in back of the cursor */
483static void input_backspace(void)
484{
485 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000486 input_backward(1);
487 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000488 }
489}
490
491
Erik Andersenf0657d32000-04-12 17:49:52 +0000492/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000493static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000494{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000495 if (cursor < len)
496 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000497}
498
499
Mark Whitley4e338752001-01-26 20:42:23 +0000500static void clean_up_and_die(int sig)
501{
502 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000503 if (sig != SIGINT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000504 exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */
Mark Whitley4e338752001-01-26 20:42:23 +0000505 cmdedit_reset_term();
506}
507
508static void cmdedit_setwidth(int w, int redraw_flg)
509{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000510 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000511 if (w <= cmdedit_termw) {
512 cmdedit_termw = cmdedit_termw % w;
513 }
Mark Whitley4e338752001-01-26 20:42:23 +0000514 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000515 cmdedit_termw = w;
516
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000517 if (redraw_flg) {
518 /* new y for current cursor */
519 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000520
521 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000522 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
523 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000524 }
Eric Andersen6faae7d2001-02-16 20:09:17 +0000525 }
Mark Whitley4e338752001-01-26 20:42:23 +0000526}
527
528extern void cmdedit_init(void)
529{
Eric Andersen61173a52001-03-19 17:48:55 +0000530 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000531 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
532 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000533 win_changed(-SIGWINCH);
534 handlers_sets |= SET_WCHG_HANDLERS;
535 }
536
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000537 if ((handlers_sets & SET_ATEXIT) == 0) {
538#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
539 struct passwd *entry;
540
541 my_euid = geteuid();
542 entry = getpwuid(my_euid);
543 if (entry) {
544 user_buf = xstrdup(entry->pw_name);
545 home_pwd_buf = xstrdup(entry->pw_dir);
546 }
547#endif
548
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000549#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000550
551#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
552 my_euid = geteuid();
553#endif
554 my_uid = getuid();
555 my_gid = getgid();
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000556#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000557 handlers_sets |= SET_ATEXIT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000558 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000559 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000560
561 if ((handlers_sets & SET_TERM_HANDLERS) == 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000562 signal(SIGKILL, clean_up_and_die);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000563 signal(SIGINT, clean_up_and_die);
Mark Whitley4e338752001-01-26 20:42:23 +0000564 signal(SIGQUIT, clean_up_and_die);
565 signal(SIGTERM, clean_up_and_die);
566 handlers_sets |= SET_TERM_HANDLERS;
567 }
568}
Erik Andersenf0657d32000-04-12 17:49:52 +0000569
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000570#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000571
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000572static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000573{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000574 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
575 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
576 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
577 (st->st_mode & S_IXOTH)) return TRUE;
578 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000579}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000580
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000581#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000582
583static char **username_tab_completion(char *ud, int *num_matches)
584{
585 struct passwd *entry;
586 int userlen;
587 char *temp;
588
589
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000590 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000591 userlen = strlen(ud);
592
593 if (num_matches == 0) { /* "~/..." or "~user/..." */
594 char *sav_ud = ud - 1;
595 char *home = 0;
596
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000597 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000598 home = home_pwd_buf;
599 } else {
600 /* "~user/..." */
601 temp = strchr(ud, '/');
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000602 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000603 entry = getpwnam(ud);
604 *temp = '/'; /* restore ~user/... */
605 ud = temp;
606 if (entry)
607 home = entry->pw_dir;
608 }
609 if (home) {
610 if ((userlen + strlen(home) + 1) < BUFSIZ) {
611 char temp2[BUFSIZ]; /* argument size */
612
613 /* /home/user/... */
614 sprintf(temp2, "%s%s", home, ud);
615 strcpy(sav_ud, temp2);
616 }
617 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000618 return 0; /* void, result save to argument :-) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000619 } else {
620 /* "~[^/]*" */
621 char **matches = (char **) NULL;
622 int nm = 0;
623
624 setpwent();
625
626 while ((entry = getpwent()) != NULL) {
627 /* Null usernames should result in all users as possible completions. */
628 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
629
630 temp = xmalloc(3 + strlen(entry->pw_name));
631 sprintf(temp, "~%s/", entry->pw_name);
632 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
633
634 matches[nm++] = temp;
635 }
636 }
637
638 endpwent();
639 (*num_matches) = nm;
640 return (matches);
641 }
642}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000643#endif /* BB_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000644
645enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000646 FIND_EXE_ONLY = 0,
647 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000648 FIND_FILE_ONLY = 2,
649};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000650
Mark Whitley4e338752001-01-26 20:42:23 +0000651static int path_parse(char ***p, int flags)
652{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000653 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000654 char *tmp;
655 char *pth;
656
Mark Whitley4e338752001-01-26 20:42:23 +0000657 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000658 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
659 /* PATH=<empty> or PATH=:<empty> */
660 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000661 return 1;
662 }
663
664 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000665 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000666
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000667 for (;;) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000668 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000669 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000670 if (tmp) {
671 if (*++tmp == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000672 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000673 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000674 break;
675 }
676
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000677 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000678
679 tmp = pth;
680 (*p)[0] = xstrdup(tmp);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000681 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000682
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000683 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000684 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000685 if (tmp) {
686 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
687 if (*++tmp == 0)
688 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000689 } else
690 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000691 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000692 }
693
694 return npth;
695}
696
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000697static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000698{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000699 int l = 0;
700 char *s = xmalloc((strlen(found) + 1) * 2);
701
702 while (*found) {
703 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
704 s[l++] = '\\';
705 s[l++] = *found++;
706 }
707 s[l] = 0;
708 return s;
709}
710
711static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000712 int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000713{
714
715 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000716 DIR *dir;
717 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000718 char dirbuf[BUFSIZ];
719 int nm = *num_matches;
720 struct stat st;
721 char *path1[1];
722 char **paths = path1;
723 int npaths;
724 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000725 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000726 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000727
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000728 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000729
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000730 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000731 /* no dir, if flags==EXE_ONLY - get paths, else "." */
732 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000733 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000734 } else {
735 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000736 /* save for change */
737 strcpy(dirbuf, command);
738 /* set dir only */
739 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000740#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000741 if (dirbuf[0] == '~') /* ~/... or ~user/... */
742 username_tab_completion(dirbuf, 0);
743#endif
744 /* "strip" dirname in command */
745 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000746
Mark Whitley4e338752001-01-26 20:42:23 +0000747 paths[0] = dirbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000748 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000749 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000750
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000751 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000752
Mark Whitley4e338752001-01-26 20:42:23 +0000753 dir = opendir(paths[i]);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000754 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000755 continue;
756
757 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000758 char *str_found = next->d_name;
759
Mark Whitley4e338752001-01-26 20:42:23 +0000760 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000761 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000762 continue;
763 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000764 if (*str_found == '.' && *pfind == 0) {
765 if (*paths[i] == '/' && paths[i][1] == 0
766 && str_found[1] == 0) str_found = ""; /* only "/" */
767 else
Mark Whitley4e338752001-01-26 20:42:23 +0000768 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000769 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000770 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000771 /* hmm, remover in progress? */
Eric Andersene5dfced2001-04-09 22:48:12 +0000772 if (stat(found, &st) < 0)
773 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000774 /* find with dirs ? */
775 if (paths[i] != dirbuf)
776 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000777 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000778 /* name is directory */
Eric Andersene5dfced2001-04-09 22:48:12 +0000779 str_found = found;
780 found = concat_path_file(found, "");
781 free(str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000782 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000783 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000784 /* not put found file if search only dirs for cd */
Eric Andersene5dfced2001-04-09 22:48:12 +0000785 if (type == FIND_DIR_ONLY)
786 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000787 str_found = add_quote_for_spec_chars(found);
788 if (type == FIND_FILE_ONLY ||
789 (type == FIND_EXE_ONLY && is_execute(&st) == TRUE))
790 strcat(str_found, " ");
791 }
Mark Whitley4e338752001-01-26 20:42:23 +0000792 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000793 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
794
795 matches[nm++] = str_found;
Eric Andersene5dfced2001-04-09 22:48:12 +0000796cont:
797 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000798 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000799 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000800 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000801 if (paths != path1) {
802 free(paths[0]); /* allocated memory only in first member */
803 free(paths);
804 }
Mark Whitley4e338752001-01-26 20:42:23 +0000805 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000806 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000807}
Erik Andersenf0657d32000-04-12 17:49:52 +0000808
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000809static int match_compare(const void *a, const void *b)
810{
811 return strcmp(*(char **) a, *(char **) b);
812}
813
814
815
816#define QUOT (UCHAR_MAX+1)
817
818#define collapse_pos(is, in) { \
819 memcpy(int_buf+is, int_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); \
820 memcpy(pos_buf+is, pos_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); }
821
822static int find_match(char *matchBuf, int *len_with_quotes)
823{
824 int i, j;
825 int command_mode;
826 int c, c2;
827 int int_buf[BUFSIZ + 1];
828 int pos_buf[BUFSIZ + 1];
829
830 /* set to integer dimension characters and own positions */
831 for (i = 0;; i++) {
832 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
833 if (int_buf[i] == 0) {
834 pos_buf[i] = -1; /* indicator end line */
835 break;
836 } else
837 pos_buf[i] = i;
838 }
839
840 /* mask \+symbol and convert '\t' to ' ' */
841 for (i = j = 0; matchBuf[i]; i++, j++)
842 if (matchBuf[i] == '\\') {
843 collapse_pos(j, j + 1);
844 int_buf[j] |= QUOT;
845 i++;
846#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
847 if (matchBuf[i] == '\t') /* algorithm equivalent */
848 int_buf[j] = ' ' | QUOT;
849#endif
850 }
851#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
852 else if (matchBuf[i] == '\t')
853 int_buf[j] = ' ';
854#endif
855
856 /* mask "symbols" or 'symbols' */
857 c2 = 0;
858 for (i = 0; int_buf[i]; i++) {
859 c = int_buf[i];
860 if (c == '\'' || c == '"') {
861 if (c2 == 0)
862 c2 = c;
863 else {
864 if (c == c2)
865 c2 = 0;
866 else
867 int_buf[i] |= QUOT;
868 }
869 } else if (c2 != 0 && c != '$')
870 int_buf[i] |= QUOT;
871 }
872
873 /* skip commands with arguments if line have commands delimiters */
874 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
875 for (i = 0; int_buf[i]; i++) {
876 c = int_buf[i];
877 c2 = int_buf[i + 1];
878 j = i ? int_buf[i - 1] : -1;
879 command_mode = 0;
880 if (c == ';' || c == '&' || c == '|') {
881 command_mode = 1 + (c == c2);
882 if (c == '&') {
883 if (j == '>' || j == '<')
884 command_mode = 0;
885 } else if (c == '|' && j == '>')
886 command_mode = 0;
887 }
888 if (command_mode) {
889 collapse_pos(0, i + command_mode);
890 i = -1; /* hack incremet */
891 }
892 }
893 /* collapse `command...` */
894 for (i = 0; int_buf[i]; i++)
895 if (int_buf[i] == '`') {
896 for (j = i + 1; int_buf[j]; j++)
897 if (int_buf[j] == '`') {
898 collapse_pos(i, j + 1);
899 j = 0;
900 break;
901 }
902 if (j) {
903 /* not found close ` - command mode, collapse all previous */
904 collapse_pos(0, i + 1);
905 break;
906 } else
907 i--; /* hack incremet */
908 }
909
910 /* collapse (command...(command...)...) or {command...{command...}...} */
911 c = 0; /* "recursive" level */
912 c2 = 0;
913 for (i = 0; int_buf[i]; i++)
914 if (int_buf[i] == '(' || int_buf[i] == '{') {
915 if (int_buf[i] == '(')
916 c++;
917 else
918 c2++;
919 collapse_pos(0, i + 1);
920 i = -1; /* hack incremet */
921 }
922 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
923 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
924 if (int_buf[i] == ')')
925 c--;
926 else
927 c2--;
928 collapse_pos(0, i + 1);
929 i = -1; /* hack incremet */
930 }
931
932 /* skip first not quote space */
933 for (i = 0; int_buf[i]; i++)
934 if (int_buf[i] != ' ')
935 break;
936 if (i)
937 collapse_pos(0, i);
938
939 /* set find mode for completion */
940 command_mode = FIND_EXE_ONLY;
941 for (i = 0; int_buf[i]; i++)
942 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
943 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000944 && matchBuf[pos_buf[0]]=='c'
945 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000946 command_mode = FIND_DIR_ONLY;
947 else {
948 command_mode = FIND_FILE_ONLY;
949 break;
950 }
951 }
952 /* "strlen" */
953 for (i = 0; int_buf[i]; i++);
954 /* find last word */
955 for (--i; i >= 0; i--) {
956 c = int_buf[i];
957 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
958 collapse_pos(0, i + 1);
959 break;
960 }
961 }
962 /* skip first not quoted '\'' or '"' */
963 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
964 /* collapse quote or unquote // or /~ */
Mark Whitley7e5291f2001-03-08 19:31:12 +0000965 while ((int_buf[i] & ~QUOT) == '/' &&
966 ((int_buf[i + 1] & ~QUOT) == '/'
967 || (int_buf[i + 1] & ~QUOT) == '~')) {
968 i++;
969 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000970
971 /* set only match and destroy quotes */
972 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000973 for (c = 0; pos_buf[i] >= 0; i++) {
974 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000975 j = pos_buf[i] + 1;
976 }
Eric Andersen4f990532001-05-31 17:15:57 +0000977 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000978 /* old lenght matchBuf with quotes symbols */
979 *len_with_quotes = j ? j - pos_buf[0] : 0;
980
981 return command_mode;
982}
983
984
985static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000986{
987 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000988 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +0000989 static char **matches;
990
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000991 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +0000992 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000993 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +0000994 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000995 free(matches);
996 matches = (char **) NULL;
997 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000998 return;
999 }
1000 if (*lastWasTab == FALSE) {
1001
1002 char *tmp;
1003 int len_found;
1004 char matchBuf[BUFSIZ];
1005 int find_type;
1006 int recalc_pos;
1007
1008 *lastWasTab = TRUE; /* flop trigger */
1009
1010 /* Make a local copy of the string -- up
1011 * to the position of the cursor */
1012 tmp = strncpy(matchBuf, command_ps, cursor);
1013 tmp[cursor] = 0;
1014
1015 find_type = find_match(matchBuf, &recalc_pos);
1016
1017 /* Free up any memory already allocated */
1018 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001019
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001020#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001021 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001022 * then try completing this word as a username. */
1023
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001024 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001025 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001026#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001027 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001028 * in the current working directory that matches. */
1029 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001030 matches =
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001031 exe_n_cwd_tab_completion(matchBuf,
1032 &num_matches, find_type);
1033 /* Remove duplicate found */
1034 if(matches) {
1035 int i, j;
1036 /* bubble */
1037 for(i=0; i<(num_matches-1); i++)
1038 for(j=i+1; j<num_matches; j++)
1039 if(matches[i]!=0 && matches[j]!=0 &&
1040 strcmp(matches[i], matches[j])==0) {
1041 free(matches[j]);
1042 matches[j]=0;
1043 }
1044 j=num_matches;
1045 num_matches = 0;
1046 for(i=0; i<j; i++)
1047 if(matches[i]) {
1048 if(!strcmp(matches[i], "./"))
1049 matches[i][1]=0;
1050 else if(!strcmp(matches[i], "../"))
1051 matches[i][2]=0;
1052 matches[num_matches++]=matches[i];
1053 }
1054 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001055 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001056 if (!matches || num_matches > 1) {
1057 char *tmp1;
1058
Mark Whitley4e338752001-01-26 20:42:23 +00001059 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001060 if (!matches)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001061 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001062 /* sort */
1063 qsort(matches, num_matches, sizeof(char *), match_compare);
1064
1065 /* find minimal match */
1066 tmp = xstrdup(matches[0]);
1067 for (tmp1 = tmp; *tmp1; tmp1++)
1068 for (len_found = 1; len_found < num_matches; len_found++)
1069 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1070 *tmp1 = 0;
1071 break;
1072 }
1073 if (*tmp == 0) { /* have unique */
1074 free(tmp);
1075 return;
1076 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001077 } else { /* one match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001078 tmp = matches[0];
1079 /* for next completion current found */
1080 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001081 }
1082
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001083 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001084 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001085 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001086
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001087 /* before word for match */
1088 command_ps[cursor - recalc_pos] = 0;
1089 /* save tail line */
1090 strcpy(matchBuf, command_ps + cursor);
1091 /* add match */
1092 strcat(command_ps, tmp);
1093 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001094 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001095 /* back to begin word for match */
1096 input_backward(recalc_pos);
1097 /* new pos */
1098 recalc_pos = cursor + len_found;
1099 /* new len */
1100 len = strlen(command_ps);
1101 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001102 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001103 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001104 if (tmp != matches[0])
1105 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001106 } else {
1107 /* Ok -- the last char was a TAB. Since they
1108 * just hit TAB again, print a list of all the
1109 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001110 if (matches && num_matches > 0) {
1111 int i, col, l;
1112 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001113
1114 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001115 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001116 for (i = 0, col = 0; i < num_matches; i++) {
1117 l = strlen(matches[i]);
1118 if (l < 14)
1119 l = 14;
1120 printf("%-14s ", matches[i]);
1121 if ((l += 2) > 16)
1122 while (l % 16) {
1123 putchar(' ');
1124 l++;
1125 }
1126 col += l;
1127 col -= (col / cmdedit_termw) * cmdedit_termw;
1128 if (col > 60 && matches[i + 1] != NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +00001129 putchar('\n');
Erik Andersenf0657d32000-04-12 17:49:52 +00001130 col = 0;
1131 }
1132 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001133 /* Go to the next line and rewrite */
1134 putchar('\n');
1135 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001136 }
1137 }
1138}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001139#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001140
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001141static void get_previous_history(struct history **hp, struct history *p)
Erik Andersenf0657d32000-04-12 17:49:52 +00001142{
Eric Andersen91a44002000-07-19 17:37:57 +00001143 if ((*hp)->s)
1144 free((*hp)->s);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001145 (*hp)->s = xstrdup(command_ps);
1146 *hp = p;
Erik Andersenf0657d32000-04-12 17:49:52 +00001147}
1148
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001149static inline void get_next_history(struct history **hp)
Erik Andersenf0657d32000-04-12 17:49:52 +00001150{
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001151 get_previous_history(hp, (*hp)->n);
Erik Andersenf0657d32000-04-12 17:49:52 +00001152}
1153
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001154enum {
1155 ESC = 27,
1156 DEL = 127,
1157};
1158
1159
Erik Andersen6273f652000-03-17 01:12:41 +00001160/*
1161 * This function is used to grab a character buffer
1162 * from the input file descriptor and allows you to
1163 * a string with full command editing (sortof like
1164 * a mini readline).
1165 *
1166 * The following standard commands are not implemented:
1167 * ESC-b -- Move back one word
1168 * ESC-f -- Move forward one word
1169 * ESC-d -- Delete back one word
1170 * ESC-h -- Delete forward one word
1171 * CTL-t -- Transpose two characters
1172 *
1173 * Furthermore, the "vi" command editing keys are not implemented.
1174 *
Erik Andersen6273f652000-03-17 01:12:41 +00001175 */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001176
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001177extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001178{
1179
Erik Andersenc7c634b2000-03-19 05:28:55 +00001180 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001181 int lastWasTab = FALSE;
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001182 unsigned char c = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001183 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +00001184
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001185 /* prepare before init handlers */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001186 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001187 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001188 command_ps = command;
1189
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001190 if (new_settings.c_cc[VERASE] == 0) { /* first call */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001191
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001192 getTermSettings(0, (void *) &initial_settings);
Erik Andersen1d1d9502000-04-21 01:26:49 +00001193 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001194 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1195 /* Turn off echoing and CTRL-C, so we can trap it */
1196 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
1197#ifndef linux
1198 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001199 new_settings.c_cc[VMIN] = 1;
1200 new_settings.c_cc[VTIME] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001201 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001202# ifndef _POSIX_VDISABLE
1203# define _POSIX_VDISABLE '\0'
1204# endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001205 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001206#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001207 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001208
1209 command[0] = 0;
1210
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001211 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001212 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001213
Eric Andersen6faae7d2001-02-16 20:09:17 +00001214 /* Now initialize things */
1215 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001216 /* Print out the command prompt */
1217 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001218
Erik Andersenc7c634b2000-03-19 05:28:55 +00001219 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001220
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001221 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001222
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001223 if (read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001224 /* if we can't read input then exit */
1225 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001226
Erik Andersen13456d12000-03-16 08:09:57 +00001227 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001228 case '\n':
1229 case '\r':
1230 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001231 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001232 break_out = 1;
1233 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001234 case 1:
1235 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001236 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001237 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001238 case 2:
1239 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001240 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001241 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001242 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001243 /* Control-c -- stop gathering input */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001244
Eric Andersen86349772000-12-18 20:25:50 +00001245 /* Link into lash to reset context to 0 on ^C and such */
1246 shell_context = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001247
1248 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001249 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001250 command[0] = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001251
Eric Andersen86349772000-12-18 20:25:50 +00001252 return;
Erik Andersenf0657d32000-04-12 17:49:52 +00001253 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001254 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001255 * if the len=0 and no chars to delete */
1256 if (len == 0) {
Eric Andersened424db2001-04-23 15:28:28 +00001257prepare_to_die:
Mark Whitley4e338752001-01-26 20:42:23 +00001258 printf("exit");
Erik Andersenf0657d32000-04-12 17:49:52 +00001259 clean_up_and_die(0);
1260 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001261 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001262 }
1263 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001264 case 5:
1265 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001266 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001267 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001268 case 6:
1269 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001270 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001271 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001272 case '\b':
1273 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001274 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001275 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001276 break;
1277 case '\t':
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001278#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001279 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001280#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001281 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001282 case 14:
1283 /* Control-n -- Get next command in history */
1284 if (hp && hp->n && hp->n->s) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001285 get_next_history(&hp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001286 goto rewrite_line;
1287 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001288 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001289 }
1290 break;
1291 case 16:
1292 /* Control-p -- Get previous command from history */
1293 if (hp && hp->p) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001294 get_previous_history(&hp, hp->p);
Erik Andersenf0657d32000-04-12 17:49:52 +00001295 goto rewrite_line;
1296 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001297 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001298 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001299 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001300 case 21:
1301 /* Control-U -- Clear line before cursor */
1302 if (cursor) {
1303 strcpy(command, command + cursor);
1304 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001305 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001306 break;
1307
1308 case ESC:{
1309 /* escape sequence follows */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001310 if (read(0, &c, 1) < 1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001311 return;
1312 /* different vt100 emulations */
1313 if (c == '[' || c == 'O') {
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001314 if (read(0, &c, 1) < 1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001315 return;
1316 }
1317 switch (c) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001318#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001319 case '\t': /* Alt-Tab */
1320
1321 input_tab(&lastWasTab);
1322 break;
1323#endif
1324 case 'A':
1325 /* Up Arrow -- Get previous command from history */
1326 if (hp && hp->p) {
1327 get_previous_history(&hp, hp->p);
1328 goto rewrite_line;
1329 } else {
1330 beep();
1331 }
1332 break;
1333 case 'B':
1334 /* Down Arrow -- Get next command in history */
1335 if (hp && hp->n && hp->n->s) {
1336 get_next_history(&hp);
1337 goto rewrite_line;
1338 } else {
1339 beep();
1340 }
1341 break;
1342
1343 /* Rewrite the line with the selected history item */
1344 rewrite_line:
1345 /* change command */
1346 len = strlen(strcpy(command, hp->s));
1347 /* redraw and go to end line */
1348 redraw(cmdedit_y, 0);
1349 break;
1350 case 'C':
1351 /* Right Arrow -- Move forward one character */
1352 input_forward();
1353 break;
1354 case 'D':
1355 /* Left Arrow -- Move back one character */
1356 input_backward(1);
1357 break;
1358 case '3':
1359 /* Delete */
1360 input_delete();
1361 break;
1362 case '1':
1363 case 'H':
1364 /* Home (Ctrl-A) */
1365 input_backward(cursor);
1366 break;
1367 case '4':
1368 case 'F':
1369 /* End (Ctrl-E) */
1370 input_end();
1371 break;
1372 default:
1373 if (!(c >= '1' && c <= '9'))
1374 c = 0;
1375 beep();
1376 }
1377 if (c >= '1' && c <= '9')
1378 do
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001379 if (read(0, &c, 1) < 1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001380 return;
1381 while (c != '~');
1382 break;
1383 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001384
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001385 default: /* If it's regular input, do the normal thing */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001386#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1387 /* Control-V -- Add non-printable symbol */
1388 if (c == 22) {
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001389 if (read(0, &c, 1) < 1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001390 return;
1391 if (c == 0) {
1392 beep();
1393 break;
1394 }
1395 } else
1396#endif
Eric Andersene5dfced2001-04-09 22:48:12 +00001397 if (!Isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001398 break;
1399
1400 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
1401 break;
1402
1403 len++;
1404
1405 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001406 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001407 *(command + cursor + 1) = 0;
1408 cmdedit_set_out_char(0);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001409 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001410 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001411
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001412 memmove(command + sc + 1, command + sc, len - sc);
1413 *(command + sc) = c;
1414 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001415 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001416 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001417 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001418 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001419 }
1420
Erik Andersenc7c634b2000-03-19 05:28:55 +00001421 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001422 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001423 if (break_out) /* Enter is the command terminator, no more input. */
1424 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001425
1426 if (c != '\t')
1427 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001428 }
1429
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001430 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001431 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001432
1433 /* Handle command history log */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001434 if (len) { /* no put empty line */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001435
1436 struct history *h = his_end;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001437 char *ss;
Mark Whitley4e338752001-01-26 20:42:23 +00001438
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001439 ss = xstrdup(command); /* duplicate */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001440
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001441 if (h == 0) {
Eric Andersen91a44002000-07-19 17:37:57 +00001442 /* No previous history -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001443 h = his_front = xmalloc(sizeof(struct history));
1444 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001445
1446 h->p = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001447 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001448 h->n->p = h;
1449 h->n->n = NULL;
1450 h->n->s = NULL;
1451 his_end = h->n;
1452 history_counter++;
1453 } else {
Eric Andersen91a44002000-07-19 17:37:57 +00001454 /* Add a new history command -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001455 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001456
1457 h->n->p = h;
1458 h->n->n = NULL;
1459 h->n->s = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001460 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001461 his_end = h->n;
1462
1463 /* After max history, remove the oldest command */
1464 if (history_counter >= MAX_HISTORY) {
1465
1466 struct history *p = his_front->n;
1467
1468 p->p = NULL;
1469 free(his_front->s);
1470 free(his_front);
1471 his_front = p;
1472 } else {
1473 history_counter++;
1474 }
Erik Andersen13456d12000-03-16 08:09:57 +00001475 }
Eric Andersen004015e2001-05-21 20:30:51 +00001476#if defined(BB_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001477 num_ok_lines++;
1478#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001479 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001480 command[len++] = '\n'; /* set '\n' */
1481 command[len] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001482#if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001483 input_tab(0); /* strong free */
1484#endif
Eric Andersen004015e2001-05-21 20:30:51 +00001485#if defined(BB_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001486 free(cmdedit_prompt);
1487#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001488 return;
Erik Andersen13456d12000-03-16 08:09:57 +00001489}
1490
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001491
Mark Whitley4e338752001-01-26 20:42:23 +00001492/* Undo the effects of cmdedit_init(). */
Eric Andersen501c88b2000-07-28 15:14:45 +00001493extern void cmdedit_terminate(void)
1494{
1495 cmdedit_reset_term();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001496 if ((handlers_sets & SET_TERM_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +00001497 signal(SIGKILL, SIG_DFL);
1498 signal(SIGINT, SIG_DFL);
1499 signal(SIGQUIT, SIG_DFL);
1500 signal(SIGTERM, SIG_DFL);
1501 signal(SIGWINCH, SIG_DFL);
1502 handlers_sets &= ~SET_TERM_HANDLERS;
1503 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001504}
1505
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001506#endif /* BB_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001507
1508
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001509#ifdef TEST
1510
Eric Andersene5dfced2001-04-09 22:48:12 +00001511const char *applet_name = "debug stuff usage";
1512const char *memory_exhausted = "Memory exhausted";
1513
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001514#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1515#include <locale.h>
1516#endif
1517
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001518unsigned int shell_context;
1519
1520int main(int argc, char **argv)
1521{
1522 char buff[BUFSIZ];
1523 char *prompt =
Eric Andersen004015e2001-05-21 20:30:51 +00001524#if defined(BB_FEATURE_SH_FANCY_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001525 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001526\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001527\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001528#else
1529 "% ";
1530#endif
1531
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001532#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1533 setlocale(LC_ALL, "");
1534#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001535 shell_context = 1;
1536 do {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001537 int l;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001538 cmdedit_read_input(prompt, buff);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001539 l = strlen(buff);
1540 if(l > 0 && buff[l-1] == '\n')
1541 buff[l-1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001542 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1543 } while (shell_context);
1544 printf("*** cmdedit_read_input() detect ^C\n");
1545 return 0;
1546}
1547
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001548#endif /* TEST */