blob: 1b5cbbfabfa76e0127d62fb5dc937fbe20c9c4d1 [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 Andersen6faae7d2001-02-16 20:09:17 +000034//#define TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000035
Eric Andersencbe31da2001-02-20 06:14:08 +000036#include <stdio.h>
37#include <errno.h>
38#include <unistd.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/ioctl.h>
42#include <ctype.h>
43#include <signal.h>
44#include <limits.h>
45
Eric Andersen5f2c79d2001-02-16 18:36:04 +000046#ifndef TEST
Mark Whitley4e338752001-01-26 20:42:23 +000047
Eric Andersen3570a342000-09-25 21:45:58 +000048#include "busybox.h"
Mark Whitley4e338752001-01-26 20:42:23 +000049
Eric Andersen5f2c79d2001-02-16 18:36:04 +000050#define D(x)
51
52#else
53
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000054#define BB_FEATURE_COMMAND_EDITING
55#define BB_FEATURE_COMMAND_TAB_COMPLETION
56#define BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen94456f52001-02-18 20:26:48 +000057#define BB_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000058#undef BB_FEATURE_SIMPLE_PROMPT
Eric Andersen94456f52001-02-18 20:26:48 +000059#define BB_FEATURE_CLEAN_UP
Eric Andersen5f2c79d2001-02-16 18:36:04 +000060
Eric Andersen5f2c79d2001-02-16 18:36:04 +000061#define D(x) x
62
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000063#ifndef TRUE
64#define TRUE 1
65#endif
66#ifndef FALSE
67#define FALSE 0
68#endif
69
Eric Andersen5f2c79d2001-02-16 18:36:04 +000070#endif /* TEST */
71
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000072#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5165fbe2001-02-20 06:42:29 +000073#include <dirent.h>
74#include <sys/stat.h>
75#endif
76
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000077#ifdef BB_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000078
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000079#ifndef BB_FEATURE_COMMAND_TAB_COMPLETION
80#undef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +000081#endif
82
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000083#if defined(BB_FEATURE_COMMAND_USERNAME_COMPLETION) || !defined(BB_FEATURE_SIMPLE_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000084#define BB_FEATURE_GETUSERNAME_AND_HOMEDIR
85#endif
86
Eric Andersen5f2c79d2001-02-16 18:36:04 +000087#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
88#ifndef TEST
Eric Andersenaf4ac772001-02-01 22:43:49 +000089#include "pwd_grp/pwd.h"
Eric Andersen5f2c79d2001-02-16 18:36:04 +000090#else
91#include <pwd.h>
92#endif /* TEST */
93#endif /* advanced FEATURES */
Eric Andersenaf4ac772001-02-01 22:43:49 +000094
95
Eric Andersen5f2c79d2001-02-16 18:36:04 +000096#ifdef TEST
97void *xrealloc(void *old, size_t size)
98{
99 return realloc(old, size);
100}
Erik Andersen13456d12000-03-16 08:09:57 +0000101
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000102void *xmalloc(size_t size)
103{
104 return malloc(size);
105}
106char *xstrdup(const char *s)
107{
108 return strdup(s);
109}
110
111void *xcalloc(size_t size, size_t se)
112{
113 return calloc(size, se);
114}
115
116#define error_msg(s, d) fprintf(stderr, s, d)
117#endif
118
119
120struct history {
121 char *s;
122 struct history *p;
123 struct history *n;
Mark Whitley59ab0252001-01-23 22:30:04 +0000124};
125
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000126/* Maximum length of the linked list for the command line history */
127static const int MAX_HISTORY = 15;
Erik Andersen13456d12000-03-16 08:09:57 +0000128
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000129/* First element in command line list */
130static struct history *his_front = NULL;
131
132/* Last element in command line list */
133static struct history *his_end = NULL;
134
Erik Andersen1d1d9502000-04-21 01:26:49 +0000135
136/* ED: sparc termios is broken: revert back to old termio handling. */
Erik Andersen1d1d9502000-04-21 01:26:49 +0000137
138#if #cpu(sparc)
139# include <termio.h>
140# define termios termio
141# define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp)
142# define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp)
143#else
144# include <termios.h>
145# define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
146# define getTermSettings(fd,argp) tcgetattr(fd, argp);
147#endif
148
149/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000150static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000151
152
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000153#ifndef _POSIX_VDISABLE
154#define _POSIX_VDISABLE '\0'
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000155#endif
156
Erik Andersen1d1d9502000-04-21 01:26:49 +0000157
Mark Whitley4e338752001-01-26 20:42:23 +0000158static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000159volatile int cmdedit_termw = 80; /* actual terminal width */
160static int history_counter = 0; /* Number of commands in history list */
Mark Whitley4e338752001-01-26 20:42:23 +0000161static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000162volatile int handlers_sets = 0; /* Set next bites: */
163
Mark Whitley4e338752001-01-26 20:42:23 +0000164enum {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000165 SET_ATEXIT = 1, /* when atexit() has been called
166 and get euid,uid,gid to fast compare */
167 SET_TERM_HANDLERS = 2, /* set many terminates signal handlers */
168 SET_WCHG_HANDLERS = 4, /* winchg signal handler */
169 SET_RESET_TERM = 8, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000170};
Erik Andersen13456d12000-03-16 08:09:57 +0000171
Mark Whitley4e338752001-01-26 20:42:23 +0000172
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000173static int cmdedit_x; /* real x terminal position */
174static int cmdedit_y; /* pseudoreal y terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000175static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000176
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000177static int cursor; /* required global for signal handler */
178static int len; /* --- "" - - "" - -"- --""-- --""--- */
179static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000180static
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000181#ifdef BB_FEATURE_SIMPLE_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000182 const
183#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000184char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000185
186/* Link into lash to reset context to 0 on ^C and such */
Eric Andersen86349772000-12-18 20:25:50 +0000187extern unsigned int shell_context;
188
Erik Andersen13456d12000-03-16 08:09:57 +0000189
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000190#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
191static char *user_buf = "";
192static char *home_pwd_buf = "";
193static int my_euid;
194#endif
195
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000196#ifndef BB_FEATURE_SIMPLE_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000197static char *hostname_buf = "";
198static int num_ok_lines = 1;
199#endif
200
201
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000202#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000203
204#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
205static int my_euid;
206#endif
207
208static int my_uid;
209static int my_gid;
210
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000211#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000212
Erik Andersen13456d12000-03-16 08:09:57 +0000213
Mark Whitley4e338752001-01-26 20:42:23 +0000214static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000215
Mark Whitley4e338752001-01-26 20:42:23 +0000216static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000217{
218 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000219 static __sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000220
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000221 /* emulate || signal call */
222 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Mark Whitley4e338752001-01-26 20:42:23 +0000223 ioctl(0, TIOCGWINSZ, &win);
224 if (win.ws_col > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000225 cmdedit_setwidth(win.ws_col, nsig == SIGWINCH);
226 }
Mark Whitley4e338752001-01-26 20:42:23 +0000227 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000228 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000229
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000230 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000231 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000232 else if (nsig == SIGWINCH) /* signaled called handler */
233 signal(SIGWINCH, win_changed); /* set for next call */
234 else /* nsig == 0 */
235 /* set previous handler */
236 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000237}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000238
239static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000240{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000241 if ((handlers_sets & SET_RESET_TERM) != 0) {
Erik Andersena6c75222000-04-18 00:00:52 +0000242 /* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000243 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000244 handlers_sets &= ~SET_RESET_TERM;
245 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000246 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000247 /* reset SIGWINCH handler to previous (default) */
248 win_changed(0);
249 handlers_sets &= ~SET_WCHG_HANDLERS;
250 }
251 fflush(stdout);
Eric Andersenb040d4f2000-07-25 18:01:20 +0000252#ifdef BB_FEATURE_CLEAN_UP
253 if (his_front) {
254 struct history *n;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000255
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000256 while (his_front != his_end) {
Eric Andersenb040d4f2000-07-25 18:01:20 +0000257 n = his_front->n;
258 free(his_front->s);
259 free(his_front);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000260 his_front = n;
Eric Andersenb040d4f2000-07-25 18:01:20 +0000261 }
262 }
263#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000264}
265
Mark Whitley4e338752001-01-26 20:42:23 +0000266
Mark Whitley4e338752001-01-26 20:42:23 +0000267/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000268static void cmdedit_set_out_char(int next_char)
269{
270
271 int c = command_ps[cursor];
272
273 if (c == 0)
274 c = ' '; /* destroy end char? */
275#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
276 if (!isprint(c)) { /* Inverse put non-printable characters */
277 if (((unsigned char) c) >= 128)
278 c -= 128;
279 if (((unsigned char) c) < ' ')
280 c += '@';
281 if (c == 127)
282 c = '?';
283 printf("\033[7m%c\033[0m", c);
284 } else
285#endif
286 putchar(c);
287 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000288 /* terminal is scrolled down */
289 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000290 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000291
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000292 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000293 next_char = ' ';
294 /* destroy "(auto)margin" */
295 putchar(next_char);
296 putchar('\b');
297 }
298 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000299}
300
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000301/* Move to end line. Bonus: rewrite line from cursor */
302static void input_end(void)
303{
304 while (cursor < len)
305 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000306}
307
308/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000309static void goto_new_line(void)
310{
Mark Whitley4e338752001-01-26 20:42:23 +0000311 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000312 if (cmdedit_x)
313 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000314}
315
316
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000317static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000318{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000319 fputs(s, stdout);
320}
321static inline void beep(void)
322{
323 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000324}
325
Mark Whitley4e338752001-01-26 20:42:23 +0000326/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000327/* special for slow terminal */
328static void input_backward(int num)
329{
330 if (num > cursor)
331 num = cursor;
332 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000333
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000334 if (cmdedit_x >= num) { /* no to up line */
335 cmdedit_x -= num;
336 if (num < 4)
337 while (num-- > 0)
338 putchar('\b');
339
340 else
341 printf("\033[%dD", num);
342 } else {
343 int count_y;
344
345 if (cmdedit_x) {
346 putchar('\r'); /* back to first terminal pos. */
347 num -= cmdedit_x; /* set previous backward */
348 }
349 count_y = 1 + num / cmdedit_termw;
350 printf("\033[%dA", count_y);
351 cmdedit_y -= count_y;
352 /* require forward after uping */
353 cmdedit_x = cmdedit_termw * count_y - num;
354 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000355 }
Erik Andersen13456d12000-03-16 08:09:57 +0000356}
357
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000358static void put_prompt(void)
359{
360 out1str(cmdedit_prompt);
361 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
362 cursor = 0;
363}
364
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000365#ifdef BB_FEATURE_SIMPLE_PROMPT
366static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000367{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000368 cmdedit_prompt = prmt_ptr;
369 cmdedit_prmt_len = strlen(prmt_ptr);
370 put_prompt();
371}
372#else
373static void add_to_prompt(char **prmt_mem_ptr, int *alm,
374 int *prmt_len, const char *addb)
375{
376 *prmt_len += strlen(addb);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000377 if (*alm < (*prmt_len) + 1) {
378 *alm = (*prmt_len) + 1;
379 *prmt_mem_ptr = xrealloc(*prmt_mem_ptr, *alm);
380 }
381 strcat(*prmt_mem_ptr, addb);
382}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000383
384static void parse_prompt(const char *prmt_ptr)
385{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000386 int alm = strlen(prmt_ptr) + 1; /* supposedly require memory */
387 int prmt_len = 0;
388 int sub_len = 0;
389 int flg_not_length = '[';
390 char *prmt_mem_ptr = xstrdup(prmt_ptr);
391 char pwd_buf[PATH_MAX + 1];
392 char buf[16];
393 int c;
394
395 pwd_buf[0] = 0;
396 *prmt_mem_ptr = 0;
397
398 while (*prmt_ptr) {
399 c = *prmt_ptr++;
400 if (c == '\\') {
401 c = *prmt_ptr;
402 if (c == 0)
403 break;
404 prmt_ptr++;
405 switch (c) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000406#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000407 case 'u':
408 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, user_buf);
409 continue;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000410#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000411 case 'h':
412 if (hostname_buf[0] == 0) {
413 hostname_buf = xcalloc(256, 1);
414 if (gethostname(hostname_buf, 255) < 0) {
415 strcpy(hostname_buf, "?");
416 } else {
417 char *s = strchr(hostname_buf, '.');
418
419 if (s)
420 *s = 0;
421 }
422 }
423 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len,
424 hostname_buf);
425 continue;
426 case '$':
427 c = my_euid == 0 ? '#' : '$';
428 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000429#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430 case 'w':
431 if (pwd_buf[0] == 0) {
432 int l;
433
434 getcwd(pwd_buf, PATH_MAX);
435 l = strlen(home_pwd_buf);
436 if (home_pwd_buf[0] != 0 &&
437 strncmp(home_pwd_buf, pwd_buf, l) == 0) {
438 strcpy(pwd_buf + 1, pwd_buf + l);
439 pwd_buf[0] = '~';
440 }
441 }
442 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf);
443 continue;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000444#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000445 case '!':
446 snprintf(buf, sizeof(buf), "%d", num_ok_lines);
447 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf);
448 continue;
449 case 'e':
450 case 'E': /* \e \E = \033 */
451 c = '\033';
452 break;
453 case 'x':
454 case 'X':
455 case '0':
456 case '1':
457 case '2':
458 case '3':
459 case '4':
460 case '5':
461 case '6':
462 case '7':{
463 int l;
464 int ho = 0;
465 char *eho;
466
467 if (c == 'X')
468 c = 'x';
469
470 for (l = 0; l < 3;) {
471
472 buf[l++] = *prmt_ptr;
473 buf[l] = 0;
474 ho = strtol(buf, &eho, c == 'x' ? 16 : 8);
475 if (ho > UCHAR_MAX || (eho - buf) < l) {
476 l--;
477 break;
478 }
479 prmt_ptr++;
480 }
481 buf[l] = 0;
482 ho = strtol(buf, 0, c == 'x' ? 16 : 8);
483 c = ho == 0 ? '?' : (char) ho;
484 break;
485 }
486 case '[':
487 case ']':
488 if (c == flg_not_length) {
489 flg_not_length = flg_not_length == '[' ? ']' : '[';
490 continue;
491 }
492 break;
493 }
494 }
495 buf[0] = c;
496 buf[1] = 0;
497 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf);
498 if (flg_not_length == ']')
499 sub_len++;
500 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000501#if 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000502 cmdedit_prmt_len = prmt_len - sub_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000503 cmdedit_prompt = prmt_ptr;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000504#endif
505 cmdedit_prompt = prmt_mem_ptr;
506 cmdedit_prmt_len = strlen(cmdedit_prompt);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000507 put_prompt();
508}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000509#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000510
511
512/* draw promt, editor line, and clear tail */
513static void redraw(int y, int back_cursor)
514{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000515 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000516 printf("\033[%dA", y);
517 cmdedit_y = 0; /* new quasireal y */
518 putchar('\r');
519 put_prompt();
520 input_end(); /* rewrite */
521 printf("\033[J"); /* destroy tail after cursor */
522 input_backward(back_cursor);
523}
524
Erik Andersenf0657d32000-04-12 17:49:52 +0000525/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000526static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000527{
Mark Whitley4e338752001-01-26 20:42:23 +0000528 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000529
Mark Whitley4e338752001-01-26 20:42:23 +0000530 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000531 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000532
533 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000534 len--;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000535 input_end(); /* rewtite new line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000536 cmdedit_set_out_char(0); /* destroy end char */
537 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000538}
539
Mark Whitley4e338752001-01-26 20:42:23 +0000540/* Delete the char in back of the cursor */
541static void input_backspace(void)
542{
543 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000544 input_backward(1);
545 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000546 }
547}
548
549
Erik Andersenf0657d32000-04-12 17:49:52 +0000550/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000551static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000552{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000553 if (cursor < len)
554 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000555}
556
557
Mark Whitley4e338752001-01-26 20:42:23 +0000558static void clean_up_and_die(int sig)
559{
560 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000561 if (sig != SIGINT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000562 exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */
Mark Whitley4e338752001-01-26 20:42:23 +0000563 cmdedit_reset_term();
564}
565
566static void cmdedit_setwidth(int w, int redraw_flg)
567{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000568 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000569 if (w <= cmdedit_termw) {
570 cmdedit_termw = cmdedit_termw % w;
571 }
Mark Whitley4e338752001-01-26 20:42:23 +0000572 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000573 cmdedit_termw = w;
574
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000575 if (redraw_flg) {
576 /* new y for current cursor */
577 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000578
579 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000580 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
581 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000582 }
Eric Andersen6faae7d2001-02-16 20:09:17 +0000583 }
Mark Whitley4e338752001-01-26 20:42:23 +0000584}
585
586extern void cmdedit_init(void)
587{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000588 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
589 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000590 win_changed(-SIGWINCH);
591 handlers_sets |= SET_WCHG_HANDLERS;
592 }
593
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000594 if ((handlers_sets & SET_ATEXIT) == 0) {
595#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
596 struct passwd *entry;
597
598 my_euid = geteuid();
599 entry = getpwuid(my_euid);
600 if (entry) {
601 user_buf = xstrdup(entry->pw_name);
602 home_pwd_buf = xstrdup(entry->pw_dir);
603 }
604#endif
605
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000606#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000607
608#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
609 my_euid = geteuid();
610#endif
611 my_uid = getuid();
612 my_gid = getgid();
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000613#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000614 handlers_sets |= SET_ATEXIT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000615 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000616 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000617
618 if ((handlers_sets & SET_TERM_HANDLERS) == 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000619 signal(SIGKILL, clean_up_and_die);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000620 signal(SIGINT, clean_up_and_die);
Mark Whitley4e338752001-01-26 20:42:23 +0000621 signal(SIGQUIT, clean_up_and_die);
622 signal(SIGTERM, clean_up_and_die);
623 handlers_sets |= SET_TERM_HANDLERS;
624 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000625
Mark Whitley4e338752001-01-26 20:42:23 +0000626}
Erik Andersenf0657d32000-04-12 17:49:52 +0000627
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000628#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000629
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000630static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000631{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000632 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
633 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
634 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
635 (st->st_mode & S_IXOTH)) return TRUE;
636 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000637}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000638
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000639#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000640
641static char **username_tab_completion(char *ud, int *num_matches)
642{
643 struct passwd *entry;
644 int userlen;
645 char *temp;
646
647
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000648 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000649 userlen = strlen(ud);
650
651 if (num_matches == 0) { /* "~/..." or "~user/..." */
652 char *sav_ud = ud - 1;
653 char *home = 0;
654
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000655 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000656 home = home_pwd_buf;
657 } else {
658 /* "~user/..." */
659 temp = strchr(ud, '/');
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000660 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000661 entry = getpwnam(ud);
662 *temp = '/'; /* restore ~user/... */
663 ud = temp;
664 if (entry)
665 home = entry->pw_dir;
666 }
667 if (home) {
668 if ((userlen + strlen(home) + 1) < BUFSIZ) {
669 char temp2[BUFSIZ]; /* argument size */
670
671 /* /home/user/... */
672 sprintf(temp2, "%s%s", home, ud);
673 strcpy(sav_ud, temp2);
674 }
675 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000676 return 0; /* void, result save to argument :-) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000677 } else {
678 /* "~[^/]*" */
679 char **matches = (char **) NULL;
680 int nm = 0;
681
682 setpwent();
683
684 while ((entry = getpwent()) != NULL) {
685 /* Null usernames should result in all users as possible completions. */
686 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
687
688 temp = xmalloc(3 + strlen(entry->pw_name));
689 sprintf(temp, "~%s/", entry->pw_name);
690 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
691
692 matches[nm++] = temp;
693 }
694 }
695
696 endpwent();
697 (*num_matches) = nm;
698 return (matches);
699 }
700}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000701#endif /* BB_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000702
703enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000704 FIND_EXE_ONLY = 0,
705 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000706 FIND_FILE_ONLY = 2,
707};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000708
Mark Whitley4e338752001-01-26 20:42:23 +0000709static int path_parse(char ***p, int flags)
710{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000711 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000712 char *tmp;
713 char *pth;
714
Mark Whitley4e338752001-01-26 20:42:23 +0000715 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000716 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
717 /* PATH=<empty> or PATH=:<empty> */
718 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000719 return 1;
720 }
721
722 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000723 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000724
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000725 for (;;) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000726 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000727 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000728 if (tmp) {
729 if (*++tmp == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000730 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000731 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000732 break;
733 }
734
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000735 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000736
737 tmp = pth;
738 (*p)[0] = xstrdup(tmp);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000739 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000740
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000741 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000742 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000743 if (tmp) {
744 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
745 if (*++tmp == 0)
746 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000747 } else
748 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000749 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000750 }
751
752 return npth;
753}
754
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000755static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000756{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000757 int l = 0;
758 char *s = xmalloc((strlen(found) + 1) * 2);
759
760 while (*found) {
761 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
762 s[l++] = '\\';
763 s[l++] = *found++;
764 }
765 s[l] = 0;
766 return s;
767}
768
769static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000770 int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000771{
772
773 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000774 DIR *dir;
775 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000776 char dirbuf[BUFSIZ];
777 int nm = *num_matches;
778 struct stat st;
779 char *path1[1];
780 char **paths = path1;
781 int npaths;
782 int i;
783 char found[BUFSIZ + 4 + PATH_MAX];
784 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000785
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000786 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000787
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000788 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000789 /* no dir, if flags==EXE_ONLY - get paths, else "." */
790 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000791 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000792 } else {
793 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000794 /* save for change */
795 strcpy(dirbuf, command);
796 /* set dir only */
797 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000798#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000799 if (dirbuf[0] == '~') /* ~/... or ~user/... */
800 username_tab_completion(dirbuf, 0);
801#endif
802 /* "strip" dirname in command */
803 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000804
Mark Whitley4e338752001-01-26 20:42:23 +0000805 paths[0] = dirbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000806 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000807 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000808
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000809 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000810
Mark Whitley4e338752001-01-26 20:42:23 +0000811 dir = opendir(paths[i]);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000812 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000813 continue;
814
815 while ((next = readdir(dir)) != NULL) {
816 const char *str_merge = "%s/%s";
817 char *str_found = next->d_name;
818
Mark Whitley4e338752001-01-26 20:42:23 +0000819 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000820 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000821 continue;
822 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000823 if (*str_found == '.' && *pfind == 0) {
824 if (*paths[i] == '/' && paths[i][1] == 0
825 && str_found[1] == 0) str_found = ""; /* only "/" */
826 else
Mark Whitley4e338752001-01-26 20:42:23 +0000827 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000828 }
829 if (paths[i][strlen(paths[i]) - 1] == '/')
830 str_merge = "%s%s";
831 sprintf(found, str_merge, paths[i], str_found);
832 /* hmm, remover in progress? */
833 if (stat(found, &st) < 0)
834 continue;
835 /* find with dirs ? */
836 if (paths[i] != dirbuf)
837 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000838 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000839 /* name is directory */
840 /* algorithmic only "/" ? */
841 if (*str_found)
842 strcat(found, "/");
843 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000844 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000845 /* not put found file if search only dirs for cd */
846 if (type == FIND_DIR_ONLY)
847 continue;
848 str_found = add_quote_for_spec_chars(found);
849 if (type == FIND_FILE_ONLY ||
850 (type == FIND_EXE_ONLY && is_execute(&st) == TRUE))
851 strcat(str_found, " ");
852 }
Mark Whitley4e338752001-01-26 20:42:23 +0000853 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000854 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
855
856 matches[nm++] = str_found;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000857 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000858 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000859 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000860 if (paths != path1) {
861 free(paths[0]); /* allocated memory only in first member */
862 free(paths);
863 }
Mark Whitley4e338752001-01-26 20:42:23 +0000864 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000865 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000866}
Erik Andersenf0657d32000-04-12 17:49:52 +0000867
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000868static int match_compare(const void *a, const void *b)
869{
870 return strcmp(*(char **) a, *(char **) b);
871}
872
873
874
875#define QUOT (UCHAR_MAX+1)
876
877#define collapse_pos(is, in) { \
878 memcpy(int_buf+is, int_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); \
879 memcpy(pos_buf+is, pos_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); }
880
881static int find_match(char *matchBuf, int *len_with_quotes)
882{
883 int i, j;
884 int command_mode;
885 int c, c2;
886 int int_buf[BUFSIZ + 1];
887 int pos_buf[BUFSIZ + 1];
888
889 /* set to integer dimension characters and own positions */
890 for (i = 0;; i++) {
891 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
892 if (int_buf[i] == 0) {
893 pos_buf[i] = -1; /* indicator end line */
894 break;
895 } else
896 pos_buf[i] = i;
897 }
898
899 /* mask \+symbol and convert '\t' to ' ' */
900 for (i = j = 0; matchBuf[i]; i++, j++)
901 if (matchBuf[i] == '\\') {
902 collapse_pos(j, j + 1);
903 int_buf[j] |= QUOT;
904 i++;
905#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
906 if (matchBuf[i] == '\t') /* algorithm equivalent */
907 int_buf[j] = ' ' | QUOT;
908#endif
909 }
910#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
911 else if (matchBuf[i] == '\t')
912 int_buf[j] = ' ';
913#endif
914
915 /* mask "symbols" or 'symbols' */
916 c2 = 0;
917 for (i = 0; int_buf[i]; i++) {
918 c = int_buf[i];
919 if (c == '\'' || c == '"') {
920 if (c2 == 0)
921 c2 = c;
922 else {
923 if (c == c2)
924 c2 = 0;
925 else
926 int_buf[i] |= QUOT;
927 }
928 } else if (c2 != 0 && c != '$')
929 int_buf[i] |= QUOT;
930 }
931
932 /* skip commands with arguments if line have commands delimiters */
933 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
934 for (i = 0; int_buf[i]; i++) {
935 c = int_buf[i];
936 c2 = int_buf[i + 1];
937 j = i ? int_buf[i - 1] : -1;
938 command_mode = 0;
939 if (c == ';' || c == '&' || c == '|') {
940 command_mode = 1 + (c == c2);
941 if (c == '&') {
942 if (j == '>' || j == '<')
943 command_mode = 0;
944 } else if (c == '|' && j == '>')
945 command_mode = 0;
946 }
947 if (command_mode) {
948 collapse_pos(0, i + command_mode);
949 i = -1; /* hack incremet */
950 }
951 }
952 /* collapse `command...` */
953 for (i = 0; int_buf[i]; i++)
954 if (int_buf[i] == '`') {
955 for (j = i + 1; int_buf[j]; j++)
956 if (int_buf[j] == '`') {
957 collapse_pos(i, j + 1);
958 j = 0;
959 break;
960 }
961 if (j) {
962 /* not found close ` - command mode, collapse all previous */
963 collapse_pos(0, i + 1);
964 break;
965 } else
966 i--; /* hack incremet */
967 }
968
969 /* collapse (command...(command...)...) or {command...{command...}...} */
970 c = 0; /* "recursive" level */
971 c2 = 0;
972 for (i = 0; int_buf[i]; i++)
973 if (int_buf[i] == '(' || int_buf[i] == '{') {
974 if (int_buf[i] == '(')
975 c++;
976 else
977 c2++;
978 collapse_pos(0, i + 1);
979 i = -1; /* hack incremet */
980 }
981 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
982 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
983 if (int_buf[i] == ')')
984 c--;
985 else
986 c2--;
987 collapse_pos(0, i + 1);
988 i = -1; /* hack incremet */
989 }
990
991 /* skip first not quote space */
992 for (i = 0; int_buf[i]; i++)
993 if (int_buf[i] != ' ')
994 break;
995 if (i)
996 collapse_pos(0, i);
997
998 /* set find mode for completion */
999 command_mode = FIND_EXE_ONLY;
1000 for (i = 0; int_buf[i]; i++)
1001 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
1002 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001003 && matchBuf[pos_buf[0]]=='c'
1004 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001005 command_mode = FIND_DIR_ONLY;
1006 else {
1007 command_mode = FIND_FILE_ONLY;
1008 break;
1009 }
1010 }
1011 /* "strlen" */
1012 for (i = 0; int_buf[i]; i++);
1013 /* find last word */
1014 for (--i; i >= 0; i--) {
1015 c = int_buf[i];
1016 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
1017 collapse_pos(0, i + 1);
1018 break;
1019 }
1020 }
1021 /* skip first not quoted '\'' or '"' */
1022 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
1023 /* collapse quote or unquote // or /~ */
Mark Whitley7e5291f2001-03-08 19:31:12 +00001024 while ((int_buf[i] & ~QUOT) == '/' &&
1025 ((int_buf[i + 1] & ~QUOT) == '/'
1026 || (int_buf[i + 1] & ~QUOT) == '~')) {
1027 i++;
1028 }
1029 if (i) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001030 collapse_pos(0, i);
Mark Whitley7e5291f2001-03-08 19:31:12 +00001031 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001032
1033 /* set only match and destroy quotes */
1034 j = 0;
1035 for (i = 0; pos_buf[i] >= 0; i++) {
1036 matchBuf[i] = matchBuf[pos_buf[i]];
1037 j = pos_buf[i] + 1;
1038 }
1039 matchBuf[i] = 0;
1040 /* old lenght matchBuf with quotes symbols */
1041 *len_with_quotes = j ? j - pos_buf[0] : 0;
1042
1043 return command_mode;
1044}
1045
1046
1047static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001048{
1049 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001050 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +00001051 static char **matches;
1052
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001053 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001054 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001055 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001056 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001057 free(matches);
1058 matches = (char **) NULL;
1059 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001060 return;
1061 }
1062 if (*lastWasTab == FALSE) {
1063
1064 char *tmp;
1065 int len_found;
1066 char matchBuf[BUFSIZ];
1067 int find_type;
1068 int recalc_pos;
1069
1070 *lastWasTab = TRUE; /* flop trigger */
1071
1072 /* Make a local copy of the string -- up
1073 * to the position of the cursor */
1074 tmp = strncpy(matchBuf, command_ps, cursor);
1075 tmp[cursor] = 0;
1076
1077 find_type = find_match(matchBuf, &recalc_pos);
1078
1079 /* Free up any memory already allocated */
1080 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001081
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001082#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001083 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001084 * then try completing this word as a username. */
1085
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001086 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001087 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001088#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001089 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001090 * in the current working directory that matches. */
1091 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001092 matches =
1093 exe_n_cwd_tab_completion(matchBuf, &num_matches,
1094 find_type);
Erik Andersenf0657d32000-04-12 17:49:52 +00001095
1096 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001097 if (!matches || num_matches > 1) {
1098 char *tmp1;
1099
Mark Whitley4e338752001-01-26 20:42:23 +00001100 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001101 if (!matches)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001102 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001103 /* sort */
1104 qsort(matches, num_matches, sizeof(char *), match_compare);
1105
1106 /* find minimal match */
1107 tmp = xstrdup(matches[0]);
1108 for (tmp1 = tmp; *tmp1; tmp1++)
1109 for (len_found = 1; len_found < num_matches; len_found++)
1110 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1111 *tmp1 = 0;
1112 break;
1113 }
1114 if (*tmp == 0) { /* have unique */
1115 free(tmp);
1116 return;
1117 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001118 } else { /* one match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001119 tmp = matches[0];
1120 /* for next completion current found */
1121 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001122 }
1123
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001124 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001125 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001126 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001127
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001128 /* before word for match */
1129 command_ps[cursor - recalc_pos] = 0;
1130 /* save tail line */
1131 strcpy(matchBuf, command_ps + cursor);
1132 /* add match */
1133 strcat(command_ps, tmp);
1134 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001135 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001136 /* back to begin word for match */
1137 input_backward(recalc_pos);
1138 /* new pos */
1139 recalc_pos = cursor + len_found;
1140 /* new len */
1141 len = strlen(command_ps);
1142 /* write out the matched command */
1143 input_end();
1144 input_backward(cursor - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001145 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001146 if (tmp != matches[0])
1147 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001148 } else {
1149 /* Ok -- the last char was a TAB. Since they
1150 * just hit TAB again, print a list of all the
1151 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001152 if (matches && num_matches > 0) {
1153 int i, col, l;
1154 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001155
1156 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001157 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001158 for (i = 0, col = 0; i < num_matches; i++) {
1159 l = strlen(matches[i]);
1160 if (l < 14)
1161 l = 14;
1162 printf("%-14s ", matches[i]);
1163 if ((l += 2) > 16)
1164 while (l % 16) {
1165 putchar(' ');
1166 l++;
1167 }
1168 col += l;
1169 col -= (col / cmdedit_termw) * cmdedit_termw;
1170 if (col > 60 && matches[i + 1] != NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +00001171 putchar('\n');
Erik Andersenf0657d32000-04-12 17:49:52 +00001172 col = 0;
1173 }
1174 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001175 /* Go to the next line and rewrite */
1176 putchar('\n');
1177 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001178 }
1179 }
1180}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001181#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001182
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001183static void get_previous_history(struct history **hp, struct history *p)
Erik Andersenf0657d32000-04-12 17:49:52 +00001184{
Eric Andersen91a44002000-07-19 17:37:57 +00001185 if ((*hp)->s)
1186 free((*hp)->s);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001187 (*hp)->s = xstrdup(command_ps);
1188 *hp = p;
Erik Andersenf0657d32000-04-12 17:49:52 +00001189}
1190
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001191static inline void get_next_history(struct history **hp)
Erik Andersenf0657d32000-04-12 17:49:52 +00001192{
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001193 get_previous_history(hp, (*hp)->n);
Erik Andersenf0657d32000-04-12 17:49:52 +00001194}
1195
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001196enum {
1197 ESC = 27,
1198 DEL = 127,
1199};
1200
1201
Erik Andersen6273f652000-03-17 01:12:41 +00001202/*
1203 * This function is used to grab a character buffer
1204 * from the input file descriptor and allows you to
1205 * a string with full command editing (sortof like
1206 * a mini readline).
1207 *
1208 * The following standard commands are not implemented:
1209 * ESC-b -- Move back one word
1210 * ESC-f -- Move forward one word
1211 * ESC-d -- Delete back one word
1212 * ESC-h -- Delete forward one word
1213 * CTL-t -- Transpose two characters
1214 *
1215 * Furthermore, the "vi" command editing keys are not implemented.
1216 *
Erik Andersen6273f652000-03-17 01:12:41 +00001217 */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001218
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001219extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001220{
1221
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001222 int inputFd = fileno(stdin);
Mark Whitley4e338752001-01-26 20:42:23 +00001223
Erik Andersenc7c634b2000-03-19 05:28:55 +00001224 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001225 int lastWasTab = FALSE;
1226 char c = 0;
1227 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +00001228
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001229 /* prepare before init handlers */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001230 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001231 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001232 command_ps = command;
1233
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001234 if (new_settings.c_cc[VMIN] == 0) { /* first call */
1235
1236 getTermSettings(inputFd, (void *) &initial_settings);
Erik Andersen1d1d9502000-04-21 01:26:49 +00001237 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001238
Erik Andersen1d1d9502000-04-21 01:26:49 +00001239 new_settings.c_cc[VMIN] = 1;
1240 new_settings.c_cc[VTIME] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001241 /* Turn off CTRL-C, so we can trap it */
1242 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001243 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001244 /* Turn off echoing */
1245 new_settings.c_lflag &= ~(ECHO | ECHOCTL | ECHONL);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001246 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001247
1248 command[0] = 0;
1249
1250 setTermSettings(inputFd, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001251 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001252
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001253 /* Print out the command prompt */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001254 parse_prompt(prompt);
Eric Andersen6faae7d2001-02-16 20:09:17 +00001255 /* Now initialize things */
1256 cmdedit_init();
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001257
Erik Andersenc7c634b2000-03-19 05:28:55 +00001258 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001259
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001260 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001261
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001262 if (read(inputFd, &c, 1) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +00001263 return;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001264
Erik Andersen13456d12000-03-16 08:09:57 +00001265 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001266 case '\n':
1267 case '\r':
1268 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001269 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001270 break_out = 1;
1271 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001272 case 1:
1273 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001274 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001275 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001276 case 2:
1277 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001278 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001279 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001280 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001281 /* Control-c -- stop gathering input */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001282
Eric Andersen86349772000-12-18 20:25:50 +00001283 /* Link into lash to reset context to 0 on ^C and such */
1284 shell_context = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001285
1286 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001287 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001288 command[0] = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001289
Eric Andersen86349772000-12-18 20:25:50 +00001290 return;
Erik Andersenf0657d32000-04-12 17:49:52 +00001291 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001292 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001293 * if the len=0 and no chars to delete */
1294 if (len == 0) {
Mark Whitley4e338752001-01-26 20:42:23 +00001295 printf("exit");
Erik Andersenf0657d32000-04-12 17:49:52 +00001296 clean_up_and_die(0);
1297 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001298 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001299 }
1300 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001301 case 5:
1302 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001303 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001304 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001305 case 6:
1306 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001307 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001308 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001309 case '\b':
1310 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001311 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001312 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001313 break;
1314 case '\t':
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001315#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001316 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001317#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001318 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001319 case 14:
1320 /* Control-n -- Get next command in history */
1321 if (hp && hp->n && hp->n->s) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001322 get_next_history(&hp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001323 goto rewrite_line;
1324 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001325 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001326 }
1327 break;
1328 case 16:
1329 /* Control-p -- Get previous command from history */
1330 if (hp && hp->p) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001331 get_previous_history(&hp, hp->p);
Erik Andersenf0657d32000-04-12 17:49:52 +00001332 goto rewrite_line;
1333 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001334 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001335 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001336 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001337 case 21:
1338 /* Control-U -- Clear line before cursor */
1339 if (cursor) {
1340 strcpy(command, command + cursor);
1341 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001342 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001343 break;
1344
1345 case ESC:{
1346 /* escape sequence follows */
1347 if (read(inputFd, &c, 1) < 1)
1348 return;
1349 /* different vt100 emulations */
1350 if (c == '[' || c == 'O') {
1351 if (read(inputFd, &c, 1) < 1)
1352 return;
1353 }
1354 switch (c) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001355#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001356 case '\t': /* Alt-Tab */
1357
1358 input_tab(&lastWasTab);
1359 break;
1360#endif
1361 case 'A':
1362 /* Up Arrow -- Get previous command from history */
1363 if (hp && hp->p) {
1364 get_previous_history(&hp, hp->p);
1365 goto rewrite_line;
1366 } else {
1367 beep();
1368 }
1369 break;
1370 case 'B':
1371 /* Down Arrow -- Get next command in history */
1372 if (hp && hp->n && hp->n->s) {
1373 get_next_history(&hp);
1374 goto rewrite_line;
1375 } else {
1376 beep();
1377 }
1378 break;
1379
1380 /* Rewrite the line with the selected history item */
1381 rewrite_line:
1382 /* change command */
1383 len = strlen(strcpy(command, hp->s));
1384 /* redraw and go to end line */
1385 redraw(cmdedit_y, 0);
1386 break;
1387 case 'C':
1388 /* Right Arrow -- Move forward one character */
1389 input_forward();
1390 break;
1391 case 'D':
1392 /* Left Arrow -- Move back one character */
1393 input_backward(1);
1394 break;
1395 case '3':
1396 /* Delete */
1397 input_delete();
1398 break;
1399 case '1':
1400 case 'H':
1401 /* Home (Ctrl-A) */
1402 input_backward(cursor);
1403 break;
1404 case '4':
1405 case 'F':
1406 /* End (Ctrl-E) */
1407 input_end();
1408 break;
1409 default:
1410 if (!(c >= '1' && c <= '9'))
1411 c = 0;
1412 beep();
1413 }
1414 if (c >= '1' && c <= '9')
1415 do
1416 if (read(inputFd, &c, 1) < 1)
1417 return;
1418 while (c != '~');
1419 break;
1420 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001421
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001422 default: /* If it's regular input, do the normal thing */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001423#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1424 /* Control-V -- Add non-printable symbol */
1425 if (c == 22) {
1426 if (read(inputFd, &c, 1) < 1)
1427 return;
1428 if (c == 0) {
1429 beep();
1430 break;
1431 }
1432 } else
1433#endif
1434 if (!isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001435 break;
1436
1437 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
1438 break;
1439
1440 len++;
1441
1442 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001443 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001444 *(command + cursor + 1) = 0;
1445 cmdedit_set_out_char(0);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001446 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001447 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001448
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001449 memmove(command + sc + 1, command + sc, len - sc);
1450 *(command + sc) = c;
1451 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001452 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001453 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001454 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001455 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001456 }
1457
Erik Andersenc7c634b2000-03-19 05:28:55 +00001458 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001459 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001460 if (break_out) /* Enter is the command terminator, no more input. */
1461 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001462
1463 if (c != '\t')
1464 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001465 }
1466
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001467 setTermSettings(inputFd, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001468 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001469
1470 /* Handle command history log */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001471 if (len) { /* no put empty line */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001472
1473 struct history *h = his_end;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001474 char *ss;
Mark Whitley4e338752001-01-26 20:42:23 +00001475
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001476 ss = xstrdup(command); /* duplicate */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001477
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001478 if (h == 0) {
Eric Andersen91a44002000-07-19 17:37:57 +00001479 /* No previous history -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001480 h = his_front = xmalloc(sizeof(struct history));
1481 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001482
1483 h->p = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001484 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001485 h->n->p = h;
1486 h->n->n = NULL;
1487 h->n->s = NULL;
1488 his_end = h->n;
1489 history_counter++;
1490 } else {
Eric Andersen91a44002000-07-19 17:37:57 +00001491 /* Add a new history command -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001492 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001493
1494 h->n->p = h;
1495 h->n->n = NULL;
1496 h->n->s = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001497 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001498 his_end = h->n;
1499
1500 /* After max history, remove the oldest command */
1501 if (history_counter >= MAX_HISTORY) {
1502
1503 struct history *p = his_front->n;
1504
1505 p->p = NULL;
1506 free(his_front->s);
1507 free(his_front);
1508 his_front = p;
1509 } else {
1510 history_counter++;
1511 }
Erik Andersen13456d12000-03-16 08:09:57 +00001512 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001513#if !defined(BB_FEATURE_SIMPLE_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001514 num_ok_lines++;
1515#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001516 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001517 command[len++] = '\n'; /* set '\n' */
1518 command[len] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001519#if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001520 input_tab(0); /* strong free */
1521#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001522#if !defined(BB_FEATURE_SIMPLE_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001523 free(cmdedit_prompt);
1524#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001525 return;
Erik Andersen13456d12000-03-16 08:09:57 +00001526}
1527
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001528
Mark Whitley4e338752001-01-26 20:42:23 +00001529/* Undo the effects of cmdedit_init(). */
Eric Andersen501c88b2000-07-28 15:14:45 +00001530extern void cmdedit_terminate(void)
1531{
1532 cmdedit_reset_term();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001533 if ((handlers_sets & SET_TERM_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +00001534 signal(SIGKILL, SIG_DFL);
1535 signal(SIGINT, SIG_DFL);
1536 signal(SIGQUIT, SIG_DFL);
1537 signal(SIGTERM, SIG_DFL);
1538 signal(SIGWINCH, SIG_DFL);
1539 handlers_sets &= ~SET_TERM_HANDLERS;
1540 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001541}
1542
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001543#endif /* BB_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001544
1545
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001546#ifdef TEST
1547
1548unsigned int shell_context;
1549
1550int main(int argc, char **argv)
1551{
1552 char buff[BUFSIZ];
1553 char *prompt =
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001554#if !defined(BB_FEATURE_SIMPLE_PROMPT)
1555 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001556\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001557\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001558#else
1559 "% ";
1560#endif
1561
1562 shell_context = 1;
1563 do {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001564 int l;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001565 cmdedit_read_input(prompt, buff);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001566 l = strlen(buff);
1567 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);
1570 } while (shell_context);
1571 printf("*** cmdedit_read_input() detect ^C\n");
1572 return 0;
1573}
1574
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001575#endif /* TEST */