blob: 9cb53522fe0c730b2ed50b24327dbd58d19f3bda [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)
Erik Andersen13456d12000-03-16 08:09:57 +000030 */
31
Mark Whitley4e338752001-01-26 20:42:23 +000032
Eric Andersen6faae7d2001-02-16 20:09:17 +000033//#define TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000034
Eric Andersencbe31da2001-02-20 06:14:08 +000035#include <stdio.h>
36#include <errno.h>
37#include <unistd.h>
38#include <stdlib.h>
39#include <string.h>
40#include <sys/ioctl.h>
41#include <ctype.h>
42#include <signal.h>
43#include <limits.h>
44
45#ifdef BB_FEATURE_SH_TAB_COMPLETION
46#include <dirent.h>
47#include <sys/stat.h>
48#endif
49
Eric Andersen5f2c79d2001-02-16 18:36:04 +000050
51#ifndef TEST
Mark Whitley4e338752001-01-26 20:42:23 +000052
Eric Andersen3570a342000-09-25 21:45:58 +000053#include "busybox.h"
Mark Whitley4e338752001-01-26 20:42:23 +000054
Eric Andersen5f2c79d2001-02-16 18:36:04 +000055#define D(x)
56
57#else
58
Eric Andersen94456f52001-02-18 20:26:48 +000059#define BB_FEATURE_SH_COMMAND_EDITING
60#define BB_FEATURE_SH_TAB_COMPLETION
61#define BB_FEATURE_SH_USERNAME_COMPLETION
62#define BB_FEATURE_NONPRINTABLE_INVERSE_PUT
63#define BB_FEATURE_BASH_STYLE_PROMT
64#define BB_FEATURE_CLEAN_UP
Eric Andersen5f2c79d2001-02-16 18:36:04 +000065
Eric Andersen5f2c79d2001-02-16 18:36:04 +000066#define D(x) x
67
68#endif /* TEST */
69
Erik Andersen13456d12000-03-16 08:09:57 +000070#ifdef BB_FEATURE_SH_COMMAND_EDITING
71
Eric Andersen5f2c79d2001-02-16 18:36:04 +000072#ifndef BB_FEATURE_SH_TAB_COMPLETION
Eric Andersen28a78ab2001-02-16 20:26:50 +000073#undef BB_FEATURE_SH_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +000074#endif
75
Eric Andersen28a78ab2001-02-16 20:26:50 +000076#if defined(BB_FEATURE_SH_USERNAME_COMPLETION) || defined(BB_FEATURE_BASH_STYLE_PROMT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000077#define BB_FEATURE_GETUSERNAME_AND_HOMEDIR
78#endif
79
Eric Andersen5f2c79d2001-02-16 18:36:04 +000080#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
81#ifndef TEST
Eric Andersenaf4ac772001-02-01 22:43:49 +000082#include "pwd_grp/pwd.h"
Eric Andersen5f2c79d2001-02-16 18:36:04 +000083#else
84#include <pwd.h>
85#endif /* TEST */
86#endif /* advanced FEATURES */
Eric Andersenaf4ac772001-02-01 22:43:49 +000087
88
Eric Andersen5f2c79d2001-02-16 18:36:04 +000089#ifdef TEST
90void *xrealloc(void *old, size_t size)
91{
92 return realloc(old, size);
93}
Erik Andersen13456d12000-03-16 08:09:57 +000094
Eric Andersen5f2c79d2001-02-16 18:36:04 +000095void *xmalloc(size_t size)
96{
97 return malloc(size);
98}
99char *xstrdup(const char *s)
100{
101 return strdup(s);
102}
103
104void *xcalloc(size_t size, size_t se)
105{
106 return calloc(size, se);
107}
108
109#define error_msg(s, d) fprintf(stderr, s, d)
110#endif
111
112
113struct history {
114 char *s;
115 struct history *p;
116 struct history *n;
Mark Whitley59ab0252001-01-23 22:30:04 +0000117};
118
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000119/* Maximum length of the linked list for the command line history */
120static const int MAX_HISTORY = 15;
Erik Andersen13456d12000-03-16 08:09:57 +0000121
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000122/* First element in command line list */
123static struct history *his_front = NULL;
124
125/* Last element in command line list */
126static struct history *his_end = NULL;
127
Erik Andersen1d1d9502000-04-21 01:26:49 +0000128
129/* ED: sparc termios is broken: revert back to old termio handling. */
Erik Andersen1d1d9502000-04-21 01:26:49 +0000130
131#if #cpu(sparc)
132# include <termio.h>
133# define termios termio
134# define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp)
135# define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp)
136#else
137# include <termios.h>
138# define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
139# define getTermSettings(fd,argp) tcgetattr(fd, argp);
140#endif
141
142/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000143static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000144
145
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000146#ifndef _POSIX_VDISABLE
147#define _POSIX_VDISABLE '\0'
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000148#endif
149
Erik Andersen1d1d9502000-04-21 01:26:49 +0000150
Mark Whitley4e338752001-01-26 20:42:23 +0000151static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000152volatile int cmdedit_termw = 80; /* actual terminal width */
153static int history_counter = 0; /* Number of commands in history list */
Mark Whitley4e338752001-01-26 20:42:23 +0000154static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000155volatile int handlers_sets = 0; /* Set next bites: */
156
Mark Whitley4e338752001-01-26 20:42:23 +0000157enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000158 SET_ATEXIT = 1, /* when atexit() has been called and
159 get euid,uid,gid to fast compare */
160 SET_TERM_HANDLERS = 2, /* set many terminates signal handlers */
161 SET_WCHG_HANDLERS = 4, /* winchg signal handler */
162 SET_RESET_TERM = 8, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000163};
Erik Andersen13456d12000-03-16 08:09:57 +0000164
Mark Whitley4e338752001-01-26 20:42:23 +0000165
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000166static int cmdedit_x; /* real x terminal position */
167static int cmdedit_y; /* pseudoreal y terminal position */
168static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000169
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000170static int cursor; /* required global for signal handler */
171static int len; /* --- "" - - "" - -"- --""-- --""--- */
172static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
173static
174#ifndef BB_FEATURE_BASH_STYLE_PROMT
175 const
176#endif
177char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
178
179/* Link into lash to reset context to 0 on ^C and such */
Eric Andersen86349772000-12-18 20:25:50 +0000180extern unsigned int shell_context;
181
Erik Andersen13456d12000-03-16 08:09:57 +0000182
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000183#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
184static char *user_buf = "";
185static char *home_pwd_buf = "";
186static int my_euid;
187#endif
188
189#ifdef BB_FEATURE_BASH_STYLE_PROMT
190static char *hostname_buf = "";
191static int num_ok_lines = 1;
192#endif
193
194
195#ifdef BB_FEATURE_SH_TAB_COMPLETION
196
197#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
198static int my_euid;
199#endif
200
201static int my_uid;
202static int my_gid;
203
204#endif /* BB_FEATURE_SH_TAB_COMPLETION */
205
Erik Andersen13456d12000-03-16 08:09:57 +0000206
Mark Whitley4e338752001-01-26 20:42:23 +0000207static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000208
Mark Whitley4e338752001-01-26 20:42:23 +0000209static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000210{
211 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000212 static __sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000213
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000214 /* emulate || signal call */
215 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Mark Whitley4e338752001-01-26 20:42:23 +0000216 ioctl(0, TIOCGWINSZ, &win);
217 if (win.ws_col > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000218 cmdedit_setwidth(win.ws_col, nsig == SIGWINCH);
219 }
Mark Whitley4e338752001-01-26 20:42:23 +0000220 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000221 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000222
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000223 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000224 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000225 else if (nsig == SIGWINCH) /* signaled called handler */
226 signal(SIGWINCH, win_changed); /* set for next call */
227 else /* nsig == 0 */
228 /* set previous handler */
229 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000230}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000231
232static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000233{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000234 if ((handlers_sets & SET_RESET_TERM) != 0) {
Erik Andersena6c75222000-04-18 00:00:52 +0000235 /* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000236 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000237 handlers_sets &= ~SET_RESET_TERM;
238 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000239 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000240 /* reset SIGWINCH handler to previous (default) */
241 win_changed(0);
242 handlers_sets &= ~SET_WCHG_HANDLERS;
243 }
244 fflush(stdout);
Eric Andersenb040d4f2000-07-25 18:01:20 +0000245#ifdef BB_FEATURE_CLEAN_UP
246 if (his_front) {
247 struct history *n;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000248
Eric Andersenb040d4f2000-07-25 18:01:20 +0000249 //while(his_front!=his_end) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000250 while (his_front != his_end) {
Eric Andersenb040d4f2000-07-25 18:01:20 +0000251 n = his_front->n;
252 free(his_front->s);
253 free(his_front);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000254 his_front = n;
Eric Andersenb040d4f2000-07-25 18:01:20 +0000255 }
256 }
257#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000258}
259
Mark Whitley4e338752001-01-26 20:42:23 +0000260
Mark Whitley4e338752001-01-26 20:42:23 +0000261/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000262static void cmdedit_set_out_char(int next_char)
263{
264
265 int c = command_ps[cursor];
266
267 if (c == 0)
268 c = ' '; /* destroy end char? */
269#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
270 if (!isprint(c)) { /* Inverse put non-printable characters */
271 if (((unsigned char) c) >= 128)
272 c -= 128;
273 if (((unsigned char) c) < ' ')
274 c += '@';
275 if (c == 127)
276 c = '?';
277 printf("\033[7m%c\033[0m", c);
278 } else
279#endif
280 putchar(c);
281 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000282 /* terminal is scrolled down */
283 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000284 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000285
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000286 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000287 next_char = ' ';
288 /* destroy "(auto)margin" */
289 putchar(next_char);
290 putchar('\b');
291 }
292 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000293}
294
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000295/* Move to end line. Bonus: rewrite line from cursor */
296static void input_end(void)
297{
298 while (cursor < len)
299 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000300}
301
302/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000303static void goto_new_line(void)
304{
Mark Whitley4e338752001-01-26 20:42:23 +0000305 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000306 if (cmdedit_x)
307 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000308}
309
310
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000311static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000312{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000313 fputs(s, stdout);
314}
315static inline void beep(void)
316{
317 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000318}
319
Mark Whitley4e338752001-01-26 20:42:23 +0000320/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000321/* special for slow terminal */
322static void input_backward(int num)
323{
324 if (num > cursor)
325 num = cursor;
326 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000327
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000328 if (cmdedit_x >= num) { /* no to up line */
329 cmdedit_x -= num;
330 if (num < 4)
331 while (num-- > 0)
332 putchar('\b');
333
334 else
335 printf("\033[%dD", num);
336 } else {
337 int count_y;
338
339 if (cmdedit_x) {
340 putchar('\r'); /* back to first terminal pos. */
341 num -= cmdedit_x; /* set previous backward */
342 }
343 count_y = 1 + num / cmdedit_termw;
344 printf("\033[%dA", count_y);
345 cmdedit_y -= count_y;
346 /* require forward after uping */
347 cmdedit_x = cmdedit_termw * count_y - num;
348 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000349 }
Erik Andersen13456d12000-03-16 08:09:57 +0000350}
351
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000352static void put_prompt(void)
353{
354 out1str(cmdedit_prompt);
355 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
356 cursor = 0;
357}
358
359#ifdef BB_FEATURE_BASH_STYLE_PROMT
360static void
361add_to_prompt(char **prmt_mem_ptr, int *alm, int *prmt_len,
362 const char *addb)
363{
364 int l = strlen(addb);
365
366 *prmt_len += l;
367 if (*alm < (*prmt_len) + 1) {
368 *alm = (*prmt_len) + 1;
369 *prmt_mem_ptr = xrealloc(*prmt_mem_ptr, *alm);
370 }
371 strcat(*prmt_mem_ptr, addb);
372}
373#endif
374
375static void parse_prompt(const char *prmt_ptr)
376{
377#ifdef BB_FEATURE_BASH_STYLE_PROMT
378 int alm = strlen(prmt_ptr) + 1; /* supposedly require memory */
379 int prmt_len = 0;
380 int sub_len = 0;
381 int flg_not_length = '[';
382 char *prmt_mem_ptr = xstrdup(prmt_ptr);
383 char pwd_buf[PATH_MAX + 1];
384 char buf[16];
385 int c;
386
387 pwd_buf[0] = 0;
388 *prmt_mem_ptr = 0;
389
390 while (*prmt_ptr) {
391 c = *prmt_ptr++;
392 if (c == '\\') {
393 c = *prmt_ptr;
394 if (c == 0)
395 break;
396 prmt_ptr++;
397 switch (c) {
398 case 'u':
399 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, user_buf);
400 continue;
401 case 'h':
402 if (hostname_buf[0] == 0) {
403 hostname_buf = xcalloc(256, 1);
404 if (gethostname(hostname_buf, 255) < 0) {
405 strcpy(hostname_buf, "?");
406 } else {
407 char *s = strchr(hostname_buf, '.');
408
409 if (s)
410 *s = 0;
411 }
412 }
413 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len,
414 hostname_buf);
415 continue;
416 case '$':
417 c = my_euid == 0 ? '#' : '$';
418 break;
419 case 'w':
420 if (pwd_buf[0] == 0) {
421 int l;
422
423 getcwd(pwd_buf, PATH_MAX);
424 l = strlen(home_pwd_buf);
425 if (home_pwd_buf[0] != 0 &&
426 strncmp(home_pwd_buf, pwd_buf, l) == 0) {
427 strcpy(pwd_buf + 1, pwd_buf + l);
428 pwd_buf[0] = '~';
429 }
430 }
431 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf);
432 continue;
433 case '!':
434 snprintf(buf, sizeof(buf), "%d", num_ok_lines);
435 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf);
436 continue;
437 case 'e':
438 case 'E': /* \e \E = \033 */
439 c = '\033';
440 break;
441 case 'x':
442 case 'X':
443 case '0':
444 case '1':
445 case '2':
446 case '3':
447 case '4':
448 case '5':
449 case '6':
450 case '7':{
451 int l;
452 int ho = 0;
453 char *eho;
454
455 if (c == 'X')
456 c = 'x';
457
458 for (l = 0; l < 3;) {
459
460 buf[l++] = *prmt_ptr;
461 buf[l] = 0;
462 ho = strtol(buf, &eho, c == 'x' ? 16 : 8);
463 if (ho > UCHAR_MAX || (eho - buf) < l) {
464 l--;
465 break;
466 }
467 prmt_ptr++;
468 }
469 buf[l] = 0;
470 ho = strtol(buf, 0, c == 'x' ? 16 : 8);
471 c = ho == 0 ? '?' : (char) ho;
472 break;
473 }
474 case '[':
475 case ']':
476 if (c == flg_not_length) {
477 flg_not_length = flg_not_length == '[' ? ']' : '[';
478 continue;
479 }
480 break;
481 }
482 }
483 buf[0] = c;
484 buf[1] = 0;
485 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf);
486 if (flg_not_length == ']')
487 sub_len++;
488 }
489 cmdedit_prmt_len = prmt_len - sub_len;
490 cmdedit_prompt = prmt_mem_ptr;
491#else
492 cmdedit_prompt = prmt_ptr;
493 cmdedit_prmt_len = strlen(prmt_ptr);
494#endif
495 put_prompt();
496}
497
498
499/* draw promt, editor line, and clear tail */
500static void redraw(int y, int back_cursor)
501{
502 if (y > 0) /* up to start y */
503 printf("\033[%dA", y);
504 cmdedit_y = 0; /* new quasireal y */
505 putchar('\r');
506 put_prompt();
507 input_end(); /* rewrite */
508 printf("\033[J"); /* destroy tail after cursor */
509 input_backward(back_cursor);
510}
511
Erik Andersenf0657d32000-04-12 17:49:52 +0000512/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000513static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000514{
Mark Whitley4e338752001-01-26 20:42:23 +0000515 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000516
Mark Whitley4e338752001-01-26 20:42:23 +0000517 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000518 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000519
520 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000521 len--;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000522 input_end(); /* rewtite new line */
523 cmdedit_set_out_char(0); /* destroy end char */
524 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000525}
526
Mark Whitley4e338752001-01-26 20:42:23 +0000527/* Delete the char in back of the cursor */
528static void input_backspace(void)
529{
530 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000531 input_backward(1);
532 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000533 }
534}
535
536
Erik Andersenf0657d32000-04-12 17:49:52 +0000537/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000538static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000539{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000540 if (cursor < len)
541 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000542}
543
544
Mark Whitley4e338752001-01-26 20:42:23 +0000545static void clean_up_and_die(int sig)
546{
547 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000548 if (sig != SIGINT)
549 exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */
Mark Whitley4e338752001-01-26 20:42:23 +0000550 cmdedit_reset_term();
551}
552
553static void cmdedit_setwidth(int w, int redraw_flg)
554{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000555 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000556 if (w <= cmdedit_termw) {
557 cmdedit_termw = cmdedit_termw % w;
558 }
Mark Whitley4e338752001-01-26 20:42:23 +0000559 if (w > cmdedit_termw) {
560
561 cmdedit_termw = w;
562
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000563 if (redraw_flg) {
564 /* new y for current cursor */
565 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000566
567 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000568 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
569 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000570 }
Eric Andersen6faae7d2001-02-16 20:09:17 +0000571 }
Mark Whitley4e338752001-01-26 20:42:23 +0000572}
573
574extern void cmdedit_init(void)
575{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000576 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
577 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000578 win_changed(-SIGWINCH);
579 handlers_sets |= SET_WCHG_HANDLERS;
580 }
581
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000582 if ((handlers_sets & SET_ATEXIT) == 0) {
583#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
584 struct passwd *entry;
585
586 my_euid = geteuid();
587 entry = getpwuid(my_euid);
588 if (entry) {
589 user_buf = xstrdup(entry->pw_name);
590 home_pwd_buf = xstrdup(entry->pw_dir);
591 }
592#endif
593
594#ifdef BB_FEATURE_SH_TAB_COMPLETION
595
596#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
597 my_euid = geteuid();
598#endif
599 my_uid = getuid();
600 my_gid = getgid();
601#endif /* BB_FEATURE_SH_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000602 handlers_sets |= SET_ATEXIT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000603 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000604 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000605
606 if ((handlers_sets & SET_TERM_HANDLERS) == 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000607 signal(SIGKILL, clean_up_and_die);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000608 signal(SIGINT, clean_up_and_die);
Mark Whitley4e338752001-01-26 20:42:23 +0000609 signal(SIGQUIT, clean_up_and_die);
610 signal(SIGTERM, clean_up_and_die);
611 handlers_sets |= SET_TERM_HANDLERS;
612 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000613
Mark Whitley4e338752001-01-26 20:42:23 +0000614}
Erik Andersenf0657d32000-04-12 17:49:52 +0000615
616#ifdef BB_FEATURE_SH_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000617
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000618static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000619{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000620 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
621 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
622 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
623 (st->st_mode & S_IXOTH)) return TRUE;
624 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000625}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000626
Eric Andersen28a78ab2001-02-16 20:26:50 +0000627#ifdef BB_FEATURE_SH_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000628
629static char **username_tab_completion(char *ud, int *num_matches)
630{
631 struct passwd *entry;
632 int userlen;
633 char *temp;
634
635
636 ud++; /* ~user/... to user/... */
637 userlen = strlen(ud);
638
639 if (num_matches == 0) { /* "~/..." or "~user/..." */
640 char *sav_ud = ud - 1;
641 char *home = 0;
642
643 if (*ud == '/') { /* "~/..." */
644 home = home_pwd_buf;
645 } else {
646 /* "~user/..." */
647 temp = strchr(ud, '/');
648 *temp = 0; /* ~user\0 */
649 entry = getpwnam(ud);
650 *temp = '/'; /* restore ~user/... */
651 ud = temp;
652 if (entry)
653 home = entry->pw_dir;
654 }
655 if (home) {
656 if ((userlen + strlen(home) + 1) < BUFSIZ) {
657 char temp2[BUFSIZ]; /* argument size */
658
659 /* /home/user/... */
660 sprintf(temp2, "%s%s", home, ud);
661 strcpy(sav_ud, temp2);
662 }
663 }
664 return 0; /* void, result save to argument :-) */
665 } else {
666 /* "~[^/]*" */
667 char **matches = (char **) NULL;
668 int nm = 0;
669
670 setpwent();
671
672 while ((entry = getpwent()) != NULL) {
673 /* Null usernames should result in all users as possible completions. */
674 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
675
676 temp = xmalloc(3 + strlen(entry->pw_name));
677 sprintf(temp, "~%s/", entry->pw_name);
678 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
679
680 matches[nm++] = temp;
681 }
682 }
683
684 endpwent();
685 (*num_matches) = nm;
686 return (matches);
687 }
688}
Eric Andersen28a78ab2001-02-16 20:26:50 +0000689#endif /* BB_FEATURE_SH_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000690
691enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000692 FIND_EXE_ONLY = 0,
693 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000694 FIND_FILE_ONLY = 2,
695};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000696
Mark Whitley4e338752001-01-26 20:42:23 +0000697static int path_parse(char ***p, int flags)
698{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000699 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000700 char *tmp;
701 char *pth;
702
Mark Whitley4e338752001-01-26 20:42:23 +0000703 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000704 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
705 /* PATH=<empty> or PATH=:<empty> */
706 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000707 return 1;
708 }
709
710 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000711 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000712
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000713 for (;;) {
714 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000715 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000716 if (tmp) {
717 if (*++tmp == 0)
718 break; /* :<empty> */
719 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000720 break;
721 }
722
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000723 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000724
725 tmp = pth;
726 (*p)[0] = xstrdup(tmp);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000727 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000728
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000729 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000730 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000731 if (tmp) {
732 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
733 if (*++tmp == 0)
734 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000735 } else
736 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000737 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000738 }
739
740 return npth;
741}
742
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000743static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000744{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000745 int l = 0;
746 char *s = xmalloc((strlen(found) + 1) * 2);
747
748 while (*found) {
749 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
750 s[l++] = '\\';
751 s[l++] = *found++;
752 }
753 s[l] = 0;
754 return s;
755}
756
757static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
758 int type)
759{
760
761 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000762 DIR *dir;
763 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000764 char dirbuf[BUFSIZ];
765 int nm = *num_matches;
766 struct stat st;
767 char *path1[1];
768 char **paths = path1;
769 int npaths;
770 int i;
771 char found[BUFSIZ + 4 + PATH_MAX];
772 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000773
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000774 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000775
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000776 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000777 /* no dir, if flags==EXE_ONLY - get paths, else "." */
778 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000779 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000780 } else {
781 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000782 /* save for change */
783 strcpy(dirbuf, command);
784 /* set dir only */
785 dirbuf[(pfind - command) + 1] = 0;
Eric Andersen28a78ab2001-02-16 20:26:50 +0000786#ifdef BB_FEATURE_SH_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000787 if (dirbuf[0] == '~') /* ~/... or ~user/... */
788 username_tab_completion(dirbuf, 0);
789#endif
790 /* "strip" dirname in command */
791 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000792
Mark Whitley4e338752001-01-26 20:42:23 +0000793 paths[0] = dirbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000794 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000795 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000796
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000797 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000798
Mark Whitley4e338752001-01-26 20:42:23 +0000799 dir = opendir(paths[i]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000800 if (!dir) /* Don't print an error */
801 continue;
802
803 while ((next = readdir(dir)) != NULL) {
804 const char *str_merge = "%s/%s";
805 char *str_found = next->d_name;
806
Mark Whitley4e338752001-01-26 20:42:23 +0000807 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000808 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000809 continue;
810 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000811 if (*str_found == '.' && *pfind == 0) {
812 if (*paths[i] == '/' && paths[i][1] == 0
813 && str_found[1] == 0) str_found = ""; /* only "/" */
814 else
Mark Whitley4e338752001-01-26 20:42:23 +0000815 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000816 }
817 if (paths[i][strlen(paths[i]) - 1] == '/')
818 str_merge = "%s%s";
819 sprintf(found, str_merge, paths[i], str_found);
820 /* hmm, remover in progress? */
821 if (stat(found, &st) < 0)
822 continue;
823 /* find with dirs ? */
824 if (paths[i] != dirbuf)
825 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000826 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000827 /* name is directory */
828 /* algorithmic only "/" ? */
829 if (*str_found)
830 strcat(found, "/");
831 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000832 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000833 /* not put found file if search only dirs for cd */
834 if (type == FIND_DIR_ONLY)
835 continue;
836 str_found = add_quote_for_spec_chars(found);
837 if (type == FIND_FILE_ONLY ||
838 (type == FIND_EXE_ONLY && is_execute(&st) == TRUE))
839 strcat(str_found, " ");
840 }
Mark Whitley4e338752001-01-26 20:42:23 +0000841 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000842 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
843
844 matches[nm++] = str_found;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000845 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000846 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000847 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000848 if (paths != path1) {
849 free(paths[0]); /* allocated memory only in first member */
850 free(paths);
851 }
Mark Whitley4e338752001-01-26 20:42:23 +0000852 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000853 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000854}
Erik Andersenf0657d32000-04-12 17:49:52 +0000855
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000856static int match_compare(const void *a, const void *b)
857{
858 return strcmp(*(char **) a, *(char **) b);
859}
860
861
862
863#define QUOT (UCHAR_MAX+1)
864
865#define collapse_pos(is, in) { \
866 memcpy(int_buf+is, int_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); \
867 memcpy(pos_buf+is, pos_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); }
868
869static int find_match(char *matchBuf, int *len_with_quotes)
870{
871 int i, j;
872 int command_mode;
873 int c, c2;
874 int int_buf[BUFSIZ + 1];
875 int pos_buf[BUFSIZ + 1];
876
877 /* set to integer dimension characters and own positions */
878 for (i = 0;; i++) {
879 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
880 if (int_buf[i] == 0) {
881 pos_buf[i] = -1; /* indicator end line */
882 break;
883 } else
884 pos_buf[i] = i;
885 }
886
887 /* mask \+symbol and convert '\t' to ' ' */
888 for (i = j = 0; matchBuf[i]; i++, j++)
889 if (matchBuf[i] == '\\') {
890 collapse_pos(j, j + 1);
891 int_buf[j] |= QUOT;
892 i++;
893#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
894 if (matchBuf[i] == '\t') /* algorithm equivalent */
895 int_buf[j] = ' ' | QUOT;
896#endif
897 }
898#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
899 else if (matchBuf[i] == '\t')
900 int_buf[j] = ' ';
901#endif
902
903 /* mask "symbols" or 'symbols' */
904 c2 = 0;
905 for (i = 0; int_buf[i]; i++) {
906 c = int_buf[i];
907 if (c == '\'' || c == '"') {
908 if (c2 == 0)
909 c2 = c;
910 else {
911 if (c == c2)
912 c2 = 0;
913 else
914 int_buf[i] |= QUOT;
915 }
916 } else if (c2 != 0 && c != '$')
917 int_buf[i] |= QUOT;
918 }
919
920 /* skip commands with arguments if line have commands delimiters */
921 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
922 for (i = 0; int_buf[i]; i++) {
923 c = int_buf[i];
924 c2 = int_buf[i + 1];
925 j = i ? int_buf[i - 1] : -1;
926 command_mode = 0;
927 if (c == ';' || c == '&' || c == '|') {
928 command_mode = 1 + (c == c2);
929 if (c == '&') {
930 if (j == '>' || j == '<')
931 command_mode = 0;
932 } else if (c == '|' && j == '>')
933 command_mode = 0;
934 }
935 if (command_mode) {
936 collapse_pos(0, i + command_mode);
937 i = -1; /* hack incremet */
938 }
939 }
940 /* collapse `command...` */
941 for (i = 0; int_buf[i]; i++)
942 if (int_buf[i] == '`') {
943 for (j = i + 1; int_buf[j]; j++)
944 if (int_buf[j] == '`') {
945 collapse_pos(i, j + 1);
946 j = 0;
947 break;
948 }
949 if (j) {
950 /* not found close ` - command mode, collapse all previous */
951 collapse_pos(0, i + 1);
952 break;
953 } else
954 i--; /* hack incremet */
955 }
956
957 /* collapse (command...(command...)...) or {command...{command...}...} */
958 c = 0; /* "recursive" level */
959 c2 = 0;
960 for (i = 0; int_buf[i]; i++)
961 if (int_buf[i] == '(' || int_buf[i] == '{') {
962 if (int_buf[i] == '(')
963 c++;
964 else
965 c2++;
966 collapse_pos(0, i + 1);
967 i = -1; /* hack incremet */
968 }
969 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
970 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
971 if (int_buf[i] == ')')
972 c--;
973 else
974 c2--;
975 collapse_pos(0, i + 1);
976 i = -1; /* hack incremet */
977 }
978
979 /* skip first not quote space */
980 for (i = 0; int_buf[i]; i++)
981 if (int_buf[i] != ' ')
982 break;
983 if (i)
984 collapse_pos(0, i);
985
986 /* set find mode for completion */
987 command_mode = FIND_EXE_ONLY;
988 for (i = 0; int_buf[i]; i++)
989 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
990 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
991 && strncmp(&matchBuf[pos_buf[0]], "cd", 2) == 0)
992 command_mode = FIND_DIR_ONLY;
993 else {
994 command_mode = FIND_FILE_ONLY;
995 break;
996 }
997 }
998 /* "strlen" */
999 for (i = 0; int_buf[i]; i++);
1000 /* find last word */
1001 for (--i; i >= 0; i--) {
1002 c = int_buf[i];
1003 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
1004 collapse_pos(0, i + 1);
1005 break;
1006 }
1007 }
1008 /* skip first not quoted '\'' or '"' */
1009 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
1010 /* collapse quote or unquote // or /~ */
1011 while ((int_buf[i] & ~QUOT) == '/' && (
1012 (int_buf[i + 1] & ~QUOT) == '/'
1013 || (int_buf[i + 1] & ~QUOT) ==
1014 '~')) i++;
1015 if (i)
1016 collapse_pos(0, i);
1017
1018 /* set only match and destroy quotes */
1019 j = 0;
1020 for (i = 0; pos_buf[i] >= 0; i++) {
1021 matchBuf[i] = matchBuf[pos_buf[i]];
1022 j = pos_buf[i] + 1;
1023 }
1024 matchBuf[i] = 0;
1025 /* old lenght matchBuf with quotes symbols */
1026 *len_with_quotes = j ? j - pos_buf[0] : 0;
1027
1028 return command_mode;
1029}
1030
1031
1032static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001033{
1034 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001035 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +00001036 static char **matches;
1037
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001038 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001039 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001040 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001041 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001042 free(matches);
1043 matches = (char **) NULL;
1044 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001045 return;
1046 }
1047 if (*lastWasTab == FALSE) {
1048
1049 char *tmp;
1050 int len_found;
1051 char matchBuf[BUFSIZ];
1052 int find_type;
1053 int recalc_pos;
1054
1055 *lastWasTab = TRUE; /* flop trigger */
1056
1057 /* Make a local copy of the string -- up
1058 * to the position of the cursor */
1059 tmp = strncpy(matchBuf, command_ps, cursor);
1060 tmp[cursor] = 0;
1061
1062 find_type = find_match(matchBuf, &recalc_pos);
1063
1064 /* Free up any memory already allocated */
1065 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001066
Eric Andersen28a78ab2001-02-16 20:26:50 +00001067#ifdef BB_FEATURE_SH_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001068 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001069 * then try completing this word as a username. */
1070
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001071 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001072 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001073#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001074 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001075 * in the current working directory that matches. */
1076 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001077 matches =
1078 exe_n_cwd_tab_completion(matchBuf, &num_matches,
1079 find_type);
Erik Andersenf0657d32000-04-12 17:49:52 +00001080
1081 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001082 if (!matches || num_matches > 1) {
1083 char *tmp1;
1084
Mark Whitley4e338752001-01-26 20:42:23 +00001085 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001086 if (!matches)
1087 return; /* not found */
1088 /* sort */
1089 qsort(matches, num_matches, sizeof(char *), match_compare);
1090
1091 /* find minimal match */
1092 tmp = xstrdup(matches[0]);
1093 for (tmp1 = tmp; *tmp1; tmp1++)
1094 for (len_found = 1; len_found < num_matches; len_found++)
1095 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1096 *tmp1 = 0;
1097 break;
1098 }
1099 if (*tmp == 0) { /* have unique */
1100 free(tmp);
1101 return;
1102 }
1103 } else { /* one match */
1104 tmp = matches[0];
1105 /* for next completion current found */
1106 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001107 }
1108
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001109 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001110 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001111 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001112
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001113 /* before word for match */
1114 command_ps[cursor - recalc_pos] = 0;
1115 /* save tail line */
1116 strcpy(matchBuf, command_ps + cursor);
1117 /* add match */
1118 strcat(command_ps, tmp);
1119 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001120 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001121 /* back to begin word for match */
1122 input_backward(recalc_pos);
1123 /* new pos */
1124 recalc_pos = cursor + len_found;
1125 /* new len */
1126 len = strlen(command_ps);
1127 /* write out the matched command */
1128 input_end();
1129 input_backward(cursor - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001130 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001131 if (tmp != matches[0])
1132 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001133 } else {
1134 /* Ok -- the last char was a TAB. Since they
1135 * just hit TAB again, print a list of all the
1136 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001137 if (matches && num_matches > 0) {
1138 int i, col, l;
1139 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001140
1141 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001142 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001143 for (i = 0, col = 0; i < num_matches; i++) {
1144 l = strlen(matches[i]);
1145 if (l < 14)
1146 l = 14;
1147 printf("%-14s ", matches[i]);
1148 if ((l += 2) > 16)
1149 while (l % 16) {
1150 putchar(' ');
1151 l++;
1152 }
1153 col += l;
1154 col -= (col / cmdedit_termw) * cmdedit_termw;
1155 if (col > 60 && matches[i + 1] != NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +00001156 putchar('\n');
Erik Andersenf0657d32000-04-12 17:49:52 +00001157 col = 0;
1158 }
1159 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001160 /* Go to the next line and rewrite */
1161 putchar('\n');
1162 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001163 }
1164 }
1165}
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001166#endif /* BB_FEATURE_SH_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001167
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001168static void get_previous_history(struct history **hp, struct history *p)
Erik Andersenf0657d32000-04-12 17:49:52 +00001169{
Eric Andersen91a44002000-07-19 17:37:57 +00001170 if ((*hp)->s)
1171 free((*hp)->s);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001172 (*hp)->s = xstrdup(command_ps);
1173 *hp = p;
Erik Andersenf0657d32000-04-12 17:49:52 +00001174}
1175
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001176static inline void get_next_history(struct history **hp)
Erik Andersenf0657d32000-04-12 17:49:52 +00001177{
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001178 get_previous_history(hp, (*hp)->n);
Erik Andersenf0657d32000-04-12 17:49:52 +00001179}
1180
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001181enum {
1182 ESC = 27,
1183 DEL = 127,
1184};
1185
1186
Erik Andersen6273f652000-03-17 01:12:41 +00001187/*
1188 * This function is used to grab a character buffer
1189 * from the input file descriptor and allows you to
1190 * a string with full command editing (sortof like
1191 * a mini readline).
1192 *
1193 * The following standard commands are not implemented:
1194 * ESC-b -- Move back one word
1195 * ESC-f -- Move forward one word
1196 * ESC-d -- Delete back one word
1197 * ESC-h -- Delete forward one word
1198 * CTL-t -- Transpose two characters
1199 *
1200 * Furthermore, the "vi" command editing keys are not implemented.
1201 *
Erik Andersen6273f652000-03-17 01:12:41 +00001202 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001203extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001204{
1205
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001206 int inputFd = fileno(stdin);
Mark Whitley4e338752001-01-26 20:42:23 +00001207
Erik Andersenc7c634b2000-03-19 05:28:55 +00001208 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001209 int lastWasTab = FALSE;
1210 char c = 0;
1211 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +00001212
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001213 /* prepare before init handlers */
1214 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001215 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001216 command_ps = command;
1217
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001218 if (new_settings.c_cc[VMIN] == 0) { /* first call */
1219
1220 getTermSettings(inputFd, (void *) &initial_settings);
Erik Andersen1d1d9502000-04-21 01:26:49 +00001221 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001222
Erik Andersen1d1d9502000-04-21 01:26:49 +00001223 new_settings.c_cc[VMIN] = 1;
1224 new_settings.c_cc[VTIME] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001225 new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001226 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001227 new_settings.c_lflag &= ~(ECHO | ECHOCTL | ECHONL); /* Turn off echoing */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001228 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001229
1230 command[0] = 0;
1231
1232 setTermSettings(inputFd, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001233 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001234
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001235 /* Print out the command prompt */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001236 parse_prompt(prompt);
Eric Andersen6faae7d2001-02-16 20:09:17 +00001237 /* Now initialize things */
1238 cmdedit_init();
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001239
Erik Andersenc7c634b2000-03-19 05:28:55 +00001240 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001241
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001242 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001243
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001244 if (read(inputFd, &c, 1) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +00001245 return;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001246
Erik Andersen13456d12000-03-16 08:09:57 +00001247 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001248 case '\n':
1249 case '\r':
1250 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001251 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001252 break_out = 1;
1253 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001254 case 1:
1255 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001256 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001257 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001258 case 2:
1259 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001260 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001261 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001262 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001263 /* Control-c -- stop gathering input */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001264
Eric Andersen86349772000-12-18 20:25:50 +00001265 /* Link into lash to reset context to 0 on ^C and such */
1266 shell_context = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001267
1268 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001269 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001270 command[0] = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001271
Eric Andersen86349772000-12-18 20:25:50 +00001272 return;
Erik Andersenf0657d32000-04-12 17:49:52 +00001273 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001274 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001275 * if the len=0 and no chars to delete */
1276 if (len == 0) {
Mark Whitley4e338752001-01-26 20:42:23 +00001277 printf("exit");
Erik Andersenf0657d32000-04-12 17:49:52 +00001278 clean_up_and_die(0);
1279 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001280 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001281 }
1282 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001283 case 5:
1284 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001285 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001286 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001287 case 6:
1288 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001289 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001290 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001291 case '\b':
1292 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001293 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001294 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001295 break;
1296 case '\t':
Erik Andersena2685732000-04-09 18:27:46 +00001297#ifdef BB_FEATURE_SH_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001298 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001299#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001300 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001301 case 14:
1302 /* Control-n -- Get next command in history */
1303 if (hp && hp->n && hp->n->s) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001304 get_next_history(&hp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001305 goto rewrite_line;
1306 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001307 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001308 }
1309 break;
1310 case 16:
1311 /* Control-p -- Get previous command from history */
1312 if (hp && hp->p) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001313 get_previous_history(&hp, hp->p);
Erik Andersenf0657d32000-04-12 17:49:52 +00001314 goto rewrite_line;
1315 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001316 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001317 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001318 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001319 case 21:
1320 /* Control-U -- Clear line before cursor */
1321 if (cursor) {
1322 strcpy(command, command + cursor);
1323 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001324 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001325 break;
1326
1327 case ESC:{
1328 /* escape sequence follows */
1329 if (read(inputFd, &c, 1) < 1)
1330 return;
1331 /* different vt100 emulations */
1332 if (c == '[' || c == 'O') {
1333 if (read(inputFd, &c, 1) < 1)
1334 return;
1335 }
1336 switch (c) {
1337#ifdef BB_FEATURE_SH_TAB_COMPLETION
1338 case '\t': /* Alt-Tab */
1339
1340 input_tab(&lastWasTab);
1341 break;
1342#endif
1343 case 'A':
1344 /* Up Arrow -- Get previous command from history */
1345 if (hp && hp->p) {
1346 get_previous_history(&hp, hp->p);
1347 goto rewrite_line;
1348 } else {
1349 beep();
1350 }
1351 break;
1352 case 'B':
1353 /* Down Arrow -- Get next command in history */
1354 if (hp && hp->n && hp->n->s) {
1355 get_next_history(&hp);
1356 goto rewrite_line;
1357 } else {
1358 beep();
1359 }
1360 break;
1361
1362 /* Rewrite the line with the selected history item */
1363 rewrite_line:
1364 /* change command */
1365 len = strlen(strcpy(command, hp->s));
1366 /* redraw and go to end line */
1367 redraw(cmdedit_y, 0);
1368 break;
1369 case 'C':
1370 /* Right Arrow -- Move forward one character */
1371 input_forward();
1372 break;
1373 case 'D':
1374 /* Left Arrow -- Move back one character */
1375 input_backward(1);
1376 break;
1377 case '3':
1378 /* Delete */
1379 input_delete();
1380 break;
1381 case '1':
1382 case 'H':
1383 /* Home (Ctrl-A) */
1384 input_backward(cursor);
1385 break;
1386 case '4':
1387 case 'F':
1388 /* End (Ctrl-E) */
1389 input_end();
1390 break;
1391 default:
1392 if (!(c >= '1' && c <= '9'))
1393 c = 0;
1394 beep();
1395 }
1396 if (c >= '1' && c <= '9')
1397 do
1398 if (read(inputFd, &c, 1) < 1)
1399 return;
1400 while (c != '~');
1401 break;
1402 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001403
1404 default: /* If it's regular input, do the normal thing */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001405#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1406 /* Control-V -- Add non-printable symbol */
1407 if (c == 22) {
1408 if (read(inputFd, &c, 1) < 1)
1409 return;
1410 if (c == 0) {
1411 beep();
1412 break;
1413 }
1414 } else
1415#endif
1416 if (!isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001417 break;
1418
1419 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
1420 break;
1421
1422 len++;
1423
1424 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001425 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001426 *(command + cursor + 1) = 0;
1427 cmdedit_set_out_char(0);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001428 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001429 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001430
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001431 memmove(command + sc + 1, command + sc, len - sc);
1432 *(command + sc) = c;
1433 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001434 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001435 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001436 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001437 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001438 }
1439
Erik Andersenc7c634b2000-03-19 05:28:55 +00001440 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001441 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001442 if (break_out) /* Enter is the command terminator, no more input. */
1443 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001444
1445 if (c != '\t')
1446 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001447 }
1448
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001449 setTermSettings(inputFd, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001450 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001451
1452 /* Handle command history log */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001453 if (len) { /* no put empty line */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001454
1455 struct history *h = his_end;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001456 char *ss;
Mark Whitley4e338752001-01-26 20:42:23 +00001457
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001458 ss = xstrdup(command); /* duplicate */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001459
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001460 if (h == 0) {
Eric Andersen91a44002000-07-19 17:37:57 +00001461 /* No previous history -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001462 h = his_front = xmalloc(sizeof(struct history));
1463 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001464
1465 h->p = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001466 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001467 h->n->p = h;
1468 h->n->n = NULL;
1469 h->n->s = NULL;
1470 his_end = h->n;
1471 history_counter++;
1472 } else {
Eric Andersen91a44002000-07-19 17:37:57 +00001473 /* Add a new history command -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001474 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001475
1476 h->n->p = h;
1477 h->n->n = NULL;
1478 h->n->s = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001479 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001480 his_end = h->n;
1481
1482 /* After max history, remove the oldest command */
1483 if (history_counter >= MAX_HISTORY) {
1484
1485 struct history *p = his_front->n;
1486
1487 p->p = NULL;
1488 free(his_front->s);
1489 free(his_front);
1490 his_front = p;
1491 } else {
1492 history_counter++;
1493 }
Erik Andersen13456d12000-03-16 08:09:57 +00001494 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001495#if defined(BB_FEATURE_BASH_STYLE_PROMT)
1496 num_ok_lines++;
1497#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001498 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001499 command[len++] = '\n'; /* set '\n' */
1500 command[len] = 0;
1501#if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_SH_TAB_COMPLETION)
1502 input_tab(0); /* strong free */
1503#endif
1504#if defined(BB_FEATURE_BASH_STYLE_PROMT)
1505 free(cmdedit_prompt);
1506#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001507 return;
Erik Andersen13456d12000-03-16 08:09:57 +00001508}
1509
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001510
Mark Whitley4e338752001-01-26 20:42:23 +00001511/* Undo the effects of cmdedit_init(). */
Eric Andersen501c88b2000-07-28 15:14:45 +00001512extern void cmdedit_terminate(void)
1513{
1514 cmdedit_reset_term();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001515 if ((handlers_sets & SET_TERM_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +00001516 signal(SIGKILL, SIG_DFL);
1517 signal(SIGINT, SIG_DFL);
1518 signal(SIGQUIT, SIG_DFL);
1519 signal(SIGTERM, SIG_DFL);
1520 signal(SIGWINCH, SIG_DFL);
1521 handlers_sets &= ~SET_TERM_HANDLERS;
1522 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001523}
1524
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001525#endif /* BB_FEATURE_SH_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001526
1527
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001528#ifdef TEST
1529
1530unsigned int shell_context;
1531
1532int main(int argc, char **argv)
1533{
1534 char buff[BUFSIZ];
1535 char *prompt =
1536#if defined(BB_FEATURE_BASH_STYLE_PROMT)
1537 "\\[\\033[32;1m\\]\\u@\\[\\033[33;1m\\]\\h:\
1538\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
1539\\!\\[\\033[36;1m\\]\\$ \\[\\033[0m\\]";
1540#else
1541 "% ";
1542#endif
1543
1544 shell_context = 1;
1545 do {
1546 cmdedit_read_input(prompt, buff);
1547 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1548 } while (shell_context);
1549 printf("*** cmdedit_read_input() detect ^C\n");
1550 return 0;
1551}
1552
1553#endif /* TEST */