blob: 272d22fc037595e41bb637e2f09b155d2e3151f1 [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 /~ */
Mark Whitley7e5291f2001-03-08 19:31:12 +00001010 while ((int_buf[i] & ~QUOT) == '/' &&
1011 ((int_buf[i + 1] & ~QUOT) == '/'
1012 || (int_buf[i + 1] & ~QUOT) == '~')) {
1013 i++;
1014 }
1015 if (i) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001016 collapse_pos(0, i);
Mark Whitley7e5291f2001-03-08 19:31:12 +00001017 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001018
1019 /* set only match and destroy quotes */
1020 j = 0;
1021 for (i = 0; pos_buf[i] >= 0; i++) {
1022 matchBuf[i] = matchBuf[pos_buf[i]];
1023 j = pos_buf[i] + 1;
1024 }
1025 matchBuf[i] = 0;
1026 /* old lenght matchBuf with quotes symbols */
1027 *len_with_quotes = j ? j - pos_buf[0] : 0;
1028
1029 return command_mode;
1030}
1031
1032
1033static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001034{
1035 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001036 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +00001037 static char **matches;
1038
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001039 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001040 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001041 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001042 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001043 free(matches);
1044 matches = (char **) NULL;
1045 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001046 return;
1047 }
1048 if (*lastWasTab == FALSE) {
1049
1050 char *tmp;
1051 int len_found;
1052 char matchBuf[BUFSIZ];
1053 int find_type;
1054 int recalc_pos;
1055
1056 *lastWasTab = TRUE; /* flop trigger */
1057
1058 /* Make a local copy of the string -- up
1059 * to the position of the cursor */
1060 tmp = strncpy(matchBuf, command_ps, cursor);
1061 tmp[cursor] = 0;
1062
1063 find_type = find_match(matchBuf, &recalc_pos);
1064
1065 /* Free up any memory already allocated */
1066 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001067
Eric Andersen28a78ab2001-02-16 20:26:50 +00001068#ifdef BB_FEATURE_SH_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001069 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001070 * then try completing this word as a username. */
1071
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001072 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001073 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001074#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001075 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001076 * in the current working directory that matches. */
1077 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001078 matches =
1079 exe_n_cwd_tab_completion(matchBuf, &num_matches,
1080 find_type);
Erik Andersenf0657d32000-04-12 17:49:52 +00001081
1082 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001083 if (!matches || num_matches > 1) {
1084 char *tmp1;
1085
Mark Whitley4e338752001-01-26 20:42:23 +00001086 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001087 if (!matches)
1088 return; /* not found */
1089 /* sort */
1090 qsort(matches, num_matches, sizeof(char *), match_compare);
1091
1092 /* find minimal match */
1093 tmp = xstrdup(matches[0]);
1094 for (tmp1 = tmp; *tmp1; tmp1++)
1095 for (len_found = 1; len_found < num_matches; len_found++)
1096 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1097 *tmp1 = 0;
1098 break;
1099 }
1100 if (*tmp == 0) { /* have unique */
1101 free(tmp);
1102 return;
1103 }
1104 } else { /* one match */
1105 tmp = matches[0];
1106 /* for next completion current found */
1107 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001108 }
1109
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001110 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001111 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001112 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001113
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001114 /* before word for match */
1115 command_ps[cursor - recalc_pos] = 0;
1116 /* save tail line */
1117 strcpy(matchBuf, command_ps + cursor);
1118 /* add match */
1119 strcat(command_ps, tmp);
1120 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001121 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001122 /* back to begin word for match */
1123 input_backward(recalc_pos);
1124 /* new pos */
1125 recalc_pos = cursor + len_found;
1126 /* new len */
1127 len = strlen(command_ps);
1128 /* write out the matched command */
1129 input_end();
1130 input_backward(cursor - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001131 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001132 if (tmp != matches[0])
1133 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001134 } else {
1135 /* Ok -- the last char was a TAB. Since they
1136 * just hit TAB again, print a list of all the
1137 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001138 if (matches && num_matches > 0) {
1139 int i, col, l;
1140 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001141
1142 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001143 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001144 for (i = 0, col = 0; i < num_matches; i++) {
1145 l = strlen(matches[i]);
1146 if (l < 14)
1147 l = 14;
1148 printf("%-14s ", matches[i]);
1149 if ((l += 2) > 16)
1150 while (l % 16) {
1151 putchar(' ');
1152 l++;
1153 }
1154 col += l;
1155 col -= (col / cmdedit_termw) * cmdedit_termw;
1156 if (col > 60 && matches[i + 1] != NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +00001157 putchar('\n');
Erik Andersenf0657d32000-04-12 17:49:52 +00001158 col = 0;
1159 }
1160 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001161 /* Go to the next line and rewrite */
1162 putchar('\n');
1163 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001164 }
1165 }
1166}
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001167#endif /* BB_FEATURE_SH_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001168
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001169static void get_previous_history(struct history **hp, struct history *p)
Erik Andersenf0657d32000-04-12 17:49:52 +00001170{
Eric Andersen91a44002000-07-19 17:37:57 +00001171 if ((*hp)->s)
1172 free((*hp)->s);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001173 (*hp)->s = xstrdup(command_ps);
1174 *hp = p;
Erik Andersenf0657d32000-04-12 17:49:52 +00001175}
1176
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001177static inline void get_next_history(struct history **hp)
Erik Andersenf0657d32000-04-12 17:49:52 +00001178{
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001179 get_previous_history(hp, (*hp)->n);
Erik Andersenf0657d32000-04-12 17:49:52 +00001180}
1181
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001182enum {
1183 ESC = 27,
1184 DEL = 127,
1185};
1186
1187
Erik Andersen6273f652000-03-17 01:12:41 +00001188/*
1189 * This function is used to grab a character buffer
1190 * from the input file descriptor and allows you to
1191 * a string with full command editing (sortof like
1192 * a mini readline).
1193 *
1194 * The following standard commands are not implemented:
1195 * ESC-b -- Move back one word
1196 * ESC-f -- Move forward one word
1197 * ESC-d -- Delete back one word
1198 * ESC-h -- Delete forward one word
1199 * CTL-t -- Transpose two characters
1200 *
1201 * Furthermore, the "vi" command editing keys are not implemented.
1202 *
Erik Andersen6273f652000-03-17 01:12:41 +00001203 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001204extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001205{
1206
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001207 int inputFd = fileno(stdin);
Mark Whitley4e338752001-01-26 20:42:23 +00001208
Erik Andersenc7c634b2000-03-19 05:28:55 +00001209 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001210 int lastWasTab = FALSE;
1211 char c = 0;
1212 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +00001213
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001214 /* prepare before init handlers */
1215 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001216 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001217 command_ps = command;
1218
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001219 if (new_settings.c_cc[VMIN] == 0) { /* first call */
1220
1221 getTermSettings(inputFd, (void *) &initial_settings);
Erik Andersen1d1d9502000-04-21 01:26:49 +00001222 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001223
Erik Andersen1d1d9502000-04-21 01:26:49 +00001224 new_settings.c_cc[VMIN] = 1;
1225 new_settings.c_cc[VTIME] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001226 new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001227 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001228 new_settings.c_lflag &= ~(ECHO | ECHOCTL | ECHONL); /* Turn off echoing */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001229 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001230
1231 command[0] = 0;
1232
1233 setTermSettings(inputFd, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001234 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001235
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001236 /* Print out the command prompt */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001237 parse_prompt(prompt);
Eric Andersen6faae7d2001-02-16 20:09:17 +00001238 /* Now initialize things */
1239 cmdedit_init();
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 */