blob: eee32131a5870d01c91f430e6f2c0240be8c1d1d [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 Andersenbdfd0d72001-10-24 05:00:29 +000046#ifdef CONFIG_LOCALE_SUPPORT
Eric Andersene5dfced2001-04-09 22:48:12 +000047#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 Andersenbdfd0d72001-10-24 05:00:29 +000058#define CONFIG_FEATURE_COMMAND_EDITING
59#define CONFIG_FEATURE_COMMAND_TAB_COMPLETION
60#define CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
61#define CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
62#define CONFIG_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 Andersenbdfd0d72001-10-24 05:00:29 +000068#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5165fbe2001-02-20 06:42:29 +000069#include <dirent.h>
70#include <sys/stat.h>
71#endif
72
Eric Andersenbdfd0d72001-10-24 05:00:29 +000073#ifdef CONFIG_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000074
Eric Andersenbdfd0d72001-10-24 05:00:29 +000075#ifndef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
76#undef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +000077#endif
78
Eric Andersenbdfd0d72001-10-24 05:00:29 +000079#if defined(CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION) || defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
80#define CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +000081#endif
82
Eric Andersenbdfd0d72001-10-24 05:00:29 +000083#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Glenn L McGrath78b0e372001-06-26 02:06:08 +000084# ifndef TEST
Eric Andersen887ca792002-07-03 23:19:26 +000085# include "pwd_.h"
Glenn L McGrath78b0e372001-06-26 02:06:08 +000086# 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 Andersenbdfd0d72001-10-24 05:00:29 +0000139#ifndef CONFIG_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 Andersenbdfd0d72001-10-24 05:00:29 +0000144#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000145static char *user_buf = "";
146static char *home_pwd_buf = "";
147static int my_euid;
148#endif
149
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000150#ifdef CONFIG_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 Andersenbdfd0d72001-10-24 05:00:29 +0000156#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000157
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000158#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000159static int my_euid;
160#endif
161
162static int my_uid;
163static int my_gid;
164
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000165#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000166
Eric Andersen95b52012001-08-02 09:52:40 +0000167/* It seems that libc5 doesn't know what a sighandler_t is... */
168#if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
169typedef void (*sighandler_t) (int);
170#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000171
Mark Whitley4e338752001-01-26 20:42:23 +0000172static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000173
Mark Whitley4e338752001-01-26 20:42:23 +0000174static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000175{
176 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen8d79ce82001-07-22 23:00:15 +0000177 static sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000178
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000179 /* emulate || signal call */
180 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Mark Whitley4e338752001-01-26 20:42:23 +0000181 ioctl(0, TIOCGWINSZ, &win);
182 if (win.ws_col > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000183 cmdedit_setwidth(win.ws_col, nsig == SIGWINCH);
184 }
Mark Whitley4e338752001-01-26 20:42:23 +0000185 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000186 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000187
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000188 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000189 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000190 else if (nsig == SIGWINCH) /* signaled called handler */
191 signal(SIGWINCH, win_changed); /* set for next call */
192 else /* nsig == 0 */
193 /* set previous handler */
194 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000195}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000196
197static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000198{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000199 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000200/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000201 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000202 handlers_sets &= ~SET_RESET_TERM;
203 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000204 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000205 /* reset SIGWINCH handler to previous (default) */
206 win_changed(0);
207 handlers_sets &= ~SET_WCHG_HANDLERS;
208 }
209 fflush(stdout);
Eric Andersenae103612002-04-24 23:08:23 +0000210#if 0
211//#ifdef CONFIG_FEATURE_CLEAN_UP
Eric Andersenb040d4f2000-07-25 18:01:20 +0000212 if (his_front) {
213 struct history *n;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000214
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000215 while (his_front != his_end) {
Eric Andersenb040d4f2000-07-25 18:01:20 +0000216 n = his_front->n;
217 free(his_front->s);
218 free(his_front);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000219 his_front = n;
Eric Andersenb040d4f2000-07-25 18:01:20 +0000220 }
221 }
222#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000223}
224
Mark Whitley4e338752001-01-26 20:42:23 +0000225
Mark Whitley4e338752001-01-26 20:42:23 +0000226/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000227static void cmdedit_set_out_char(int next_char)
228{
229
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000230 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000231
232 if (c == 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000233 c = ' '; /* destroy end char? */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000234#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersene5dfced2001-04-09 22:48:12 +0000235 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000236 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000237 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000238 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000239 c += '@';
240 if (c == 127)
241 c = '?';
242 printf("\033[7m%c\033[0m", c);
243 } else
244#endif
245 putchar(c);
246 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000247 /* terminal is scrolled down */
248 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000249 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000250
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000251 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000252 next_char = ' ';
253 /* destroy "(auto)margin" */
254 putchar(next_char);
255 putchar('\b');
256 }
257 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000258}
259
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000260/* Move to end line. Bonus: rewrite line from cursor */
261static void input_end(void)
262{
263 while (cursor < len)
264 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000265}
266
267/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000268static void goto_new_line(void)
269{
Mark Whitley4e338752001-01-26 20:42:23 +0000270 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000271 if (cmdedit_x)
272 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000273}
274
275
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000276static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000277{
Robert Grieblb2301592002-07-30 23:13:51 +0000278 if ( s )
279 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000280}
281static inline void beep(void)
282{
283 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000284}
285
Mark Whitley4e338752001-01-26 20:42:23 +0000286/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000287/* special for slow terminal */
288static void input_backward(int num)
289{
290 if (num > cursor)
291 num = cursor;
Eric Andersene5dfced2001-04-09 22:48:12 +0000292 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000293
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000294 if (cmdedit_x >= num) { /* no to up line */
295 cmdedit_x -= num;
296 if (num < 4)
297 while (num-- > 0)
298 putchar('\b');
299
300 else
301 printf("\033[%dD", num);
302 } else {
303 int count_y;
304
305 if (cmdedit_x) {
306 putchar('\r'); /* back to first terminal pos. */
307 num -= cmdedit_x; /* set previous backward */
308 }
309 count_y = 1 + num / cmdedit_termw;
310 printf("\033[%dA", count_y);
311 cmdedit_y -= count_y;
312 /* require forward after uping */
313 cmdedit_x = cmdedit_termw * count_y - num;
314 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000315 }
Erik Andersen13456d12000-03-16 08:09:57 +0000316}
317
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000318static void put_prompt(void)
319{
320 out1str(cmdedit_prompt);
321 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
322 cursor = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000323 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000324}
325
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000326#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000327static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000328{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000329 cmdedit_prompt = prmt_ptr;
330 cmdedit_prmt_len = strlen(prmt_ptr);
331 put_prompt();
332}
333#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000334static void parse_prompt(const char *prmt_ptr)
335{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000336 int prmt_len = 0;
337 int sub_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000338 char flg_not_length = '[';
339 char *prmt_mem_ptr = xcalloc(1, 1);
340 char *pwd_buf = xgetcwd(0);
341 char buf2[PATH_MAX + 1];
342 char buf[2];
343 char c;
344 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000345
Eric Andersen5f265b72001-05-11 16:58:46 +0000346 if (!pwd_buf) {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000347 pwd_buf=(char *)unknown;
Eric Andersen5f265b72001-05-11 16:58:46 +0000348 }
349
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000350 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000351 pbuf = buf;
352 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000353 c = *prmt_ptr++;
354 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000355 const char *cp = prmt_ptr;
356 int l;
357
358 c = process_escape_sequence(&prmt_ptr);
359 if(prmt_ptr==cp) {
360 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000361 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000362 c = *prmt_ptr++;
363 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000364#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000365 case 'u':
366 pbuf = user_buf;
367 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000368#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000369 case 'h':
370 pbuf = hostname_buf;
371 if (*pbuf == 0) {
372 pbuf = xcalloc(256, 1);
373 if (gethostname(pbuf, 255) < 0) {
374 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000375 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000376 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000377
378 if (s)
379 *s = 0;
380 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000381 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000382 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000383 break;
384 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000385 c = my_euid == 0 ? '#' : '$';
386 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000387#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000388 case 'w':
389 pbuf = pwd_buf;
390 l = strlen(home_pwd_buf);
391 if (home_pwd_buf[0] != 0 &&
392 strncmp(home_pwd_buf, pbuf, l) == 0 &&
393 (pbuf[l]=='/' || pbuf[l]=='\0') &&
394 strlen(pwd_buf+l)<PATH_MAX) {
395 pbuf = buf2;
396 *pbuf = '~';
397 strcpy(pbuf+1, pwd_buf+l);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000398 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000399 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000400#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000401 case 'W':
402 pbuf = pwd_buf;
403 cp = strrchr(pbuf,'/');
404 if ( (cp != NULL) && (cp != pbuf) )
405 pbuf += (cp-pbuf)+1;
406 break;
407 case '!':
408 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
409 break;
410 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000411 c = '\033';
412 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000413 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000414 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000415 int h;
416 buf2[l++] = *prmt_ptr;
417 buf2[l] = 0;
418 h = strtol(buf2, &pbuf, 16);
419 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000420 l--;
421 break;
422 }
423 prmt_ptr++;
424 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000425 buf2[l] = 0;
426 c = (char)strtol(buf2, 0, 16);
427 if(c==0)
428 c = '?';
429 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000431 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000432 if (c == flg_not_length) {
433 flg_not_length = flg_not_length == '[' ? ']' : '[';
434 continue;
435 }
436 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000437 }
438 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000439 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000440 if(pbuf == buf)
441 *pbuf = c;
442 prmt_len += strlen(pbuf);
443 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000444 if (flg_not_length == ']')
445 sub_len++;
446 }
Eric Andersen8f697842001-07-02 15:36:57 +0000447 if(pwd_buf!=(char *)unknown)
448 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000449 cmdedit_prompt = prmt_mem_ptr;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000450 cmdedit_prmt_len = prmt_len - sub_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000451 put_prompt();
452}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000453#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000454
455
456/* draw promt, editor line, and clear tail */
457static void redraw(int y, int back_cursor)
458{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000459 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000460 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000461 putchar('\r');
462 put_prompt();
463 input_end(); /* rewrite */
464 printf("\033[J"); /* destroy tail after cursor */
465 input_backward(back_cursor);
466}
467
Erik Andersenf0657d32000-04-12 17:49:52 +0000468/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000469static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000470{
Mark Whitley4e338752001-01-26 20:42:23 +0000471 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000472
Mark Whitley4e338752001-01-26 20:42:23 +0000473 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000474 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000475
476 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000477 len--;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000478 input_end(); /* rewtite new line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000479 cmdedit_set_out_char(0); /* destroy end char */
480 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000481}
482
Mark Whitley4e338752001-01-26 20:42:23 +0000483/* Delete the char in back of the cursor */
484static void input_backspace(void)
485{
486 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000487 input_backward(1);
488 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000489 }
490}
491
492
Erik Andersenf0657d32000-04-12 17:49:52 +0000493/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000494static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000495{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000496 if (cursor < len)
497 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000498}
499
500
Mark Whitley4e338752001-01-26 20:42:23 +0000501static void cmdedit_setwidth(int w, int redraw_flg)
502{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000503 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000504 if (w <= cmdedit_termw) {
505 cmdedit_termw = cmdedit_termw % w;
506 }
Mark Whitley4e338752001-01-26 20:42:23 +0000507 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000508 cmdedit_termw = w;
509
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000510 if (redraw_flg) {
511 /* new y for current cursor */
512 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000513
514 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000515 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
516 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000517 }
Eric Andersen6faae7d2001-02-16 20:09:17 +0000518 }
Mark Whitley4e338752001-01-26 20:42:23 +0000519}
520
Eric Andersen7467c8d2001-07-12 20:26:32 +0000521static void cmdedit_init(void)
Mark Whitley4e338752001-01-26 20:42:23 +0000522{
Eric Andersen61173a52001-03-19 17:48:55 +0000523 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000524 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
525 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000526 win_changed(-SIGWINCH);
527 handlers_sets |= SET_WCHG_HANDLERS;
528 }
529
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000530 if ((handlers_sets & SET_ATEXIT) == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000531#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000532 struct passwd *entry;
533
534 my_euid = geteuid();
535 entry = getpwuid(my_euid);
536 if (entry) {
537 user_buf = xstrdup(entry->pw_name);
538 home_pwd_buf = xstrdup(entry->pw_dir);
539 }
540#endif
541
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000542#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000543
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000544#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000545 my_euid = geteuid();
546#endif
547 my_uid = getuid();
548 my_gid = getgid();
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000549#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000550 handlers_sets |= SET_ATEXIT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000551 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000552 }
Mark Whitley4e338752001-01-26 20:42:23 +0000553}
Erik Andersenf0657d32000-04-12 17:49:52 +0000554
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000555#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000556
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000557static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000558{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000559 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
560 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
561 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
562 (st->st_mode & S_IXOTH)) return TRUE;
563 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000564}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000565
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000566#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000567
568static char **username_tab_completion(char *ud, int *num_matches)
569{
570 struct passwd *entry;
571 int userlen;
572 char *temp;
573
574
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000575 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000576 userlen = strlen(ud);
577
578 if (num_matches == 0) { /* "~/..." or "~user/..." */
579 char *sav_ud = ud - 1;
580 char *home = 0;
581
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000582 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000583 home = home_pwd_buf;
584 } else {
585 /* "~user/..." */
586 temp = strchr(ud, '/');
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000587 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000588 entry = getpwnam(ud);
589 *temp = '/'; /* restore ~user/... */
590 ud = temp;
591 if (entry)
592 home = entry->pw_dir;
593 }
594 if (home) {
595 if ((userlen + strlen(home) + 1) < BUFSIZ) {
596 char temp2[BUFSIZ]; /* argument size */
597
598 /* /home/user/... */
599 sprintf(temp2, "%s%s", home, ud);
600 strcpy(sav_ud, temp2);
601 }
602 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000603 return 0; /* void, result save to argument :-) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000604 } else {
605 /* "~[^/]*" */
606 char **matches = (char **) NULL;
607 int nm = 0;
608
609 setpwent();
610
611 while ((entry = getpwent()) != NULL) {
612 /* Null usernames should result in all users as possible completions. */
613 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
614
Robert Griebld378c312002-07-19 00:05:54 +0000615 bb_asprintf(&temp, "~%s/", entry->pw_name);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000616 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
617
618 matches[nm++] = temp;
619 }
620 }
621
622 endpwent();
623 (*num_matches) = nm;
624 return (matches);
625 }
626}
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000627#endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000628
629enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000630 FIND_EXE_ONLY = 0,
631 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000632 FIND_FILE_ONLY = 2,
633};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000634
Mark Whitley4e338752001-01-26 20:42:23 +0000635static int path_parse(char ***p, int flags)
636{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000637 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000638 char *tmp;
639 char *pth;
640
Mark Whitley4e338752001-01-26 20:42:23 +0000641 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000642 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
643 /* PATH=<empty> or PATH=:<empty> */
644 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000645 return 1;
646 }
647
648 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000649 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000650
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000651 for (;;) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000652 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000653 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000654 if (tmp) {
655 if (*++tmp == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000656 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000657 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000658 break;
659 }
660
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000661 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000662
663 tmp = pth;
664 (*p)[0] = xstrdup(tmp);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000665 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000666
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000667 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000668 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000669 if (tmp) {
670 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
671 if (*++tmp == 0)
672 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000673 } else
674 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000675 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000676 }
677
678 return npth;
679}
680
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000681static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000682{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000683 int l = 0;
684 char *s = xmalloc((strlen(found) + 1) * 2);
685
686 while (*found) {
687 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
688 s[l++] = '\\';
689 s[l++] = *found++;
690 }
691 s[l] = 0;
692 return s;
693}
694
695static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000696 int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000697{
698
699 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000700 DIR *dir;
701 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000702 char dirbuf[BUFSIZ];
703 int nm = *num_matches;
704 struct stat st;
705 char *path1[1];
706 char **paths = path1;
707 int npaths;
708 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000709 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000710 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000711
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000712 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000713
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000714 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000715 /* no dir, if flags==EXE_ONLY - get paths, else "." */
716 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000717 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000718 } else {
719 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000720 /* save for change */
721 strcpy(dirbuf, command);
722 /* set dir only */
723 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000724#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000725 if (dirbuf[0] == '~') /* ~/... or ~user/... */
726 username_tab_completion(dirbuf, 0);
727#endif
728 /* "strip" dirname in command */
729 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000730
Mark Whitley4e338752001-01-26 20:42:23 +0000731 paths[0] = dirbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000732 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000733 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000734
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000735 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000736
Mark Whitley4e338752001-01-26 20:42:23 +0000737 dir = opendir(paths[i]);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000738 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000739 continue;
740
741 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000742 char *str_found = next->d_name;
743
Mark Whitley4e338752001-01-26 20:42:23 +0000744 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000745 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000746 continue;
747 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000748 if (*str_found == '.' && *pfind == 0) {
749 if (*paths[i] == '/' && paths[i][1] == 0
750 && str_found[1] == 0) str_found = ""; /* only "/" */
751 else
Mark Whitley4e338752001-01-26 20:42:23 +0000752 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000753 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000754 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000755 /* hmm, remover in progress? */
Eric Andersene5dfced2001-04-09 22:48:12 +0000756 if (stat(found, &st) < 0)
757 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000758 /* find with dirs ? */
759 if (paths[i] != dirbuf)
760 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000761 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000762 /* name is directory */
Eric Andersene5dfced2001-04-09 22:48:12 +0000763 str_found = found;
764 found = concat_path_file(found, "");
765 free(str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000766 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000767 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000768 /* not put found file if search only dirs for cd */
Eric Andersene5dfced2001-04-09 22:48:12 +0000769 if (type == FIND_DIR_ONLY)
770 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000771 str_found = add_quote_for_spec_chars(found);
772 if (type == FIND_FILE_ONLY ||
Matt Kraai1f0c4362001-12-20 23:13:26 +0000773 (type == FIND_EXE_ONLY && is_execute(&st)))
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000774 strcat(str_found, " ");
775 }
Mark Whitley4e338752001-01-26 20:42:23 +0000776 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000777 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
778
779 matches[nm++] = str_found;
Eric Andersene5dfced2001-04-09 22:48:12 +0000780cont:
781 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000782 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000783 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000784 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000785 if (paths != path1) {
786 free(paths[0]); /* allocated memory only in first member */
787 free(paths);
788 }
Mark Whitley4e338752001-01-26 20:42:23 +0000789 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000790 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000791}
Erik Andersenf0657d32000-04-12 17:49:52 +0000792
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000793static int match_compare(const void *a, const void *b)
794{
795 return strcmp(*(char **) a, *(char **) b);
796}
797
798
799
800#define QUOT (UCHAR_MAX+1)
801
802#define collapse_pos(is, in) { \
Eric Andersen889a3012002-03-20 14:31:15 +0000803 memcpy(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
804 memcpy(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000805
806static int find_match(char *matchBuf, int *len_with_quotes)
807{
808 int i, j;
809 int command_mode;
810 int c, c2;
811 int int_buf[BUFSIZ + 1];
812 int pos_buf[BUFSIZ + 1];
813
814 /* set to integer dimension characters and own positions */
815 for (i = 0;; i++) {
816 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
817 if (int_buf[i] == 0) {
818 pos_buf[i] = -1; /* indicator end line */
819 break;
820 } else
821 pos_buf[i] = i;
822 }
823
824 /* mask \+symbol and convert '\t' to ' ' */
825 for (i = j = 0; matchBuf[i]; i++, j++)
826 if (matchBuf[i] == '\\') {
827 collapse_pos(j, j + 1);
828 int_buf[j] |= QUOT;
829 i++;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000830#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000831 if (matchBuf[i] == '\t') /* algorithm equivalent */
832 int_buf[j] = ' ' | QUOT;
833#endif
834 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000835#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000836 else if (matchBuf[i] == '\t')
837 int_buf[j] = ' ';
838#endif
839
840 /* mask "symbols" or 'symbols' */
841 c2 = 0;
842 for (i = 0; int_buf[i]; i++) {
843 c = int_buf[i];
844 if (c == '\'' || c == '"') {
845 if (c2 == 0)
846 c2 = c;
847 else {
848 if (c == c2)
849 c2 = 0;
850 else
851 int_buf[i] |= QUOT;
852 }
853 } else if (c2 != 0 && c != '$')
854 int_buf[i] |= QUOT;
855 }
856
857 /* skip commands with arguments if line have commands delimiters */
858 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
859 for (i = 0; int_buf[i]; i++) {
860 c = int_buf[i];
861 c2 = int_buf[i + 1];
862 j = i ? int_buf[i - 1] : -1;
863 command_mode = 0;
864 if (c == ';' || c == '&' || c == '|') {
865 command_mode = 1 + (c == c2);
866 if (c == '&') {
867 if (j == '>' || j == '<')
868 command_mode = 0;
869 } else if (c == '|' && j == '>')
870 command_mode = 0;
871 }
872 if (command_mode) {
873 collapse_pos(0, i + command_mode);
874 i = -1; /* hack incremet */
875 }
876 }
877 /* collapse `command...` */
878 for (i = 0; int_buf[i]; i++)
879 if (int_buf[i] == '`') {
880 for (j = i + 1; int_buf[j]; j++)
881 if (int_buf[j] == '`') {
882 collapse_pos(i, j + 1);
883 j = 0;
884 break;
885 }
886 if (j) {
887 /* not found close ` - command mode, collapse all previous */
888 collapse_pos(0, i + 1);
889 break;
890 } else
891 i--; /* hack incremet */
892 }
893
894 /* collapse (command...(command...)...) or {command...{command...}...} */
895 c = 0; /* "recursive" level */
896 c2 = 0;
897 for (i = 0; int_buf[i]; i++)
898 if (int_buf[i] == '(' || int_buf[i] == '{') {
899 if (int_buf[i] == '(')
900 c++;
901 else
902 c2++;
903 collapse_pos(0, i + 1);
904 i = -1; /* hack incremet */
905 }
906 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
907 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
908 if (int_buf[i] == ')')
909 c--;
910 else
911 c2--;
912 collapse_pos(0, i + 1);
913 i = -1; /* hack incremet */
914 }
915
916 /* skip first not quote space */
917 for (i = 0; int_buf[i]; i++)
918 if (int_buf[i] != ' ')
919 break;
920 if (i)
921 collapse_pos(0, i);
922
923 /* set find mode for completion */
924 command_mode = FIND_EXE_ONLY;
925 for (i = 0; int_buf[i]; i++)
926 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
927 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000928 && matchBuf[pos_buf[0]]=='c'
929 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000930 command_mode = FIND_DIR_ONLY;
931 else {
932 command_mode = FIND_FILE_ONLY;
933 break;
934 }
935 }
936 /* "strlen" */
937 for (i = 0; int_buf[i]; i++);
938 /* find last word */
939 for (--i; i >= 0; i--) {
940 c = int_buf[i];
941 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
942 collapse_pos(0, i + 1);
943 break;
944 }
945 }
946 /* skip first not quoted '\'' or '"' */
947 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
948 /* collapse quote or unquote // or /~ */
Mark Whitley7e5291f2001-03-08 19:31:12 +0000949 while ((int_buf[i] & ~QUOT) == '/' &&
950 ((int_buf[i + 1] & ~QUOT) == '/'
951 || (int_buf[i + 1] & ~QUOT) == '~')) {
952 i++;
953 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000954
955 /* set only match and destroy quotes */
956 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000957 for (c = 0; pos_buf[i] >= 0; i++) {
958 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000959 j = pos_buf[i] + 1;
960 }
Eric Andersen4f990532001-05-31 17:15:57 +0000961 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000962 /* old lenght matchBuf with quotes symbols */
963 *len_with_quotes = j ? j - pos_buf[0] : 0;
964
965 return command_mode;
966}
967
968
969static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000970{
971 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000972 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +0000973 static char **matches;
974
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000975 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +0000976 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000977 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +0000978 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000979 free(matches);
980 matches = (char **) NULL;
981 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000982 return;
983 }
Matt Kraai1f0c4362001-12-20 23:13:26 +0000984 if (! *lastWasTab) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000985
986 char *tmp;
987 int len_found;
988 char matchBuf[BUFSIZ];
989 int find_type;
990 int recalc_pos;
991
992 *lastWasTab = TRUE; /* flop trigger */
993
994 /* Make a local copy of the string -- up
995 * to the position of the cursor */
996 tmp = strncpy(matchBuf, command_ps, cursor);
997 tmp[cursor] = 0;
998
999 find_type = find_match(matchBuf, &recalc_pos);
1000
1001 /* Free up any memory already allocated */
1002 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001003
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001004#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001005 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001006 * then try completing this word as a username. */
1007
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001008 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001009 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001010#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001011 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001012 * in the current working directory that matches. */
1013 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001014 matches =
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001015 exe_n_cwd_tab_completion(matchBuf,
1016 &num_matches, find_type);
1017 /* Remove duplicate found */
1018 if(matches) {
1019 int i, j;
1020 /* bubble */
1021 for(i=0; i<(num_matches-1); i++)
1022 for(j=i+1; j<num_matches; j++)
1023 if(matches[i]!=0 && matches[j]!=0 &&
1024 strcmp(matches[i], matches[j])==0) {
1025 free(matches[j]);
1026 matches[j]=0;
1027 }
1028 j=num_matches;
1029 num_matches = 0;
1030 for(i=0; i<j; i++)
1031 if(matches[i]) {
1032 if(!strcmp(matches[i], "./"))
1033 matches[i][1]=0;
1034 else if(!strcmp(matches[i], "../"))
1035 matches[i][2]=0;
1036 matches[num_matches++]=matches[i];
1037 }
1038 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001039 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001040 if (!matches || num_matches > 1) {
1041 char *tmp1;
1042
Mark Whitley4e338752001-01-26 20:42:23 +00001043 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001044 if (!matches)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001045 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001046 /* sort */
1047 qsort(matches, num_matches, sizeof(char *), match_compare);
1048
1049 /* find minimal match */
1050 tmp = xstrdup(matches[0]);
1051 for (tmp1 = tmp; *tmp1; tmp1++)
1052 for (len_found = 1; len_found < num_matches; len_found++)
1053 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1054 *tmp1 = 0;
1055 break;
1056 }
1057 if (*tmp == 0) { /* have unique */
1058 free(tmp);
1059 return;
1060 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001061 } else { /* one match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001062 tmp = matches[0];
1063 /* for next completion current found */
1064 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001065 }
1066
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001067 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001068 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001069 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001070
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001071 /* before word for match */
1072 command_ps[cursor - recalc_pos] = 0;
1073 /* save tail line */
1074 strcpy(matchBuf, command_ps + cursor);
1075 /* add match */
1076 strcat(command_ps, tmp);
1077 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001078 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001079 /* back to begin word for match */
1080 input_backward(recalc_pos);
1081 /* new pos */
1082 recalc_pos = cursor + len_found;
1083 /* new len */
1084 len = strlen(command_ps);
1085 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001086 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001087 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001088 if (tmp != matches[0])
1089 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001090 } else {
1091 /* Ok -- the last char was a TAB. Since they
1092 * just hit TAB again, print a list of all the
1093 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001094 if (matches && num_matches > 0) {
1095 int i, col, l;
1096 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001097
1098 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001099 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001100 for (i = 0, col = 0; i < num_matches; i++) {
1101 l = strlen(matches[i]);
1102 if (l < 14)
1103 l = 14;
1104 printf("%-14s ", matches[i]);
1105 if ((l += 2) > 16)
1106 while (l % 16) {
1107 putchar(' ');
1108 l++;
1109 }
1110 col += l;
1111 col -= (col / cmdedit_termw) * cmdedit_termw;
1112 if (col > 60 && matches[i + 1] != NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +00001113 putchar('\n');
Erik Andersenf0657d32000-04-12 17:49:52 +00001114 col = 0;
1115 }
1116 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001117 /* Go to the next line and rewrite */
1118 putchar('\n');
1119 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001120 }
1121 }
1122}
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001123#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001124
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001125static void get_previous_history(struct history **hp, struct history *p)
Erik Andersenf0657d32000-04-12 17:49:52 +00001126{
Eric Andersen91a44002000-07-19 17:37:57 +00001127 if ((*hp)->s)
1128 free((*hp)->s);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001129 (*hp)->s = xstrdup(command_ps);
1130 *hp = p;
Erik Andersenf0657d32000-04-12 17:49:52 +00001131}
1132
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001133static inline void get_next_history(struct history **hp)
Erik Andersenf0657d32000-04-12 17:49:52 +00001134{
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001135 get_previous_history(hp, (*hp)->n);
Erik Andersenf0657d32000-04-12 17:49:52 +00001136}
1137
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001138enum {
1139 ESC = 27,
1140 DEL = 127,
1141};
1142
1143
Erik Andersen6273f652000-03-17 01:12:41 +00001144/*
1145 * This function is used to grab a character buffer
1146 * from the input file descriptor and allows you to
1147 * a string with full command editing (sortof like
1148 * a mini readline).
1149 *
1150 * The following standard commands are not implemented:
1151 * ESC-b -- Move back one word
1152 * ESC-f -- Move forward one word
1153 * ESC-d -- Delete back one word
1154 * ESC-h -- Delete forward one word
1155 * CTL-t -- Transpose two characters
1156 *
1157 * Furthermore, the "vi" command editing keys are not implemented.
1158 *
Erik Andersen6273f652000-03-17 01:12:41 +00001159 */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001160
Eric Andersen044228d2001-07-17 01:12:36 +00001161
1162int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001163{
1164
Erik Andersenc7c634b2000-03-19 05:28:55 +00001165 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001166 int lastWasTab = FALSE;
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001167 unsigned char c = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001168 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +00001169
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001170 /* prepare before init handlers */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001171 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001172 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001173 command_ps = command;
1174
Eric Andersen34506362001-08-02 05:02:46 +00001175 getTermSettings(0, (void *) &initial_settings);
1176 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1177 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1178 /* Turn off echoing and CTRL-C, so we can trap it */
1179 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001180#ifndef linux
Eric Andersen34506362001-08-02 05:02:46 +00001181 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1182 new_settings.c_cc[VMIN] = 1;
1183 new_settings.c_cc[VTIME] = 0;
1184 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001185# ifndef _POSIX_VDISABLE
1186# define _POSIX_VDISABLE '\0'
1187# endif
Eric Andersen34506362001-08-02 05:02:46 +00001188 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001189#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001190 command[0] = 0;
1191
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001192 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001193 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001194
Eric Andersen6faae7d2001-02-16 20:09:17 +00001195 /* Now initialize things */
1196 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001197 /* Print out the command prompt */
1198 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001199
Erik Andersenc7c634b2000-03-19 05:28:55 +00001200 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001201
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001202 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001203
Eric Andersen7467c8d2001-07-12 20:26:32 +00001204 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001205 /* if we can't read input then exit */
1206 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001207
Erik Andersen13456d12000-03-16 08:09:57 +00001208 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001209 case '\n':
1210 case '\r':
1211 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001212 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001213 break_out = 1;
1214 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001215 case 1:
1216 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001217 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001218 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001219 case 2:
1220 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001221 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001222 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001223 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001224 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001225 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001226 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001227 len = 0;
1228 lastWasTab = FALSE;
1229 put_prompt();
1230 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001231 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001232 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001233 * if the len=0 and no chars to delete */
1234 if (len == 0) {
Eric Andersened424db2001-04-23 15:28:28 +00001235prepare_to_die:
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001236#if !defined(CONFIG_ASH)
Mark Whitley4e338752001-01-26 20:42:23 +00001237 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001238 goto_new_line();
1239 /* cmdedit_reset_term() called in atexit */
1240 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001241#else
1242 break_out = -1; /* for control stoped jobs */
1243 break;
1244#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001245 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001246 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001247 }
1248 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001249 case 5:
1250 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001251 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001252 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001253 case 6:
1254 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001255 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001256 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001257 case '\b':
1258 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001259 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001260 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001261 break;
1262 case '\t':
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001263#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001264 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001265#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001266 break;
Eric Andersen65a07302002-04-13 13:26:49 +00001267 case 11:
1268 /* Control-k -- clear to end of line */
1269 *(command + cursor) = 0;
1270 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001271 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001272 break;
1273 case 12:
1274 {
1275 /* Control-l -- clear screen */
1276 int old_cursor = cursor;
1277 printf("\033[H");
1278 redraw(0, len-old_cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001279 }
1280 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001281 case 14:
1282 /* Control-n -- Get next command in history */
1283 if (hp && hp->n && hp->n->s) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001284 get_next_history(&hp);
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 }
1289 break;
1290 case 16:
1291 /* Control-p -- Get previous command from history */
1292 if (hp && hp->p) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001293 get_previous_history(&hp, hp->p);
Erik Andersenf0657d32000-04-12 17:49:52 +00001294 goto rewrite_line;
1295 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001296 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001297 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001298 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001299 case 21:
1300 /* Control-U -- Clear line before cursor */
1301 if (cursor) {
1302 strcpy(command, command + cursor);
1303 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001304 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001305 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001306 case ESC:{
1307 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001308 if (safe_read(0, &c, 1) < 1)
1309 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001310 /* different vt100 emulations */
1311 if (c == '[' || c == 'O') {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001312 if (safe_read(0, &c, 1) < 1)
1313 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001314 }
1315 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001316#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001317 case '\t': /* Alt-Tab */
1318
1319 input_tab(&lastWasTab);
1320 break;
1321#endif
1322 case 'A':
1323 /* Up Arrow -- Get previous command from history */
1324 if (hp && hp->p) {
1325 get_previous_history(&hp, hp->p);
1326 goto rewrite_line;
1327 } else {
1328 beep();
1329 }
1330 break;
1331 case 'B':
1332 /* Down Arrow -- Get next command in history */
1333 if (hp && hp->n && hp->n->s) {
1334 get_next_history(&hp);
1335 goto rewrite_line;
1336 } else {
1337 beep();
1338 }
1339 break;
1340
1341 /* Rewrite the line with the selected history item */
1342 rewrite_line:
1343 /* change command */
1344 len = strlen(strcpy(command, hp->s));
1345 /* redraw and go to end line */
1346 redraw(cmdedit_y, 0);
1347 break;
1348 case 'C':
1349 /* Right Arrow -- Move forward one character */
1350 input_forward();
1351 break;
1352 case 'D':
1353 /* Left Arrow -- Move back one character */
1354 input_backward(1);
1355 break;
1356 case '3':
1357 /* Delete */
1358 input_delete();
1359 break;
1360 case '1':
1361 case 'H':
1362 /* Home (Ctrl-A) */
1363 input_backward(cursor);
1364 break;
1365 case '4':
1366 case 'F':
1367 /* End (Ctrl-E) */
1368 input_end();
1369 break;
1370 default:
1371 if (!(c >= '1' && c <= '9'))
1372 c = 0;
1373 beep();
1374 }
1375 if (c >= '1' && c <= '9')
1376 do
Eric Andersen7467c8d2001-07-12 20:26:32 +00001377 if (safe_read(0, &c, 1) < 1)
1378 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001379 while (c != '~');
1380 break;
1381 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001382
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001383 default: /* If it's regular input, do the normal thing */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001384#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001385 /* Control-V -- Add non-printable symbol */
1386 if (c == 22) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001387 if (safe_read(0, &c, 1) < 1)
1388 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001389 if (c == 0) {
1390 beep();
1391 break;
1392 }
1393 } else
1394#endif
Eric Andersene5dfced2001-04-09 22:48:12 +00001395 if (!Isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001396 break;
1397
1398 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
1399 break;
1400
1401 len++;
1402
1403 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001404 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001405 *(command + cursor + 1) = 0;
1406 cmdedit_set_out_char(0);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001407 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001408 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001409
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001410 memmove(command + sc + 1, command + sc, len - sc);
1411 *(command + sc) = c;
1412 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001413 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001414 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001415 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001416 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001417 }
1418
Erik Andersenc7c634b2000-03-19 05:28:55 +00001419 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001420 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001421 if (break_out) /* Enter is the command terminator, no more input. */
1422 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001423
1424 if (c != '\t')
1425 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001426 }
1427
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001428 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001429 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001430
1431 /* Handle command history log */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001432 if (len) { /* no put empty line */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001433
1434 struct history *h = his_end;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001435 char *ss;
Mark Whitley4e338752001-01-26 20:42:23 +00001436
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001437 ss = xstrdup(command); /* duplicate */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001438
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001439 if (h == 0) {
Eric Andersen91a44002000-07-19 17:37:57 +00001440 /* No previous history -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001441 h = his_front = xmalloc(sizeof(struct history));
1442 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001443
1444 h->p = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001445 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001446 h->n->p = h;
1447 h->n->n = NULL;
1448 h->n->s = NULL;
1449 his_end = h->n;
1450 history_counter++;
1451 } else {
Eric Andersen91a44002000-07-19 17:37:57 +00001452 /* Add a new history command -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +00001453 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +00001454
1455 h->n->p = h;
1456 h->n->n = NULL;
1457 h->n->s = NULL;
Mark Whitley4e338752001-01-26 20:42:23 +00001458 h->s = ss;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001459 his_end = h->n;
1460
1461 /* After max history, remove the oldest command */
1462 if (history_counter >= MAX_HISTORY) {
1463
1464 struct history *p = his_front->n;
1465
1466 p->p = NULL;
1467 free(his_front->s);
1468 free(his_front);
1469 his_front = p;
1470 } else {
1471 history_counter++;
1472 }
Erik Andersen13456d12000-03-16 08:09:57 +00001473 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001474#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001475 num_ok_lines++;
1476#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001477 }
Eric Andersen044228d2001-07-17 01:12:36 +00001478 if(break_out>0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001479 command[len++] = '\n'; /* set '\n' */
1480 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001481 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001482#if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001483 input_tab(0); /* strong free */
1484#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001485#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001486 free(cmdedit_prompt);
1487#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001488 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001489 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001490}
1491
Eric Andersen7467c8d2001-07-12 20:26:32 +00001492
1493
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001494#endif /* CONFIG_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001495
1496
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001497#ifdef TEST
1498
Eric Andersene5dfced2001-04-09 22:48:12 +00001499const char *applet_name = "debug stuff usage";
1500const char *memory_exhausted = "Memory exhausted";
1501
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001502#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001503#include <locale.h>
1504#endif
1505
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001506int main(int argc, char **argv)
1507{
1508 char buff[BUFSIZ];
1509 char *prompt =
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001510#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001511 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001512\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001513\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001514#else
1515 "% ";
1516#endif
1517
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001518#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001519 setlocale(LC_ALL, "");
1520#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001521 while(1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001522 int l;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001523 cmdedit_read_input(prompt, buff);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001524 l = strlen(buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001525 if(l==0)
1526 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001527 if(l > 0 && buff[l-1] == '\n')
1528 buff[l-1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001529 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001530 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001531 printf("*** cmdedit_read_input() detect ^C\n");
1532 return 0;
1533}
1534
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001535#endif /* TEST */