blob: 993e83b21a05c6f595847f8704a52837dea2a9b0 [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
Eric Andersen5f2c79d2001-02-16 18:36:04 +000045#ifndef TEST
Mark Whitley4e338752001-01-26 20:42:23 +000046
Eric Andersen3570a342000-09-25 21:45:58 +000047#include "busybox.h"
Mark Whitley4e338752001-01-26 20:42:23 +000048
Eric Andersen5f2c79d2001-02-16 18:36:04 +000049#define D(x)
50
51#else
52
Eric Andersen94456f52001-02-18 20:26:48 +000053#define BB_FEATURE_SH_COMMAND_EDITING
54#define BB_FEATURE_SH_TAB_COMPLETION
55#define BB_FEATURE_SH_USERNAME_COMPLETION
56#define BB_FEATURE_NONPRINTABLE_INVERSE_PUT
57#define BB_FEATURE_BASH_STYLE_PROMT
58#define BB_FEATURE_CLEAN_UP
Eric Andersen5f2c79d2001-02-16 18:36:04 +000059
Eric Andersen5f2c79d2001-02-16 18:36:04 +000060#define D(x) x
61
62#endif /* TEST */
63
Eric Andersen5165fbe2001-02-20 06:42:29 +000064#ifdef BB_FEATURE_SH_TAB_COMPLETION
65#include <dirent.h>
66#include <sys/stat.h>
67#endif
68
Erik Andersen13456d12000-03-16 08:09:57 +000069#ifdef BB_FEATURE_SH_COMMAND_EDITING
70
Eric Andersen5f2c79d2001-02-16 18:36:04 +000071#ifndef BB_FEATURE_SH_TAB_COMPLETION
Eric Andersen28a78ab2001-02-16 20:26:50 +000072#undef BB_FEATURE_SH_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +000073#endif
74
Eric Andersen28a78ab2001-02-16 20:26:50 +000075#if defined(BB_FEATURE_SH_USERNAME_COMPLETION) || defined(BB_FEATURE_BASH_STYLE_PROMT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000076#define BB_FEATURE_GETUSERNAME_AND_HOMEDIR
77#endif
78
Eric Andersen5f2c79d2001-02-16 18:36:04 +000079#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
80#ifndef TEST
Eric Andersenaf4ac772001-02-01 22:43:49 +000081#include "pwd_grp/pwd.h"
Eric Andersen5f2c79d2001-02-16 18:36:04 +000082#else
83#include <pwd.h>
84#endif /* TEST */
85#endif /* advanced FEATURES */
Eric Andersenaf4ac772001-02-01 22:43:49 +000086
87
Eric Andersen5f2c79d2001-02-16 18:36:04 +000088#ifdef TEST
89void *xrealloc(void *old, size_t size)
90{
91 return realloc(old, size);
92}
Erik Andersen13456d12000-03-16 08:09:57 +000093
Eric Andersen5f2c79d2001-02-16 18:36:04 +000094void *xmalloc(size_t size)
95{
96 return malloc(size);
97}
98char *xstrdup(const char *s)
99{
100 return strdup(s);
101}
102
103void *xcalloc(size_t size, size_t se)
104{
105 return calloc(size, se);
106}
107
108#define error_msg(s, d) fprintf(stderr, s, d)
109#endif
110
111
112struct history {
113 char *s;
114 struct history *p;
115 struct history *n;
Mark Whitley59ab0252001-01-23 22:30:04 +0000116};
117
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000118/* Maximum length of the linked list for the command line history */
119static const int MAX_HISTORY = 15;
Erik Andersen13456d12000-03-16 08:09:57 +0000120
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000121/* First element in command line list */
122static struct history *his_front = NULL;
123
124/* Last element in command line list */
125static struct history *his_end = NULL;
126
Erik Andersen1d1d9502000-04-21 01:26:49 +0000127
128/* ED: sparc termios is broken: revert back to old termio handling. */
Erik Andersen1d1d9502000-04-21 01:26:49 +0000129
130#if #cpu(sparc)
131# include <termio.h>
132# define termios termio
133# define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp)
134# define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp)
135#else
136# include <termios.h>
137# define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
138# define getTermSettings(fd,argp) tcgetattr(fd, argp);
139#endif
140
141/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000142static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000143
144
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000145#ifndef _POSIX_VDISABLE
146#define _POSIX_VDISABLE '\0'
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000147#endif
148
Erik Andersen1d1d9502000-04-21 01:26:49 +0000149
Mark Whitley4e338752001-01-26 20:42:23 +0000150static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000151volatile int cmdedit_termw = 80; /* actual terminal width */
152static int history_counter = 0; /* Number of commands in history list */
Mark Whitley4e338752001-01-26 20:42:23 +0000153static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000154volatile int handlers_sets = 0; /* Set next bites: */
155
Mark Whitley4e338752001-01-26 20:42:23 +0000156enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000157 SET_ATEXIT = 1, /* when atexit() has been called and
158 get euid,uid,gid to fast compare */
159 SET_TERM_HANDLERS = 2, /* set many terminates signal handlers */
160 SET_WCHG_HANDLERS = 4, /* winchg signal handler */
161 SET_RESET_TERM = 8, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000162};
Erik Andersen13456d12000-03-16 08:09:57 +0000163
Mark Whitley4e338752001-01-26 20:42:23 +0000164
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000165static int cmdedit_x; /* real x terminal position */
166static int cmdedit_y; /* pseudoreal y terminal position */
167static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000168
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000169static int cursor; /* required global for signal handler */
170static int len; /* --- "" - - "" - -"- --""-- --""--- */
171static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
172static
173#ifndef BB_FEATURE_BASH_STYLE_PROMT
174 const
175#endif
176char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
177
178/* Link into lash to reset context to 0 on ^C and such */
Eric Andersen86349772000-12-18 20:25:50 +0000179extern unsigned int shell_context;
180
Erik Andersen13456d12000-03-16 08:09:57 +0000181
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000182#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
183static char *user_buf = "";
184static char *home_pwd_buf = "";
185static int my_euid;
186#endif
187
188#ifdef BB_FEATURE_BASH_STYLE_PROMT
189static char *hostname_buf = "";
190static int num_ok_lines = 1;
191#endif
192
193
194#ifdef BB_FEATURE_SH_TAB_COMPLETION
195
196#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
197static int my_euid;
198#endif
199
200static int my_uid;
201static int my_gid;
202
203#endif /* BB_FEATURE_SH_TAB_COMPLETION */
204
Erik Andersen13456d12000-03-16 08:09:57 +0000205
Mark Whitley4e338752001-01-26 20:42:23 +0000206static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000207
Mark Whitley4e338752001-01-26 20:42:23 +0000208static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000209{
210 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000211 static __sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000212
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000213 /* emulate || signal call */
214 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Mark Whitley4e338752001-01-26 20:42:23 +0000215 ioctl(0, TIOCGWINSZ, &win);
216 if (win.ws_col > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000217 cmdedit_setwidth(win.ws_col, nsig == SIGWINCH);
218 }
Mark Whitley4e338752001-01-26 20:42:23 +0000219 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000220 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000221
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000222 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000223 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000224 else if (nsig == SIGWINCH) /* signaled called handler */
225 signal(SIGWINCH, win_changed); /* set for next call */
226 else /* nsig == 0 */
227 /* set previous handler */
228 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000229}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000230
231static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000232{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000233 if ((handlers_sets & SET_RESET_TERM) != 0) {
Erik Andersena6c75222000-04-18 00:00:52 +0000234 /* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000235 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000236 handlers_sets &= ~SET_RESET_TERM;
237 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000238 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000239 /* reset SIGWINCH handler to previous (default) */
240 win_changed(0);
241 handlers_sets &= ~SET_WCHG_HANDLERS;
242 }
243 fflush(stdout);
Eric Andersenb040d4f2000-07-25 18:01:20 +0000244#ifdef BB_FEATURE_CLEAN_UP
245 if (his_front) {
246 struct history *n;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000247
Eric Andersenb040d4f2000-07-25 18:01:20 +0000248 //while(his_front!=his_end) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000249 while (his_front != his_end) {
Eric Andersenb040d4f2000-07-25 18:01:20 +0000250 n = his_front->n;
251 free(his_front->s);
252 free(his_front);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000253 his_front = n;
Eric Andersenb040d4f2000-07-25 18:01:20 +0000254 }
255 }
256#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000257}
258
Mark Whitley4e338752001-01-26 20:42:23 +0000259
Mark Whitley4e338752001-01-26 20:42:23 +0000260/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000261static void cmdedit_set_out_char(int next_char)
262{
263
264 int c = command_ps[cursor];
265
266 if (c == 0)
267 c = ' '; /* destroy end char? */
268#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
269 if (!isprint(c)) { /* Inverse put non-printable characters */
270 if (((unsigned char) c) >= 128)
271 c -= 128;
272 if (((unsigned char) c) < ' ')
273 c += '@';
274 if (c == 127)
275 c = '?';
276 printf("\033[7m%c\033[0m", c);
277 } else
278#endif
279 putchar(c);
280 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000281 /* terminal is scrolled down */
282 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000283 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000284
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000285 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000286 next_char = ' ';
287 /* destroy "(auto)margin" */
288 putchar(next_char);
289 putchar('\b');
290 }
291 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000292}
293
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000294/* Move to end line. Bonus: rewrite line from cursor */
295static void input_end(void)
296{
297 while (cursor < len)
298 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000299}
300
301/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000302static void goto_new_line(void)
303{
Mark Whitley4e338752001-01-26 20:42:23 +0000304 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000305 if (cmdedit_x)
306 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000307}
308
309
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000310static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000311{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000312 fputs(s, stdout);
313}
314static inline void beep(void)
315{
316 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000317}
318
Mark Whitley4e338752001-01-26 20:42:23 +0000319/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000320/* special for slow terminal */
321static void input_backward(int num)
322{
323 if (num > cursor)
324 num = cursor;
325 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000326
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000327 if (cmdedit_x >= num) { /* no to up line */
328 cmdedit_x -= num;
329 if (num < 4)
330 while (num-- > 0)
331 putchar('\b');
332
333 else
334 printf("\033[%dD", num);
335 } else {
336 int count_y;
337
338 if (cmdedit_x) {
339 putchar('\r'); /* back to first terminal pos. */
340 num -= cmdedit_x; /* set previous backward */
341 }
342 count_y = 1 + num / cmdedit_termw;
343 printf("\033[%dA", count_y);
344 cmdedit_y -= count_y;
345 /* require forward after uping */
346 cmdedit_x = cmdedit_termw * count_y - num;
347 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000348 }
Erik Andersen13456d12000-03-16 08:09:57 +0000349}
350
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000351static void put_prompt(void)
352{
353 out1str(cmdedit_prompt);
354 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
355 cursor = 0;
356}
357
358#ifdef BB_FEATURE_BASH_STYLE_PROMT
359static void
360add_to_prompt(char **prmt_mem_ptr, int *alm, int *prmt_len,
361 const char *addb)
362{
363 int l = strlen(addb);
364
365 *prmt_len += l;
366 if (*alm < (*prmt_len) + 1) {
367 *alm = (*prmt_len) + 1;
368 *prmt_mem_ptr = xrealloc(*prmt_mem_ptr, *alm);
369 }
370 strcat(*prmt_mem_ptr, addb);
371}
372#endif
373
374static void parse_prompt(const char *prmt_ptr)
375{
376#ifdef BB_FEATURE_BASH_STYLE_PROMT
377 int alm = strlen(prmt_ptr) + 1; /* supposedly require memory */
378 int prmt_len = 0;
379 int sub_len = 0;
380 int flg_not_length = '[';
381 char *prmt_mem_ptr = xstrdup(prmt_ptr);
382 char pwd_buf[PATH_MAX + 1];
383 char buf[16];
384 int c;
385
386 pwd_buf[0] = 0;
387 *prmt_mem_ptr = 0;
388
389 while (*prmt_ptr) {
390 c = *prmt_ptr++;
391 if (c == '\\') {
392 c = *prmt_ptr;
393 if (c == 0)
394 break;
395 prmt_ptr++;
396 switch (c) {
397 case 'u':
398 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, user_buf);
399 continue;
400 case 'h':
401 if (hostname_buf[0] == 0) {
402 hostname_buf = xcalloc(256, 1);
403 if (gethostname(hostname_buf, 255) < 0) {
404 strcpy(hostname_buf, "?");
405 } else {
406 char *s = strchr(hostname_buf, '.');
407
408 if (s)
409 *s = 0;
410 }
411 }
412 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len,
413 hostname_buf);
414 continue;
415 case '$':
416 c = my_euid == 0 ? '#' : '$';
417 break;
418 case 'w':
419 if (pwd_buf[0] == 0) {
420 int l;
421
422 getcwd(pwd_buf, PATH_MAX);
423 l = strlen(home_pwd_buf);
424 if (home_pwd_buf[0] != 0 &&
425 strncmp(home_pwd_buf, pwd_buf, l) == 0) {
426 strcpy(pwd_buf + 1, pwd_buf + l);
427 pwd_buf[0] = '~';
428 }
429 }
430 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf);
431 continue;
432 case '!':
433 snprintf(buf, sizeof(buf), "%d", num_ok_lines);
434 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf);
435 continue;
436 case 'e':
437 case 'E': /* \e \E = \033 */
438 c = '\033';
439 break;
440 case 'x':
441 case 'X':
442 case '0':
443 case '1':
444 case '2':
445 case '3':
446 case '4':
447 case '5':
448 case '6':
449 case '7':{
450 int l;
451 int ho = 0;
452 char *eho;
453
454 if (c == 'X')
455 c = 'x';
456
457 for (l = 0; l < 3;) {
458
459 buf[l++] = *prmt_ptr;
460 buf[l] = 0;
461 ho = strtol(buf, &eho, c == 'x' ? 16 : 8);
462 if (ho > UCHAR_MAX || (eho - buf) < l) {
463 l--;
464 break;
465 }
466 prmt_ptr++;
467 }
468 buf[l] = 0;
469 ho = strtol(buf, 0, c == 'x' ? 16 : 8);
470 c = ho == 0 ? '?' : (char) ho;
471 break;
472 }
473 case '[':
474 case ']':
475 if (c == flg_not_length) {
476 flg_not_length = flg_not_length == '[' ? ']' : '[';
477 continue;
478 }
479 break;
480 }
481 }
482 buf[0] = c;
483 buf[1] = 0;
484 add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf);
485 if (flg_not_length == ']')
486 sub_len++;
487 }
488 cmdedit_prmt_len = prmt_len - sub_len;
489 cmdedit_prompt = prmt_mem_ptr;
490#else
491 cmdedit_prompt = prmt_ptr;
492 cmdedit_prmt_len = strlen(prmt_ptr);
493#endif
494 put_prompt();
495}
496
497
498/* draw promt, editor line, and clear tail */
499static void redraw(int y, int back_cursor)
500{
501 if (y > 0) /* up to start y */
502 printf("\033[%dA", y);
503 cmdedit_y = 0; /* new quasireal y */
504 putchar('\r');
505 put_prompt();
506 input_end(); /* rewrite */
507 printf("\033[J"); /* destroy tail after cursor */
508 input_backward(back_cursor);
509}
510
Erik Andersenf0657d32000-04-12 17:49:52 +0000511/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000512static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000513{
Mark Whitley4e338752001-01-26 20:42:23 +0000514 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000515
Mark Whitley4e338752001-01-26 20:42:23 +0000516 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000517 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518
519 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000520 len--;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000521 input_end(); /* rewtite new line */
522 cmdedit_set_out_char(0); /* destroy end char */
523 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000524}
525
Mark Whitley4e338752001-01-26 20:42:23 +0000526/* Delete the char in back of the cursor */
527static void input_backspace(void)
528{
529 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000530 input_backward(1);
531 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000532 }
533}
534
535
Erik Andersenf0657d32000-04-12 17:49:52 +0000536/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000537static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000538{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000539 if (cursor < len)
540 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000541}
542
543
Mark Whitley4e338752001-01-26 20:42:23 +0000544static void clean_up_and_die(int sig)
545{
546 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000547 if (sig != SIGINT)
548 exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */
Mark Whitley4e338752001-01-26 20:42:23 +0000549 cmdedit_reset_term();
550}
551
552static void cmdedit_setwidth(int w, int redraw_flg)
553{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000554 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000555 if (w <= cmdedit_termw) {
556 cmdedit_termw = cmdedit_termw % w;
557 }
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 }
Eric Andersen6faae7d2001-02-16 20:09:17 +0000570 }
Mark Whitley4e338752001-01-26 20:42:23 +0000571}
572
573extern void cmdedit_init(void)
574{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000575 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
576 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000577 win_changed(-SIGWINCH);
578 handlers_sets |= SET_WCHG_HANDLERS;
579 }
580
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000581 if ((handlers_sets & SET_ATEXIT) == 0) {
582#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
583 struct passwd *entry;
584
585 my_euid = geteuid();
586 entry = getpwuid(my_euid);
587 if (entry) {
588 user_buf = xstrdup(entry->pw_name);
589 home_pwd_buf = xstrdup(entry->pw_dir);
590 }
591#endif
592
593#ifdef BB_FEATURE_SH_TAB_COMPLETION
594
595#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
596 my_euid = geteuid();
597#endif
598 my_uid = getuid();
599 my_gid = getgid();
600#endif /* BB_FEATURE_SH_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000601 handlers_sets |= SET_ATEXIT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000602 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000603 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000604
605 if ((handlers_sets & SET_TERM_HANDLERS) == 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000606 signal(SIGKILL, clean_up_and_die);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000607 signal(SIGINT, clean_up_and_die);
Mark Whitley4e338752001-01-26 20:42:23 +0000608 signal(SIGQUIT, clean_up_and_die);
609 signal(SIGTERM, clean_up_and_die);
610 handlers_sets |= SET_TERM_HANDLERS;
611 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000612
Mark Whitley4e338752001-01-26 20:42:23 +0000613}
Erik Andersenf0657d32000-04-12 17:49:52 +0000614
615#ifdef BB_FEATURE_SH_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000616
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000617static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000618{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000619 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
620 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
621 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
622 (st->st_mode & S_IXOTH)) return TRUE;
623 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000624}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000625
Eric Andersen28a78ab2001-02-16 20:26:50 +0000626#ifdef BB_FEATURE_SH_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000627
628static char **username_tab_completion(char *ud, int *num_matches)
629{
630 struct passwd *entry;
631 int userlen;
632 char *temp;
633
634
635 ud++; /* ~user/... to user/... */
636 userlen = strlen(ud);
637
638 if (num_matches == 0) { /* "~/..." or "~user/..." */
639 char *sav_ud = ud - 1;
640 char *home = 0;
641
642 if (*ud == '/') { /* "~/..." */
643 home = home_pwd_buf;
644 } else {
645 /* "~user/..." */
646 temp = strchr(ud, '/');
647 *temp = 0; /* ~user\0 */
648 entry = getpwnam(ud);
649 *temp = '/'; /* restore ~user/... */
650 ud = temp;
651 if (entry)
652 home = entry->pw_dir;
653 }
654 if (home) {
655 if ((userlen + strlen(home) + 1) < BUFSIZ) {
656 char temp2[BUFSIZ]; /* argument size */
657
658 /* /home/user/... */
659 sprintf(temp2, "%s%s", home, ud);
660 strcpy(sav_ud, temp2);
661 }
662 }
663 return 0; /* void, result save to argument :-) */
664 } else {
665 /* "~[^/]*" */
666 char **matches = (char **) NULL;
667 int nm = 0;
668
669 setpwent();
670
671 while ((entry = getpwent()) != NULL) {
672 /* Null usernames should result in all users as possible completions. */
673 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
674
675 temp = xmalloc(3 + strlen(entry->pw_name));
676 sprintf(temp, "~%s/", entry->pw_name);
677 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
678
679 matches[nm++] = temp;
680 }
681 }
682
683 endpwent();
684 (*num_matches) = nm;
685 return (matches);
686 }
687}
Eric Andersen28a78ab2001-02-16 20:26:50 +0000688#endif /* BB_FEATURE_SH_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000689
690enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000691 FIND_EXE_ONLY = 0,
692 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000693 FIND_FILE_ONLY = 2,
694};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000695
Mark Whitley4e338752001-01-26 20:42:23 +0000696static int path_parse(char ***p, int flags)
697{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000698 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000699 char *tmp;
700 char *pth;
701
Mark Whitley4e338752001-01-26 20:42:23 +0000702 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000703 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
704 /* PATH=<empty> or PATH=:<empty> */
705 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000706 return 1;
707 }
708
709 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000710 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000711
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000712 for (;;) {
713 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000714 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000715 if (tmp) {
716 if (*++tmp == 0)
717 break; /* :<empty> */
718 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000719 break;
720 }
721
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000722 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000723
724 tmp = pth;
725 (*p)[0] = xstrdup(tmp);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000726 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000727
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000728 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000729 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000730 if (tmp) {
731 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
732 if (*++tmp == 0)
733 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000734 } else
735 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000736 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000737 }
738
739 return npth;
740}
741
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000742static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000743{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000744 int l = 0;
745 char *s = xmalloc((strlen(found) + 1) * 2);
746
747 while (*found) {
748 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
749 s[l++] = '\\';
750 s[l++] = *found++;
751 }
752 s[l] = 0;
753 return s;
754}
755
756static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
757 int type)
758{
759
760 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000761 DIR *dir;
762 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000763 char dirbuf[BUFSIZ];
764 int nm = *num_matches;
765 struct stat st;
766 char *path1[1];
767 char **paths = path1;
768 int npaths;
769 int i;
770 char found[BUFSIZ + 4 + PATH_MAX];
771 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000772
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000773 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000774
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000775 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000776 /* no dir, if flags==EXE_ONLY - get paths, else "." */
777 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000778 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000779 } else {
780 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000781 /* save for change */
782 strcpy(dirbuf, command);
783 /* set dir only */
784 dirbuf[(pfind - command) + 1] = 0;
Eric Andersen28a78ab2001-02-16 20:26:50 +0000785#ifdef BB_FEATURE_SH_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000786 if (dirbuf[0] == '~') /* ~/... or ~user/... */
787 username_tab_completion(dirbuf, 0);
788#endif
789 /* "strip" dirname in command */
790 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000791
Mark Whitley4e338752001-01-26 20:42:23 +0000792 paths[0] = dirbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000793 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000794 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000795
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000796 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000797
Mark Whitley4e338752001-01-26 20:42:23 +0000798 dir = opendir(paths[i]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000799 if (!dir) /* Don't print an error */
800 continue;
801
802 while ((next = readdir(dir)) != NULL) {
803 const char *str_merge = "%s/%s";
804 char *str_found = next->d_name;
805
Mark Whitley4e338752001-01-26 20:42:23 +0000806 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000807 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000808 continue;
809 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000810 if (*str_found == '.' && *pfind == 0) {
811 if (*paths[i] == '/' && paths[i][1] == 0
812 && str_found[1] == 0) str_found = ""; /* only "/" */
813 else
Mark Whitley4e338752001-01-26 20:42:23 +0000814 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000815 }
816 if (paths[i][strlen(paths[i]) - 1] == '/')
817 str_merge = "%s%s";
818 sprintf(found, str_merge, paths[i], str_found);
819 /* hmm, remover in progress? */
820 if (stat(found, &st) < 0)
821 continue;
822 /* find with dirs ? */
823 if (paths[i] != dirbuf)
824 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000825 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000826 /* name is directory */
827 /* algorithmic only "/" ? */
828 if (*str_found)
829 strcat(found, "/");
830 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000831 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000832 /* not put found file if search only dirs for cd */
833 if (type == FIND_DIR_ONLY)
834 continue;
835 str_found = add_quote_for_spec_chars(found);
836 if (type == FIND_FILE_ONLY ||
837 (type == FIND_EXE_ONLY && is_execute(&st) == TRUE))
838 strcat(str_found, " ");
839 }
Mark Whitley4e338752001-01-26 20:42:23 +0000840 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000841 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
842
843 matches[nm++] = str_found;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000844 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000845 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000846 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000847 if (paths != path1) {
848 free(paths[0]); /* allocated memory only in first member */
849 free(paths);
850 }
Mark Whitley4e338752001-01-26 20:42:23 +0000851 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000852 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000853}
Erik Andersenf0657d32000-04-12 17:49:52 +0000854
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000855static int match_compare(const void *a, const void *b)
856{
857 return strcmp(*(char **) a, *(char **) b);
858}
859
860
861
862#define QUOT (UCHAR_MAX+1)
863
864#define collapse_pos(is, in) { \
865 memcpy(int_buf+is, int_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); \
866 memcpy(pos_buf+is, pos_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); }
867
868static int find_match(char *matchBuf, int *len_with_quotes)
869{
870 int i, j;
871 int command_mode;
872 int c, c2;
873 int int_buf[BUFSIZ + 1];
874 int pos_buf[BUFSIZ + 1];
875
876 /* set to integer dimension characters and own positions */
877 for (i = 0;; i++) {
878 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
879 if (int_buf[i] == 0) {
880 pos_buf[i] = -1; /* indicator end line */
881 break;
882 } else
883 pos_buf[i] = i;
884 }
885
886 /* mask \+symbol and convert '\t' to ' ' */
887 for (i = j = 0; matchBuf[i]; i++, j++)
888 if (matchBuf[i] == '\\') {
889 collapse_pos(j, j + 1);
890 int_buf[j] |= QUOT;
891 i++;
892#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
893 if (matchBuf[i] == '\t') /* algorithm equivalent */
894 int_buf[j] = ' ' | QUOT;
895#endif
896 }
897#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
898 else if (matchBuf[i] == '\t')
899 int_buf[j] = ' ';
900#endif
901
902 /* mask "symbols" or 'symbols' */
903 c2 = 0;
904 for (i = 0; int_buf[i]; i++) {
905 c = int_buf[i];
906 if (c == '\'' || c == '"') {
907 if (c2 == 0)
908 c2 = c;
909 else {
910 if (c == c2)
911 c2 = 0;
912 else
913 int_buf[i] |= QUOT;
914 }
915 } else if (c2 != 0 && c != '$')
916 int_buf[i] |= QUOT;
917 }
918
919 /* skip commands with arguments if line have commands delimiters */
920 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
921 for (i = 0; int_buf[i]; i++) {
922 c = int_buf[i];
923 c2 = int_buf[i + 1];
924 j = i ? int_buf[i - 1] : -1;
925 command_mode = 0;
926 if (c == ';' || c == '&' || c == '|') {
927 command_mode = 1 + (c == c2);
928 if (c == '&') {
929 if (j == '>' || j == '<')
930 command_mode = 0;
931 } else if (c == '|' && j == '>')
932 command_mode = 0;
933 }
934 if (command_mode) {
935 collapse_pos(0, i + command_mode);
936 i = -1; /* hack incremet */
937 }
938 }
939 /* collapse `command...` */
940 for (i = 0; int_buf[i]; i++)
941 if (int_buf[i] == '`') {
942 for (j = i + 1; int_buf[j]; j++)
943 if (int_buf[j] == '`') {
944 collapse_pos(i, j + 1);
945 j = 0;
946 break;
947 }
948 if (j) {
949 /* not found close ` - command mode, collapse all previous */
950 collapse_pos(0, i + 1);
951 break;
952 } else
953 i--; /* hack incremet */
954 }
955
956 /* collapse (command...(command...)...) or {command...{command...}...} */
957 c = 0; /* "recursive" level */
958 c2 = 0;
959 for (i = 0; int_buf[i]; i++)
960 if (int_buf[i] == '(' || int_buf[i] == '{') {
961 if (int_buf[i] == '(')
962 c++;
963 else
964 c2++;
965 collapse_pos(0, i + 1);
966 i = -1; /* hack incremet */
967 }
968 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
969 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
970 if (int_buf[i] == ')')
971 c--;
972 else
973 c2--;
974 collapse_pos(0, i + 1);
975 i = -1; /* hack incremet */
976 }
977
978 /* skip first not quote space */
979 for (i = 0; int_buf[i]; i++)
980 if (int_buf[i] != ' ')
981 break;
982 if (i)
983 collapse_pos(0, i);
984
985 /* set find mode for completion */
986 command_mode = FIND_EXE_ONLY;
987 for (i = 0; int_buf[i]; i++)
988 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
989 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
990 && strncmp(&matchBuf[pos_buf[0]], "cd", 2) == 0)
991 command_mode = FIND_DIR_ONLY;
992 else {
993 command_mode = FIND_FILE_ONLY;
994 break;
995 }
996 }
997 /* "strlen" */
998 for (i = 0; int_buf[i]; i++);
999 /* find last word */
1000 for (--i; i >= 0; i--) {
1001 c = int_buf[i];
1002 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
1003 collapse_pos(0, i + 1);
1004 break;
1005 }
1006 }
1007 /* skip first not quoted '\'' or '"' */
1008 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
1009 /* collapse quote or unquote // or /~ */
1010 while ((int_buf[i] & ~QUOT) == '/' && (
1011 (int_buf[i + 1] & ~QUOT) == '/'
1012 || (int_buf[i + 1] & ~QUOT) ==
1013 '~')) i++;
1014 if (i)
1015 collapse_pos(0, i);
1016
1017 /* set only match and destroy quotes */
1018 j = 0;
1019 for (i = 0; pos_buf[i] >= 0; i++) {
1020 matchBuf[i] = matchBuf[pos_buf[i]];
1021 j = pos_buf[i] + 1;
1022 }
1023 matchBuf[i] = 0;
1024 /* old lenght matchBuf with quotes symbols */
1025 *len_with_quotes = j ? j - pos_buf[0] : 0;
1026
1027 return command_mode;
1028}
1029
1030
1031static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001032{
1033 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001034 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +00001035 static char **matches;
1036
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001037 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001038 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001039 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001040 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001041 free(matches);
1042 matches = (char **) NULL;
1043 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001044 return;
1045 }
1046 if (*lastWasTab == FALSE) {
1047
1048 char *tmp;
1049 int len_found;
1050 char matchBuf[BUFSIZ];
1051 int find_type;
1052 int recalc_pos;
1053
1054 *lastWasTab = TRUE; /* flop trigger */
1055
1056 /* Make a local copy of the string -- up
1057 * to the position of the cursor */
1058 tmp = strncpy(matchBuf, command_ps, cursor);
1059 tmp[cursor] = 0;
1060
1061 find_type = find_match(matchBuf, &recalc_pos);
1062
1063 /* Free up any memory already allocated */
1064 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001065
Eric Andersen28a78ab2001-02-16 20:26:50 +00001066#ifdef BB_FEATURE_SH_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001067 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001068 * then try completing this word as a username. */
1069
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001070 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001071 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001072#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001073 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001074 * in the current working directory that matches. */
1075 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001076 matches =
1077 exe_n_cwd_tab_completion(matchBuf, &num_matches,
1078 find_type);
Erik Andersenf0657d32000-04-12 17:49:52 +00001079
1080 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001081 if (!matches || num_matches > 1) {
1082 char *tmp1;
1083
Mark Whitley4e338752001-01-26 20:42:23 +00001084 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001085 if (!matches)
1086 return; /* not found */
1087 /* sort */
1088 qsort(matches, num_matches, sizeof(char *), match_compare);
1089
1090 /* find minimal match */
1091 tmp = xstrdup(matches[0]);
1092 for (tmp1 = tmp; *tmp1; tmp1++)
1093 for (len_found = 1; len_found < num_matches; len_found++)
1094 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1095 *tmp1 = 0;
1096 break;
1097 }
1098 if (*tmp == 0) { /* have unique */
1099 free(tmp);
1100 return;
1101 }
1102 } else { /* one match */
1103 tmp = matches[0];
1104 /* for next completion current found */
1105 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001106 }
1107
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001108 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001109 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001110 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001111
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001112 /* before word for match */
1113 command_ps[cursor - recalc_pos] = 0;
1114 /* save tail line */
1115 strcpy(matchBuf, command_ps + cursor);
1116 /* add match */
1117 strcat(command_ps, tmp);
1118 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001119 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001120 /* back to begin word for match */
1121 input_backward(recalc_pos);
1122 /* new pos */
1123 recalc_pos = cursor + len_found;
1124 /* new len */
1125 len = strlen(command_ps);
1126 /* write out the matched command */
1127 input_end();
1128 input_backward(cursor - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001129 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001130 if (tmp != matches[0])
1131 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001132 } else {
1133 /* Ok -- the last char was a TAB. Since they
1134 * just hit TAB again, print a list of all the
1135 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001136 if (matches && num_matches > 0) {
1137 int i, col, l;
1138 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001139
1140 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001141 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001142 for (i = 0, col = 0; i < num_matches; i++) {
1143 l = strlen(matches[i]);
1144 if (l < 14)
1145 l = 14;
1146 printf("%-14s ", matches[i]);
1147 if ((l += 2) > 16)
1148 while (l % 16) {
1149 putchar(' ');
1150 l++;
1151 }
1152 col += l;
1153 col -= (col / cmdedit_termw) * cmdedit_termw;
1154 if (col > 60 && matches[i + 1] != NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +00001155 putchar('\n');
Erik Andersenf0657d32000-04-12 17:49:52 +00001156 col = 0;
1157 }
1158 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001159 /* Go to the next line and rewrite */
1160 putchar('\n');
1161 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001162 }
1163 }
1164}
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001165#endif /* BB_FEATURE_SH_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001166
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001167static void get_previous_history(struct history **hp, struct history *p)
Erik Andersenf0657d32000-04-12 17:49:52 +00001168{
Eric Andersen91a44002000-07-19 17:37:57 +00001169 if ((*hp)->s)
1170 free((*hp)->s);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001171 (*hp)->s = xstrdup(command_ps);
1172 *hp = p;
Erik Andersenf0657d32000-04-12 17:49:52 +00001173}
1174
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001175static inline void get_next_history(struct history **hp)
Erik Andersenf0657d32000-04-12 17:49:52 +00001176{
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001177 get_previous_history(hp, (*hp)->n);
Erik Andersenf0657d32000-04-12 17:49:52 +00001178}
1179
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001180enum {
1181 ESC = 27,
1182 DEL = 127,
1183};
1184
1185
Erik Andersen6273f652000-03-17 01:12:41 +00001186/*
1187 * This function is used to grab a character buffer
1188 * from the input file descriptor and allows you to
1189 * a string with full command editing (sortof like
1190 * a mini readline).
1191 *
1192 * The following standard commands are not implemented:
1193 * ESC-b -- Move back one word
1194 * ESC-f -- Move forward one word
1195 * ESC-d -- Delete back one word
1196 * ESC-h -- Delete forward one word
1197 * CTL-t -- Transpose two characters
1198 *
1199 * Furthermore, the "vi" command editing keys are not implemented.
1200 *
Erik Andersen6273f652000-03-17 01:12:41 +00001201 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001202extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001203{
1204
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001205 int inputFd = fileno(stdin);
Mark Whitley4e338752001-01-26 20:42:23 +00001206
Erik Andersenc7c634b2000-03-19 05:28:55 +00001207 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001208 int lastWasTab = FALSE;
1209 char c = 0;
1210 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +00001211
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001212 /* prepare before init handlers */
1213 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001214 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001215 command_ps = command;
1216
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001217 if (new_settings.c_cc[VMIN] == 0) { /* first call */
1218
1219 getTermSettings(inputFd, (void *) &initial_settings);
Erik Andersen1d1d9502000-04-21 01:26:49 +00001220 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001221
Erik Andersen1d1d9502000-04-21 01:26:49 +00001222 new_settings.c_cc[VMIN] = 1;
1223 new_settings.c_cc[VTIME] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001224 new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001225 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001226 new_settings.c_lflag &= ~(ECHO | ECHOCTL | ECHONL); /* Turn off echoing */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001227 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001228
1229 command[0] = 0;
1230
1231 setTermSettings(inputFd, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001232 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001233
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001234 /* Print out the command prompt */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001235 parse_prompt(prompt);
Eric Andersen6faae7d2001-02-16 20:09:17 +00001236 /* Now initialize things */
1237 cmdedit_init();
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001238
Erik Andersenc7c634b2000-03-19 05:28:55 +00001239 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001240
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001241 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001242
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001243 if (read(inputFd, &c, 1) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +00001244 return;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001245
Erik Andersen13456d12000-03-16 08:09:57 +00001246 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001247 case '\n':
1248 case '\r':
1249 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001250 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001251 break_out = 1;
1252 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001253 case 1:
1254 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001255 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001256 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001257 case 2:
1258 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001259 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001260 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001261 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001262 /* Control-c -- stop gathering input */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001263
Eric Andersen86349772000-12-18 20:25:50 +00001264 /* Link into lash to reset context to 0 on ^C and such */
1265 shell_context = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001266
1267 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001268 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001269 command[0] = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001270
Eric Andersen86349772000-12-18 20:25:50 +00001271 return;
Erik Andersenf0657d32000-04-12 17:49:52 +00001272 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001273 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001274 * if the len=0 and no chars to delete */
1275 if (len == 0) {
Mark Whitley4e338752001-01-26 20:42:23 +00001276 printf("exit");
Erik Andersenf0657d32000-04-12 17:49:52 +00001277 clean_up_and_die(0);
1278 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001279 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001280 }
1281 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001282 case 5:
1283 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001284 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001285 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001286 case 6:
1287 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001288 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001289 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001290 case '\b':
1291 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001292 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001293 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001294 break;
1295 case '\t':
Erik Andersena2685732000-04-09 18:27:46 +00001296#ifdef BB_FEATURE_SH_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001297 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001298#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001299 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001300 case 14:
1301 /* Control-n -- Get next command in history */
1302 if (hp && hp->n && hp->n->s) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001303 get_next_history(&hp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001304 goto rewrite_line;
1305 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001306 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001307 }
1308 break;
1309 case 16:
1310 /* Control-p -- Get previous command from history */
1311 if (hp && hp->p) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001312 get_previous_history(&hp, hp->p);
Erik Andersenf0657d32000-04-12 17:49:52 +00001313 goto rewrite_line;
1314 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001315 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001316 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001317 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001318 case 21:
1319 /* Control-U -- Clear line before cursor */
1320 if (cursor) {
1321 strcpy(command, command + cursor);
1322 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001323 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001324 break;
1325
1326 case ESC:{
1327 /* escape sequence follows */
1328 if (read(inputFd, &c, 1) < 1)
1329 return;
1330 /* different vt100 emulations */
1331 if (c == '[' || c == 'O') {
1332 if (read(inputFd, &c, 1) < 1)
1333 return;
1334 }
1335 switch (c) {
1336#ifdef BB_FEATURE_SH_TAB_COMPLETION
1337 case '\t': /* Alt-Tab */
1338
1339 input_tab(&lastWasTab);
1340 break;
1341#endif
1342 case 'A':
1343 /* Up Arrow -- Get previous command from history */
1344 if (hp && hp->p) {
1345 get_previous_history(&hp, hp->p);
1346 goto rewrite_line;
1347 } else {
1348 beep();
1349 }
1350 break;
1351 case 'B':
1352 /* Down Arrow -- Get next command in history */
1353 if (hp && hp->n && hp->n->s) {
1354 get_next_history(&hp);
1355 goto rewrite_line;
1356 } else {
1357 beep();
1358 }
1359 break;
1360
1361 /* Rewrite the line with the selected history item */
1362 rewrite_line:
1363 /* change command */
1364 len = strlen(strcpy(command, hp->s));
1365 /* redraw and go to end line */
1366 redraw(cmdedit_y, 0);
1367 break;
1368 case 'C':
1369 /* Right Arrow -- Move forward one character */
1370 input_forward();
1371 break;
1372 case 'D':
1373 /* Left Arrow -- Move back one character */
1374 input_backward(1);
1375 break;
1376 case '3':
1377 /* Delete */
1378 input_delete();
1379 break;
1380 case '1':
1381 case 'H':
1382 /* Home (Ctrl-A) */
1383 input_backward(cursor);
1384 break;
1385 case '4':
1386 case 'F':
1387 /* End (Ctrl-E) */
1388 input_end();
1389 break;
1390 default:
1391 if (!(c >= '1' && c <= '9'))
1392 c = 0;
1393 beep();
1394 }
1395 if (c >= '1' && c <= '9')
1396 do
1397 if (read(inputFd, &c, 1) < 1)
1398 return;
1399 while (c != '~');
1400 break;
1401 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001402
1403 default: /* If it's regular input, do the normal thing */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001404#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1405 /* Control-V -- Add non-printable symbol */
1406 if (c == 22) {
1407 if (read(inputFd, &c, 1) < 1)
1408 return;
1409 if (c == 0) {
1410 beep();
1411 break;
1412 }
1413 } else
1414#endif
1415 if (!isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001416 break;
1417
1418 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
1419 break;
1420
1421 len++;
1422
1423 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001424 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001425 *(command + cursor + 1) = 0;
1426 cmdedit_set_out_char(0);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001427 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001428 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001429
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001430 memmove(command + sc + 1, command + sc, len - sc);
1431 *(command + sc) = c;
1432 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001433 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001434 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001435 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001436 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001437 }
1438
Erik Andersenc7c634b2000-03-19 05:28:55 +00001439 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001440 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001441 if (break_out) /* Enter is the command terminator, no more input. */
1442 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001443
1444 if (c != '\t')
1445 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001446 }
1447
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001448 setTermSettings(inputFd, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001449 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001450
1451 /* Handle command history log */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001452 if (len) { /* no put empty line */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001453
1454 struct history *h = his_end;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001455 char *ss;
Mark Whitley4e338752001-01-26 20:42:23 +00001456
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001457 ss = xstrdup(command); /* duplicate */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001458
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001459 if (h == 0) {
Eric Andersen91a44002000-07-19 17:37:57 +00001460 /* No previous history -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001461 h = his_front = xmalloc(sizeof(struct history));
1462 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001463
1464 h->p = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001465 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001466 h->n->p = h;
1467 h->n->n = NULL;
1468 h->n->s = NULL;
1469 his_end = h->n;
1470 history_counter++;
1471 } else {
Eric Andersen91a44002000-07-19 17:37:57 +00001472 /* Add a new history command -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001473 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001474
1475 h->n->p = h;
1476 h->n->n = NULL;
1477 h->n->s = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001478 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001479 his_end = h->n;
1480
1481 /* After max history, remove the oldest command */
1482 if (history_counter >= MAX_HISTORY) {
1483
1484 struct history *p = his_front->n;
1485
1486 p->p = NULL;
1487 free(his_front->s);
1488 free(his_front);
1489 his_front = p;
1490 } else {
1491 history_counter++;
1492 }
Erik Andersen13456d12000-03-16 08:09:57 +00001493 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001494#if defined(BB_FEATURE_BASH_STYLE_PROMT)
1495 num_ok_lines++;
1496#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001497 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001498 command[len++] = '\n'; /* set '\n' */
1499 command[len] = 0;
1500#if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_SH_TAB_COMPLETION)
1501 input_tab(0); /* strong free */
1502#endif
1503#if defined(BB_FEATURE_BASH_STYLE_PROMT)
1504 free(cmdedit_prompt);
1505#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001506 return;
Erik Andersen13456d12000-03-16 08:09:57 +00001507}
1508
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001509
Mark Whitley4e338752001-01-26 20:42:23 +00001510/* Undo the effects of cmdedit_init(). */
Eric Andersen501c88b2000-07-28 15:14:45 +00001511extern void cmdedit_terminate(void)
1512{
1513 cmdedit_reset_term();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001514 if ((handlers_sets & SET_TERM_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +00001515 signal(SIGKILL, SIG_DFL);
1516 signal(SIGINT, SIG_DFL);
1517 signal(SIGQUIT, SIG_DFL);
1518 signal(SIGTERM, SIG_DFL);
1519 signal(SIGWINCH, SIG_DFL);
1520 handlers_sets &= ~SET_TERM_HANDLERS;
1521 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001522}
1523
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001524#endif /* BB_FEATURE_SH_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001525
1526
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001527#ifdef TEST
1528
1529unsigned int shell_context;
1530
1531int main(int argc, char **argv)
1532{
1533 char buff[BUFSIZ];
1534 char *prompt =
1535#if defined(BB_FEATURE_BASH_STYLE_PROMT)
1536 "\\[\\033[32;1m\\]\\u@\\[\\033[33;1m\\]\\h:\
1537\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
1538\\!\\[\\033[36;1m\\]\\$ \\[\\033[0m\\]";
1539#else
1540 "% ";
1541#endif
1542
1543 shell_context = 1;
1544 do {
1545 cmdedit_read_input(prompt, buff);
1546 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1547 } while (shell_context);
1548 printf("*** cmdedit_read_input() detect ^C\n");
1549 return 0;
1550}
1551
1552#endif /* TEST */