blob: 73378e659f079719feec59fa1b2154a1889ad79c [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/* Maximum length of the linked list for the command line history */
Robert Griebl350d26b2002-12-03 22:45:46 +000093#ifndef CONFIG_FEATURE_COMMAND_HISTORY
94#define MAX_HISTORY 15
95#else
96#define MAX_HISTORY CONFIG_FEATURE_COMMAND_HISTORY
97#endif
98
Glenn L McGrath062c74f2002-11-27 09:29:49 +000099#if MAX_HISTORY < 1
100#warning cmdedit: You set MAX_HISTORY < 1. The history algorithm switched off.
101#else
102static char *history[MAX_HISTORY+1]; /* history + current */
103/* saved history lines */
104static int n_history;
105/* current pointer to history line */
106static int cur_history;
107#endif
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 */
Mark Whitley4e338752001-01-26 20:42:23 +0000119static
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000120volatile int handlers_sets = 0; /* Set next bites: */
121
Mark Whitley4e338752001-01-26 20:42:23 +0000122enum {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000123 SET_ATEXIT = 1, /* when atexit() has been called
124 and get euid,uid,gid to fast compare */
Eric Andersen7467c8d2001-07-12 20:26:32 +0000125 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
126 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000127};
Erik Andersen13456d12000-03-16 08:09:57 +0000128
Mark Whitley4e338752001-01-26 20:42:23 +0000129
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000130static int cmdedit_x; /* real x terminal position */
131static int cmdedit_y; /* pseudoreal y terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000132static int cmdedit_prmt_len; /* lenght prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +0000133
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000134static int cursor; /* required global for signal handler */
135static int len; /* --- "" - - "" - -"- --""-- --""--- */
136static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000137static
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000138#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000139 const
140#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000141char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000142
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000143#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000144static char *user_buf = "";
145static char *home_pwd_buf = "";
146static int my_euid;
147#endif
148
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000149#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000150static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000151static int num_ok_lines = 1;
152#endif
153
154
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000155#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000156
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000157#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000158static int my_euid;
159#endif
160
161static int my_uid;
162static int my_gid;
163
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000164#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000165
Eric Andersen95b52012001-08-02 09:52:40 +0000166/* It seems that libc5 doesn't know what a sighandler_t is... */
167#if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
168typedef void (*sighandler_t) (int);
169#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000170
Mark Whitley4e338752001-01-26 20:42:23 +0000171static void cmdedit_setwidth(int w, int redraw_flg);
Erik Andersen13456d12000-03-16 08:09:57 +0000172
Mark Whitley4e338752001-01-26 20:42:23 +0000173static void win_changed(int nsig)
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000174{
175 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen8d79ce82001-07-22 23:00:15 +0000176 static sighandler_t previous_SIGWINCH_handler; /* for reset */
Erik Andersen61677fe2000-04-13 01:18:56 +0000177
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000178 /* emulate || signal call */
179 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
Mark Whitley4e338752001-01-26 20:42:23 +0000180 ioctl(0, TIOCGWINSZ, &win);
181 if (win.ws_col > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000182 cmdedit_setwidth(win.ws_col, nsig == SIGWINCH);
183 }
Mark Whitley4e338752001-01-26 20:42:23 +0000184 }
Eric Andersen4bbdd782001-01-30 22:23:17 +0000185 /* Unix not all standart in recall signal */
Mark Whitley4e338752001-01-26 20:42:23 +0000186
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000187 if (nsig == -SIGWINCH) /* save previous handler */
Mark Whitley4e338752001-01-26 20:42:23 +0000188 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000189 else if (nsig == SIGWINCH) /* signaled called handler */
190 signal(SIGWINCH, win_changed); /* set for next call */
191 else /* nsig == 0 */
192 /* set previous handler */
193 signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
Mark Whitley4e338752001-01-26 20:42:23 +0000194}
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000195
196static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000197{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000198 if ((handlers_sets & SET_RESET_TERM) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000199/* sparc and other have broken termios support: use old termio handling. */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000200 setTermSettings(fileno(stdin), (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +0000201 handlers_sets &= ~SET_RESET_TERM;
202 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000203 if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
Mark Whitley4e338752001-01-26 20:42:23 +0000204 /* reset SIGWINCH handler to previous (default) */
205 win_changed(0);
206 handlers_sets &= ~SET_WCHG_HANDLERS;
207 }
208 fflush(stdout);
Erik Andersen13456d12000-03-16 08:09:57 +0000209}
210
Mark Whitley4e338752001-01-26 20:42:23 +0000211
Mark Whitley4e338752001-01-26 20:42:23 +0000212/* special for recount position for scroll and remove terminal margin effect */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000213static void cmdedit_set_out_char(int next_char)
214{
215
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000216 int c = (int)((unsigned char) command_ps[cursor]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000217
218 if (c == 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000219 c = ' '; /* destroy end char? */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000220#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersene5dfced2001-04-09 22:48:12 +0000221 if (!Isprint(c)) { /* Inverse put non-printable characters */
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000222 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000223 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000224 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000225 c += '@';
226 if (c == 127)
227 c = '?';
228 printf("\033[7m%c\033[0m", c);
229 } else
230#endif
231 putchar(c);
232 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000233 /* terminal is scrolled down */
234 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000235 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000236
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000237 if (!next_char)
Mark Whitley4e338752001-01-26 20:42:23 +0000238 next_char = ' ';
239 /* destroy "(auto)margin" */
240 putchar(next_char);
241 putchar('\b');
242 }
243 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000244}
245
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000246/* Move to end line. Bonus: rewrite line from cursor */
247static void input_end(void)
248{
249 while (cursor < len)
250 cmdedit_set_out_char(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000251}
252
253/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000254static void goto_new_line(void)
255{
Mark Whitley4e338752001-01-26 20:42:23 +0000256 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000257 if (cmdedit_x)
258 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000259}
260
261
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000262static inline void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000263{
Robert Grieblb2301592002-07-30 23:13:51 +0000264 if ( s )
265 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000266}
267static inline void beep(void)
268{
269 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000270}
271
Mark Whitley4e338752001-01-26 20:42:23 +0000272/* Move back one charactor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000273/* special for slow terminal */
274static void input_backward(int num)
275{
276 if (num > cursor)
277 num = cursor;
Eric Andersene5dfced2001-04-09 22:48:12 +0000278 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000279
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000280 if (cmdedit_x >= num) { /* no to up line */
281 cmdedit_x -= num;
282 if (num < 4)
283 while (num-- > 0)
284 putchar('\b');
285
286 else
287 printf("\033[%dD", num);
288 } else {
289 int count_y;
290
291 if (cmdedit_x) {
292 putchar('\r'); /* back to first terminal pos. */
293 num -= cmdedit_x; /* set previous backward */
294 }
295 count_y = 1 + num / cmdedit_termw;
296 printf("\033[%dA", count_y);
297 cmdedit_y -= count_y;
298 /* require forward after uping */
299 cmdedit_x = cmdedit_termw * count_y - num;
300 printf("\033[%dC", cmdedit_x); /* set term cursor */
Erik Andersen13456d12000-03-16 08:09:57 +0000301 }
Erik Andersen13456d12000-03-16 08:09:57 +0000302}
303
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000304static void put_prompt(void)
305{
306 out1str(cmdedit_prompt);
307 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
308 cursor = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000309 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000310}
311
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000312#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000313static void parse_prompt(const char *prmt_ptr)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000314{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000315 cmdedit_prompt = prmt_ptr;
316 cmdedit_prmt_len = strlen(prmt_ptr);
317 put_prompt();
318}
319#else
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000320static void parse_prompt(const char *prmt_ptr)
321{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000322 int prmt_len = 0;
323 int sub_len = 0;
Eric Andersene5dfced2001-04-09 22:48:12 +0000324 char flg_not_length = '[';
325 char *prmt_mem_ptr = xcalloc(1, 1);
326 char *pwd_buf = xgetcwd(0);
327 char buf2[PATH_MAX + 1];
328 char buf[2];
329 char c;
330 char *pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000331
Eric Andersen5f265b72001-05-11 16:58:46 +0000332 if (!pwd_buf) {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000333 pwd_buf=(char *)unknown;
Eric Andersen5f265b72001-05-11 16:58:46 +0000334 }
335
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000336 while (*prmt_ptr) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000337 pbuf = buf;
338 pbuf[1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000339 c = *prmt_ptr++;
340 if (c == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000341 const char *cp = prmt_ptr;
342 int l;
343
344 c = process_escape_sequence(&prmt_ptr);
345 if(prmt_ptr==cp) {
346 if (*cp == 0)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000347 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000348 c = *prmt_ptr++;
349 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000350#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000351 case 'u':
352 pbuf = user_buf;
353 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000354#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000355 case 'h':
356 pbuf = hostname_buf;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000357 if (pbuf == 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000358 pbuf = xcalloc(256, 1);
359 if (gethostname(pbuf, 255) < 0) {
360 strcpy(pbuf, "?");
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000361 } else {
Eric Andersene5dfced2001-04-09 22:48:12 +0000362 char *s = strchr(pbuf, '.');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000363
364 if (s)
365 *s = 0;
366 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000367 hostname_buf = pbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000368 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000369 break;
370 case '$':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000371 c = my_euid == 0 ? '#' : '$';
372 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000373#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersene5dfced2001-04-09 22:48:12 +0000374 case 'w':
375 pbuf = pwd_buf;
376 l = strlen(home_pwd_buf);
377 if (home_pwd_buf[0] != 0 &&
378 strncmp(home_pwd_buf, pbuf, l) == 0 &&
379 (pbuf[l]=='/' || pbuf[l]=='\0') &&
380 strlen(pwd_buf+l)<PATH_MAX) {
381 pbuf = buf2;
382 *pbuf = '~';
383 strcpy(pbuf+1, pwd_buf+l);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000384 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000385 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000386#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000387 case 'W':
388 pbuf = pwd_buf;
389 cp = strrchr(pbuf,'/');
390 if ( (cp != NULL) && (cp != pbuf) )
391 pbuf += (cp-pbuf)+1;
392 break;
393 case '!':
394 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
395 break;
396 case 'e': case 'E': /* \e \E = \033 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000397 c = '\033';
398 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000399 case 'x': case 'X':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000400 for (l = 0; l < 3;) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000401 int h;
402 buf2[l++] = *prmt_ptr;
403 buf2[l] = 0;
404 h = strtol(buf2, &pbuf, 16);
405 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000406 l--;
407 break;
408 }
409 prmt_ptr++;
410 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000411 buf2[l] = 0;
412 c = (char)strtol(buf2, 0, 16);
413 if(c==0)
414 c = '?';
415 pbuf = buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000416 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000417 case '[': case ']':
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000418 if (c == flg_not_length) {
419 flg_not_length = flg_not_length == '[' ? ']' : '[';
420 continue;
421 }
422 break;
Eric Andersene5dfced2001-04-09 22:48:12 +0000423 }
424 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000425 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000426 if(pbuf == buf)
427 *pbuf = c;
428 prmt_len += strlen(pbuf);
429 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430 if (flg_not_length == ']')
431 sub_len++;
432 }
Eric Andersen8f697842001-07-02 15:36:57 +0000433 if(pwd_buf!=(char *)unknown)
434 free(pwd_buf);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000435 cmdedit_prompt = prmt_mem_ptr;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000436 cmdedit_prmt_len = prmt_len - sub_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000437 put_prompt();
438}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000439#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000440
441
442/* draw promt, editor line, and clear tail */
443static void redraw(int y, int back_cursor)
444{
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000445 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000446 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000447 putchar('\r');
448 put_prompt();
449 input_end(); /* rewrite */
450 printf("\033[J"); /* destroy tail after cursor */
451 input_backward(back_cursor);
452}
453
Erik Andersenf0657d32000-04-12 17:49:52 +0000454/* Delete the char in front of the cursor */
Mark Whitley4e338752001-01-26 20:42:23 +0000455static void input_delete(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000456{
Mark Whitley4e338752001-01-26 20:42:23 +0000457 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000458
Mark Whitley4e338752001-01-26 20:42:23 +0000459 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000460 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000461
462 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000463 len--;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000464 input_end(); /* rewtite new line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000465 cmdedit_set_out_char(0); /* destroy end char */
466 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000467}
468
Mark Whitley4e338752001-01-26 20:42:23 +0000469/* Delete the char in back of the cursor */
470static void input_backspace(void)
471{
472 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000473 input_backward(1);
474 input_delete();
Mark Whitley4e338752001-01-26 20:42:23 +0000475 }
476}
477
478
Erik Andersenf0657d32000-04-12 17:49:52 +0000479/* Move forward one charactor */
Mark Whitley4e338752001-01-26 20:42:23 +0000480static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000481{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000482 if (cursor < len)
483 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000484}
485
486
Mark Whitley4e338752001-01-26 20:42:23 +0000487static void cmdedit_setwidth(int w, int redraw_flg)
488{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000489 cmdedit_termw = cmdedit_prmt_len + 2;
Eric Andersen6faae7d2001-02-16 20:09:17 +0000490 if (w <= cmdedit_termw) {
491 cmdedit_termw = cmdedit_termw % w;
492 }
Mark Whitley4e338752001-01-26 20:42:23 +0000493 if (w > cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000494 cmdedit_termw = w;
495
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000496 if (redraw_flg) {
497 /* new y for current cursor */
498 int new_y = (cursor + cmdedit_prmt_len) / w;
Mark Whitley4e338752001-01-26 20:42:23 +0000499
500 /* redraw */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000501 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
502 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +0000503 }
Eric Andersen6faae7d2001-02-16 20:09:17 +0000504 }
Mark Whitley4e338752001-01-26 20:42:23 +0000505}
506
Eric Andersen7467c8d2001-07-12 20:26:32 +0000507static void cmdedit_init(void)
Mark Whitley4e338752001-01-26 20:42:23 +0000508{
Eric Andersen61173a52001-03-19 17:48:55 +0000509 cmdedit_prmt_len = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000510 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
511 /* emulate usage handler to set handler and call yours work */
Mark Whitley4e338752001-01-26 20:42:23 +0000512 win_changed(-SIGWINCH);
513 handlers_sets |= SET_WCHG_HANDLERS;
514 }
515
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000516 if ((handlers_sets & SET_ATEXIT) == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000517#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518 struct passwd *entry;
519
520 my_euid = geteuid();
521 entry = getpwuid(my_euid);
522 if (entry) {
523 user_buf = xstrdup(entry->pw_name);
524 home_pwd_buf = xstrdup(entry->pw_dir);
525 }
526#endif
527
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000528#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000529
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000530#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000531 my_euid = geteuid();
532#endif
533 my_uid = getuid();
534 my_gid = getgid();
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000535#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000536 handlers_sets |= SET_ATEXIT;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000537 atexit(cmdedit_reset_term); /* be sure to do this only once */
Mark Whitley4e338752001-01-26 20:42:23 +0000538 }
Mark Whitley4e338752001-01-26 20:42:23 +0000539}
Erik Andersenf0657d32000-04-12 17:49:52 +0000540
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000541#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000542
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000543static int is_execute(const struct stat *st)
Erik Andersen6273f652000-03-17 01:12:41 +0000544{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000545 if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
546 (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
547 (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
548 (st->st_mode & S_IXOTH)) return TRUE;
549 return FALSE;
Erik Andersen6273f652000-03-17 01:12:41 +0000550}
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000551
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000552#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000553
554static char **username_tab_completion(char *ud, int *num_matches)
555{
556 struct passwd *entry;
557 int userlen;
558 char *temp;
559
560
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000561 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000562 userlen = strlen(ud);
563
564 if (num_matches == 0) { /* "~/..." or "~user/..." */
565 char *sav_ud = ud - 1;
566 char *home = 0;
567
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000568 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000569 home = home_pwd_buf;
570 } else {
571 /* "~user/..." */
572 temp = strchr(ud, '/');
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000573 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000574 entry = getpwnam(ud);
575 *temp = '/'; /* restore ~user/... */
576 ud = temp;
577 if (entry)
578 home = entry->pw_dir;
579 }
580 if (home) {
581 if ((userlen + strlen(home) + 1) < BUFSIZ) {
582 char temp2[BUFSIZ]; /* argument size */
583
584 /* /home/user/... */
585 sprintf(temp2, "%s%s", home, ud);
586 strcpy(sav_ud, temp2);
587 }
588 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000589 return 0; /* void, result save to argument :-) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000590 } else {
591 /* "~[^/]*" */
592 char **matches = (char **) NULL;
593 int nm = 0;
594
595 setpwent();
596
597 while ((entry = getpwent()) != NULL) {
598 /* Null usernames should result in all users as possible completions. */
599 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
600
Robert Griebld378c312002-07-19 00:05:54 +0000601 bb_asprintf(&temp, "~%s/", entry->pw_name);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000602 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
603
604 matches[nm++] = temp;
605 }
606 }
607
608 endpwent();
609 (*num_matches) = nm;
610 return (matches);
611 }
612}
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000613#endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000614
615enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000616 FIND_EXE_ONLY = 0,
617 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000618 FIND_FILE_ONLY = 2,
619};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000620
Mark Whitley4e338752001-01-26 20:42:23 +0000621static int path_parse(char ***p, int flags)
622{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000623 int npth;
Mark Whitley4e338752001-01-26 20:42:23 +0000624 char *tmp;
625 char *pth;
626
Mark Whitley4e338752001-01-26 20:42:23 +0000627 /* if not setenv PATH variable, to search cur dir "." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000628 if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 ||
629 /* PATH=<empty> or PATH=:<empty> */
630 *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
Mark Whitley4e338752001-01-26 20:42:23 +0000631 return 1;
632 }
633
634 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000635 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000636
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000637 for (;;) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000638 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000639 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000640 if (tmp) {
641 if (*++tmp == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000642 break; /* :<empty> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000643 } else
Mark Whitley4e338752001-01-26 20:42:23 +0000644 break;
645 }
646
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000647 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000648
649 tmp = pth;
650 (*p)[0] = xstrdup(tmp);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000651 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000652
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000653 for (;;) {
Mark Whitley4e338752001-01-26 20:42:23 +0000654 tmp = strchr(tmp, ':');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000655 if (tmp) {
656 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
657 if (*++tmp == 0)
658 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000659 } else
660 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000661 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000662 }
663
664 return npth;
665}
666
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000667static char *add_quote_for_spec_chars(char *found)
Erik Andersen6273f652000-03-17 01:12:41 +0000668{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000669 int l = 0;
670 char *s = xmalloc((strlen(found) + 1) * 2);
671
672 while (*found) {
673 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
674 s[l++] = '\\';
675 s[l++] = *found++;
676 }
677 s[l] = 0;
678 return s;
679}
680
681static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000682 int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000683{
684
685 char **matches = 0;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000686 DIR *dir;
687 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000688 char dirbuf[BUFSIZ];
689 int nm = *num_matches;
690 struct stat st;
691 char *path1[1];
692 char **paths = path1;
693 int npaths;
694 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000695 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000696 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000697
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000698 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000699
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000700 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000701 /* no dir, if flags==EXE_ONLY - get paths, else "." */
702 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000703 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000704 } else {
705 /* with dir */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000706 /* save for change */
707 strcpy(dirbuf, command);
708 /* set dir only */
709 dirbuf[(pfind - command) + 1] = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000710#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000711 if (dirbuf[0] == '~') /* ~/... or ~user/... */
712 username_tab_completion(dirbuf, 0);
713#endif
714 /* "strip" dirname in command */
715 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000716
Mark Whitley4e338752001-01-26 20:42:23 +0000717 paths[0] = dirbuf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000718 npaths = 1; /* only 1 dir */
Mark Whitley4e338752001-01-26 20:42:23 +0000719 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000720
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000721 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000722
Mark Whitley4e338752001-01-26 20:42:23 +0000723 dir = opendir(paths[i]);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000724 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000725 continue;
726
727 while ((next = readdir(dir)) != NULL) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000728 char *str_found = next->d_name;
729
Mark Whitley4e338752001-01-26 20:42:23 +0000730 /* matched ? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000731 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000732 continue;
733 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000734 if (*str_found == '.' && *pfind == 0) {
735 if (*paths[i] == '/' && paths[i][1] == 0
736 && str_found[1] == 0) str_found = ""; /* only "/" */
737 else
Mark Whitley4e338752001-01-26 20:42:23 +0000738 continue;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000739 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000740 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000741 /* hmm, remover in progress? */
Eric Andersene5dfced2001-04-09 22:48:12 +0000742 if (stat(found, &st) < 0)
743 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000744 /* find with dirs ? */
745 if (paths[i] != dirbuf)
746 strcpy(found, next->d_name); /* only name */
Mark Whitley4e338752001-01-26 20:42:23 +0000747 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000748 /* name is directory */
Eric Andersene5dfced2001-04-09 22:48:12 +0000749 str_found = found;
750 found = concat_path_file(found, "");
751 free(str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000752 str_found = add_quote_for_spec_chars(found);
Mark Whitley4e338752001-01-26 20:42:23 +0000753 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000754 /* not put found file if search only dirs for cd */
Eric Andersene5dfced2001-04-09 22:48:12 +0000755 if (type == FIND_DIR_ONLY)
756 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000757 str_found = add_quote_for_spec_chars(found);
758 if (type == FIND_FILE_ONLY ||
Matt Kraai1f0c4362001-12-20 23:13:26 +0000759 (type == FIND_EXE_ONLY && is_execute(&st)))
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000760 strcat(str_found, " ");
761 }
Mark Whitley4e338752001-01-26 20:42:23 +0000762 /* Add it to the list */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000763 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
764
765 matches[nm++] = str_found;
Eric Andersene5dfced2001-04-09 22:48:12 +0000766cont:
767 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000768 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000769 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000770 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000771 if (paths != path1) {
772 free(paths[0]); /* allocated memory only in first member */
773 free(paths);
774 }
Mark Whitley4e338752001-01-26 20:42:23 +0000775 *num_matches = nm;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000776 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000777}
Erik Andersenf0657d32000-04-12 17:49:52 +0000778
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000779static int match_compare(const void *a, const void *b)
780{
781 return strcmp(*(char **) a, *(char **) b);
782}
783
784
785
786#define QUOT (UCHAR_MAX+1)
787
788#define collapse_pos(is, in) { \
Eric Andersen889a3012002-03-20 14:31:15 +0000789 memcpy(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
790 memcpy(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000791
792static int find_match(char *matchBuf, int *len_with_quotes)
793{
794 int i, j;
795 int command_mode;
796 int c, c2;
797 int int_buf[BUFSIZ + 1];
798 int pos_buf[BUFSIZ + 1];
799
800 /* set to integer dimension characters and own positions */
801 for (i = 0;; i++) {
802 int_buf[i] = (int) ((unsigned char) matchBuf[i]);
803 if (int_buf[i] == 0) {
804 pos_buf[i] = -1; /* indicator end line */
805 break;
806 } else
807 pos_buf[i] = i;
808 }
809
810 /* mask \+symbol and convert '\t' to ' ' */
811 for (i = j = 0; matchBuf[i]; i++, j++)
812 if (matchBuf[i] == '\\') {
813 collapse_pos(j, j + 1);
814 int_buf[j] |= QUOT;
815 i++;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000816#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000817 if (matchBuf[i] == '\t') /* algorithm equivalent */
818 int_buf[j] = ' ' | QUOT;
819#endif
820 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000821#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000822 else if (matchBuf[i] == '\t')
823 int_buf[j] = ' ';
824#endif
825
826 /* mask "symbols" or 'symbols' */
827 c2 = 0;
828 for (i = 0; int_buf[i]; i++) {
829 c = int_buf[i];
830 if (c == '\'' || c == '"') {
831 if (c2 == 0)
832 c2 = c;
833 else {
834 if (c == c2)
835 c2 = 0;
836 else
837 int_buf[i] |= QUOT;
838 }
839 } else if (c2 != 0 && c != '$')
840 int_buf[i] |= QUOT;
841 }
842
843 /* skip commands with arguments if line have commands delimiters */
844 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
845 for (i = 0; int_buf[i]; i++) {
846 c = int_buf[i];
847 c2 = int_buf[i + 1];
848 j = i ? int_buf[i - 1] : -1;
849 command_mode = 0;
850 if (c == ';' || c == '&' || c == '|') {
851 command_mode = 1 + (c == c2);
852 if (c == '&') {
853 if (j == '>' || j == '<')
854 command_mode = 0;
855 } else if (c == '|' && j == '>')
856 command_mode = 0;
857 }
858 if (command_mode) {
859 collapse_pos(0, i + command_mode);
860 i = -1; /* hack incremet */
861 }
862 }
863 /* collapse `command...` */
864 for (i = 0; int_buf[i]; i++)
865 if (int_buf[i] == '`') {
866 for (j = i + 1; int_buf[j]; j++)
867 if (int_buf[j] == '`') {
868 collapse_pos(i, j + 1);
869 j = 0;
870 break;
871 }
872 if (j) {
873 /* not found close ` - command mode, collapse all previous */
874 collapse_pos(0, i + 1);
875 break;
876 } else
877 i--; /* hack incremet */
878 }
879
880 /* collapse (command...(command...)...) or {command...{command...}...} */
881 c = 0; /* "recursive" level */
882 c2 = 0;
883 for (i = 0; int_buf[i]; i++)
884 if (int_buf[i] == '(' || int_buf[i] == '{') {
885 if (int_buf[i] == '(')
886 c++;
887 else
888 c2++;
889 collapse_pos(0, i + 1);
890 i = -1; /* hack incremet */
891 }
892 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
893 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
894 if (int_buf[i] == ')')
895 c--;
896 else
897 c2--;
898 collapse_pos(0, i + 1);
899 i = -1; /* hack incremet */
900 }
901
902 /* skip first not quote space */
903 for (i = 0; int_buf[i]; i++)
904 if (int_buf[i] != ' ')
905 break;
906 if (i)
907 collapse_pos(0, i);
908
909 /* set find mode for completion */
910 command_mode = FIND_EXE_ONLY;
911 for (i = 0; int_buf[i]; i++)
912 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
913 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000914 && matchBuf[pos_buf[0]]=='c'
915 && matchBuf[pos_buf[1]]=='d' )
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000916 command_mode = FIND_DIR_ONLY;
917 else {
918 command_mode = FIND_FILE_ONLY;
919 break;
920 }
921 }
922 /* "strlen" */
923 for (i = 0; int_buf[i]; i++);
924 /* find last word */
925 for (--i; i >= 0; i--) {
926 c = int_buf[i];
927 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
928 collapse_pos(0, i + 1);
929 break;
930 }
931 }
932 /* skip first not quoted '\'' or '"' */
933 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
934 /* collapse quote or unquote // or /~ */
Mark Whitley7e5291f2001-03-08 19:31:12 +0000935 while ((int_buf[i] & ~QUOT) == '/' &&
936 ((int_buf[i + 1] & ~QUOT) == '/'
937 || (int_buf[i + 1] & ~QUOT) == '~')) {
938 i++;
939 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000940
941 /* set only match and destroy quotes */
942 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000943 for (c = 0; pos_buf[i] >= 0; i++) {
944 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000945 j = pos_buf[i] + 1;
946 }
Eric Andersen4f990532001-05-31 17:15:57 +0000947 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000948 /* old lenght matchBuf with quotes symbols */
949 *len_with_quotes = j ? j - pos_buf[0] : 0;
950
951 return command_mode;
952}
953
954
955static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000956{
957 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000958 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +0000959 static char **matches;
960
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000961 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +0000962 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000963 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +0000964 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000965 free(matches);
966 matches = (char **) NULL;
967 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000968 return;
969 }
Matt Kraai1f0c4362001-12-20 23:13:26 +0000970 if (! *lastWasTab) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000971
972 char *tmp;
973 int len_found;
974 char matchBuf[BUFSIZ];
975 int find_type;
976 int recalc_pos;
977
978 *lastWasTab = TRUE; /* flop trigger */
979
980 /* Make a local copy of the string -- up
981 * to the position of the cursor */
982 tmp = strncpy(matchBuf, command_ps, cursor);
983 tmp[cursor] = 0;
984
985 find_type = find_match(matchBuf, &recalc_pos);
986
987 /* Free up any memory already allocated */
988 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +0000989
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000990#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000991 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000992 * then try completing this word as a username. */
993
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000994 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +0000995 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +0000996#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000997 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +0000998 * in the current working directory that matches. */
999 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001000 matches =
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001001 exe_n_cwd_tab_completion(matchBuf,
1002 &num_matches, find_type);
1003 /* Remove duplicate found */
1004 if(matches) {
1005 int i, j;
1006 /* bubble */
1007 for(i=0; i<(num_matches-1); i++)
1008 for(j=i+1; j<num_matches; j++)
1009 if(matches[i]!=0 && matches[j]!=0 &&
1010 strcmp(matches[i], matches[j])==0) {
1011 free(matches[j]);
1012 matches[j]=0;
1013 }
1014 j=num_matches;
1015 num_matches = 0;
1016 for(i=0; i<j; i++)
1017 if(matches[i]) {
1018 if(!strcmp(matches[i], "./"))
1019 matches[i][1]=0;
1020 else if(!strcmp(matches[i], "../"))
1021 matches[i][2]=0;
1022 matches[num_matches++]=matches[i];
1023 }
1024 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001025 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001026 if (!matches || num_matches > 1) {
1027 char *tmp1;
1028
Mark Whitley4e338752001-01-26 20:42:23 +00001029 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001030 if (!matches)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001031 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001032 /* sort */
1033 qsort(matches, num_matches, sizeof(char *), match_compare);
1034
1035 /* find minimal match */
1036 tmp = xstrdup(matches[0]);
1037 for (tmp1 = tmp; *tmp1; tmp1++)
1038 for (len_found = 1; len_found < num_matches; len_found++)
1039 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1040 *tmp1 = 0;
1041 break;
1042 }
1043 if (*tmp == 0) { /* have unique */
1044 free(tmp);
1045 return;
1046 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001047 } else { /* one match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001048 tmp = matches[0];
1049 /* for next completion current found */
1050 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001051 }
1052
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001053 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001054 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001055 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001056
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001057 /* before word for match */
1058 command_ps[cursor - recalc_pos] = 0;
1059 /* save tail line */
1060 strcpy(matchBuf, command_ps + cursor);
1061 /* add match */
1062 strcat(command_ps, tmp);
1063 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001064 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001065 /* back to begin word for match */
1066 input_backward(recalc_pos);
1067 /* new pos */
1068 recalc_pos = cursor + len_found;
1069 /* new len */
1070 len = strlen(command_ps);
1071 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001072 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001073 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001074 if (tmp != matches[0])
1075 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001076 } else {
1077 /* Ok -- the last char was a TAB. Since they
1078 * just hit TAB again, print a list of all the
1079 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001080 if (matches && num_matches > 0) {
1081 int i, col, l;
1082 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001083
1084 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001085 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001086 for (i = 0, col = 0; i < num_matches; i++) {
1087 l = strlen(matches[i]);
1088 if (l < 14)
1089 l = 14;
1090 printf("%-14s ", matches[i]);
1091 if ((l += 2) > 16)
1092 while (l % 16) {
1093 putchar(' ');
1094 l++;
1095 }
1096 col += l;
1097 col -= (col / cmdedit_termw) * cmdedit_termw;
1098 if (col > 60 && matches[i + 1] != NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +00001099 putchar('\n');
Erik Andersenf0657d32000-04-12 17:49:52 +00001100 col = 0;
1101 }
1102 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001103 /* Go to the next line and rewrite */
1104 putchar('\n');
1105 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001106 }
1107 }
1108}
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001109#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001110
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001111#if MAX_HISTORY >= 1
1112static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001113{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001114 if(command_ps[0] != 0 || history[cur_history] == 0) {
1115 free(history[cur_history]);
1116 history[cur_history] = xstrdup(command_ps);
1117 }
1118 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +00001119}
1120
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001121static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001122{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001123 int ch = cur_history;
1124
1125 if (ch < n_history) {
1126 get_previous_history(); /* save the current history line */
1127 return (cur_history = ch+1);
1128 } else {
1129 beep();
1130 return 0;
1131 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001132}
Robert Griebl350d26b2002-12-03 22:45:46 +00001133
1134
1135extern void load_history ( char *fromfile )
1136{
1137#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
1138 FILE *fp;
1139
1140 // cleanup old
1141 while ( n_history ) {
1142 if ( history [n_history - 1] )
1143 free ( history [n_history - 1] );
1144 n_history--;
1145 }
1146
1147 if (( fp = fopen ( fromfile, "r" ))) {
1148 char buffer [256];
1149 int i, l;
1150
1151 for ( i = 0; i < MAX_HISTORY; i++ ) {
1152 if ( !fgets ( buffer, sizeof( buffer ) - 1, fp ))
1153 break;
1154 l = xstrlen ( buffer );
1155 if ( l && buffer [l - 1] == '\n' )
1156 buffer [l - 1] = 0;
1157 history [n_history++] = xstrdup ( buffer );
1158 }
1159 fclose ( fp );
1160 }
1161 cur_history = n_history;
1162#endif
1163}
1164
1165extern void save_history ( char *tofile )
1166{
1167#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
1168 FILE *fp = fopen ( tofile, "w" );
1169
1170 if ( fp ) {
1171 int i;
1172
1173 for ( i = 0; i < n_history; i++ ) {
1174 fputs ( history [i], fp );
1175 fputc ( '\n', fp );
1176 }
1177 fclose ( fp );
1178 }
1179#endif
1180}
1181
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001182#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001183
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001184enum {
1185 ESC = 27,
1186 DEL = 127,
1187};
1188
1189
Erik Andersen6273f652000-03-17 01:12:41 +00001190/*
1191 * This function is used to grab a character buffer
1192 * from the input file descriptor and allows you to
1193 * a string with full command editing (sortof like
1194 * a mini readline).
1195 *
1196 * The following standard commands are not implemented:
1197 * ESC-b -- Move back one word
1198 * ESC-f -- Move forward one word
1199 * ESC-d -- Delete back one word
1200 * ESC-h -- Delete forward one word
1201 * CTL-t -- Transpose two characters
1202 *
1203 * Furthermore, the "vi" command editing keys are not implemented.
1204 *
Erik Andersen6273f652000-03-17 01:12:41 +00001205 */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001206
Eric Andersen044228d2001-07-17 01:12:36 +00001207
1208int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001209{
1210
Erik Andersenc7c634b2000-03-19 05:28:55 +00001211 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001212 int lastWasTab = FALSE;
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001213 unsigned char c = 0;
Erik Andersen13456d12000-03-16 08:09:57 +00001214
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001215 /* prepare before init handlers */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001216 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001217 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001218 command_ps = command;
1219
Eric Andersen34506362001-08-02 05:02:46 +00001220 getTermSettings(0, (void *) &initial_settings);
1221 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1222 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1223 /* Turn off echoing and CTRL-C, so we can trap it */
1224 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001225#ifndef linux
Eric Andersen34506362001-08-02 05:02:46 +00001226 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1227 new_settings.c_cc[VMIN] = 1;
1228 new_settings.c_cc[VTIME] = 0;
1229 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001230# ifndef _POSIX_VDISABLE
1231# define _POSIX_VDISABLE '\0'
1232# endif
Eric Andersen34506362001-08-02 05:02:46 +00001233 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001234#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001235 command[0] = 0;
1236
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001237 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001238 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001239
Eric Andersen6faae7d2001-02-16 20:09:17 +00001240 /* Now initialize things */
1241 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001242 /* Print out the command prompt */
1243 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001244
Erik Andersenc7c634b2000-03-19 05:28:55 +00001245 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001246
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001247 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001248
Eric Andersen7467c8d2001-07-12 20:26:32 +00001249 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001250 /* if we can't read input then exit */
1251 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001252
Erik Andersen13456d12000-03-16 08:09:57 +00001253 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001254 case '\n':
1255 case '\r':
1256 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001257 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001258 break_out = 1;
1259 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001260 case 1:
1261 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001262 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001263 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001264 case 2:
1265 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001266 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001267 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001268 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001269 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001270 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001271 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001272 len = 0;
1273 lastWasTab = FALSE;
1274 put_prompt();
1275 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001276 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001277 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001278 * if the len=0 and no chars to delete */
1279 if (len == 0) {
Eric Andersened424db2001-04-23 15:28:28 +00001280prepare_to_die:
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001281#if !defined(CONFIG_ASH)
Mark Whitley4e338752001-01-26 20:42:23 +00001282 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001283 goto_new_line();
1284 /* cmdedit_reset_term() called in atexit */
1285 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001286#else
1287 break_out = -1; /* for control stoped jobs */
1288 break;
1289#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001290 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001291 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001292 }
1293 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001294 case 5:
1295 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001296 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001297 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001298 case 6:
1299 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001300 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001301 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001302 case '\b':
1303 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001304 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001305 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001306 break;
1307 case '\t':
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001308#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001309 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001310#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001311 break;
Eric Andersen65a07302002-04-13 13:26:49 +00001312 case 11:
1313 /* Control-k -- clear to end of line */
1314 *(command + cursor) = 0;
1315 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001316 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001317 break;
1318 case 12:
Eric Andersen65a07302002-04-13 13:26:49 +00001319 /* Control-l -- clear screen */
Eric Andersen65a07302002-04-13 13:26:49 +00001320 printf("\033[H");
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001321 redraw(0, len-cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001322 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001323#if MAX_HISTORY >= 1
Erik Andersenf0657d32000-04-12 17:49:52 +00001324 case 14:
1325 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001326 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001327 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001328 break;
1329 case 16:
1330 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001331 if (cur_history > 0) {
1332 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001333 goto rewrite_line;
1334 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001335 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001336 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001337 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001338#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001339 case 21:
1340 /* Control-U -- Clear line before cursor */
1341 if (cursor) {
1342 strcpy(command, command + cursor);
1343 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001344 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001345 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001346 case ESC:{
1347 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001348 if (safe_read(0, &c, 1) < 1)
1349 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001350 /* different vt100 emulations */
1351 if (c == '[' || c == 'O') {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001352 if (safe_read(0, &c, 1) < 1)
1353 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001354 }
1355 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001356#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001357 case '\t': /* Alt-Tab */
1358
1359 input_tab(&lastWasTab);
1360 break;
1361#endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001362#if MAX_HISTORY >= 1
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001363 case 'A':
1364 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001365 if (cur_history > 0) {
1366 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001367 goto rewrite_line;
1368 } else {
1369 beep();
1370 }
1371 break;
1372 case 'B':
1373 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001374 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001375 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001376 /* Rewrite the line with the selected history item */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001377rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001378 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001379 len = strlen(strcpy(command, history[cur_history]));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001380 /* redraw and go to end line */
1381 redraw(cmdedit_y, 0);
1382 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001383#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001384 case 'C':
1385 /* Right Arrow -- Move forward one character */
1386 input_forward();
1387 break;
1388 case 'D':
1389 /* Left Arrow -- Move back one character */
1390 input_backward(1);
1391 break;
1392 case '3':
1393 /* Delete */
1394 input_delete();
1395 break;
1396 case '1':
1397 case 'H':
1398 /* Home (Ctrl-A) */
1399 input_backward(cursor);
1400 break;
1401 case '4':
1402 case 'F':
1403 /* End (Ctrl-E) */
1404 input_end();
1405 break;
1406 default:
1407 if (!(c >= '1' && c <= '9'))
1408 c = 0;
1409 beep();
1410 }
1411 if (c >= '1' && c <= '9')
1412 do
Eric Andersen7467c8d2001-07-12 20:26:32 +00001413 if (safe_read(0, &c, 1) < 1)
1414 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001415 while (c != '~');
1416 break;
1417 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001418
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001419 default: /* If it's regular input, do the normal thing */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001420#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001421 /* Control-V -- Add non-printable symbol */
1422 if (c == 22) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001423 if (safe_read(0, &c, 1) < 1)
1424 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001425 if (c == 0) {
1426 beep();
1427 break;
1428 }
1429 } else
1430#endif
Eric Andersene5dfced2001-04-09 22:48:12 +00001431 if (!Isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001432 break;
1433
1434 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
1435 break;
1436
1437 len++;
1438
1439 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001440 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001441 *(command + cursor + 1) = 0;
1442 cmdedit_set_out_char(0);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001443 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001444 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001445
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001446 memmove(command + sc + 1, command + sc, len - sc);
1447 *(command + sc) = c;
1448 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001449 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001450 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001451 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001452 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001453 }
1454
Erik Andersenc7c634b2000-03-19 05:28:55 +00001455 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001456 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001457 if (break_out) /* Enter is the command terminator, no more input. */
1458 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001459
1460 if (c != '\t')
1461 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001462 }
1463
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001464 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001465 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001466
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001467#if MAX_HISTORY >= 1
Erik Andersenc7c634b2000-03-19 05:28:55 +00001468 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001469 /* cleanup may be saved current command line */
1470 free(history[MAX_HISTORY]);
1471 history[MAX_HISTORY] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001472 if (len) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001473 int i = n_history;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001474 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001475 if (i >= MAX_HISTORY) {
1476 free(history[0]);
1477 for(i = 0; i < (MAX_HISTORY-1); i++)
1478 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001479 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001480 history[i++] = xstrdup(command);
1481 cur_history = i;
1482 n_history = i;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001483#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001484 num_ok_lines++;
1485#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001486 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001487#else /* MAX_HISTORY < 1 */
1488#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
1489 if (len) { /* no put empty line */
1490 num_ok_lines++;
1491 }
1492#endif
1493#endif /* MAX_HISTORY >= 1 */
Eric Andersen044228d2001-07-17 01:12:36 +00001494 if(break_out>0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001495 command[len++] = '\n'; /* set '\n' */
1496 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001497 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001498#if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001499 input_tab(0); /* strong free */
1500#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001501#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001502 free(cmdedit_prompt);
1503#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001504 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001505 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001506}
1507
Eric Andersen7467c8d2001-07-12 20:26:32 +00001508
1509
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001510#endif /* CONFIG_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001511
1512
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001513#ifdef TEST
1514
Eric Andersene5dfced2001-04-09 22:48:12 +00001515const char *applet_name = "debug stuff usage";
1516const char *memory_exhausted = "Memory exhausted";
1517
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001518#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001519#include <locale.h>
1520#endif
1521
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001522int main(int argc, char **argv)
1523{
1524 char buff[BUFSIZ];
1525 char *prompt =
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001526#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001527 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001528\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001529\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001530#else
1531 "% ";
1532#endif
1533
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001534#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001535 setlocale(LC_ALL, "");
1536#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001537 while(1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001538 int l;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001539 cmdedit_read_input(prompt, buff);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001540 l = strlen(buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001541 if(l==0)
1542 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001543 if(l > 0 && buff[l-1] == '\n')
1544 buff[l-1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001545 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001546 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001547 printf("*** cmdedit_read_input() detect ^C\n");
1548 return 0;
1549}
1550
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001551#endif /* TEST */