blob: 3b47504558b0dae95b83d1e2b1b2f873eab88af8 [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.
Eric Andersen7467c8d2001-07-12 20:26:32 +00006 * Written by: Vladimir Oleynik <dzo@simtreas.ru>
Eric Andersen5f2c79d2001-02-16 18:36:04 +00007 *
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
Eric Andersen004015e2001-05-21 20:30:51 +000079#if defined(BB_FEATURE_COMMAND_USERNAME_COMPLETION) || defined(BB_FEATURE_SH_FANCY_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
Glenn L McGrath78b0e372001-06-26 02:06:08 +000084# ifndef TEST
85# include "pwd_grp/pwd.h"
86# else
87# include <pwd.h>
88# endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000089#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
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000109#include <termios.h>
110#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
111#define getTermSettings(fd,argp) tcgetattr(fd, argp);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000112
113/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000114static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000115
116
Mark Whitley4e338752001-01-26 20:42:23 +0000117static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000118volatile int cmdedit_termw = 80; /* actual terminal width */
119static int history_counter = 0; /* Number of commands in history list */
Mark Whitley4e338752001-01-26 20:42:23 +0000120static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000121volatile int handlers_sets = 0; /* Set next bites: */
122
Mark Whitley4e338752001-01-26 20:42:23 +0000123enum {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000124 SET_ATEXIT = 1, /* when atexit() has been called
125 and get euid,uid,gid to fast compare */
Eric Andersen7467c8d2001-07-12 20:26:32 +0000126 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
127 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000128};
Erik Andersen13456d12000-03-16 08:09:57 +0000129
Mark Whitley4e338752001-01-26 20:42:23 +0000130
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000131static int cmdedit_x; /* real x terminal position */
132static int cmdedit_y; /* pseudoreal y terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000133static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000134
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000135static int cursor; /* required global for signal handler */
136static int len; /* --- "" - - "" - -"- --""-- --""--- */
137static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000138static
Eric Andersen004015e2001-05-21 20:30:51 +0000139#ifndef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000140 const
141#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000142char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000143
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000144#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
145static char *user_buf = "";
146static char *home_pwd_buf = "";
147static int my_euid;
148#endif
149
Eric Andersen004015e2001-05-21 20:30:51 +0000150#ifdef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000151static char *hostname_buf = "";
152static int num_ok_lines = 1;
153#endif
154
155
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000156#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000157
158#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
159static int my_euid;
160#endif
161
162static int my_uid;
163static int my_gid;
164
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000165#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000166
Erik Andersen13456d12000-03-16 08:09:57 +0000167
Mark Whitley4e338752001-01-26 20:42:23 +0000168static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000169
Mark Whitley4e338752001-01-26 20:42:23 +0000170static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000171{
172 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen8d79ce82001-07-22 23:00:15 +0000173 static sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000174
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000175 /* emulate || signal call */
176 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Mark Whitley4e338752001-01-26 20:42:23 +0000177 ioctl(0, TIOCGWINSZ, &win);
178 if (win.ws_col > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000179 cmdedit_setwidth(win.ws_col, nsig == SIGWINCH);
180 }
Mark Whitley4e338752001-01-26 20:42:23 +0000181 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000182 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000183
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000184 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000185 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000186 else if (nsig == SIGWINCH) /* signaled called handler */
187 signal(SIGWINCH, win_changed); /* set for next call */
188 else /* nsig == 0 */
189 /* set previous handler */
190 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000191}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000192
193static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000194{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000195 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000196/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000197 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000198 handlers_sets &= ~SET_RESET_TERM;
199 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000200 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000201 /* reset SIGWINCH handler to previous (default) */
202 win_changed(0);
203 handlers_sets &= ~SET_WCHG_HANDLERS;
204 }
205 fflush(stdout);
Eric Andersenb040d4f2000-07-25 18:01:20 +0000206#ifdef BB_FEATURE_CLEAN_UP
207 if (his_front) {
208 struct history *n;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000209
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000210 while (his_front != his_end) {
Eric Andersenb040d4f2000-07-25 18:01:20 +0000211 n = his_front->n;
212 free(his_front->s);
213 free(his_front);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000214 his_front = n;
Eric Andersenb040d4f2000-07-25 18:01:20 +0000215 }
216 }
217#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000218}
219
Mark Whitley4e338752001-01-26 20:42:23 +0000220
Mark Whitley4e338752001-01-26 20:42:23 +0000221/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000222static void cmdedit_set_out_char(int next_char)
223{
224
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000225 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000226
227 if (c == 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000228 c = ' '; /* destroy end char? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000229#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersene5dfced2001-04-09 22:48:12 +0000230 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000231 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000232 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000233 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000234 c += '@';
235 if (c == 127)
236 c = '?';
237 printf("\033[7m%c\033[0m", c);
238 } else
239#endif
240 putchar(c);
241 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000242 /* terminal is scrolled down */
243 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000244 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000245
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000246 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000247 next_char = ' ';
248 /* destroy "(auto)margin" */
249 putchar(next_char);
250 putchar('\b');
251 }
252 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000253}
254
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000255/* Move to end line. Bonus: rewrite line from cursor */
256static void input_end(void)
257{
258 while (cursor < len)
259 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000260}
261
262/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000263static void goto_new_line(void)
264{
Mark Whitley4e338752001-01-26 20:42:23 +0000265 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000266 if (cmdedit_x)
267 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000268}
269
270
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000271static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000272{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000273 fputs(s, stdout);
274}
275static inline void beep(void)
276{
277 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000278}
279
Mark Whitley4e338752001-01-26 20:42:23 +0000280/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000281/* special for slow terminal */
282static void input_backward(int num)
283{
284 if (num > cursor)
285 num = cursor;
Eric Andersene5dfced2001-04-09 22:48:12 +0000286 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000287
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000288 if (cmdedit_x >= num) { /* no to up line */
289 cmdedit_x -= num;
290 if (num < 4)
291 while (num-- > 0)
292 putchar('\b');
293
294 else
295 printf("\033[%dD", num);
296 } else {
297 int count_y;
298
299 if (cmdedit_x) {
300 putchar('\r'); /* back to first terminal pos. */
301 num -= cmdedit_x; /* set previous backward */
302 }
303 count_y = 1 + num / cmdedit_termw;
304 printf("\033[%dA", count_y);
305 cmdedit_y -= count_y;
306 /* require forward after uping */
307 cmdedit_x = cmdedit_termw * count_y - num;
308 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000309 }
Erik Andersen13456d12000-03-16 08:09:57 +0000310}
311
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000312static void put_prompt(void)
313{
314 out1str(cmdedit_prompt);
315 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
316 cursor = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000317 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000318}
319
Eric Andersen004015e2001-05-21 20:30:51 +0000320#ifndef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000321static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000322{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000323 cmdedit_prompt = prmt_ptr;
324 cmdedit_prmt_len = strlen(prmt_ptr);
325 put_prompt();
326}
327#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000328static void parse_prompt(const char *prmt_ptr)
329{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000330 int prmt_len = 0;
331 int sub_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000332 char flg_not_length = '[';
333 char *prmt_mem_ptr = xcalloc(1, 1);
334 char *pwd_buf = xgetcwd(0);
335 char buf2[PATH_MAX + 1];
336 char buf[2];
337 char c;
338 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000339
Eric Andersen5f265b72001-05-11 16:58:46 +0000340 if (!pwd_buf) {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000341 pwd_buf=(char *)unknown;
Eric Andersen5f265b72001-05-11 16:58:46 +0000342 }
343
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000344 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000345 pbuf = buf;
346 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000347 c = *prmt_ptr++;
348 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000349 const char *cp = prmt_ptr;
350 int l;
351
352 c = process_escape_sequence(&prmt_ptr);
353 if(prmt_ptr==cp) {
354 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000355 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000356 c = *prmt_ptr++;
357 switch (c) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000358#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000359 case 'u':
360 pbuf = user_buf;
361 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000362#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000363 case 'h':
364 pbuf = hostname_buf;
365 if (*pbuf == 0) {
366 pbuf = xcalloc(256, 1);
367 if (gethostname(pbuf, 255) < 0) {
368 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000369 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000370 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000371
372 if (s)
373 *s = 0;
374 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000375 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000376 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000377 break;
378 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000379 c = my_euid == 0 ? '#' : '$';
380 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000381#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000382 case 'w':
383 pbuf = pwd_buf;
384 l = strlen(home_pwd_buf);
385 if (home_pwd_buf[0] != 0 &&
386 strncmp(home_pwd_buf, pbuf, l) == 0 &&
387 (pbuf[l]=='/' || pbuf[l]=='\0') &&
388 strlen(pwd_buf+l)<PATH_MAX) {
389 pbuf = buf2;
390 *pbuf = '~';
391 strcpy(pbuf+1, pwd_buf+l);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000392 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000393 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000394#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000395 case 'W':
396 pbuf = pwd_buf;
397 cp = strrchr(pbuf,'/');
398 if ( (cp != NULL) && (cp != pbuf) )
399 pbuf += (cp-pbuf)+1;
400 break;
401 case '!':
402 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
403 break;
404 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000405 c = '\033';
406 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000407 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000408 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000409 int h;
410 buf2[l++] = *prmt_ptr;
411 buf2[l] = 0;
412 h = strtol(buf2, &pbuf, 16);
413 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000414 l--;
415 break;
416 }
417 prmt_ptr++;
418 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000419 buf2[l] = 0;
420 c = (char)strtol(buf2, 0, 16);
421 if(c==0)
422 c = '?';
423 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000424 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000425 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000426 if (c == flg_not_length) {
427 flg_not_length = flg_not_length == '[' ? ']' : '[';
428 continue;
429 }
430 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000431 }
432 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000433 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000434 if(pbuf == buf)
435 *pbuf = c;
436 prmt_len += strlen(pbuf);
437 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000438 if (flg_not_length == ']')
439 sub_len++;
440 }
Eric Andersen8f697842001-07-02 15:36:57 +0000441 if(pwd_buf!=(char *)unknown)
442 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000443 cmdedit_prompt = prmt_mem_ptr;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000444 cmdedit_prmt_len = prmt_len - sub_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000445 put_prompt();
446}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000447#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000448
449
450/* draw promt, editor line, and clear tail */
451static void redraw(int y, int back_cursor)
452{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000453 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000454 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000455 putchar('\r');
456 put_prompt();
457 input_end(); /* rewrite */
458 printf("\033[J"); /* destroy tail after cursor */
459 input_backward(back_cursor);
460}
461
Erik Andersenf0657d32000-04-12 17:49:52 +0000462/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000463static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000464{
Mark Whitley4e338752001-01-26 20:42:23 +0000465 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000466
Mark Whitley4e338752001-01-26 20:42:23 +0000467 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000468 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000469
470 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000471 len--;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000472 input_end(); /* rewtite new line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000473 cmdedit_set_out_char(0); /* destroy end char */
474 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000475}
476
Mark Whitley4e338752001-01-26 20:42:23 +0000477/* Delete the char in back of the cursor */
478static void input_backspace(void)
479{
480 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000481 input_backward(1);
482 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000483 }
484}
485
486
Erik Andersenf0657d32000-04-12 17:49:52 +0000487/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000488static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000489{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000490 if (cursor < len)
491 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000492}
493
494
Mark Whitley4e338752001-01-26 20:42:23 +0000495static void cmdedit_setwidth(int w, int redraw_flg)
496{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000497 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000498 if (w <= cmdedit_termw) {
499 cmdedit_termw = cmdedit_termw % w;
500 }
Mark Whitley4e338752001-01-26 20:42:23 +0000501 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000502 cmdedit_termw = w;
503
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000504 if (redraw_flg) {
505 /* new y for current cursor */
506 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000507
508 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000509 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
510 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000511 }
Eric Andersen6faae7d2001-02-16 20:09:17 +0000512 }
Mark Whitley4e338752001-01-26 20:42:23 +0000513}
514
Eric Andersen7467c8d2001-07-12 20:26:32 +0000515static void cmdedit_init(void)
Mark Whitley4e338752001-01-26 20:42:23 +0000516{
Eric Andersen61173a52001-03-19 17:48:55 +0000517 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
519 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000520 win_changed(-SIGWINCH);
521 handlers_sets |= SET_WCHG_HANDLERS;
522 }
523
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000524 if ((handlers_sets & SET_ATEXIT) == 0) {
525#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
526 struct passwd *entry;
527
528 my_euid = geteuid();
529 entry = getpwuid(my_euid);
530 if (entry) {
531 user_buf = xstrdup(entry->pw_name);
532 home_pwd_buf = xstrdup(entry->pw_dir);
533 }
534#endif
535
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000536#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000537
538#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
539 my_euid = geteuid();
540#endif
541 my_uid = getuid();
542 my_gid = getgid();
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000543#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000544 handlers_sets |= SET_ATEXIT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000545 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000546 }
Mark Whitley4e338752001-01-26 20:42:23 +0000547}
Erik Andersenf0657d32000-04-12 17:49:52 +0000548
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000549#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000550
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000551static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000552{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000553 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
554 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
555 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
556 (st->st_mode & S_IXOTH)) return TRUE;
557 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000558}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000559
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000560#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000561
562static char **username_tab_completion(char *ud, int *num_matches)
563{
564 struct passwd *entry;
565 int userlen;
566 char *temp;
567
568
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000569 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000570 userlen = strlen(ud);
571
572 if (num_matches == 0) { /* "~/..." or "~user/..." */
573 char *sav_ud = ud - 1;
574 char *home = 0;
575
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000576 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000577 home = home_pwd_buf;
578 } else {
579 /* "~user/..." */
580 temp = strchr(ud, '/');
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000581 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000582 entry = getpwnam(ud);
583 *temp = '/'; /* restore ~user/... */
584 ud = temp;
585 if (entry)
586 home = entry->pw_dir;
587 }
588 if (home) {
589 if ((userlen + strlen(home) + 1) < BUFSIZ) {
590 char temp2[BUFSIZ]; /* argument size */
591
592 /* /home/user/... */
593 sprintf(temp2, "%s%s", home, ud);
594 strcpy(sav_ud, temp2);
595 }
596 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000597 return 0; /* void, result save to argument :-) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000598 } else {
599 /* "~[^/]*" */
600 char **matches = (char **) NULL;
601 int nm = 0;
602
603 setpwent();
604
605 while ((entry = getpwent()) != NULL) {
606 /* Null usernames should result in all users as possible completions. */
607 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
608
609 temp = xmalloc(3 + strlen(entry->pw_name));
610 sprintf(temp, "~%s/", entry->pw_name);
611 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
612
613 matches[nm++] = temp;
614 }
615 }
616
617 endpwent();
618 (*num_matches) = nm;
619 return (matches);
620 }
621}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000622#endif /* BB_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000623
624enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000625 FIND_EXE_ONLY = 0,
626 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000627 FIND_FILE_ONLY = 2,
628};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000629
Mark Whitley4e338752001-01-26 20:42:23 +0000630static int path_parse(char ***p, int flags)
631{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000632 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000633 char *tmp;
634 char *pth;
635
Mark Whitley4e338752001-01-26 20:42:23 +0000636 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000637 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
638 /* PATH=<empty> or PATH=:<empty> */
639 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000640 return 1;
641 }
642
643 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000644 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000645
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000646 for (;;) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000647 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000648 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000649 if (tmp) {
650 if (*++tmp == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000651 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000652 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000653 break;
654 }
655
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000656 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000657
658 tmp = pth;
659 (*p)[0] = xstrdup(tmp);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000660 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000661
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000662 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000663 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000664 if (tmp) {
665 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
666 if (*++tmp == 0)
667 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000668 } else
669 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000670 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000671 }
672
673 return npth;
674}
675
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000676static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000677{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000678 int l = 0;
679 char *s = xmalloc((strlen(found) + 1) * 2);
680
681 while (*found) {
682 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
683 s[l++] = '\\';
684 s[l++] = *found++;
685 }
686 s[l] = 0;
687 return s;
688}
689
690static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000691 int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000692{
693
694 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000695 DIR *dir;
696 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000697 char dirbuf[BUFSIZ];
698 int nm = *num_matches;
699 struct stat st;
700 char *path1[1];
701 char **paths = path1;
702 int npaths;
703 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000704 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000705 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000706
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000707 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000708
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000709 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000710 /* no dir, if flags==EXE_ONLY - get paths, else "." */
711 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000712 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000713 } else {
714 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000715 /* save for change */
716 strcpy(dirbuf, command);
717 /* set dir only */
718 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000719#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000720 if (dirbuf[0] == '~') /* ~/... or ~user/... */
721 username_tab_completion(dirbuf, 0);
722#endif
723 /* "strip" dirname in command */
724 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000725
Mark Whitley4e338752001-01-26 20:42:23 +0000726 paths[0] = dirbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000727 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000728 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000729
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000730 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000731
Mark Whitley4e338752001-01-26 20:42:23 +0000732 dir = opendir(paths[i]);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000733 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000734 continue;
735
736 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000737 char *str_found = next->d_name;
738
Mark Whitley4e338752001-01-26 20:42:23 +0000739 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000740 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000741 continue;
742 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000743 if (*str_found == '.' && *pfind == 0) {
744 if (*paths[i] == '/' && paths[i][1] == 0
745 && str_found[1] == 0) str_found = ""; /* only "/" */
746 else
Mark Whitley4e338752001-01-26 20:42:23 +0000747 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000748 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000749 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000750 /* hmm, remover in progress? */
Eric Andersene5dfced2001-04-09 22:48:12 +0000751 if (stat(found, &st) < 0)
752 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000753 /* find with dirs ? */
754 if (paths[i] != dirbuf)
755 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000756 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000757 /* name is directory */
Eric Andersene5dfced2001-04-09 22:48:12 +0000758 str_found = found;
759 found = concat_path_file(found, "");
760 free(str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000761 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000762 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000763 /* not put found file if search only dirs for cd */
Eric Andersene5dfced2001-04-09 22:48:12 +0000764 if (type == FIND_DIR_ONLY)
765 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000766 str_found = add_quote_for_spec_chars(found);
767 if (type == FIND_FILE_ONLY ||
768 (type == FIND_EXE_ONLY && is_execute(&st) == TRUE))
769 strcat(str_found, " ");
770 }
Mark Whitley4e338752001-01-26 20:42:23 +0000771 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000772 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
773
774 matches[nm++] = str_found;
Eric Andersene5dfced2001-04-09 22:48:12 +0000775cont:
776 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000777 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000778 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000779 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000780 if (paths != path1) {
781 free(paths[0]); /* allocated memory only in first member */
782 free(paths);
783 }
Mark Whitley4e338752001-01-26 20:42:23 +0000784 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000785 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000786}
Erik Andersenf0657d32000-04-12 17:49:52 +0000787
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000788static int match_compare(const void *a, const void *b)
789{
790 return strcmp(*(char **) a, *(char **) b);
791}
792
793
794
795#define QUOT (UCHAR_MAX+1)
796
797#define collapse_pos(is, in) { \
798 memcpy(int_buf+is, int_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); \
799 memcpy(pos_buf+is, pos_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); }
800
801static int find_match(char *matchBuf, int *len_with_quotes)
802{
803 int i, j;
804 int command_mode;
805 int c, c2;
806 int int_buf[BUFSIZ + 1];
807 int pos_buf[BUFSIZ + 1];
808
809 /* set to integer dimension characters and own positions */
810 for (i = 0;; i++) {
811 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
812 if (int_buf[i] == 0) {
813 pos_buf[i] = -1; /* indicator end line */
814 break;
815 } else
816 pos_buf[i] = i;
817 }
818
819 /* mask \+symbol and convert '\t' to ' ' */
820 for (i = j = 0; matchBuf[i]; i++, j++)
821 if (matchBuf[i] == '\\') {
822 collapse_pos(j, j + 1);
823 int_buf[j] |= QUOT;
824 i++;
825#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
826 if (matchBuf[i] == '\t') /* algorithm equivalent */
827 int_buf[j] = ' ' | QUOT;
828#endif
829 }
830#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
831 else if (matchBuf[i] == '\t')
832 int_buf[j] = ' ';
833#endif
834
835 /* mask "symbols" or 'symbols' */
836 c2 = 0;
837 for (i = 0; int_buf[i]; i++) {
838 c = int_buf[i];
839 if (c == '\'' || c == '"') {
840 if (c2 == 0)
841 c2 = c;
842 else {
843 if (c == c2)
844 c2 = 0;
845 else
846 int_buf[i] |= QUOT;
847 }
848 } else if (c2 != 0 && c != '$')
849 int_buf[i] |= QUOT;
850 }
851
852 /* skip commands with arguments if line have commands delimiters */
853 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
854 for (i = 0; int_buf[i]; i++) {
855 c = int_buf[i];
856 c2 = int_buf[i + 1];
857 j = i ? int_buf[i - 1] : -1;
858 command_mode = 0;
859 if (c == ';' || c == '&' || c == '|') {
860 command_mode = 1 + (c == c2);
861 if (c == '&') {
862 if (j == '>' || j == '<')
863 command_mode = 0;
864 } else if (c == '|' && j == '>')
865 command_mode = 0;
866 }
867 if (command_mode) {
868 collapse_pos(0, i + command_mode);
869 i = -1; /* hack incremet */
870 }
871 }
872 /* collapse `command...` */
873 for (i = 0; int_buf[i]; i++)
874 if (int_buf[i] == '`') {
875 for (j = i + 1; int_buf[j]; j++)
876 if (int_buf[j] == '`') {
877 collapse_pos(i, j + 1);
878 j = 0;
879 break;
880 }
881 if (j) {
882 /* not found close ` - command mode, collapse all previous */
883 collapse_pos(0, i + 1);
884 break;
885 } else
886 i--; /* hack incremet */
887 }
888
889 /* collapse (command...(command...)...) or {command...{command...}...} */
890 c = 0; /* "recursive" level */
891 c2 = 0;
892 for (i = 0; int_buf[i]; i++)
893 if (int_buf[i] == '(' || int_buf[i] == '{') {
894 if (int_buf[i] == '(')
895 c++;
896 else
897 c2++;
898 collapse_pos(0, i + 1);
899 i = -1; /* hack incremet */
900 }
901 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
902 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
903 if (int_buf[i] == ')')
904 c--;
905 else
906 c2--;
907 collapse_pos(0, i + 1);
908 i = -1; /* hack incremet */
909 }
910
911 /* skip first not quote space */
912 for (i = 0; int_buf[i]; i++)
913 if (int_buf[i] != ' ')
914 break;
915 if (i)
916 collapse_pos(0, i);
917
918 /* set find mode for completion */
919 command_mode = FIND_EXE_ONLY;
920 for (i = 0; int_buf[i]; i++)
921 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
922 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000923 && matchBuf[pos_buf[0]]=='c'
924 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000925 command_mode = FIND_DIR_ONLY;
926 else {
927 command_mode = FIND_FILE_ONLY;
928 break;
929 }
930 }
931 /* "strlen" */
932 for (i = 0; int_buf[i]; i++);
933 /* find last word */
934 for (--i; i >= 0; i--) {
935 c = int_buf[i];
936 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
937 collapse_pos(0, i + 1);
938 break;
939 }
940 }
941 /* skip first not quoted '\'' or '"' */
942 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
943 /* collapse quote or unquote // or /~ */
Mark Whitley7e5291f2001-03-08 19:31:12 +0000944 while ((int_buf[i] & ~QUOT) == '/' &&
945 ((int_buf[i + 1] & ~QUOT) == '/'
946 || (int_buf[i + 1] & ~QUOT) == '~')) {
947 i++;
948 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000949
950 /* set only match and destroy quotes */
951 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000952 for (c = 0; pos_buf[i] >= 0; i++) {
953 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000954 j = pos_buf[i] + 1;
955 }
Eric Andersen4f990532001-05-31 17:15:57 +0000956 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000957 /* old lenght matchBuf with quotes symbols */
958 *len_with_quotes = j ? j - pos_buf[0] : 0;
959
960 return command_mode;
961}
962
963
964static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000965{
966 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000967 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +0000968 static char **matches;
969
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000970 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +0000971 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000972 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +0000973 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000974 free(matches);
975 matches = (char **) NULL;
976 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000977 return;
978 }
979 if (*lastWasTab == FALSE) {
980
981 char *tmp;
982 int len_found;
983 char matchBuf[BUFSIZ];
984 int find_type;
985 int recalc_pos;
986
987 *lastWasTab = TRUE; /* flop trigger */
988
989 /* Make a local copy of the string -- up
990 * to the position of the cursor */
991 tmp = strncpy(matchBuf, command_ps, cursor);
992 tmp[cursor] = 0;
993
994 find_type = find_match(matchBuf, &recalc_pos);
995
996 /* Free up any memory already allocated */
997 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +0000998
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000999#ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001000 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001001 * then try completing this word as a username. */
1002
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001003 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001004 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001005#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001006 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001007 * in the current working directory that matches. */
1008 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001009 matches =
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001010 exe_n_cwd_tab_completion(matchBuf,
1011 &num_matches, find_type);
1012 /* Remove duplicate found */
1013 if(matches) {
1014 int i, j;
1015 /* bubble */
1016 for(i=0; i<(num_matches-1); i++)
1017 for(j=i+1; j<num_matches; j++)
1018 if(matches[i]!=0 && matches[j]!=0 &&
1019 strcmp(matches[i], matches[j])==0) {
1020 free(matches[j]);
1021 matches[j]=0;
1022 }
1023 j=num_matches;
1024 num_matches = 0;
1025 for(i=0; i<j; i++)
1026 if(matches[i]) {
1027 if(!strcmp(matches[i], "./"))
1028 matches[i][1]=0;
1029 else if(!strcmp(matches[i], "../"))
1030 matches[i][2]=0;
1031 matches[num_matches++]=matches[i];
1032 }
1033 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001034 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001035 if (!matches || num_matches > 1) {
1036 char *tmp1;
1037
Mark Whitley4e338752001-01-26 20:42:23 +00001038 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001039 if (!matches)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001040 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001041 /* sort */
1042 qsort(matches, num_matches, sizeof(char *), match_compare);
1043
1044 /* find minimal match */
1045 tmp = xstrdup(matches[0]);
1046 for (tmp1 = tmp; *tmp1; tmp1++)
1047 for (len_found = 1; len_found < num_matches; len_found++)
1048 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1049 *tmp1 = 0;
1050 break;
1051 }
1052 if (*tmp == 0) { /* have unique */
1053 free(tmp);
1054 return;
1055 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001056 } else { /* one match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001057 tmp = matches[0];
1058 /* for next completion current found */
1059 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001060 }
1061
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001062 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001063 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001064 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001065
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001066 /* before word for match */
1067 command_ps[cursor - recalc_pos] = 0;
1068 /* save tail line */
1069 strcpy(matchBuf, command_ps + cursor);
1070 /* add match */
1071 strcat(command_ps, tmp);
1072 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001073 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001074 /* back to begin word for match */
1075 input_backward(recalc_pos);
1076 /* new pos */
1077 recalc_pos = cursor + len_found;
1078 /* new len */
1079 len = strlen(command_ps);
1080 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001081 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001082 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001083 if (tmp != matches[0])
1084 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001085 } else {
1086 /* Ok -- the last char was a TAB. Since they
1087 * just hit TAB again, print a list of all the
1088 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001089 if (matches && num_matches > 0) {
1090 int i, col, l;
1091 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001092
1093 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001094 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001095 for (i = 0, col = 0; i < num_matches; i++) {
1096 l = strlen(matches[i]);
1097 if (l < 14)
1098 l = 14;
1099 printf("%-14s ", matches[i]);
1100 if ((l += 2) > 16)
1101 while (l % 16) {
1102 putchar(' ');
1103 l++;
1104 }
1105 col += l;
1106 col -= (col / cmdedit_termw) * cmdedit_termw;
1107 if (col > 60 && matches[i + 1] != NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +00001108 putchar('\n');
Erik Andersenf0657d32000-04-12 17:49:52 +00001109 col = 0;
1110 }
1111 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001112 /* Go to the next line and rewrite */
1113 putchar('\n');
1114 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001115 }
1116 }
1117}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001118#endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001119
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001120static void get_previous_history(struct history **hp, struct history *p)
Erik Andersenf0657d32000-04-12 17:49:52 +00001121{
Eric Andersen91a44002000-07-19 17:37:57 +00001122 if ((*hp)->s)
1123 free((*hp)->s);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001124 (*hp)->s = xstrdup(command_ps);
1125 *hp = p;
Erik Andersenf0657d32000-04-12 17:49:52 +00001126}
1127
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001128static inline void get_next_history(struct history **hp)
Erik Andersenf0657d32000-04-12 17:49:52 +00001129{
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001130 get_previous_history(hp, (*hp)->n);
Erik Andersenf0657d32000-04-12 17:49:52 +00001131}
1132
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001133enum {
1134 ESC = 27,
1135 DEL = 127,
1136};
1137
1138
Erik Andersen6273f652000-03-17 01:12:41 +00001139/*
1140 * This function is used to grab a character buffer
1141 * from the input file descriptor and allows you to
1142 * a string with full command editing (sortof like
1143 * a mini readline).
1144 *
1145 * The following standard commands are not implemented:
1146 * ESC-b -- Move back one word
1147 * ESC-f -- Move forward one word
1148 * ESC-d -- Delete back one word
1149 * ESC-h -- Delete forward one word
1150 * CTL-t -- Transpose two characters
1151 *
1152 * Furthermore, the "vi" command editing keys are not implemented.
1153 *
Erik Andersen6273f652000-03-17 01:12:41 +00001154 */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001155
Eric Andersen044228d2001-07-17 01:12:36 +00001156
1157int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001158{
1159
Erik Andersenc7c634b2000-03-19 05:28:55 +00001160 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001161 int lastWasTab = FALSE;
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001162 unsigned char c = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001163 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +00001164
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001165 /* prepare before init handlers */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001166 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001167 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001168 command_ps = command;
1169
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001170 if (new_settings.c_cc[VERASE] == 0) { /* first call */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001171
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001172 getTermSettings(0, (void *) &initial_settings);
Erik Andersen1d1d9502000-04-21 01:26:49 +00001173 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001174 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1175 /* Turn off echoing and CTRL-C, so we can trap it */
1176 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
1177#ifndef linux
1178 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001179 new_settings.c_cc[VMIN] = 1;
1180 new_settings.c_cc[VTIME] = 0;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001181 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001182# ifndef _POSIX_VDISABLE
1183# define _POSIX_VDISABLE '\0'
1184# endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001185 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001186#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001187 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001188
1189 command[0] = 0;
1190
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001191 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001192 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001193
Eric Andersen6faae7d2001-02-16 20:09:17 +00001194 /* Now initialize things */
1195 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001196 /* Print out the command prompt */
1197 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001198
Erik Andersenc7c634b2000-03-19 05:28:55 +00001199 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001200
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001201 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001202
Eric Andersen7467c8d2001-07-12 20:26:32 +00001203 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001204 /* if we can't read input then exit */
1205 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001206
Erik Andersen13456d12000-03-16 08:09:57 +00001207 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001208 case '\n':
1209 case '\r':
1210 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001211 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001212 break_out = 1;
1213 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001214 case 1:
1215 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001216 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001217 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001218 case 2:
1219 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001220 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001221 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001222 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001223 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001224 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001225 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001226 len = 0;
1227 lastWasTab = FALSE;
1228 put_prompt();
1229 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001230 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001231 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001232 * if the len=0 and no chars to delete */
1233 if (len == 0) {
Eric Andersened424db2001-04-23 15:28:28 +00001234prepare_to_die:
Eric Andersen044228d2001-07-17 01:12:36 +00001235#if !defined(BB_FEATURE_ASH)
Mark Whitley4e338752001-01-26 20:42:23 +00001236 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001237 goto_new_line();
1238 /* cmdedit_reset_term() called in atexit */
1239 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001240#else
1241 break_out = -1; /* for control stoped jobs */
1242 break;
1243#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001244 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001245 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001246 }
1247 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001248 case 5:
1249 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001250 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001251 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001252 case 6:
1253 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001254 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001255 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001256 case '\b':
1257 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001258 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001259 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001260 break;
1261 case '\t':
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001262#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001263 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001264#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001265 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001266 case 14:
1267 /* Control-n -- Get next command in history */
1268 if (hp && hp->n && hp->n->s) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001269 get_next_history(&hp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001270 goto rewrite_line;
1271 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001272 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001273 }
1274 break;
1275 case 16:
1276 /* Control-p -- Get previous command from history */
1277 if (hp && hp->p) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001278 get_previous_history(&hp, hp->p);
Erik Andersenf0657d32000-04-12 17:49:52 +00001279 goto rewrite_line;
1280 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001281 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001282 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001283 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001284 case 21:
1285 /* Control-U -- Clear line before cursor */
1286 if (cursor) {
1287 strcpy(command, command + cursor);
1288 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001289 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001290 break;
1291
1292 case ESC:{
1293 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001294 if (safe_read(0, &c, 1) < 1)
1295 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001296 /* different vt100 emulations */
1297 if (c == '[' || c == 'O') {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001298 if (safe_read(0, &c, 1) < 1)
1299 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001300 }
1301 switch (c) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001302#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001303 case '\t': /* Alt-Tab */
1304
1305 input_tab(&lastWasTab);
1306 break;
1307#endif
1308 case 'A':
1309 /* Up Arrow -- Get previous command from history */
1310 if (hp && hp->p) {
1311 get_previous_history(&hp, hp->p);
1312 goto rewrite_line;
1313 } else {
1314 beep();
1315 }
1316 break;
1317 case 'B':
1318 /* Down Arrow -- Get next command in history */
1319 if (hp && hp->n && hp->n->s) {
1320 get_next_history(&hp);
1321 goto rewrite_line;
1322 } else {
1323 beep();
1324 }
1325 break;
1326
1327 /* Rewrite the line with the selected history item */
1328 rewrite_line:
1329 /* change command */
1330 len = strlen(strcpy(command, hp->s));
1331 /* redraw and go to end line */
1332 redraw(cmdedit_y, 0);
1333 break;
1334 case 'C':
1335 /* Right Arrow -- Move forward one character */
1336 input_forward();
1337 break;
1338 case 'D':
1339 /* Left Arrow -- Move back one character */
1340 input_backward(1);
1341 break;
1342 case '3':
1343 /* Delete */
1344 input_delete();
1345 break;
1346 case '1':
1347 case 'H':
1348 /* Home (Ctrl-A) */
1349 input_backward(cursor);
1350 break;
1351 case '4':
1352 case 'F':
1353 /* End (Ctrl-E) */
1354 input_end();
1355 break;
1356 default:
1357 if (!(c >= '1' && c <= '9'))
1358 c = 0;
1359 beep();
1360 }
1361 if (c >= '1' && c <= '9')
1362 do
Eric Andersen7467c8d2001-07-12 20:26:32 +00001363 if (safe_read(0, &c, 1) < 1)
1364 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001365 while (c != '~');
1366 break;
1367 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001368
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001369 default: /* If it's regular input, do the normal thing */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001370#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1371 /* Control-V -- Add non-printable symbol */
1372 if (c == 22) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001373 if (safe_read(0, &c, 1) < 1)
1374 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001375 if (c == 0) {
1376 beep();
1377 break;
1378 }
1379 } else
1380#endif
Eric Andersene5dfced2001-04-09 22:48:12 +00001381 if (!Isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001382 break;
1383
1384 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
1385 break;
1386
1387 len++;
1388
1389 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001390 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001391 *(command + cursor + 1) = 0;
1392 cmdedit_set_out_char(0);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001393 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001394 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001395
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001396 memmove(command + sc + 1, command + sc, len - sc);
1397 *(command + sc) = c;
1398 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001399 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001400 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001401 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001402 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001403 }
1404
Erik Andersenc7c634b2000-03-19 05:28:55 +00001405 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001406 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001407 if (break_out) /* Enter is the command terminator, no more input. */
1408 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001409
1410 if (c != '\t')
1411 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001412 }
1413
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001414 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001415 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001416
1417 /* Handle command history log */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001418 if (len) { /* no put empty line */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001419
1420 struct history *h = his_end;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001421 char *ss;
Mark Whitley4e338752001-01-26 20:42:23 +00001422
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001423 ss = xstrdup(command); /* duplicate */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001424
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001425 if (h == 0) {
Eric Andersen91a44002000-07-19 17:37:57 +00001426 /* No previous history -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001427 h = his_front = xmalloc(sizeof(struct history));
1428 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001429
1430 h->p = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001431 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001432 h->n->p = h;
1433 h->n->n = NULL;
1434 h->n->s = NULL;
1435 his_end = h->n;
1436 history_counter++;
1437 } else {
Eric Andersen91a44002000-07-19 17:37:57 +00001438 /* Add a new history command -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001439 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001440
1441 h->n->p = h;
1442 h->n->n = NULL;
1443 h->n->s = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001444 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001445 his_end = h->n;
1446
1447 /* After max history, remove the oldest command */
1448 if (history_counter >= MAX_HISTORY) {
1449
1450 struct history *p = his_front->n;
1451
1452 p->p = NULL;
1453 free(his_front->s);
1454 free(his_front);
1455 his_front = p;
1456 } else {
1457 history_counter++;
1458 }
Erik Andersen13456d12000-03-16 08:09:57 +00001459 }
Eric Andersen004015e2001-05-21 20:30:51 +00001460#if defined(BB_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001461 num_ok_lines++;
1462#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001463 }
Eric Andersen044228d2001-07-17 01:12:36 +00001464 if(break_out>0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001465 command[len++] = '\n'; /* set '\n' */
1466 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001467 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001468#if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001469 input_tab(0); /* strong free */
1470#endif
Eric Andersen004015e2001-05-21 20:30:51 +00001471#if defined(BB_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001472 free(cmdedit_prompt);
1473#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001474 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001475 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001476}
1477
Eric Andersen7467c8d2001-07-12 20:26:32 +00001478
1479
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001480#endif /* BB_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001481
1482
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001483#ifdef TEST
1484
Eric Andersene5dfced2001-04-09 22:48:12 +00001485const char *applet_name = "debug stuff usage";
1486const char *memory_exhausted = "Memory exhausted";
1487
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001488#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1489#include <locale.h>
1490#endif
1491
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001492int main(int argc, char **argv)
1493{
1494 char buff[BUFSIZ];
1495 char *prompt =
Eric Andersen004015e2001-05-21 20:30:51 +00001496#if defined(BB_FEATURE_SH_FANCY_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001497 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001498\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001499\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001500#else
1501 "% ";
1502#endif
1503
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001504#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1505 setlocale(LC_ALL, "");
1506#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001507 while(1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001508 int l;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001509 cmdedit_read_input(prompt, buff);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001510 l = strlen(buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001511 if(l==0)
1512 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001513 if(l > 0 && buff[l-1] == '\n')
1514 buff[l-1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001515 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001516 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001517 printf("*** cmdedit_read_input() detect ^C\n");
1518 return 0;
1519}
1520
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001521#endif /* TEST */