blob: ec9939312a6ce57df86fd0cbe33496c53f5d15d3 [file] [log] [blame]
Erik Andersenc7c634b2000-03-19 05:28:55 +00001/* vi: set sw=4 ts=4: */
Erik Andersen13456d12000-03-16 08:09:57 +00002/*
Eric Andersen5f2c79d2001-02-16 18:36:04 +00003 * Termios command line History and Editting.
Erik Andersen13456d12000-03-16 08:09:57 +00004 *
Eric Andersen5f2c79d2001-02-16 18:36:04 +00005 * Copyright (c) 1986-2001 may safely be consumed by a BSD or GPL license.
6 * Written by: Vladimir Oleynik <vodz@usa.net>
7 *
8 * Used ideas:
9 * Adam Rogoyski <rogoyski@cs.utexas.edu>
10 * Dave Cinege <dcinege@psychosis.com>
11 * Jakub Jelinek (c) 1995
12 * Erik Andersen <andersee@debian.org> (Majorly adjusted for busybox)
13 *
Erik Andersen13456d12000-03-16 08:09:57 +000014 * This code is 'as is' with no warranty.
Erik Andersen13456d12000-03-16 08:09:57 +000015 *
Erik Andersen13456d12000-03-16 08:09:57 +000016 *
17 */
18
19/*
20 Usage and Known bugs:
21 Terminal key codes are not extensive, and more will probably
22 need to be added. This version was created on Debian GNU/Linux 2.x.
23 Delete, Backspace, Home, End, and the arrow keys were tested
24 to work in an Xterm and console. Ctrl-A also works as Home.
Mark Whitley4e338752001-01-26 20:42:23 +000025 Ctrl-E also works as End.
Erik Andersen13456d12000-03-16 08:09:57 +000026
Eric Andersen5f2c79d2001-02-16 18:36:04 +000027 Small bugs (simple effect):
28 - not true viewing if terminal size (x*y symbols) less
29 size (prompt + editor`s line + 2 symbols)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000030 - not true viewing if length prompt less terminal width
Erik Andersen13456d12000-03-16 08:09:57 +000031 */
32
Mark Whitley4e338752001-01-26 20:42:23 +000033
Eric Andersencbe31da2001-02-20 06:14:08 +000034#include <stdio.h>
35#include <errno.h>
36#include <unistd.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/ioctl.h>
40#include <ctype.h>
41#include <signal.h>
42#include <limits.h>
43
Eric Andersen3570a342000-09-25 21:45:58 +000044#include "busybox.h"
Mark Whitley4e338752001-01-26 20:42:23 +000045
Eric Andersene5dfced2001-04-09 22:48:12 +000046#ifdef BB_LOCALE_SUPPORT
47#define Isprint(c) isprint((c))
48#else
49#define Isprint(c) ( (c) >= ' ' && (c) != ((unsigned char)'\233') )
50#endif
51
52#ifndef TEST
53
Eric Andersen5f2c79d2001-02-16 18:36:04 +000054#define D(x)
55
56#else
57
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000058#define BB_FEATURE_COMMAND_EDITING
59#define BB_FEATURE_COMMAND_TAB_COMPLETION
60#define BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen94456f52001-02-18 20:26:48 +000061#define BB_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen94456f52001-02-18 20:26:48 +000062#define BB_FEATURE_CLEAN_UP
Eric Andersen5f2c79d2001-02-16 18:36:04 +000063
Eric Andersen5f2c79d2001-02-16 18:36:04 +000064#define D(x) x
65
66#endif /* TEST */
67
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000068#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5165fbe2001-02-20 06:42:29 +000069#include <dirent.h>
70#include <sys/stat.h>
71#endif
72
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000073#ifdef BB_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000074
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000075#ifndef BB_FEATURE_COMMAND_TAB_COMPLETION
76#undef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +000077#endif
78
Mark Whitleyf5949862001-03-14 00:29:14 +000079#if defined(BB_FEATURE_COMMAND_USERNAME_COMPLETION) || !defined(BB_FEATURE_SH_SIMPLE_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000080#define BB_FEATURE_GETUSERNAME_AND_HOMEDIR
81#endif
82
Eric Andersen5f2c79d2001-02-16 18:36:04 +000083#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
84#ifndef TEST
Eric Andersenaf4ac772001-02-01 22:43:49 +000085#include "pwd_grp/pwd.h"
Eric Andersen5f2c79d2001-02-16 18:36:04 +000086#else
87#include <pwd.h>
88#endif /* TEST */
89#endif /* advanced FEATURES */
Eric Andersenaf4ac772001-02-01 22:43:49 +000090
91
Eric Andersen5f2c79d2001-02-16 18:36:04 +000092
93struct history {
94 char *s;
95 struct history *p;
96 struct history *n;
Mark Whitley59ab0252001-01-23 22:30:04 +000097};
98
Eric Andersen5f2c79d2001-02-16 18:36:04 +000099/* Maximum length of the linked list for the command line history */
100static const int MAX_HISTORY = 15;
Erik Andersen13456d12000-03-16 08:09:57 +0000101
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000102/* First element in command line list */
103static struct history *his_front = NULL;
104
105/* Last element in command line list */
106static struct history *his_end = NULL;
107
Erik Andersen1d1d9502000-04-21 01:26:49 +0000108
109/* ED: sparc termios is broken: revert back to old termio handling. */
Erik Andersen1d1d9502000-04-21 01:26:49 +0000110
111#if #cpu(sparc)
112# include <termio.h>
113# define termios termio
114# define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp)
115# define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp)
116#else
117# include <termios.h>
118# define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
119# define getTermSettings(fd,argp) tcgetattr(fd, argp);
120#endif
121
122/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000123static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000124
125
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000126#ifndef _POSIX_VDISABLE
127#define _POSIX_VDISABLE '\0'
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000128#endif
129
Erik Andersen1d1d9502000-04-21 01:26:49 +0000130
Mark Whitley4e338752001-01-26 20:42:23 +0000131static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000132volatile int cmdedit_termw = 80; /* actual terminal width */
133static int history_counter = 0; /* Number of commands in history list */
Mark Whitley4e338752001-01-26 20:42:23 +0000134static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000135volatile int handlers_sets = 0; /* Set next bites: */
136
Mark Whitley4e338752001-01-26 20:42:23 +0000137enum {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000138 SET_ATEXIT = 1, /* when atexit() has been called
139 and get euid,uid,gid to fast compare */
140 SET_TERM_HANDLERS = 2, /* set many terminates signal handlers */
141 SET_WCHG_HANDLERS = 4, /* winchg signal handler */
142 SET_RESET_TERM = 8, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000143};
Erik Andersen13456d12000-03-16 08:09:57 +0000144
Mark Whitley4e338752001-01-26 20:42:23 +0000145
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000146static int cmdedit_x; /* real x terminal position */
147static int cmdedit_y; /* pseudoreal y terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000148static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000149
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000150static int cursor; /* required global for signal handler */
151static int len; /* --- "" - - "" - -"- --""-- --""--- */
152static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000153static
Mark Whitleyf5949862001-03-14 00:29:14 +0000154#ifdef BB_FEATURE_SH_SIMPLE_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000155 const
156#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000157char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000158
159/* Link into lash to reset context to 0 on ^C and such */
Eric Andersen86349772000-12-18 20:25:50 +0000160extern unsigned int shell_context;
161
Erik Andersen13456d12000-03-16 08:09:57 +0000162
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000163#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
164static char *user_buf = "";
165static char *home_pwd_buf = "";
166static int my_euid;
167#endif
168
Mark Whitleyf5949862001-03-14 00:29:14 +0000169#ifndef BB_FEATURE_SH_SIMPLE_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000170static char *hostname_buf = "";
171static int num_ok_lines = 1;
172#endif
173
174
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000175#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000176
177#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
178static int my_euid;
179#endif
180
181static int my_uid;
182static int my_gid;
183
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000184#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000185
Erik Andersen13456d12000-03-16 08:09:57 +0000186
Mark Whitley4e338752001-01-26 20:42:23 +0000187static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000188
Mark Whitley4e338752001-01-26 20:42:23 +0000189static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000190{
191 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000192 static __sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000193
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000194 /* emulate || signal call */
195 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Mark Whitley4e338752001-01-26 20:42:23 +0000196 ioctl(0, TIOCGWINSZ, &win);
197 if (win.ws_col > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000198 cmdedit_setwidth(win.ws_col, nsig == SIGWINCH);
199 }
Mark Whitley4e338752001-01-26 20:42:23 +0000200 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000201 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000202
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000203 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000204 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000205 else if (nsig == SIGWINCH) /* signaled called handler */
206 signal(SIGWINCH, win_changed); /* set for next call */
207 else /* nsig == 0 */
208 /* set previous handler */
209 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000210}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000211
212static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000213{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000214 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000215/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000216 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000217 handlers_sets &= ~SET_RESET_TERM;
218 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000219 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000220 /* reset SIGWINCH handler to previous (default) */
221 win_changed(0);
222 handlers_sets &= ~SET_WCHG_HANDLERS;
223 }
224 fflush(stdout);
Eric Andersenb040d4f2000-07-25 18:01:20 +0000225#ifdef BB_FEATURE_CLEAN_UP
226 if (his_front) {
227 struct history *n;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000228
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000229 while (his_front != his_end) {
Eric Andersenb040d4f2000-07-25 18:01:20 +0000230 n = his_front->n;
231 free(his_front->s);
232 free(his_front);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000233 his_front = n;
Eric Andersenb040d4f2000-07-25 18:01:20 +0000234 }
235 }
236#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000237}
238
Mark Whitley4e338752001-01-26 20:42:23 +0000239
Mark Whitley4e338752001-01-26 20:42:23 +0000240/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000241static void cmdedit_set_out_char(int next_char)
242{
243
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000244 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000245
246 if (c == 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000247 c = ' '; /* destroy end char? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000248#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersene5dfced2001-04-09 22:48:12 +0000249 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000250 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000251 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000252 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000253 c += '@';
254 if (c == 127)
255 c = '?';
256 printf("\033[7m%c\033[0m", c);
257 } else
258#endif
259 putchar(c);
260 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000261 /* terminal is scrolled down */
262 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000263 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000264
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000265 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000266 next_char = ' ';
267 /* destroy "(auto)margin" */
268 putchar(next_char);
269 putchar('\b');
270 }
271 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000272}
273
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000274/* Move to end line. Bonus: rewrite line from cursor */
275static void input_end(void)
276{
277 while (cursor < len)
278 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000279}
280
281/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000282static void goto_new_line(void)
283{
Mark Whitley4e338752001-01-26 20:42:23 +0000284 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000285 if (cmdedit_x)
286 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000287}
288
289
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000290static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000291{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000292 fputs(s, stdout);
293}
294static inline void beep(void)
295{
296 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000297}
298
Mark Whitley4e338752001-01-26 20:42:23 +0000299/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000300/* special for slow terminal */
301static void input_backward(int num)
302{
303 if (num > cursor)
304 num = cursor;
Eric Andersene5dfced2001-04-09 22:48:12 +0000305 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000306
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000307 if (cmdedit_x >= num) { /* no to up line */
308 cmdedit_x -= num;
309 if (num < 4)
310 while (num-- > 0)
311 putchar('\b');
312
313 else
314 printf("\033[%dD", num);
315 } else {
316 int count_y;
317
318 if (cmdedit_x) {
319 putchar('\r'); /* back to first terminal pos. */
320 num -= cmdedit_x; /* set previous backward */
321 }
322 count_y = 1 + num / cmdedit_termw;
323 printf("\033[%dA", count_y);
324 cmdedit_y -= count_y;
325 /* require forward after uping */
326 cmdedit_x = cmdedit_termw * count_y - num;
327 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000328 }
Erik Andersen13456d12000-03-16 08:09:57 +0000329}
330
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000331static void put_prompt(void)
332{
333 out1str(cmdedit_prompt);
334 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
335 cursor = 0;
336}
337
Mark Whitleyf5949862001-03-14 00:29:14 +0000338#ifdef BB_FEATURE_SH_SIMPLE_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000339static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000340{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000341 cmdedit_prompt = prmt_ptr;
342 cmdedit_prmt_len = strlen(prmt_ptr);
343 put_prompt();
344}
345#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000346static void parse_prompt(const char *prmt_ptr)
347{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000348 int prmt_len = 0;
349 int sub_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000350 char flg_not_length = '[';
351 char *prmt_mem_ptr = xcalloc(1, 1);
352 char *pwd_buf = xgetcwd(0);
353 char buf2[PATH_MAX + 1];
354 char buf[2];
355 char c;
356 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000357
Eric Andersen5f265b72001-05-11 16:58:46 +0000358 if (!pwd_buf) {
359 pwd_buf=unknown;
360 }
361
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000362 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000363 pbuf = buf;
364 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000365 c = *prmt_ptr++;
366 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000367 const char *cp = prmt_ptr;
368 int l;
369
370 c = process_escape_sequence(&prmt_ptr);
371 if(prmt_ptr==cp) {
372 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000373 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000374 c = *prmt_ptr++;
375 switch (c) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000376#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000377 case 'u':
378 pbuf = user_buf;
379 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000380#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000381 case 'h':
382 pbuf = hostname_buf;
383 if (*pbuf == 0) {
384 pbuf = xcalloc(256, 1);
385 if (gethostname(pbuf, 255) < 0) {
386 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000387 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000388 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000389
390 if (s)
391 *s = 0;
392 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000393 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000394 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000395 break;
396 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000397 c = my_euid == 0 ? '#' : '$';
398 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000399#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000400 case 'w':
401 pbuf = pwd_buf;
402 l = strlen(home_pwd_buf);
403 if (home_pwd_buf[0] != 0 &&
404 strncmp(home_pwd_buf, pbuf, l) == 0 &&
405 (pbuf[l]=='/' || pbuf[l]=='\0') &&
406 strlen(pwd_buf+l)<PATH_MAX) {
407 pbuf = buf2;
408 *pbuf = '~';
409 strcpy(pbuf+1, pwd_buf+l);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000410 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000411 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000412#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000413 case 'W':
414 pbuf = pwd_buf;
415 cp = strrchr(pbuf,'/');
416 if ( (cp != NULL) && (cp != pbuf) )
417 pbuf += (cp-pbuf)+1;
418 break;
419 case '!':
420 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
421 break;
422 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000423 c = '\033';
424 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000425 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000426 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000427 int h;
428 buf2[l++] = *prmt_ptr;
429 buf2[l] = 0;
430 h = strtol(buf2, &pbuf, 16);
431 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000432 l--;
433 break;
434 }
435 prmt_ptr++;
436 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000437 buf2[l] = 0;
438 c = (char)strtol(buf2, 0, 16);
439 if(c==0)
440 c = '?';
441 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000442 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000443 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000444 if (c == flg_not_length) {
445 flg_not_length = flg_not_length == '[' ? ']' : '[';
446 continue;
447 }
448 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000449 }
450 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000451 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000452 if(pbuf == buf)
453 *pbuf = c;
454 prmt_len += strlen(pbuf);
455 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000456 if (flg_not_length == ']')
457 sub_len++;
458 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000459 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000460 cmdedit_prompt = prmt_mem_ptr;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000461 cmdedit_prmt_len = prmt_len - sub_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000462 put_prompt();
463}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000464#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000465
466
467/* draw promt, editor line, and clear tail */
468static void redraw(int y, int back_cursor)
469{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000470 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000471 printf("\033[%dA", y);
472 cmdedit_y = 0; /* new quasireal y */
473 putchar('\r');
474 put_prompt();
475 input_end(); /* rewrite */
476 printf("\033[J"); /* destroy tail after cursor */
477 input_backward(back_cursor);
478}
479
Erik Andersenf0657d32000-04-12 17:49:52 +0000480/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000481static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000482{
Mark Whitley4e338752001-01-26 20:42:23 +0000483 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000484
Mark Whitley4e338752001-01-26 20:42:23 +0000485 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000486 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000487
488 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000489 len--;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000490 input_end(); /* rewtite new line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000491 cmdedit_set_out_char(0); /* destroy end char */
492 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000493}
494
Mark Whitley4e338752001-01-26 20:42:23 +0000495/* Delete the char in back of the cursor */
496static void input_backspace(void)
497{
498 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000499 input_backward(1);
500 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000501 }
502}
503
504
Erik Andersenf0657d32000-04-12 17:49:52 +0000505/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000506static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000507{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000508 if (cursor < len)
509 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000510}
511
512
Mark Whitley4e338752001-01-26 20:42:23 +0000513static void clean_up_and_die(int sig)
514{
515 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000516 if (sig != SIGINT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000517 exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */
Mark Whitley4e338752001-01-26 20:42:23 +0000518 cmdedit_reset_term();
519}
520
521static void cmdedit_setwidth(int w, int redraw_flg)
522{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000523 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000524 if (w <= cmdedit_termw) {
525 cmdedit_termw = cmdedit_termw % w;
526 }
Mark Whitley4e338752001-01-26 20:42:23 +0000527 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000528 cmdedit_termw = w;
529
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000530 if (redraw_flg) {
531 /* new y for current cursor */
532 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000533
534 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000535 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
536 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000537 }
Eric Andersen6faae7d2001-02-16 20:09:17 +0000538 }
Mark Whitley4e338752001-01-26 20:42:23 +0000539}
540
541extern void cmdedit_init(void)
542{
Eric Andersen61173a52001-03-19 17:48:55 +0000543 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000544 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
545 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000546 win_changed(-SIGWINCH);
547 handlers_sets |= SET_WCHG_HANDLERS;
548 }
549
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000550 if ((handlers_sets & SET_ATEXIT) == 0) {
551#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
552 struct passwd *entry;
553
554 my_euid = geteuid();
555 entry = getpwuid(my_euid);
556 if (entry) {
557 user_buf = xstrdup(entry->pw_name);
558 home_pwd_buf = xstrdup(entry->pw_dir);
559 }
560#endif
561
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000562#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000563
564#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
565 my_euid = geteuid();
566#endif
567 my_uid = getuid();
568 my_gid = getgid();
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000569#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000570 handlers_sets |= SET_ATEXIT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000571 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000572 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000573
574 if ((handlers_sets & SET_TERM_HANDLERS) == 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000575 signal(SIGKILL, clean_up_and_die);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000576 signal(SIGINT, clean_up_and_die);
Mark Whitley4e338752001-01-26 20:42:23 +0000577 signal(SIGQUIT, clean_up_and_die);
578 signal(SIGTERM, clean_up_and_die);
579 handlers_sets |= SET_TERM_HANDLERS;
580 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000581
Mark Whitley4e338752001-01-26 20:42:23 +0000582}
Erik Andersenf0657d32000-04-12 17:49:52 +0000583
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000584#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000585
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000586static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000587{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000588 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
589 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
590 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
591 (st->st_mode & S_IXOTH)) return TRUE;
592 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000593}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000594
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000595#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000596
597static char **username_tab_completion(char *ud, int *num_matches)
598{
599 struct passwd *entry;
600 int userlen;
601 char *temp;
602
603
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000604 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000605 userlen = strlen(ud);
606
607 if (num_matches == 0) { /* "~/..." or "~user/..." */
608 char *sav_ud = ud - 1;
609 char *home = 0;
610
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000611 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000612 home = home_pwd_buf;
613 } else {
614 /* "~user/..." */
615 temp = strchr(ud, '/');
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000616 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000617 entry = getpwnam(ud);
618 *temp = '/'; /* restore ~user/... */
619 ud = temp;
620 if (entry)
621 home = entry->pw_dir;
622 }
623 if (home) {
624 if ((userlen + strlen(home) + 1) < BUFSIZ) {
625 char temp2[BUFSIZ]; /* argument size */
626
627 /* /home/user/... */
628 sprintf(temp2, "%s%s", home, ud);
629 strcpy(sav_ud, temp2);
630 }
631 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000632 return 0; /* void, result save to argument :-) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000633 } else {
634 /* "~[^/]*" */
635 char **matches = (char **) NULL;
636 int nm = 0;
637
638 setpwent();
639
640 while ((entry = getpwent()) != NULL) {
641 /* Null usernames should result in all users as possible completions. */
642 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
643
644 temp = xmalloc(3 + strlen(entry->pw_name));
645 sprintf(temp, "~%s/", entry->pw_name);
646 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
647
648 matches[nm++] = temp;
649 }
650 }
651
652 endpwent();
653 (*num_matches) = nm;
654 return (matches);
655 }
656}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000657#endif /* BB_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000658
659enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000660 FIND_EXE_ONLY = 0,
661 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000662 FIND_FILE_ONLY = 2,
663};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000664
Mark Whitley4e338752001-01-26 20:42:23 +0000665static int path_parse(char ***p, int flags)
666{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000667 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000668 char *tmp;
669 char *pth;
670
Mark Whitley4e338752001-01-26 20:42:23 +0000671 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000672 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
673 /* PATH=<empty> or PATH=:<empty> */
674 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000675 return 1;
676 }
677
678 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000679 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000680
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000681 for (;;) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000682 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000683 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000684 if (tmp) {
685 if (*++tmp == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000686 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000687 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000688 break;
689 }
690
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000691 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000692
693 tmp = pth;
694 (*p)[0] = xstrdup(tmp);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000695 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000696
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000697 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000698 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000699 if (tmp) {
700 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
701 if (*++tmp == 0)
702 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000703 } else
704 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000705 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000706 }
707
708 return npth;
709}
710
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000711static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000712{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000713 int l = 0;
714 char *s = xmalloc((strlen(found) + 1) * 2);
715
716 while (*found) {
717 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
718 s[l++] = '\\';
719 s[l++] = *found++;
720 }
721 s[l] = 0;
722 return s;
723}
724
725static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000726 int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000727{
728
729 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000730 DIR *dir;
731 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000732 char dirbuf[BUFSIZ];
733 int nm = *num_matches;
734 struct stat st;
735 char *path1[1];
736 char **paths = path1;
737 int npaths;
738 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000739 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000740 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000741
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000742 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000743
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000744 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000745 /* no dir, if flags==EXE_ONLY - get paths, else "." */
746 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000747 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000748 } else {
749 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000750 /* save for change */
751 strcpy(dirbuf, command);
752 /* set dir only */
753 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000754#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000755 if (dirbuf[0] == '~') /* ~/... or ~user/... */
756 username_tab_completion(dirbuf, 0);
757#endif
758 /* "strip" dirname in command */
759 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000760
Mark Whitley4e338752001-01-26 20:42:23 +0000761 paths[0] = dirbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000762 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000763 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000764
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000765 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000766
Mark Whitley4e338752001-01-26 20:42:23 +0000767 dir = opendir(paths[i]);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000768 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000769 continue;
770
771 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000772 char *str_found = next->d_name;
773
Mark Whitley4e338752001-01-26 20:42:23 +0000774 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000775 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000776 continue;
777 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000778 if (*str_found == '.' && *pfind == 0) {
779 if (*paths[i] == '/' && paths[i][1] == 0
780 && str_found[1] == 0) str_found = ""; /* only "/" */
781 else
Mark Whitley4e338752001-01-26 20:42:23 +0000782 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000783 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000784 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000785 /* hmm, remover in progress? */
Eric Andersene5dfced2001-04-09 22:48:12 +0000786 if (stat(found, &st) < 0)
787 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000788 /* find with dirs ? */
789 if (paths[i] != dirbuf)
790 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000791 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000792 /* name is directory */
Eric Andersene5dfced2001-04-09 22:48:12 +0000793 str_found = found;
794 found = concat_path_file(found, "");
795 free(str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000796 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000797 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000798 /* not put found file if search only dirs for cd */
Eric Andersene5dfced2001-04-09 22:48:12 +0000799 if (type == FIND_DIR_ONLY)
800 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000801 str_found = add_quote_for_spec_chars(found);
802 if (type == FIND_FILE_ONLY ||
803 (type == FIND_EXE_ONLY && is_execute(&st) == TRUE))
804 strcat(str_found, " ");
805 }
Mark Whitley4e338752001-01-26 20:42:23 +0000806 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000807 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
808
809 matches[nm++] = str_found;
Eric Andersene5dfced2001-04-09 22:48:12 +0000810cont:
811 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000812 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000813 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000814 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000815 if (paths != path1) {
816 free(paths[0]); /* allocated memory only in first member */
817 free(paths);
818 }
Mark Whitley4e338752001-01-26 20:42:23 +0000819 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000820 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000821}
Erik Andersenf0657d32000-04-12 17:49:52 +0000822
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000823static int match_compare(const void *a, const void *b)
824{
825 return strcmp(*(char **) a, *(char **) b);
826}
827
828
829
830#define QUOT (UCHAR_MAX+1)
831
832#define collapse_pos(is, in) { \
833 memcpy(int_buf+is, int_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); \
834 memcpy(pos_buf+is, pos_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); }
835
836static int find_match(char *matchBuf, int *len_with_quotes)
837{
838 int i, j;
839 int command_mode;
840 int c, c2;
841 int int_buf[BUFSIZ + 1];
842 int pos_buf[BUFSIZ + 1];
843
844 /* set to integer dimension characters and own positions */
845 for (i = 0;; i++) {
846 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
847 if (int_buf[i] == 0) {
848 pos_buf[i] = -1; /* indicator end line */
849 break;
850 } else
851 pos_buf[i] = i;
852 }
853
854 /* mask \+symbol and convert '\t' to ' ' */
855 for (i = j = 0; matchBuf[i]; i++, j++)
856 if (matchBuf[i] == '\\') {
857 collapse_pos(j, j + 1);
858 int_buf[j] |= QUOT;
859 i++;
860#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
861 if (matchBuf[i] == '\t') /* algorithm equivalent */
862 int_buf[j] = ' ' | QUOT;
863#endif
864 }
865#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
866 else if (matchBuf[i] == '\t')
867 int_buf[j] = ' ';
868#endif
869
870 /* mask "symbols" or 'symbols' */
871 c2 = 0;
872 for (i = 0; int_buf[i]; i++) {
873 c = int_buf[i];
874 if (c == '\'' || c == '"') {
875 if (c2 == 0)
876 c2 = c;
877 else {
878 if (c == c2)
879 c2 = 0;
880 else
881 int_buf[i] |= QUOT;
882 }
883 } else if (c2 != 0 && c != '$')
884 int_buf[i] |= QUOT;
885 }
886
887 /* skip commands with arguments if line have commands delimiters */
888 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
889 for (i = 0; int_buf[i]; i++) {
890 c = int_buf[i];
891 c2 = int_buf[i + 1];
892 j = i ? int_buf[i - 1] : -1;
893 command_mode = 0;
894 if (c == ';' || c == '&' || c == '|') {
895 command_mode = 1 + (c == c2);
896 if (c == '&') {
897 if (j == '>' || j == '<')
898 command_mode = 0;
899 } else if (c == '|' && j == '>')
900 command_mode = 0;
901 }
902 if (command_mode) {
903 collapse_pos(0, i + command_mode);
904 i = -1; /* hack incremet */
905 }
906 }
907 /* collapse `command...` */
908 for (i = 0; int_buf[i]; i++)
909 if (int_buf[i] == '`') {
910 for (j = i + 1; int_buf[j]; j++)
911 if (int_buf[j] == '`') {
912 collapse_pos(i, j + 1);
913 j = 0;
914 break;
915 }
916 if (j) {
917 /* not found close ` - command mode, collapse all previous */
918 collapse_pos(0, i + 1);
919 break;
920 } else
921 i--; /* hack incremet */
922 }
923
924 /* collapse (command...(command...)...) or {command...{command...}...} */
925 c = 0; /* "recursive" level */
926 c2 = 0;
927 for (i = 0; int_buf[i]; i++)
928 if (int_buf[i] == '(' || int_buf[i] == '{') {
929 if (int_buf[i] == '(')
930 c++;
931 else
932 c2++;
933 collapse_pos(0, i + 1);
934 i = -1; /* hack incremet */
935 }
936 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
937 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
938 if (int_buf[i] == ')')
939 c--;
940 else
941 c2--;
942 collapse_pos(0, i + 1);
943 i = -1; /* hack incremet */
944 }
945
946 /* skip first not quote space */
947 for (i = 0; int_buf[i]; i++)
948 if (int_buf[i] != ' ')
949 break;
950 if (i)
951 collapse_pos(0, i);
952
953 /* set find mode for completion */
954 command_mode = FIND_EXE_ONLY;
955 for (i = 0; int_buf[i]; i++)
956 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
957 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000958 && matchBuf[pos_buf[0]]=='c'
959 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000960 command_mode = FIND_DIR_ONLY;
961 else {
962 command_mode = FIND_FILE_ONLY;
963 break;
964 }
965 }
966 /* "strlen" */
967 for (i = 0; int_buf[i]; i++);
968 /* find last word */
969 for (--i; i >= 0; i--) {
970 c = int_buf[i];
971 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
972 collapse_pos(0, i + 1);
973 break;
974 }
975 }
976 /* skip first not quoted '\'' or '"' */
977 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
978 /* collapse quote or unquote // or /~ */
Mark Whitley7e5291f2001-03-08 19:31:12 +0000979 while ((int_buf[i] & ~QUOT) == '/' &&
980 ((int_buf[i + 1] & ~QUOT) == '/'
981 || (int_buf[i + 1] & ~QUOT) == '~')) {
982 i++;
983 }
984 if (i) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000985 collapse_pos(0, i);
Mark Whitley7e5291f2001-03-08 19:31:12 +0000986 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000987
988 /* set only match and destroy quotes */
989 j = 0;
990 for (i = 0; pos_buf[i] >= 0; i++) {
991 matchBuf[i] = matchBuf[pos_buf[i]];
992 j = pos_buf[i] + 1;
993 }
994 matchBuf[i] = 0;
995 /* old lenght matchBuf with quotes symbols */
996 *len_with_quotes = j ? j - pos_buf[0] : 0;
997
998 return command_mode;
999}
1000
1001
1002static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +00001003{
1004 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001005 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +00001006 static char **matches;
1007
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001008 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001009 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001010 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001011 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001012 free(matches);
1013 matches = (char **) NULL;
1014 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001015 return;
1016 }
1017 if (*lastWasTab == FALSE) {
1018
1019 char *tmp;
1020 int len_found;
1021 char matchBuf[BUFSIZ];
1022 int find_type;
1023 int recalc_pos;
1024
1025 *lastWasTab = TRUE; /* flop trigger */
1026
1027 /* Make a local copy of the string -- up
1028 * to the position of the cursor */
1029 tmp = strncpy(matchBuf, command_ps, cursor);
1030 tmp[cursor] = 0;
1031
1032 find_type = find_match(matchBuf, &recalc_pos);
1033
1034 /* Free up any memory already allocated */
1035 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001036
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001037#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001038 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001039 * then try completing this word as a username. */
1040
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001041 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001042 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001043#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001044 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001045 * in the current working directory that matches. */
1046 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001047 matches =
1048 exe_n_cwd_tab_completion(matchBuf, &num_matches,
1049 find_type);
Erik Andersenf0657d32000-04-12 17:49:52 +00001050
1051 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001052 if (!matches || num_matches > 1) {
1053 char *tmp1;
1054
Mark Whitley4e338752001-01-26 20:42:23 +00001055 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001056 if (!matches)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001057 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001058 /* sort */
1059 qsort(matches, num_matches, sizeof(char *), match_compare);
1060
1061 /* find minimal match */
1062 tmp = xstrdup(matches[0]);
1063 for (tmp1 = tmp; *tmp1; tmp1++)
1064 for (len_found = 1; len_found < num_matches; len_found++)
1065 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1066 *tmp1 = 0;
1067 break;
1068 }
1069 if (*tmp == 0) { /* have unique */
1070 free(tmp);
1071 return;
1072 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001073 } else { /* one match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001074 tmp = matches[0];
1075 /* for next completion current found */
1076 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001077 }
1078
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001079 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001080 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001081 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001082
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001083 /* before word for match */
1084 command_ps[cursor - recalc_pos] = 0;
1085 /* save tail line */
1086 strcpy(matchBuf, command_ps + cursor);
1087 /* add match */
1088 strcat(command_ps, tmp);
1089 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001090 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001091 /* back to begin word for match */
1092 input_backward(recalc_pos);
1093 /* new pos */
1094 recalc_pos = cursor + len_found;
1095 /* new len */
1096 len = strlen(command_ps);
1097 /* write out the matched command */
1098 input_end();
1099 input_backward(cursor - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001100 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001101 if (tmp != matches[0])
1102 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001103 } else {
1104 /* Ok -- the last char was a TAB. Since they
1105 * just hit TAB again, print a list of all the
1106 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001107 if (matches && num_matches > 0) {
1108 int i, col, l;
1109 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001110
1111 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001112 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001113 for (i = 0, col = 0; i < num_matches; i++) {
1114 l = strlen(matches[i]);
1115 if (l < 14)
1116 l = 14;
1117 printf("%-14s ", matches[i]);
1118 if ((l += 2) > 16)
1119 while (l % 16) {
1120 putchar(' ');
1121 l++;
1122 }
1123 col += l;
1124 col -= (col / cmdedit_termw) * cmdedit_termw;
1125 if (col > 60 && matches[i + 1] != NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +00001126 putchar('\n');
Erik Andersenf0657d32000-04-12 17:49:52 +00001127 col = 0;
1128 }
1129 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001130 /* Go to the next line and rewrite */
1131 putchar('\n');
1132 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001133 }
1134 }
1135}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001136#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001137
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001138static void get_previous_history(struct history **hp, struct history *p)
Erik Andersenf0657d32000-04-12 17:49:52 +00001139{
Eric Andersen91a44002000-07-19 17:37:57 +00001140 if ((*hp)->s)
1141 free((*hp)->s);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001142 (*hp)->s = xstrdup(command_ps);
1143 *hp = p;
Erik Andersenf0657d32000-04-12 17:49:52 +00001144}
1145
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001146static inline void get_next_history(struct history **hp)
Erik Andersenf0657d32000-04-12 17:49:52 +00001147{
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001148 get_previous_history(hp, (*hp)->n);
Erik Andersenf0657d32000-04-12 17:49:52 +00001149}
1150
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001151enum {
1152 ESC = 27,
1153 DEL = 127,
1154};
1155
1156
Erik Andersen6273f652000-03-17 01:12:41 +00001157/*
1158 * This function is used to grab a character buffer
1159 * from the input file descriptor and allows you to
1160 * a string with full command editing (sortof like
1161 * a mini readline).
1162 *
1163 * The following standard commands are not implemented:
1164 * ESC-b -- Move back one word
1165 * ESC-f -- Move forward one word
1166 * ESC-d -- Delete back one word
1167 * ESC-h -- Delete forward one word
1168 * CTL-t -- Transpose two characters
1169 *
1170 * Furthermore, the "vi" command editing keys are not implemented.
1171 *
Erik Andersen6273f652000-03-17 01:12:41 +00001172 */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001173
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001174extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001175{
1176
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001177 int inputFd = fileno(stdin);
Mark Whitley4e338752001-01-26 20:42:23 +00001178
Erik Andersenc7c634b2000-03-19 05:28:55 +00001179 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001180 int lastWasTab = FALSE;
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001181 unsigned char c = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001182 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +00001183
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001184 /* prepare before init handlers */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001185 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001186 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001187 command_ps = command;
1188
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001189 if (new_settings.c_cc[VMIN] == 0) { /* first call */
1190
1191 getTermSettings(inputFd, (void *) &initial_settings);
Erik Andersen1d1d9502000-04-21 01:26:49 +00001192 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001193
Erik Andersen1d1d9502000-04-21 01:26:49 +00001194 new_settings.c_cc[VMIN] = 1;
1195 new_settings.c_cc[VTIME] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001196 /* Turn off CTRL-C, so we can trap it */
1197 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001198 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001199 /* Turn off echoing */
1200 new_settings.c_lflag &= ~(ECHO | ECHOCTL | ECHONL);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001201 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001202
1203 command[0] = 0;
1204
1205 setTermSettings(inputFd, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001206 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001207
Eric Andersen6faae7d2001-02-16 20:09:17 +00001208 /* Now initialize things */
1209 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001210 /* Print out the command prompt */
1211 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001212
Erik Andersenc7c634b2000-03-19 05:28:55 +00001213 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001214
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001215 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001216
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001217 if (read(inputFd, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001218 /* if we can't read input then exit */
1219 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001220
Erik Andersen13456d12000-03-16 08:09:57 +00001221 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001222 case '\n':
1223 case '\r':
1224 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001225 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001226 break_out = 1;
1227 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001228 case 1:
1229 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001230 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001231 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001232 case 2:
1233 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001234 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001235 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001236 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001237 /* Control-c -- stop gathering input */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001238
Eric Andersen86349772000-12-18 20:25:50 +00001239 /* Link into lash to reset context to 0 on ^C and such */
1240 shell_context = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001241
1242 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001243 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001244 command[0] = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001245
Eric Andersen86349772000-12-18 20:25:50 +00001246 return;
Erik Andersenf0657d32000-04-12 17:49:52 +00001247 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001248 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001249 * if the len=0 and no chars to delete */
1250 if (len == 0) {
Eric Andersened424db2001-04-23 15:28:28 +00001251prepare_to_die:
Mark Whitley4e338752001-01-26 20:42:23 +00001252 printf("exit");
Erik Andersenf0657d32000-04-12 17:49:52 +00001253 clean_up_and_die(0);
1254 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001255 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001256 }
1257 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001258 case 5:
1259 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001260 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001261 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001262 case 6:
1263 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001264 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001265 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001266 case '\b':
1267 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001268 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001269 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001270 break;
1271 case '\t':
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001272#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001273 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001274#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001275 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001276 case 14:
1277 /* Control-n -- Get next command in history */
1278 if (hp && hp->n && hp->n->s) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001279 get_next_history(&hp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001280 goto rewrite_line;
1281 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001282 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001283 }
1284 break;
1285 case 16:
1286 /* Control-p -- Get previous command from history */
1287 if (hp && hp->p) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001288 get_previous_history(&hp, hp->p);
Erik Andersenf0657d32000-04-12 17:49:52 +00001289 goto rewrite_line;
1290 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001291 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001292 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001293 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001294 case 21:
1295 /* Control-U -- Clear line before cursor */
1296 if (cursor) {
1297 strcpy(command, command + cursor);
1298 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001299 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001300 break;
1301
1302 case ESC:{
1303 /* escape sequence follows */
1304 if (read(inputFd, &c, 1) < 1)
1305 return;
1306 /* different vt100 emulations */
1307 if (c == '[' || c == 'O') {
1308 if (read(inputFd, &c, 1) < 1)
1309 return;
1310 }
1311 switch (c) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001312#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001313 case '\t': /* Alt-Tab */
1314
1315 input_tab(&lastWasTab);
1316 break;
1317#endif
1318 case 'A':
1319 /* Up Arrow -- Get previous command from history */
1320 if (hp && hp->p) {
1321 get_previous_history(&hp, hp->p);
1322 goto rewrite_line;
1323 } else {
1324 beep();
1325 }
1326 break;
1327 case 'B':
1328 /* Down Arrow -- Get next command in history */
1329 if (hp && hp->n && hp->n->s) {
1330 get_next_history(&hp);
1331 goto rewrite_line;
1332 } else {
1333 beep();
1334 }
1335 break;
1336
1337 /* Rewrite the line with the selected history item */
1338 rewrite_line:
1339 /* change command */
1340 len = strlen(strcpy(command, hp->s));
1341 /* redraw and go to end line */
1342 redraw(cmdedit_y, 0);
1343 break;
1344 case 'C':
1345 /* Right Arrow -- Move forward one character */
1346 input_forward();
1347 break;
1348 case 'D':
1349 /* Left Arrow -- Move back one character */
1350 input_backward(1);
1351 break;
1352 case '3':
1353 /* Delete */
1354 input_delete();
1355 break;
1356 case '1':
1357 case 'H':
1358 /* Home (Ctrl-A) */
1359 input_backward(cursor);
1360 break;
1361 case '4':
1362 case 'F':
1363 /* End (Ctrl-E) */
1364 input_end();
1365 break;
1366 default:
1367 if (!(c >= '1' && c <= '9'))
1368 c = 0;
1369 beep();
1370 }
1371 if (c >= '1' && c <= '9')
1372 do
1373 if (read(inputFd, &c, 1) < 1)
1374 return;
1375 while (c != '~');
1376 break;
1377 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001378
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001379 default: /* If it's regular input, do the normal thing */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001380#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1381 /* Control-V -- Add non-printable symbol */
1382 if (c == 22) {
1383 if (read(inputFd, &c, 1) < 1)
1384 return;
1385 if (c == 0) {
1386 beep();
1387 break;
1388 }
1389 } else
1390#endif
Eric Andersene5dfced2001-04-09 22:48:12 +00001391 if (!Isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001392 break;
1393
1394 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
1395 break;
1396
1397 len++;
1398
1399 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001400 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001401 *(command + cursor + 1) = 0;
1402 cmdedit_set_out_char(0);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001403 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001404 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001405
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001406 memmove(command + sc + 1, command + sc, len - sc);
1407 *(command + sc) = c;
1408 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001409 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001410 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001411 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001412 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001413 }
1414
Erik Andersenc7c634b2000-03-19 05:28:55 +00001415 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001416 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001417 if (break_out) /* Enter is the command terminator, no more input. */
1418 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001419
1420 if (c != '\t')
1421 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001422 }
1423
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001424 setTermSettings(inputFd, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001425 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001426
1427 /* Handle command history log */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001428 if (len) { /* no put empty line */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001429
1430 struct history *h = his_end;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001431 char *ss;
Mark Whitley4e338752001-01-26 20:42:23 +00001432
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001433 ss = xstrdup(command); /* duplicate */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001434
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001435 if (h == 0) {
Eric Andersen91a44002000-07-19 17:37:57 +00001436 /* No previous history -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001437 h = his_front = xmalloc(sizeof(struct history));
1438 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001439
1440 h->p = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001441 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001442 h->n->p = h;
1443 h->n->n = NULL;
1444 h->n->s = NULL;
1445 his_end = h->n;
1446 history_counter++;
1447 } else {
Eric Andersen91a44002000-07-19 17:37:57 +00001448 /* Add a new history command -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001449 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001450
1451 h->n->p = h;
1452 h->n->n = NULL;
1453 h->n->s = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001454 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001455 his_end = h->n;
1456
1457 /* After max history, remove the oldest command */
1458 if (history_counter >= MAX_HISTORY) {
1459
1460 struct history *p = his_front->n;
1461
1462 p->p = NULL;
1463 free(his_front->s);
1464 free(his_front);
1465 his_front = p;
1466 } else {
1467 history_counter++;
1468 }
Erik Andersen13456d12000-03-16 08:09:57 +00001469 }
Mark Whitleyf5949862001-03-14 00:29:14 +00001470#if !defined(BB_FEATURE_SH_SIMPLE_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001471 num_ok_lines++;
1472#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001473 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001474 command[len++] = '\n'; /* set '\n' */
1475 command[len] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001476#if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001477 input_tab(0); /* strong free */
1478#endif
Mark Whitleyf5949862001-03-14 00:29:14 +00001479#if !defined(BB_FEATURE_SH_SIMPLE_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001480 free(cmdedit_prompt);
1481#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001482 return;
Erik Andersen13456d12000-03-16 08:09:57 +00001483}
1484
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001485
Mark Whitley4e338752001-01-26 20:42:23 +00001486/* Undo the effects of cmdedit_init(). */
Eric Andersen501c88b2000-07-28 15:14:45 +00001487extern void cmdedit_terminate(void)
1488{
1489 cmdedit_reset_term();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001490 if ((handlers_sets & SET_TERM_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +00001491 signal(SIGKILL, SIG_DFL);
1492 signal(SIGINT, SIG_DFL);
1493 signal(SIGQUIT, SIG_DFL);
1494 signal(SIGTERM, SIG_DFL);
1495 signal(SIGWINCH, SIG_DFL);
1496 handlers_sets &= ~SET_TERM_HANDLERS;
1497 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001498}
1499
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001500#endif /* BB_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001501
1502
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001503#ifdef TEST
1504
Eric Andersene5dfced2001-04-09 22:48:12 +00001505const char *applet_name = "debug stuff usage";
1506const char *memory_exhausted = "Memory exhausted";
1507
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001508#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1509#include <locale.h>
1510#endif
1511
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001512unsigned int shell_context;
1513
1514int main(int argc, char **argv)
1515{
1516 char buff[BUFSIZ];
1517 char *prompt =
Mark Whitleyf5949862001-03-14 00:29:14 +00001518#if !defined(BB_FEATURE_SH_SIMPLE_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001519 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001520\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001521\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001522#else
1523 "% ";
1524#endif
1525
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001526#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1527 setlocale(LC_ALL, "");
1528#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001529 shell_context = 1;
1530 do {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001531 int l;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001532 cmdedit_read_input(prompt, buff);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001533 l = strlen(buff);
1534 if(l > 0 && buff[l-1] == '\n')
1535 buff[l-1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001536 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1537 } while (shell_context);
1538 printf("*** cmdedit_read_input() detect ^C\n");
1539 return 0;
1540}
1541
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001542#endif /* TEST */