blob: 6a53c12f648ce20b9049a41983ebdc42cde2a18d [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 Andersen5f2c79d2001-02-16 18:36:04 +000033#define TEST
34
35
36#ifndef TEST
Mark Whitley4e338752001-01-26 20:42:23 +000037
Eric Andersen3570a342000-09-25 21:45:58 +000038#include "busybox.h"
Mark Whitley4e338752001-01-26 20:42:23 +000039
Eric Andersen5f2c79d2001-02-16 18:36:04 +000040#define D(x)
41
42#else
43
44#define BB_FEATURE_SH_COMMAND_EDITING
45#define BB_FEATURE_SH_TAB_COMPLETION
46#define BB_FEATURE_USERNAME_COMPLETION
47#define BB_FEATURE_NONPRINTABLE_INVERSE_PUT
48#define BB_FEATURE_BASH_STYLE_PROMT
49#define BB_FEATURE_CLEAN_UP
50
51#define TRUE 1
52#define FALSE 0
53#define D(x) x
54
55#endif /* TEST */
56
Erik Andersen13456d12000-03-16 08:09:57 +000057#ifdef BB_FEATURE_SH_COMMAND_EDITING
58
Eric Andersen5f2c79d2001-02-16 18:36:04 +000059#ifndef BB_FEATURE_SH_TAB_COMPLETION
60#undef BB_FEATURE_USERNAME_COMPLETION
61#endif
62
63#if defined(BB_FEATURE_USERNAME_COMPLETION) || defined(BB_FEATURE_BASH_STYLE_PROMT)
64#define BB_FEATURE_GETUSERNAME_AND_HOMEDIR
65#endif
66
Erik Andersen13456d12000-03-16 08:09:57 +000067#include <stdio.h>
68#include <errno.h>
69#include <unistd.h>
70#include <stdlib.h>
71#include <string.h>
Erik Andersen1d1d9502000-04-21 01:26:49 +000072#include <sys/ioctl.h>
Erik Andersen13456d12000-03-16 08:09:57 +000073#include <ctype.h>
74#include <signal.h>
Eric Andersen5f2c79d2001-02-16 18:36:04 +000075#include <limits.h>
Erik Andersen13456d12000-03-16 08:09:57 +000076
Mark Whitley4e338752001-01-26 20:42:23 +000077#ifdef BB_FEATURE_SH_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +000078#include <dirent.h>
Mark Whitley4e338752001-01-26 20:42:23 +000079#include <sys/stat.h>
80#endif
81
Eric Andersen5f2c79d2001-02-16 18:36:04 +000082#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
83#ifndef TEST
Eric Andersenaf4ac772001-02-01 22:43:49 +000084#include "pwd_grp/pwd.h"
Eric Andersen5f2c79d2001-02-16 18:36:04 +000085#else
86#include <pwd.h>
87#endif /* TEST */
88#endif /* advanced FEATURES */
Eric Andersenaf4ac772001-02-01 22:43:49 +000089
90
Eric Andersen5f2c79d2001-02-16 18:36:04 +000091#ifdef TEST
92void *xrealloc(void *old, size_t size)
93{
94 return realloc(old, size);
95}
Erik Andersen13456d12000-03-16 08:09:57 +000096
Eric Andersen5f2c79d2001-02-16 18:36:04 +000097void *xmalloc(size_t size)
98{
99 return malloc(size);
100}
101char *xstrdup(const char *s)
102{
103 return strdup(s);
104}
105
106void *xcalloc(size_t size, size_t se)
107{
108 return calloc(size, se);
109}
110
111#define error_msg(s, d) fprintf(stderr, s, d)
112#endif
113
114
115struct history {
116 char *s;
117 struct history *p;
118 struct history *n;
Mark Whitley59ab0252001-01-23 22:30:04 +0000119};
120
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000121/* Maximum length of the linked list for the command line history */
122static const int MAX_HISTORY = 15;
Erik Andersen13456d12000-03-16 08:09:57 +0000123
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000124/* First element in command line list */
125static struct history *his_front = NULL;
126
127/* Last element in command line list */
128static struct history *his_end = NULL;
129
Erik Andersen1d1d9502000-04-21 01:26:49 +0000130
131/* ED: sparc termios is broken: revert back to old termio handling. */
Erik Andersen1d1d9502000-04-21 01:26:49 +0000132
133#if #cpu(sparc)
134# include <termio.h>
135# define termios termio
136# define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp)
137# define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp)
138#else
139# include <termios.h>
140# define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
141# define getTermSettings(fd,argp) tcgetattr(fd, argp);
142#endif
143
144/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000145static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000146
147
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000148#ifndef _POSIX_VDISABLE
149#define _POSIX_VDISABLE '\0'
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000150#endif
151
Erik Andersen1d1d9502000-04-21 01:26:49 +0000152
Mark Whitley4e338752001-01-26 20:42:23 +0000153static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000154volatile int cmdedit_termw = 80; /* actual terminal width */
155static int history_counter = 0; /* Number of commands in history list */
Mark Whitley4e338752001-01-26 20:42:23 +0000156static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000157volatile int handlers_sets = 0; /* Set next bites: */
158
Mark Whitley4e338752001-01-26 20:42:23 +0000159enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000160 SET_ATEXIT = 1, /* when atexit() has been called and
161 get euid,uid,gid to fast compare */
162 SET_TERM_HANDLERS = 2, /* set many terminates signal handlers */
163 SET_WCHG_HANDLERS = 4, /* winchg signal handler */
164 SET_RESET_TERM = 8, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000165};
Erik Andersen13456d12000-03-16 08:09:57 +0000166
Mark Whitley4e338752001-01-26 20:42:23 +0000167
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000168static int cmdedit_x; /* real x terminal position */
169static int cmdedit_y; /* pseudoreal y terminal position */
170static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000171
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000172static int cursor; /* required global for signal handler */
173static int len; /* --- "" - - "" - -"- --""-- --""--- */
174static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
175static
176#ifndef BB_FEATURE_BASH_STYLE_PROMT
177 const
178#endif
179char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
180
181/* Link into lash to reset context to 0 on ^C and such */
Eric Andersen86349772000-12-18 20:25:50 +0000182extern unsigned int shell_context;
183
Erik Andersen13456d12000-03-16 08:09:57 +0000184
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000185#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
186static char *user_buf = "";
187static char *home_pwd_buf = "";
188static int my_euid;
189#endif
190
191#ifdef BB_FEATURE_BASH_STYLE_PROMT
192static char *hostname_buf = "";
193static int num_ok_lines = 1;
194#endif
195
196
197#ifdef BB_FEATURE_SH_TAB_COMPLETION
198
199#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
200static int my_euid;
201#endif
202
203static int my_uid;
204static int my_gid;
205
206#endif /* BB_FEATURE_SH_TAB_COMPLETION */
207
Erik Andersen13456d12000-03-16 08:09:57 +0000208
Mark Whitley4e338752001-01-26 20:42:23 +0000209static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000210
Mark Whitley4e338752001-01-26 20:42:23 +0000211static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000212{
213 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000214 static __sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000215
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000216 /* emulate || signal call */
217 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Mark Whitley4e338752001-01-26 20:42:23 +0000218 ioctl(0, TIOCGWINSZ, &win);
219 if (win.ws_col > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000220 cmdedit_setwidth(win.ws_col, nsig == SIGWINCH);
221 }
Mark Whitley4e338752001-01-26 20:42:23 +0000222 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000223 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000224
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000225 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000226 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000227 else if (nsig == SIGWINCH) /* signaled called handler */
228 signal(SIGWINCH, win_changed); /* set for next call */
229 else /* nsig == 0 */
230 /* set previous handler */
231 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000232}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000233
234static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000235{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000236 if ((handlers_sets & SET_RESET_TERM) != 0) {
Erik Andersena6c75222000-04-18 00:00:52 +0000237 /* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000238 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000239 handlers_sets &= ~SET_RESET_TERM;
240 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000241 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000242 /* reset SIGWINCH handler to previous (default) */
243 win_changed(0);
244 handlers_sets &= ~SET_WCHG_HANDLERS;
245 }
246 fflush(stdout);
Eric Andersenb040d4f2000-07-25 18:01:20 +0000247#ifdef BB_FEATURE_CLEAN_UP
248 if (his_front) {
249 struct history *n;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000250
Eric Andersenb040d4f2000-07-25 18:01:20 +0000251 //while(his_front!=his_end) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000252 while (his_front != his_end) {
Eric Andersenb040d4f2000-07-25 18:01:20 +0000253 n = his_front->n;
254 free(his_front->s);
255 free(his_front);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000256 his_front = n;
Eric Andersenb040d4f2000-07-25 18:01:20 +0000257 }
258 }
259#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000260}
261
Mark Whitley4e338752001-01-26 20:42:23 +0000262
Mark Whitley4e338752001-01-26 20:42:23 +0000263/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000264static void cmdedit_set_out_char(int next_char)
265{
266
267 int c = command_ps[cursor];
268
269 if (c == 0)
270 c = ' '; /* destroy end char? */
271#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
272 if (!isprint(c)) { /* Inverse put non-printable characters */
273 if (((unsigned char) c) >= 128)
274 c -= 128;
275 if (((unsigned char) c) < ' ')
276 c += '@';
277 if (c == 127)
278 c = '?';
279 printf("\033[7m%c\033[0m", c);
280 } else
281#endif
282 putchar(c);
283 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000284 /* terminal is scrolled down */
285 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000286 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000287
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000288 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000289 next_char = ' ';
290 /* destroy "(auto)margin" */
291 putchar(next_char);
292 putchar('\b');
293 }
294 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000295}
296
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000297/* Move to end line. Bonus: rewrite line from cursor */
298static void input_end(void)
299{
300 while (cursor < len)
301 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000302}
303
304/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000305static void goto_new_line(void)
306{
Mark Whitley4e338752001-01-26 20:42:23 +0000307 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000308 if (cmdedit_x)
309 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000310}
311
312
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000313static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000314{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000315 fputs(s, stdout);
316}
317static inline void beep(void)
318{
319 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000320}
321
Mark Whitley4e338752001-01-26 20:42:23 +0000322/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000323/* special for slow terminal */
324static void input_backward(int num)
325{
326 if (num > cursor)
327 num = cursor;
328 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000329
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000330 if (cmdedit_x >= num) { /* no to up line */
331 cmdedit_x -= num;
332 if (num < 4)
333 while (num-- > 0)
334 putchar('\b');
335
336 else
337 printf("\033[%dD", num);
338 } else {
339 int count_y;
340
341 if (cmdedit_x) {
342 putchar('\r'); /* back to first terminal pos. */
343 num -= cmdedit_x; /* set previous backward */
344 }
345 count_y = 1 + num / cmdedit_termw;
346 printf("\033[%dA", count_y);
347 cmdedit_y -= count_y;
348 /* require forward after uping */
349 cmdedit_x = cmdedit_termw * count_y - num;
350 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000351 }
Erik Andersen13456d12000-03-16 08:09:57 +0000352}
353
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000354static void put_prompt(void)
355{
356 out1str(cmdedit_prompt);
357 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
358 cursor = 0;
359}
360
361#ifdef BB_FEATURE_BASH_STYLE_PROMT
362static void
363add_to_prompt(char **prmt_mem_ptr, int *alm, int *prmt_len,
364 const char *addb)
365{
366 int l = strlen(addb);
367
368 *prmt_len += l;
369 if (*alm < (*prmt_len) + 1) {
370 *alm = (*prmt_len) + 1;
371 *prmt_mem_ptr = xrealloc(*prmt_mem_ptr, *alm);
372 }
373 strcat(*prmt_mem_ptr, addb);
374}
375#endif
376
377static void parse_prompt(const char *prmt_ptr)
378{
379#ifdef BB_FEATURE_BASH_STYLE_PROMT
380 int alm = strlen(prmt_ptr) + 1; /* supposedly require memory */
381 int prmt_len = 0;
382 int sub_len = 0;
383 int flg_not_length = '[';
384 char *prmt_mem_ptr = xstrdup(prmt_ptr);
385 char pwd_buf[PATH_MAX + 1];
386 char buf[16];
387 int c;
388
389 pwd_buf[0] = 0;
390 *prmt_mem_ptr = 0;
391
392 while (*prmt_ptr) {
393 c = *prmt_ptr++;
394 if (c == '\\') {
395 c = *prmt_ptr;
396 if (c == 0)
397 break;
398 prmt_ptr++;
399 switch (c) {
400 case 'u':
401 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, user_buf);
402 continue;
403 case 'h':
404 if (hostname_buf[0] == 0) {
405 hostname_buf = xcalloc(256, 1);
406 if (gethostname(hostname_buf, 255) < 0) {
407 strcpy(hostname_buf, "?");
408 } else {
409 char *s = strchr(hostname_buf, '.');
410
411 if (s)
412 *s = 0;
413 }
414 }
415 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len,
416 hostname_buf);
417 continue;
418 case '$':
419 c = my_euid == 0 ? '#' : '$';
420 break;
421 case 'w':
422 if (pwd_buf[0] == 0) {
423 int l;
424
425 getcwd(pwd_buf, PATH_MAX);
426 l = strlen(home_pwd_buf);
427 if (home_pwd_buf[0] != 0 &&
428 strncmp(home_pwd_buf, pwd_buf, l) == 0) {
429 strcpy(pwd_buf + 1, pwd_buf + l);
430 pwd_buf[0] = '~';
431 }
432 }
433 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf);
434 continue;
435 case '!':
436 snprintf(buf, sizeof(buf), "%d", num_ok_lines);
437 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf);
438 continue;
439 case 'e':
440 case 'E': /* \e \E = \033 */
441 c = '\033';
442 break;
443 case 'x':
444 case 'X':
445 case '0':
446 case '1':
447 case '2':
448 case '3':
449 case '4':
450 case '5':
451 case '6':
452 case '7':{
453 int l;
454 int ho = 0;
455 char *eho;
456
457 if (c == 'X')
458 c = 'x';
459
460 for (l = 0; l < 3;) {
461
462 buf[l++] = *prmt_ptr;
463 buf[l] = 0;
464 ho = strtol(buf, &eho, c == 'x' ? 16 : 8);
465 if (ho > UCHAR_MAX || (eho - buf) < l) {
466 l--;
467 break;
468 }
469 prmt_ptr++;
470 }
471 buf[l] = 0;
472 ho = strtol(buf, 0, c == 'x' ? 16 : 8);
473 c = ho == 0 ? '?' : (char) ho;
474 break;
475 }
476 case '[':
477 case ']':
478 if (c == flg_not_length) {
479 flg_not_length = flg_not_length == '[' ? ']' : '[';
480 continue;
481 }
482 break;
483 }
484 }
485 buf[0] = c;
486 buf[1] = 0;
487 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf);
488 if (flg_not_length == ']')
489 sub_len++;
490 }
491 cmdedit_prmt_len = prmt_len - sub_len;
492 cmdedit_prompt = prmt_mem_ptr;
493#else
494 cmdedit_prompt = prmt_ptr;
495 cmdedit_prmt_len = strlen(prmt_ptr);
496#endif
497 put_prompt();
498}
499
500
501/* draw promt, editor line, and clear tail */
502static void redraw(int y, int back_cursor)
503{
504 if (y > 0) /* up to start y */
505 printf("\033[%dA", y);
506 cmdedit_y = 0; /* new quasireal y */
507 putchar('\r');
508 put_prompt();
509 input_end(); /* rewrite */
510 printf("\033[J"); /* destroy tail after cursor */
511 input_backward(back_cursor);
512}
513
Erik Andersenf0657d32000-04-12 17:49:52 +0000514/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000515static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000516{
Mark Whitley4e338752001-01-26 20:42:23 +0000517 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000518
Mark Whitley4e338752001-01-26 20:42:23 +0000519 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000520 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000521
522 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000523 len--;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000524 input_end(); /* rewtite new line */
525 cmdedit_set_out_char(0); /* destroy end char */
526 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000527}
528
Mark Whitley4e338752001-01-26 20:42:23 +0000529/* Delete the char in back of the cursor */
530static void input_backspace(void)
531{
532 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000533 input_backward(1);
534 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000535 }
536}
537
538
Erik Andersenf0657d32000-04-12 17:49:52 +0000539/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000540static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000541{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000542 if (cursor < len)
543 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000544}
545
546
Mark Whitley4e338752001-01-26 20:42:23 +0000547static void clean_up_and_die(int sig)
548{
549 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000550 if (sig != SIGINT)
551 exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */
Mark Whitley4e338752001-01-26 20:42:23 +0000552 cmdedit_reset_term();
553}
554
555static void cmdedit_setwidth(int w, int redraw_flg)
556{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000557 cmdedit_termw = cmdedit_prmt_len + 2;
Mark Whitley4e338752001-01-26 20:42:23 +0000558 if (w > cmdedit_termw) {
559
560 cmdedit_termw = w;
561
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000562 if (redraw_flg) {
563 /* new y for current cursor */
564 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000565
566 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000567 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
568 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000569 }
570 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000571 error_msg("\n*** Error: minimum screen width is %d",
572 cmdedit_termw);
Mark Whitley4e338752001-01-26 20:42:23 +0000573 }
574}
575
576extern void cmdedit_init(void)
577{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000578 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
579 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000580 win_changed(-SIGWINCH);
581 handlers_sets |= SET_WCHG_HANDLERS;
582 }
583
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000584 if ((handlers_sets & SET_ATEXIT) == 0) {
585#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
586 struct passwd *entry;
587
588 my_euid = geteuid();
589 entry = getpwuid(my_euid);
590 if (entry) {
591 user_buf = xstrdup(entry->pw_name);
592 home_pwd_buf = xstrdup(entry->pw_dir);
593 }
594#endif
595
596#ifdef BB_FEATURE_SH_TAB_COMPLETION
597
598#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
599 my_euid = geteuid();
600#endif
601 my_uid = getuid();
602 my_gid = getgid();
603#endif /* BB_FEATURE_SH_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000604 handlers_sets |= SET_ATEXIT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000605 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000606 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000607
608 if ((handlers_sets & SET_TERM_HANDLERS) == 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000609 signal(SIGKILL, clean_up_and_die);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000610 signal(SIGINT, clean_up_and_die);
Mark Whitley4e338752001-01-26 20:42:23 +0000611 signal(SIGQUIT, clean_up_and_die);
612 signal(SIGTERM, clean_up_and_die);
613 handlers_sets |= SET_TERM_HANDLERS;
614 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000615
Mark Whitley4e338752001-01-26 20:42:23 +0000616}
Erik Andersenf0657d32000-04-12 17:49:52 +0000617
618#ifdef BB_FEATURE_SH_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000619
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000620static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000621{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000622 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
623 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
624 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
625 (st->st_mode & S_IXOTH)) return TRUE;
626 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000627}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000628
629#ifdef BB_FEATURE_USERNAME_COMPLETION
630
631static char **username_tab_completion(char *ud, int *num_matches)
632{
633 struct passwd *entry;
634 int userlen;
635 char *temp;
636
637
638 ud++; /* ~user/... to user/... */
639 userlen = strlen(ud);
640
641 if (num_matches == 0) { /* "~/..." or "~user/..." */
642 char *sav_ud = ud - 1;
643 char *home = 0;
644
645 if (*ud == '/') { /* "~/..." */
646 home = home_pwd_buf;
647 } else {
648 /* "~user/..." */
649 temp = strchr(ud, '/');
650 *temp = 0; /* ~user\0 */
651 entry = getpwnam(ud);
652 *temp = '/'; /* restore ~user/... */
653 ud = temp;
654 if (entry)
655 home = entry->pw_dir;
656 }
657 if (home) {
658 if ((userlen + strlen(home) + 1) < BUFSIZ) {
659 char temp2[BUFSIZ]; /* argument size */
660
661 /* /home/user/... */
662 sprintf(temp2, "%s%s", home, ud);
663 strcpy(sav_ud, temp2);
664 }
665 }
666 return 0; /* void, result save to argument :-) */
667 } else {
668 /* "~[^/]*" */
669 char **matches = (char **) NULL;
670 int nm = 0;
671
672 setpwent();
673
674 while ((entry = getpwent()) != NULL) {
675 /* Null usernames should result in all users as possible completions. */
676 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
677
678 temp = xmalloc(3 + strlen(entry->pw_name));
679 sprintf(temp, "~%s/", entry->pw_name);
680 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
681
682 matches[nm++] = temp;
683 }
684 }
685
686 endpwent();
687 (*num_matches) = nm;
688 return (matches);
689 }
690}
691#endif /* BB_FEATURE_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000692
693enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000694 FIND_EXE_ONLY = 0,
695 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000696 FIND_FILE_ONLY = 2,
697};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000698
Mark Whitley4e338752001-01-26 20:42:23 +0000699static int path_parse(char ***p, int flags)
700{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000701 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000702 char *tmp;
703 char *pth;
704
Mark Whitley4e338752001-01-26 20:42:23 +0000705 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000706 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
707 /* PATH=<empty> or PATH=:<empty> */
708 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000709 return 1;
710 }
711
712 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000713 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000714
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000715 for (;;) {
716 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000717 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000718 if (tmp) {
719 if (*++tmp == 0)
720 break; /* :<empty> */
721 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000722 break;
723 }
724
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000725 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000726
727 tmp = pth;
728 (*p)[0] = xstrdup(tmp);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000729 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000730
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000731 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000732 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000733 if (tmp) {
734 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
735 if (*++tmp == 0)
736 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000737 } else
738 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000739 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000740 }
741
742 return npth;
743}
744
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000745static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000746{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000747 int l = 0;
748 char *s = xmalloc((strlen(found) + 1) * 2);
749
750 while (*found) {
751 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
752 s[l++] = '\\';
753 s[l++] = *found++;
754 }
755 s[l] = 0;
756 return s;
757}
758
759static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
760 int type)
761{
762
763 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000764 DIR *dir;
765 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000766 char dirbuf[BUFSIZ];
767 int nm = *num_matches;
768 struct stat st;
769 char *path1[1];
770 char **paths = path1;
771 int npaths;
772 int i;
773 char found[BUFSIZ + 4 + PATH_MAX];
774 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000775
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000776 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000777
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000778 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000779 /* no dir, if flags==EXE_ONLY - get paths, else "." */
780 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000781 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000782 } else {
783 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000784 /* save for change */
785 strcpy(dirbuf, command);
786 /* set dir only */
787 dirbuf[(pfind - command) + 1] = 0;
788#ifdef BB_FEATURE_USERNAME_COMPLETION
789 if (dirbuf[0] == '~') /* ~/... or ~user/... */
790 username_tab_completion(dirbuf, 0);
791#endif
792 /* "strip" dirname in command */
793 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000794
Mark Whitley4e338752001-01-26 20:42:23 +0000795 paths[0] = dirbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000796 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000797 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000798
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000799 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000800
Mark Whitley4e338752001-01-26 20:42:23 +0000801 dir = opendir(paths[i]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000802 if (!dir) /* Don't print an error */
803 continue;
804
805 while ((next = readdir(dir)) != NULL) {
806 const char *str_merge = "%s/%s";
807 char *str_found = next->d_name;
808
Mark Whitley4e338752001-01-26 20:42:23 +0000809 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000810 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000811 continue;
812 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000813 if (*str_found == '.' && *pfind == 0) {
814 if (*paths[i] == '/' && paths[i][1] == 0
815 && str_found[1] == 0) str_found = ""; /* only "/" */
816 else
Mark Whitley4e338752001-01-26 20:42:23 +0000817 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000818 }
819 if (paths[i][strlen(paths[i]) - 1] == '/')
820 str_merge = "%s%s";
821 sprintf(found, str_merge, paths[i], str_found);
822 /* hmm, remover in progress? */
823 if (stat(found, &st) < 0)
824 continue;
825 /* find with dirs ? */
826 if (paths[i] != dirbuf)
827 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000828 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000829 /* name is directory */
830 /* algorithmic only "/" ? */
831 if (*str_found)
832 strcat(found, "/");
833 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000834 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000835 /* not put found file if search only dirs for cd */
836 if (type == FIND_DIR_ONLY)
837 continue;
838 str_found = add_quote_for_spec_chars(found);
839 if (type == FIND_FILE_ONLY ||
840 (type == FIND_EXE_ONLY && is_execute(&st) == TRUE))
841 strcat(str_found, " ");
842 }
Mark Whitley4e338752001-01-26 20:42:23 +0000843 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000844 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
845
846 matches[nm++] = str_found;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000847 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000848 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000849 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000850 if (paths != path1) {
851 free(paths[0]); /* allocated memory only in first member */
852 free(paths);
853 }
Mark Whitley4e338752001-01-26 20:42:23 +0000854 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000855 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000856}
Erik Andersenf0657d32000-04-12 17:49:52 +0000857
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000858static int match_compare(const void *a, const void *b)
859{
860 return strcmp(*(char **) a, *(char **) b);
861}
862
863
864
865#define QUOT (UCHAR_MAX+1)
866
867#define collapse_pos(is, in) { \
868 memcpy(int_buf+is, int_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); \
869 memcpy(pos_buf+is, pos_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); }
870
871static int find_match(char *matchBuf, int *len_with_quotes)
872{
873 int i, j;
874 int command_mode;
875 int c, c2;
876 int int_buf[BUFSIZ + 1];
877 int pos_buf[BUFSIZ + 1];
878
879 /* set to integer dimension characters and own positions */
880 for (i = 0;; i++) {
881 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
882 if (int_buf[i] == 0) {
883 pos_buf[i] = -1; /* indicator end line */
884 break;
885 } else
886 pos_buf[i] = i;
887 }
888
889 /* mask \+symbol and convert '\t' to ' ' */
890 for (i = j = 0; matchBuf[i]; i++, j++)
891 if (matchBuf[i] == '\\') {
892 collapse_pos(j, j + 1);
893 int_buf[j] |= QUOT;
894 i++;
895#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
896 if (matchBuf[i] == '\t') /* algorithm equivalent */
897 int_buf[j] = ' ' | QUOT;
898#endif
899 }
900#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
901 else if (matchBuf[i] == '\t')
902 int_buf[j] = ' ';
903#endif
904
905 /* mask "symbols" or 'symbols' */
906 c2 = 0;
907 for (i = 0; int_buf[i]; i++) {
908 c = int_buf[i];
909 if (c == '\'' || c == '"') {
910 if (c2 == 0)
911 c2 = c;
912 else {
913 if (c == c2)
914 c2 = 0;
915 else
916 int_buf[i] |= QUOT;
917 }
918 } else if (c2 != 0 && c != '$')
919 int_buf[i] |= QUOT;
920 }
921
922 /* skip commands with arguments if line have commands delimiters */
923 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
924 for (i = 0; int_buf[i]; i++) {
925 c = int_buf[i];
926 c2 = int_buf[i + 1];
927 j = i ? int_buf[i - 1] : -1;
928 command_mode = 0;
929 if (c == ';' || c == '&' || c == '|') {
930 command_mode = 1 + (c == c2);
931 if (c == '&') {
932 if (j == '>' || j == '<')
933 command_mode = 0;
934 } else if (c == '|' && j == '>')
935 command_mode = 0;
936 }
937 if (command_mode) {
938 collapse_pos(0, i + command_mode);
939 i = -1; /* hack incremet */
940 }
941 }
942 /* collapse `command...` */
943 for (i = 0; int_buf[i]; i++)
944 if (int_buf[i] == '`') {
945 for (j = i + 1; int_buf[j]; j++)
946 if (int_buf[j] == '`') {
947 collapse_pos(i, j + 1);
948 j = 0;
949 break;
950 }
951 if (j) {
952 /* not found close ` - command mode, collapse all previous */
953 collapse_pos(0, i + 1);
954 break;
955 } else
956 i--; /* hack incremet */
957 }
958
959 /* collapse (command...(command...)...) or {command...{command...}...} */
960 c = 0; /* "recursive" level */
961 c2 = 0;
962 for (i = 0; int_buf[i]; i++)
963 if (int_buf[i] == '(' || int_buf[i] == '{') {
964 if (int_buf[i] == '(')
965 c++;
966 else
967 c2++;
968 collapse_pos(0, i + 1);
969 i = -1; /* hack incremet */
970 }
971 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
972 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
973 if (int_buf[i] == ')')
974 c--;
975 else
976 c2--;
977 collapse_pos(0, i + 1);
978 i = -1; /* hack incremet */
979 }
980
981 /* skip first not quote space */
982 for (i = 0; int_buf[i]; i++)
983 if (int_buf[i] != ' ')
984 break;
985 if (i)
986 collapse_pos(0, i);
987
988 /* set find mode for completion */
989 command_mode = FIND_EXE_ONLY;
990 for (i = 0; int_buf[i]; i++)
991 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
992 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
993 && strncmp(&matchBuf[pos_buf[0]], "cd", 2) == 0)
994 command_mode = FIND_DIR_ONLY;
995 else {
996 command_mode = FIND_FILE_ONLY;
997 break;
998 }
999 }
1000 /* "strlen" */
1001 for (i = 0; int_buf[i]; i++);
1002 /* find last word */
1003 for (--i; i >= 0; i--) {
1004 c = int_buf[i];
1005 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
1006 collapse_pos(0, i + 1);
1007 break;
1008 }
1009 }
1010 /* skip first not quoted '\'' or '"' */
1011 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
1012 /* collapse quote or unquote // or /~ */
1013 while ((int_buf[i] & ~QUOT) == '/' && (
1014 (int_buf[i + 1] & ~QUOT) == '/'
1015 || (int_buf[i + 1] & ~QUOT) ==
1016 '~')) i++;
1017 if (i)
1018 collapse_pos(0, i);
1019
1020 /* set only match and destroy quotes */
1021 j = 0;
1022 for (i = 0; pos_buf[i] >= 0; i++) {
1023 matchBuf[i] = matchBuf[pos_buf[i]];
1024 j = pos_buf[i] + 1;
1025 }
1026 matchBuf[i] = 0;
1027 /* old lenght matchBuf with quotes symbols */
1028 *len_with_quotes = j ? j - pos_buf[0] : 0;
1029
1030 return command_mode;
1031}
1032
1033
1034static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001035{
1036 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001037 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +00001038 static char **matches;
1039
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001040 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001041 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001042 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001043 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001044 free(matches);
1045 matches = (char **) NULL;
1046 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001047 return;
1048 }
1049 if (*lastWasTab == FALSE) {
1050
1051 char *tmp;
1052 int len_found;
1053 char matchBuf[BUFSIZ];
1054 int find_type;
1055 int recalc_pos;
1056
1057 *lastWasTab = TRUE; /* flop trigger */
1058
1059 /* Make a local copy of the string -- up
1060 * to the position of the cursor */
1061 tmp = strncpy(matchBuf, command_ps, cursor);
1062 tmp[cursor] = 0;
1063
1064 find_type = find_match(matchBuf, &recalc_pos);
1065
1066 /* Free up any memory already allocated */
1067 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001068
Mark Whitley4e338752001-01-26 20:42:23 +00001069#ifdef BB_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001070 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001071 * then try completing this word as a username. */
1072
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001073 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001074 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001075#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001076 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001077 * in the current working directory that matches. */
1078 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001079 matches =
1080 exe_n_cwd_tab_completion(matchBuf, &num_matches,
1081 find_type);
Erik Andersenf0657d32000-04-12 17:49:52 +00001082
1083 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001084 if (!matches || num_matches > 1) {
1085 char *tmp1;
1086
Mark Whitley4e338752001-01-26 20:42:23 +00001087 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001088 if (!matches)
1089 return; /* not found */
1090 /* sort */
1091 qsort(matches, num_matches, sizeof(char *), match_compare);
1092
1093 /* find minimal match */
1094 tmp = xstrdup(matches[0]);
1095 for (tmp1 = tmp; *tmp1; tmp1++)
1096 for (len_found = 1; len_found < num_matches; len_found++)
1097 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1098 *tmp1 = 0;
1099 break;
1100 }
1101 if (*tmp == 0) { /* have unique */
1102 free(tmp);
1103 return;
1104 }
1105 } else { /* one match */
1106 tmp = matches[0];
1107 /* for next completion current found */
1108 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001109 }
1110
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001111 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001112 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001113 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001114
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001115 /* before word for match */
1116 command_ps[cursor - recalc_pos] = 0;
1117 /* save tail line */
1118 strcpy(matchBuf, command_ps + cursor);
1119 /* add match */
1120 strcat(command_ps, tmp);
1121 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001122 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001123 /* back to begin word for match */
1124 input_backward(recalc_pos);
1125 /* new pos */
1126 recalc_pos = cursor + len_found;
1127 /* new len */
1128 len = strlen(command_ps);
1129 /* write out the matched command */
1130 input_end();
1131 input_backward(cursor - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001132 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001133 if (tmp != matches[0])
1134 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001135 } else {
1136 /* Ok -- the last char was a TAB. Since they
1137 * just hit TAB again, print a list of all the
1138 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001139 if (matches && num_matches > 0) {
1140 int i, col, l;
1141 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001142
1143 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001144 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001145 for (i = 0, col = 0; i < num_matches; i++) {
1146 l = strlen(matches[i]);
1147 if (l < 14)
1148 l = 14;
1149 printf("%-14s ", matches[i]);
1150 if ((l += 2) > 16)
1151 while (l % 16) {
1152 putchar(' ');
1153 l++;
1154 }
1155 col += l;
1156 col -= (col / cmdedit_termw) * cmdedit_termw;
1157 if (col > 60 && matches[i + 1] != NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +00001158 putchar('\n');
Erik Andersenf0657d32000-04-12 17:49:52 +00001159 col = 0;
1160 }
1161 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001162 /* Go to the next line and rewrite */
1163 putchar('\n');
1164 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001165 }
1166 }
1167}
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001168#endif /* BB_FEATURE_SH_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001169
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001170static void get_previous_history(struct history **hp, struct history *p)
Erik Andersenf0657d32000-04-12 17:49:52 +00001171{
Eric Andersen91a44002000-07-19 17:37:57 +00001172 if ((*hp)->s)
1173 free((*hp)->s);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001174 (*hp)->s = xstrdup(command_ps);
1175 *hp = p;
Erik Andersenf0657d32000-04-12 17:49:52 +00001176}
1177
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001178static inline void get_next_history(struct history **hp)
Erik Andersenf0657d32000-04-12 17:49:52 +00001179{
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001180 get_previous_history(hp, (*hp)->n);
Erik Andersenf0657d32000-04-12 17:49:52 +00001181}
1182
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001183enum {
1184 ESC = 27,
1185 DEL = 127,
1186};
1187
1188
Erik Andersen6273f652000-03-17 01:12:41 +00001189/*
1190 * This function is used to grab a character buffer
1191 * from the input file descriptor and allows you to
1192 * a string with full command editing (sortof like
1193 * a mini readline).
1194 *
1195 * The following standard commands are not implemented:
1196 * ESC-b -- Move back one word
1197 * ESC-f -- Move forward one word
1198 * ESC-d -- Delete back one word
1199 * ESC-h -- Delete forward one word
1200 * CTL-t -- Transpose two characters
1201 *
1202 * Furthermore, the "vi" command editing keys are not implemented.
1203 *
Erik Andersen6273f652000-03-17 01:12:41 +00001204 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001205extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001206{
1207
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001208 int inputFd = fileno(stdin);
Mark Whitley4e338752001-01-26 20:42:23 +00001209
Erik Andersenc7c634b2000-03-19 05:28:55 +00001210 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001211 int lastWasTab = FALSE;
1212 char c = 0;
1213 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +00001214
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001215 /* prepare before init handlers */
1216 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001217 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001218 command_ps = command;
1219
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001220 if (new_settings.c_cc[VMIN] == 0) { /* first call */
1221
1222 getTermSettings(inputFd, (void *) &initial_settings);
Erik Andersen1d1d9502000-04-21 01:26:49 +00001223 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001224
Erik Andersen1d1d9502000-04-21 01:26:49 +00001225 new_settings.c_cc[VMIN] = 1;
1226 new_settings.c_cc[VTIME] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001227 new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001228 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001229 new_settings.c_lflag &= ~(ECHO | ECHOCTL | ECHONL); /* Turn off echoing */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001230 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001231
1232 command[0] = 0;
1233
1234 setTermSettings(inputFd, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001235 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001236
Mark Whitley4e338752001-01-26 20:42:23 +00001237 cmdedit_init();
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001238 /* Print out the command prompt */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001239 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001240
Erik Andersenc7c634b2000-03-19 05:28:55 +00001241 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001242
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001243 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001244
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001245 if (read(inputFd, &c, 1) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +00001246 return;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001247
Erik Andersen13456d12000-03-16 08:09:57 +00001248 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001249 case '\n':
1250 case '\r':
1251 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001252 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001253 break_out = 1;
1254 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001255 case 1:
1256 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001257 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001258 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001259 case 2:
1260 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001261 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001262 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001263 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001264 /* Control-c -- stop gathering input */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001265
Eric Andersen86349772000-12-18 20:25:50 +00001266 /* Link into lash to reset context to 0 on ^C and such */
1267 shell_context = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001268
1269 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001270 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001271 command[0] = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001272
Eric Andersen86349772000-12-18 20:25:50 +00001273 return;
Erik Andersenf0657d32000-04-12 17:49:52 +00001274 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001275 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001276 * if the len=0 and no chars to delete */
1277 if (len == 0) {
Mark Whitley4e338752001-01-26 20:42:23 +00001278 printf("exit");
Erik Andersenf0657d32000-04-12 17:49:52 +00001279 clean_up_and_die(0);
1280 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001281 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001282 }
1283 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001284 case 5:
1285 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001286 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001287 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001288 case 6:
1289 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001290 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001291 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001292 case '\b':
1293 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001294 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001295 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001296 break;
1297 case '\t':
Erik Andersena2685732000-04-09 18:27:46 +00001298#ifdef BB_FEATURE_SH_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001299 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001300#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001301 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001302 case 14:
1303 /* Control-n -- Get next command in history */
1304 if (hp && hp->n && hp->n->s) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001305 get_next_history(&hp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001306 goto rewrite_line;
1307 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001308 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001309 }
1310 break;
1311 case 16:
1312 /* Control-p -- Get previous command from history */
1313 if (hp && hp->p) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001314 get_previous_history(&hp, hp->p);
Erik Andersenf0657d32000-04-12 17:49:52 +00001315 goto rewrite_line;
1316 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001317 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001318 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001319 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001320 case 21:
1321 /* Control-U -- Clear line before cursor */
1322 if (cursor) {
1323 strcpy(command, command + cursor);
1324 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001325 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001326 break;
1327
1328 case ESC:{
1329 /* escape sequence follows */
1330 if (read(inputFd, &c, 1) < 1)
1331 return;
1332 /* different vt100 emulations */
1333 if (c == '[' || c == 'O') {
1334 if (read(inputFd, &c, 1) < 1)
1335 return;
1336 }
1337 switch (c) {
1338#ifdef BB_FEATURE_SH_TAB_COMPLETION
1339 case '\t': /* Alt-Tab */
1340
1341 input_tab(&lastWasTab);
1342 break;
1343#endif
1344 case 'A':
1345 /* Up Arrow -- Get previous command from history */
1346 if (hp && hp->p) {
1347 get_previous_history(&hp, hp->p);
1348 goto rewrite_line;
1349 } else {
1350 beep();
1351 }
1352 break;
1353 case 'B':
1354 /* Down Arrow -- Get next command in history */
1355 if (hp && hp->n && hp->n->s) {
1356 get_next_history(&hp);
1357 goto rewrite_line;
1358 } else {
1359 beep();
1360 }
1361 break;
1362
1363 /* Rewrite the line with the selected history item */
1364 rewrite_line:
1365 /* change command */
1366 len = strlen(strcpy(command, hp->s));
1367 /* redraw and go to end line */
1368 redraw(cmdedit_y, 0);
1369 break;
1370 case 'C':
1371 /* Right Arrow -- Move forward one character */
1372 input_forward();
1373 break;
1374 case 'D':
1375 /* Left Arrow -- Move back one character */
1376 input_backward(1);
1377 break;
1378 case '3':
1379 /* Delete */
1380 input_delete();
1381 break;
1382 case '1':
1383 case 'H':
1384 /* Home (Ctrl-A) */
1385 input_backward(cursor);
1386 break;
1387 case '4':
1388 case 'F':
1389 /* End (Ctrl-E) */
1390 input_end();
1391 break;
1392 default:
1393 if (!(c >= '1' && c <= '9'))
1394 c = 0;
1395 beep();
1396 }
1397 if (c >= '1' && c <= '9')
1398 do
1399 if (read(inputFd, &c, 1) < 1)
1400 return;
1401 while (c != '~');
1402 break;
1403 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001404
1405 default: /* If it's regular input, do the normal thing */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001406#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1407 /* Control-V -- Add non-printable symbol */
1408 if (c == 22) {
1409 if (read(inputFd, &c, 1) < 1)
1410 return;
1411 if (c == 0) {
1412 beep();
1413 break;
1414 }
1415 } else
1416#endif
1417 if (!isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001418 break;
1419
1420 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
1421 break;
1422
1423 len++;
1424
1425 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001426 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001427 *(command + cursor + 1) = 0;
1428 cmdedit_set_out_char(0);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001429 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001430 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001431
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001432 memmove(command + sc + 1, command + sc, len - sc);
1433 *(command + sc) = c;
1434 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001435 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001436 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001437 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001438 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001439 }
1440
Erik Andersenc7c634b2000-03-19 05:28:55 +00001441 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001442 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001443 if (break_out) /* Enter is the command terminator, no more input. */
1444 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001445
1446 if (c != '\t')
1447 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001448 }
1449
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001450 setTermSettings(inputFd, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001451 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001452
1453 /* Handle command history log */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001454 if (len) { /* no put empty line */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001455
1456 struct history *h = his_end;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001457 char *ss;
Mark Whitley4e338752001-01-26 20:42:23 +00001458
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001459 ss = xstrdup(command); /* duplicate */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001460
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001461 if (h == 0) {
Eric Andersen91a44002000-07-19 17:37:57 +00001462 /* No previous history -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001463 h = his_front = xmalloc(sizeof(struct history));
1464 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001465
1466 h->p = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001467 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001468 h->n->p = h;
1469 h->n->n = NULL;
1470 h->n->s = NULL;
1471 his_end = h->n;
1472 history_counter++;
1473 } else {
Eric Andersen91a44002000-07-19 17:37:57 +00001474 /* Add a new history command -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001475 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001476
1477 h->n->p = h;
1478 h->n->n = NULL;
1479 h->n->s = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001480 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001481 his_end = h->n;
1482
1483 /* After max history, remove the oldest command */
1484 if (history_counter >= MAX_HISTORY) {
1485
1486 struct history *p = his_front->n;
1487
1488 p->p = NULL;
1489 free(his_front->s);
1490 free(his_front);
1491 his_front = p;
1492 } else {
1493 history_counter++;
1494 }
Erik Andersen13456d12000-03-16 08:09:57 +00001495 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001496#if defined(BB_FEATURE_BASH_STYLE_PROMT)
1497 num_ok_lines++;
1498#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001499 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001500 command[len++] = '\n'; /* set '\n' */
1501 command[len] = 0;
1502#if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_SH_TAB_COMPLETION)
1503 input_tab(0); /* strong free */
1504#endif
1505#if defined(BB_FEATURE_BASH_STYLE_PROMT)
1506 free(cmdedit_prompt);
1507#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001508 return;
Erik Andersen13456d12000-03-16 08:09:57 +00001509}
1510
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001511
Mark Whitley4e338752001-01-26 20:42:23 +00001512/* Undo the effects of cmdedit_init(). */
Eric Andersen501c88b2000-07-28 15:14:45 +00001513extern void cmdedit_terminate(void)
1514{
1515 cmdedit_reset_term();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001516 if ((handlers_sets & SET_TERM_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +00001517 signal(SIGKILL, SIG_DFL);
1518 signal(SIGINT, SIG_DFL);
1519 signal(SIGQUIT, SIG_DFL);
1520 signal(SIGTERM, SIG_DFL);
1521 signal(SIGWINCH, SIG_DFL);
1522 handlers_sets &= ~SET_TERM_HANDLERS;
1523 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001524}
1525
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001526#endif /* BB_FEATURE_SH_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001527
1528
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001529#ifdef TEST
1530
1531unsigned int shell_context;
1532
1533int main(int argc, char **argv)
1534{
1535 char buff[BUFSIZ];
1536 char *prompt =
1537#if defined(BB_FEATURE_BASH_STYLE_PROMT)
1538 "\\[\\033[32;1m\\]\\u@\\[\\033[33;1m\\]\\h:\
1539\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
1540\\!\\[\\033[36;1m\\]\\$ \\[\\033[0m\\]";
1541#else
1542 "% ";
1543#endif
1544
1545 shell_context = 1;
1546 do {
1547 cmdedit_read_input(prompt, buff);
1548 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1549 } while (shell_context);
1550 printf("*** cmdedit_read_input() detect ^C\n");
1551 return 0;
1552}
1553
1554#endif /* TEST */