blob: ce5450032eff30ccdf69d367134d65e013031fb0 [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
358 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000359 pbuf = buf;
360 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000361 c = *prmt_ptr++;
362 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000363 const char *cp = prmt_ptr;
364 int l;
365
366 c = process_escape_sequence(&prmt_ptr);
367 if(prmt_ptr==cp) {
368 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000369 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000370 c = *prmt_ptr++;
371 switch (c) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000372#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000373 case 'u':
374 pbuf = user_buf;
375 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000376#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000377 case 'h':
378 pbuf = hostname_buf;
379 if (*pbuf == 0) {
380 pbuf = xcalloc(256, 1);
381 if (gethostname(pbuf, 255) < 0) {
382 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000383 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000384 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000385
386 if (s)
387 *s = 0;
388 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000389 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000390 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000391 break;
392 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000393 c = my_euid == 0 ? '#' : '$';
394 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000395#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000396 case 'w':
397 pbuf = pwd_buf;
398 l = strlen(home_pwd_buf);
399 if (home_pwd_buf[0] != 0 &&
400 strncmp(home_pwd_buf, pbuf, l) == 0 &&
401 (pbuf[l]=='/' || pbuf[l]=='\0') &&
402 strlen(pwd_buf+l)<PATH_MAX) {
403 pbuf = buf2;
404 *pbuf = '~';
405 strcpy(pbuf+1, pwd_buf+l);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000406 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000407 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000408#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000409 case 'W':
410 pbuf = pwd_buf;
411 cp = strrchr(pbuf,'/');
412 if ( (cp != NULL) && (cp != pbuf) )
413 pbuf += (cp-pbuf)+1;
414 break;
415 case '!':
416 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
417 break;
418 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000419 c = '\033';
420 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000421 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000422 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000423 int h;
424 buf2[l++] = *prmt_ptr;
425 buf2[l] = 0;
426 h = strtol(buf2, &pbuf, 16);
427 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000428 l--;
429 break;
430 }
431 prmt_ptr++;
432 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000433 buf2[l] = 0;
434 c = (char)strtol(buf2, 0, 16);
435 if(c==0)
436 c = '?';
437 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000438 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000439 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000440 if (c == flg_not_length) {
441 flg_not_length = flg_not_length == '[' ? ']' : '[';
442 continue;
443 }
444 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000445 }
446 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000447 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000448 if(pbuf == buf)
449 *pbuf = c;
450 prmt_len += strlen(pbuf);
451 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000452 if (flg_not_length == ']')
453 sub_len++;
454 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000455 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000456 cmdedit_prompt = prmt_mem_ptr;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000457 cmdedit_prmt_len = prmt_len - sub_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000458 put_prompt();
459}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000460#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000461
462
463/* draw promt, editor line, and clear tail */
464static void redraw(int y, int back_cursor)
465{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000466 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000467 printf("\033[%dA", y);
468 cmdedit_y = 0; /* new quasireal y */
469 putchar('\r');
470 put_prompt();
471 input_end(); /* rewrite */
472 printf("\033[J"); /* destroy tail after cursor */
473 input_backward(back_cursor);
474}
475
Erik Andersenf0657d32000-04-12 17:49:52 +0000476/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000477static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000478{
Mark Whitley4e338752001-01-26 20:42:23 +0000479 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000480
Mark Whitley4e338752001-01-26 20:42:23 +0000481 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000482 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000483
484 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000485 len--;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000486 input_end(); /* rewtite new line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000487 cmdedit_set_out_char(0); /* destroy end char */
488 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000489}
490
Mark Whitley4e338752001-01-26 20:42:23 +0000491/* Delete the char in back of the cursor */
492static void input_backspace(void)
493{
494 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000495 input_backward(1);
496 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000497 }
498}
499
500
Erik Andersenf0657d32000-04-12 17:49:52 +0000501/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000502static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000503{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000504 if (cursor < len)
505 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000506}
507
508
Mark Whitley4e338752001-01-26 20:42:23 +0000509static void clean_up_and_die(int sig)
510{
511 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000512 if (sig != SIGINT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000513 exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */
Mark Whitley4e338752001-01-26 20:42:23 +0000514 cmdedit_reset_term();
515}
516
517static void cmdedit_setwidth(int w, int redraw_flg)
518{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000519 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000520 if (w <= cmdedit_termw) {
521 cmdedit_termw = cmdedit_termw % w;
522 }
Mark Whitley4e338752001-01-26 20:42:23 +0000523 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000524 cmdedit_termw = w;
525
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000526 if (redraw_flg) {
527 /* new y for current cursor */
528 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000529
530 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000531 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
532 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000533 }
Eric Andersen6faae7d2001-02-16 20:09:17 +0000534 }
Mark Whitley4e338752001-01-26 20:42:23 +0000535}
536
537extern void cmdedit_init(void)
538{
Eric Andersen61173a52001-03-19 17:48:55 +0000539 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000540 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
541 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000542 win_changed(-SIGWINCH);
543 handlers_sets |= SET_WCHG_HANDLERS;
544 }
545
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000546 if ((handlers_sets & SET_ATEXIT) == 0) {
547#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
548 struct passwd *entry;
549
550 my_euid = geteuid();
551 entry = getpwuid(my_euid);
552 if (entry) {
553 user_buf = xstrdup(entry->pw_name);
554 home_pwd_buf = xstrdup(entry->pw_dir);
555 }
556#endif
557
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000558#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000559
560#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
561 my_euid = geteuid();
562#endif
563 my_uid = getuid();
564 my_gid = getgid();
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000565#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000566 handlers_sets |= SET_ATEXIT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000567 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000568 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000569
570 if ((handlers_sets & SET_TERM_HANDLERS) == 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000571 signal(SIGKILL, clean_up_and_die);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000572 signal(SIGINT, clean_up_and_die);
Mark Whitley4e338752001-01-26 20:42:23 +0000573 signal(SIGQUIT, clean_up_and_die);
574 signal(SIGTERM, clean_up_and_die);
575 handlers_sets |= SET_TERM_HANDLERS;
576 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000577
Mark Whitley4e338752001-01-26 20:42:23 +0000578}
Erik Andersenf0657d32000-04-12 17:49:52 +0000579
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000580#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000581
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000582static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000583{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000584 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
585 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
586 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
587 (st->st_mode & S_IXOTH)) return TRUE;
588 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000589}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000590
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000591#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000592
593static char **username_tab_completion(char *ud, int *num_matches)
594{
595 struct passwd *entry;
596 int userlen;
597 char *temp;
598
599
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000600 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000601 userlen = strlen(ud);
602
603 if (num_matches == 0) { /* "~/..." or "~user/..." */
604 char *sav_ud = ud - 1;
605 char *home = 0;
606
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000607 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000608 home = home_pwd_buf;
609 } else {
610 /* "~user/..." */
611 temp = strchr(ud, '/');
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000612 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000613 entry = getpwnam(ud);
614 *temp = '/'; /* restore ~user/... */
615 ud = temp;
616 if (entry)
617 home = entry->pw_dir;
618 }
619 if (home) {
620 if ((userlen + strlen(home) + 1) < BUFSIZ) {
621 char temp2[BUFSIZ]; /* argument size */
622
623 /* /home/user/... */
624 sprintf(temp2, "%s%s", home, ud);
625 strcpy(sav_ud, temp2);
626 }
627 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000628 return 0; /* void, result save to argument :-) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000629 } else {
630 /* "~[^/]*" */
631 char **matches = (char **) NULL;
632 int nm = 0;
633
634 setpwent();
635
636 while ((entry = getpwent()) != NULL) {
637 /* Null usernames should result in all users as possible completions. */
638 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
639
640 temp = xmalloc(3 + strlen(entry->pw_name));
641 sprintf(temp, "~%s/", entry->pw_name);
642 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
643
644 matches[nm++] = temp;
645 }
646 }
647
648 endpwent();
649 (*num_matches) = nm;
650 return (matches);
651 }
652}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000653#endif /* BB_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000654
655enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000656 FIND_EXE_ONLY = 0,
657 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000658 FIND_FILE_ONLY = 2,
659};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000660
Mark Whitley4e338752001-01-26 20:42:23 +0000661static int path_parse(char ***p, int flags)
662{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000663 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000664 char *tmp;
665 char *pth;
666
Mark Whitley4e338752001-01-26 20:42:23 +0000667 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000668 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
669 /* PATH=<empty> or PATH=:<empty> */
670 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000671 return 1;
672 }
673
674 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000675 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000676
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000677 for (;;) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000678 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000679 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000680 if (tmp) {
681 if (*++tmp == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000682 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000683 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000684 break;
685 }
686
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000687 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000688
689 tmp = pth;
690 (*p)[0] = xstrdup(tmp);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000691 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000692
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000693 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000694 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000695 if (tmp) {
696 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
697 if (*++tmp == 0)
698 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000699 } else
700 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000701 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000702 }
703
704 return npth;
705}
706
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000707static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000708{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000709 int l = 0;
710 char *s = xmalloc((strlen(found) + 1) * 2);
711
712 while (*found) {
713 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
714 s[l++] = '\\';
715 s[l++] = *found++;
716 }
717 s[l] = 0;
718 return s;
719}
720
721static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000722 int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000723{
724
725 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000726 DIR *dir;
727 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000728 char dirbuf[BUFSIZ];
729 int nm = *num_matches;
730 struct stat st;
731 char *path1[1];
732 char **paths = path1;
733 int npaths;
734 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000735 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000736 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000737
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000738 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000739
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000740 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000741 /* no dir, if flags==EXE_ONLY - get paths, else "." */
742 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000743 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000744 } else {
745 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000746 /* save for change */
747 strcpy(dirbuf, command);
748 /* set dir only */
749 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000750#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000751 if (dirbuf[0] == '~') /* ~/... or ~user/... */
752 username_tab_completion(dirbuf, 0);
753#endif
754 /* "strip" dirname in command */
755 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000756
Mark Whitley4e338752001-01-26 20:42:23 +0000757 paths[0] = dirbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000758 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000759 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000760
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000761 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000762
Mark Whitley4e338752001-01-26 20:42:23 +0000763 dir = opendir(paths[i]);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000764 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000765 continue;
766
767 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000768 char *str_found = next->d_name;
769
Mark Whitley4e338752001-01-26 20:42:23 +0000770 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000771 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000772 continue;
773 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000774 if (*str_found == '.' && *pfind == 0) {
775 if (*paths[i] == '/' && paths[i][1] == 0
776 && str_found[1] == 0) str_found = ""; /* only "/" */
777 else
Mark Whitley4e338752001-01-26 20:42:23 +0000778 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000779 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000780 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000781 /* hmm, remover in progress? */
Eric Andersene5dfced2001-04-09 22:48:12 +0000782 if (stat(found, &st) < 0)
783 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000784 /* find with dirs ? */
785 if (paths[i] != dirbuf)
786 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000787 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000788 /* name is directory */
Eric Andersene5dfced2001-04-09 22:48:12 +0000789 str_found = found;
790 found = concat_path_file(found, "");
791 free(str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000792 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000793 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000794 /* not put found file if search only dirs for cd */
Eric Andersene5dfced2001-04-09 22:48:12 +0000795 if (type == FIND_DIR_ONLY)
796 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000797 str_found = add_quote_for_spec_chars(found);
798 if (type == FIND_FILE_ONLY ||
799 (type == FIND_EXE_ONLY && is_execute(&st) == TRUE))
800 strcat(str_found, " ");
801 }
Mark Whitley4e338752001-01-26 20:42:23 +0000802 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000803 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
804
805 matches[nm++] = str_found;
Eric Andersene5dfced2001-04-09 22:48:12 +0000806cont:
807 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000808 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000809 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000810 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000811 if (paths != path1) {
812 free(paths[0]); /* allocated memory only in first member */
813 free(paths);
814 }
Mark Whitley4e338752001-01-26 20:42:23 +0000815 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000816 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000817}
Erik Andersenf0657d32000-04-12 17:49:52 +0000818
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000819static int match_compare(const void *a, const void *b)
820{
821 return strcmp(*(char **) a, *(char **) b);
822}
823
824
825
826#define QUOT (UCHAR_MAX+1)
827
828#define collapse_pos(is, in) { \
829 memcpy(int_buf+is, int_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); \
830 memcpy(pos_buf+is, pos_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); }
831
832static int find_match(char *matchBuf, int *len_with_quotes)
833{
834 int i, j;
835 int command_mode;
836 int c, c2;
837 int int_buf[BUFSIZ + 1];
838 int pos_buf[BUFSIZ + 1];
839
840 /* set to integer dimension characters and own positions */
841 for (i = 0;; i++) {
842 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
843 if (int_buf[i] == 0) {
844 pos_buf[i] = -1; /* indicator end line */
845 break;
846 } else
847 pos_buf[i] = i;
848 }
849
850 /* mask \+symbol and convert '\t' to ' ' */
851 for (i = j = 0; matchBuf[i]; i++, j++)
852 if (matchBuf[i] == '\\') {
853 collapse_pos(j, j + 1);
854 int_buf[j] |= QUOT;
855 i++;
856#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
857 if (matchBuf[i] == '\t') /* algorithm equivalent */
858 int_buf[j] = ' ' | QUOT;
859#endif
860 }
861#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
862 else if (matchBuf[i] == '\t')
863 int_buf[j] = ' ';
864#endif
865
866 /* mask "symbols" or 'symbols' */
867 c2 = 0;
868 for (i = 0; int_buf[i]; i++) {
869 c = int_buf[i];
870 if (c == '\'' || c == '"') {
871 if (c2 == 0)
872 c2 = c;
873 else {
874 if (c == c2)
875 c2 = 0;
876 else
877 int_buf[i] |= QUOT;
878 }
879 } else if (c2 != 0 && c != '$')
880 int_buf[i] |= QUOT;
881 }
882
883 /* skip commands with arguments if line have commands delimiters */
884 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
885 for (i = 0; int_buf[i]; i++) {
886 c = int_buf[i];
887 c2 = int_buf[i + 1];
888 j = i ? int_buf[i - 1] : -1;
889 command_mode = 0;
890 if (c == ';' || c == '&' || c == '|') {
891 command_mode = 1 + (c == c2);
892 if (c == '&') {
893 if (j == '>' || j == '<')
894 command_mode = 0;
895 } else if (c == '|' && j == '>')
896 command_mode = 0;
897 }
898 if (command_mode) {
899 collapse_pos(0, i + command_mode);
900 i = -1; /* hack incremet */
901 }
902 }
903 /* collapse `command...` */
904 for (i = 0; int_buf[i]; i++)
905 if (int_buf[i] == '`') {
906 for (j = i + 1; int_buf[j]; j++)
907 if (int_buf[j] == '`') {
908 collapse_pos(i, j + 1);
909 j = 0;
910 break;
911 }
912 if (j) {
913 /* not found close ` - command mode, collapse all previous */
914 collapse_pos(0, i + 1);
915 break;
916 } else
917 i--; /* hack incremet */
918 }
919
920 /* collapse (command...(command...)...) or {command...{command...}...} */
921 c = 0; /* "recursive" level */
922 c2 = 0;
923 for (i = 0; int_buf[i]; i++)
924 if (int_buf[i] == '(' || int_buf[i] == '{') {
925 if (int_buf[i] == '(')
926 c++;
927 else
928 c2++;
929 collapse_pos(0, i + 1);
930 i = -1; /* hack incremet */
931 }
932 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
933 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
934 if (int_buf[i] == ')')
935 c--;
936 else
937 c2--;
938 collapse_pos(0, i + 1);
939 i = -1; /* hack incremet */
940 }
941
942 /* skip first not quote space */
943 for (i = 0; int_buf[i]; i++)
944 if (int_buf[i] != ' ')
945 break;
946 if (i)
947 collapse_pos(0, i);
948
949 /* set find mode for completion */
950 command_mode = FIND_EXE_ONLY;
951 for (i = 0; int_buf[i]; i++)
952 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
953 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000954 && matchBuf[pos_buf[0]]=='c'
955 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000956 command_mode = FIND_DIR_ONLY;
957 else {
958 command_mode = FIND_FILE_ONLY;
959 break;
960 }
961 }
962 /* "strlen" */
963 for (i = 0; int_buf[i]; i++);
964 /* find last word */
965 for (--i; i >= 0; i--) {
966 c = int_buf[i];
967 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
968 collapse_pos(0, i + 1);
969 break;
970 }
971 }
972 /* skip first not quoted '\'' or '"' */
973 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
974 /* collapse quote or unquote // or /~ */
Mark Whitley7e5291f2001-03-08 19:31:12 +0000975 while ((int_buf[i] & ~QUOT) == '/' &&
976 ((int_buf[i + 1] & ~QUOT) == '/'
977 || (int_buf[i + 1] & ~QUOT) == '~')) {
978 i++;
979 }
980 if (i) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000981 collapse_pos(0, i);
Mark Whitley7e5291f2001-03-08 19:31:12 +0000982 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000983
984 /* set only match and destroy quotes */
985 j = 0;
986 for (i = 0; pos_buf[i] >= 0; i++) {
987 matchBuf[i] = matchBuf[pos_buf[i]];
988 j = pos_buf[i] + 1;
989 }
990 matchBuf[i] = 0;
991 /* old lenght matchBuf with quotes symbols */
992 *len_with_quotes = j ? j - pos_buf[0] : 0;
993
994 return command_mode;
995}
996
997
998static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000999{
1000 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001001 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +00001002 static char **matches;
1003
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001004 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001005 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001006 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001007 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001008 free(matches);
1009 matches = (char **) NULL;
1010 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001011 return;
1012 }
1013 if (*lastWasTab == FALSE) {
1014
1015 char *tmp;
1016 int len_found;
1017 char matchBuf[BUFSIZ];
1018 int find_type;
1019 int recalc_pos;
1020
1021 *lastWasTab = TRUE; /* flop trigger */
1022
1023 /* Make a local copy of the string -- up
1024 * to the position of the cursor */
1025 tmp = strncpy(matchBuf, command_ps, cursor);
1026 tmp[cursor] = 0;
1027
1028 find_type = find_match(matchBuf, &recalc_pos);
1029
1030 /* Free up any memory already allocated */
1031 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001032
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001033#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001034 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001035 * then try completing this word as a username. */
1036
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001037 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001038 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001039#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001040 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001041 * in the current working directory that matches. */
1042 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001043 matches =
1044 exe_n_cwd_tab_completion(matchBuf, &num_matches,
1045 find_type);
Erik Andersenf0657d32000-04-12 17:49:52 +00001046
1047 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001048 if (!matches || num_matches > 1) {
1049 char *tmp1;
1050
Mark Whitley4e338752001-01-26 20:42:23 +00001051 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001052 if (!matches)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001053 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001054 /* sort */
1055 qsort(matches, num_matches, sizeof(char *), match_compare);
1056
1057 /* find minimal match */
1058 tmp = xstrdup(matches[0]);
1059 for (tmp1 = tmp; *tmp1; tmp1++)
1060 for (len_found = 1; len_found < num_matches; len_found++)
1061 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1062 *tmp1 = 0;
1063 break;
1064 }
1065 if (*tmp == 0) { /* have unique */
1066 free(tmp);
1067 return;
1068 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001069 } else { /* one match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001070 tmp = matches[0];
1071 /* for next completion current found */
1072 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001073 }
1074
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001075 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001076 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001077 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001078
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001079 /* before word for match */
1080 command_ps[cursor - recalc_pos] = 0;
1081 /* save tail line */
1082 strcpy(matchBuf, command_ps + cursor);
1083 /* add match */
1084 strcat(command_ps, tmp);
1085 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001086 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001087 /* back to begin word for match */
1088 input_backward(recalc_pos);
1089 /* new pos */
1090 recalc_pos = cursor + len_found;
1091 /* new len */
1092 len = strlen(command_ps);
1093 /* write out the matched command */
1094 input_end();
1095 input_backward(cursor - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001096 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001097 if (tmp != matches[0])
1098 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001099 } else {
1100 /* Ok -- the last char was a TAB. Since they
1101 * just hit TAB again, print a list of all the
1102 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001103 if (matches && num_matches > 0) {
1104 int i, col, l;
1105 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001106
1107 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001108 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001109 for (i = 0, col = 0; i < num_matches; i++) {
1110 l = strlen(matches[i]);
1111 if (l < 14)
1112 l = 14;
1113 printf("%-14s ", matches[i]);
1114 if ((l += 2) > 16)
1115 while (l % 16) {
1116 putchar(' ');
1117 l++;
1118 }
1119 col += l;
1120 col -= (col / cmdedit_termw) * cmdedit_termw;
1121 if (col > 60 && matches[i + 1] != NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +00001122 putchar('\n');
Erik Andersenf0657d32000-04-12 17:49:52 +00001123 col = 0;
1124 }
1125 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001126 /* Go to the next line and rewrite */
1127 putchar('\n');
1128 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001129 }
1130 }
1131}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001132#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001133
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001134static void get_previous_history(struct history **hp, struct history *p)
Erik Andersenf0657d32000-04-12 17:49:52 +00001135{
Eric Andersen91a44002000-07-19 17:37:57 +00001136 if ((*hp)->s)
1137 free((*hp)->s);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001138 (*hp)->s = xstrdup(command_ps);
1139 *hp = p;
Erik Andersenf0657d32000-04-12 17:49:52 +00001140}
1141
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001142static inline void get_next_history(struct history **hp)
Erik Andersenf0657d32000-04-12 17:49:52 +00001143{
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001144 get_previous_history(hp, (*hp)->n);
Erik Andersenf0657d32000-04-12 17:49:52 +00001145}
1146
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001147enum {
1148 ESC = 27,
1149 DEL = 127,
1150};
1151
1152
Erik Andersen6273f652000-03-17 01:12:41 +00001153/*
1154 * This function is used to grab a character buffer
1155 * from the input file descriptor and allows you to
1156 * a string with full command editing (sortof like
1157 * a mini readline).
1158 *
1159 * The following standard commands are not implemented:
1160 * ESC-b -- Move back one word
1161 * ESC-f -- Move forward one word
1162 * ESC-d -- Delete back one word
1163 * ESC-h -- Delete forward one word
1164 * CTL-t -- Transpose two characters
1165 *
1166 * Furthermore, the "vi" command editing keys are not implemented.
1167 *
Erik Andersen6273f652000-03-17 01:12:41 +00001168 */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001169
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001170extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001171{
1172
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001173 int inputFd = fileno(stdin);
Mark Whitley4e338752001-01-26 20:42:23 +00001174
Erik Andersenc7c634b2000-03-19 05:28:55 +00001175 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001176 int lastWasTab = FALSE;
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001177 unsigned char c = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001178 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +00001179
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001180 /* prepare before init handlers */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001181 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001182 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001183 command_ps = command;
1184
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001185 if (new_settings.c_cc[VMIN] == 0) { /* first call */
1186
1187 getTermSettings(inputFd, (void *) &initial_settings);
Erik Andersen1d1d9502000-04-21 01:26:49 +00001188 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001189
Erik Andersen1d1d9502000-04-21 01:26:49 +00001190 new_settings.c_cc[VMIN] = 1;
1191 new_settings.c_cc[VTIME] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001192 /* Turn off CTRL-C, so we can trap it */
1193 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001194 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001195 /* Turn off echoing */
1196 new_settings.c_lflag &= ~(ECHO | ECHOCTL | ECHONL);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001197 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001198
1199 command[0] = 0;
1200
1201 setTermSettings(inputFd, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001202 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001203
Eric Andersen6faae7d2001-02-16 20:09:17 +00001204 /* Now initialize things */
1205 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001206 /* Print out the command prompt */
1207 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001208
Erik Andersenc7c634b2000-03-19 05:28:55 +00001209 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001210
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001211 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001212
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001213 if (read(inputFd, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001214 /* if we can't read input then exit */
1215 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001216
Erik Andersen13456d12000-03-16 08:09:57 +00001217 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001218 case '\n':
1219 case '\r':
1220 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001221 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001222 break_out = 1;
1223 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001224 case 1:
1225 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001226 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001227 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001228 case 2:
1229 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001230 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001231 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001232 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001233 /* Control-c -- stop gathering input */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001234
Eric Andersen86349772000-12-18 20:25:50 +00001235 /* Link into lash to reset context to 0 on ^C and such */
1236 shell_context = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001237
1238 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001239 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001240 command[0] = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001241
Eric Andersen86349772000-12-18 20:25:50 +00001242 return;
Erik Andersenf0657d32000-04-12 17:49:52 +00001243 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001244 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001245 * if the len=0 and no chars to delete */
1246 if (len == 0) {
Eric Andersened424db2001-04-23 15:28:28 +00001247prepare_to_die:
Mark Whitley4e338752001-01-26 20:42:23 +00001248 printf("exit");
Erik Andersenf0657d32000-04-12 17:49:52 +00001249 clean_up_and_die(0);
1250 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001251 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001252 }
1253 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001254 case 5:
1255 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001256 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001257 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001258 case 6:
1259 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001260 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001261 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001262 case '\b':
1263 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001264 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001265 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001266 break;
1267 case '\t':
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001268#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001269 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001270#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001271 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001272 case 14:
1273 /* Control-n -- Get next command in history */
1274 if (hp && hp->n && hp->n->s) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001275 get_next_history(&hp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001276 goto rewrite_line;
1277 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001278 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001279 }
1280 break;
1281 case 16:
1282 /* Control-p -- Get previous command from history */
1283 if (hp && hp->p) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001284 get_previous_history(&hp, hp->p);
Erik Andersenf0657d32000-04-12 17:49:52 +00001285 goto rewrite_line;
1286 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001287 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001288 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001289 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001290 case 21:
1291 /* Control-U -- Clear line before cursor */
1292 if (cursor) {
1293 strcpy(command, command + cursor);
1294 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001295 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001296 break;
1297
1298 case ESC:{
1299 /* escape sequence follows */
1300 if (read(inputFd, &c, 1) < 1)
1301 return;
1302 /* different vt100 emulations */
1303 if (c == '[' || c == 'O') {
1304 if (read(inputFd, &c, 1) < 1)
1305 return;
1306 }
1307 switch (c) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001308#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001309 case '\t': /* Alt-Tab */
1310
1311 input_tab(&lastWasTab);
1312 break;
1313#endif
1314 case 'A':
1315 /* Up Arrow -- Get previous command from history */
1316 if (hp && hp->p) {
1317 get_previous_history(&hp, hp->p);
1318 goto rewrite_line;
1319 } else {
1320 beep();
1321 }
1322 break;
1323 case 'B':
1324 /* Down Arrow -- Get next command in history */
1325 if (hp && hp->n && hp->n->s) {
1326 get_next_history(&hp);
1327 goto rewrite_line;
1328 } else {
1329 beep();
1330 }
1331 break;
1332
1333 /* Rewrite the line with the selected history item */
1334 rewrite_line:
1335 /* change command */
1336 len = strlen(strcpy(command, hp->s));
1337 /* redraw and go to end line */
1338 redraw(cmdedit_y, 0);
1339 break;
1340 case 'C':
1341 /* Right Arrow -- Move forward one character */
1342 input_forward();
1343 break;
1344 case 'D':
1345 /* Left Arrow -- Move back one character */
1346 input_backward(1);
1347 break;
1348 case '3':
1349 /* Delete */
1350 input_delete();
1351 break;
1352 case '1':
1353 case 'H':
1354 /* Home (Ctrl-A) */
1355 input_backward(cursor);
1356 break;
1357 case '4':
1358 case 'F':
1359 /* End (Ctrl-E) */
1360 input_end();
1361 break;
1362 default:
1363 if (!(c >= '1' && c <= '9'))
1364 c = 0;
1365 beep();
1366 }
1367 if (c >= '1' && c <= '9')
1368 do
1369 if (read(inputFd, &c, 1) < 1)
1370 return;
1371 while (c != '~');
1372 break;
1373 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001374
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001375 default: /* If it's regular input, do the normal thing */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001376#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1377 /* Control-V -- Add non-printable symbol */
1378 if (c == 22) {
1379 if (read(inputFd, &c, 1) < 1)
1380 return;
1381 if (c == 0) {
1382 beep();
1383 break;
1384 }
1385 } else
1386#endif
Eric Andersene5dfced2001-04-09 22:48:12 +00001387 if (!Isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001388 break;
1389
1390 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
1391 break;
1392
1393 len++;
1394
1395 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001396 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001397 *(command + cursor + 1) = 0;
1398 cmdedit_set_out_char(0);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001399 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001400 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001401
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001402 memmove(command + sc + 1, command + sc, len - sc);
1403 *(command + sc) = c;
1404 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001405 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001406 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001407 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001408 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001409 }
1410
Erik Andersenc7c634b2000-03-19 05:28:55 +00001411 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001412 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001413 if (break_out) /* Enter is the command terminator, no more input. */
1414 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001415
1416 if (c != '\t')
1417 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001418 }
1419
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001420 setTermSettings(inputFd, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001421 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001422
1423 /* Handle command history log */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001424 if (len) { /* no put empty line */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001425
1426 struct history *h = his_end;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001427 char *ss;
Mark Whitley4e338752001-01-26 20:42:23 +00001428
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001429 ss = xstrdup(command); /* duplicate */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001430
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001431 if (h == 0) {
Eric Andersen91a44002000-07-19 17:37:57 +00001432 /* No previous history -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001433 h = his_front = xmalloc(sizeof(struct history));
1434 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001435
1436 h->p = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001437 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001438 h->n->p = h;
1439 h->n->n = NULL;
1440 h->n->s = NULL;
1441 his_end = h->n;
1442 history_counter++;
1443 } else {
Eric Andersen91a44002000-07-19 17:37:57 +00001444 /* Add a new history command -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001445 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001446
1447 h->n->p = h;
1448 h->n->n = NULL;
1449 h->n->s = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001450 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001451 his_end = h->n;
1452
1453 /* After max history, remove the oldest command */
1454 if (history_counter >= MAX_HISTORY) {
1455
1456 struct history *p = his_front->n;
1457
1458 p->p = NULL;
1459 free(his_front->s);
1460 free(his_front);
1461 his_front = p;
1462 } else {
1463 history_counter++;
1464 }
Erik Andersen13456d12000-03-16 08:09:57 +00001465 }
Mark Whitleyf5949862001-03-14 00:29:14 +00001466#if !defined(BB_FEATURE_SH_SIMPLE_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001467 num_ok_lines++;
1468#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001469 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001470 command[len++] = '\n'; /* set '\n' */
1471 command[len] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001472#if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001473 input_tab(0); /* strong free */
1474#endif
Mark Whitleyf5949862001-03-14 00:29:14 +00001475#if !defined(BB_FEATURE_SH_SIMPLE_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001476 free(cmdedit_prompt);
1477#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001478 return;
Erik Andersen13456d12000-03-16 08:09:57 +00001479}
1480
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001481
Mark Whitley4e338752001-01-26 20:42:23 +00001482/* Undo the effects of cmdedit_init(). */
Eric Andersen501c88b2000-07-28 15:14:45 +00001483extern void cmdedit_terminate(void)
1484{
1485 cmdedit_reset_term();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001486 if ((handlers_sets & SET_TERM_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +00001487 signal(SIGKILL, SIG_DFL);
1488 signal(SIGINT, SIG_DFL);
1489 signal(SIGQUIT, SIG_DFL);
1490 signal(SIGTERM, SIG_DFL);
1491 signal(SIGWINCH, SIG_DFL);
1492 handlers_sets &= ~SET_TERM_HANDLERS;
1493 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001494}
1495
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001496#endif /* BB_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001497
1498
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001499#ifdef TEST
1500
Eric Andersene5dfced2001-04-09 22:48:12 +00001501const char *applet_name = "debug stuff usage";
1502const char *memory_exhausted = "Memory exhausted";
1503
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001504#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1505#include <locale.h>
1506#endif
1507
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001508unsigned int shell_context;
1509
1510int main(int argc, char **argv)
1511{
1512 char buff[BUFSIZ];
1513 char *prompt =
Mark Whitleyf5949862001-03-14 00:29:14 +00001514#if !defined(BB_FEATURE_SH_SIMPLE_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001515 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001516\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001517\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001518#else
1519 "% ";
1520#endif
1521
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001522#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1523 setlocale(LC_ALL, "");
1524#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001525 shell_context = 1;
1526 do {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001527 int l;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001528 cmdedit_read_input(prompt, buff);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001529 l = strlen(buff);
1530 if(l > 0 && buff[l-1] == '\n')
1531 buff[l-1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001532 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1533 } while (shell_context);
1534 printf("*** cmdedit_read_input() detect ^C\n");
1535 return 0;
1536}
1537
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001538#endif /* TEST */