blob: 2ea61614de009cefb7febc849dd360591a7d06b5 [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
Glenn L McGrath4d001292003-01-06 01:11:50 +0000954/*
955 display by column original ideas from ls applet,
956 very optimize by my :)
957*/
958static void showfiles(char **matches, int nfiles)
959{
960 int ncols, row;
961 int column_width = 0;
962 int nrows = nfiles;
963
964 /* find the longest file name- use that as the column width */
965 for (row = 0; row < nrows; row++) {
966 int l = strlen(matches[row]);
967
968 if (column_width < l)
969 column_width = l;
970 }
971 column_width += 2; /* min space for columns */
972 ncols = cmdedit_termw / column_width;
973
974 if (ncols > 1) {
975 nrows /= ncols;
976 if(nfiles % ncols)
977 nrows++; /* round up fractionals */
978 column_width = -column_width; /* for printf("%-Ns", ...); */
979 } else {
980 ncols = 1;
981 }
982 for (row = 0; row < nrows; row++) {
983 int n = row;
984 int nc;
985
986 for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++)
987 printf("%*s", column_width, matches[n]);
988 printf("%s\n", matches[n]);
989 }
990}
991
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000992
993static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000994{
995 /* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000996 static int num_matches;
Mark Whitley4e338752001-01-26 20:42:23 +0000997 static char **matches;
998
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000999 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +00001000 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001001 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001002 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +00001003 free(matches);
1004 matches = (char **) NULL;
1005 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001006 return;
1007 }
Matt Kraai1f0c4362001-12-20 23:13:26 +00001008 if (! *lastWasTab) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001009
1010 char *tmp;
1011 int len_found;
1012 char matchBuf[BUFSIZ];
1013 int find_type;
1014 int recalc_pos;
1015
1016 *lastWasTab = TRUE; /* flop trigger */
1017
1018 /* Make a local copy of the string -- up
1019 * to the position of the cursor */
1020 tmp = strncpy(matchBuf, command_ps, cursor);
1021 tmp[cursor] = 0;
1022
1023 find_type = find_match(matchBuf, &recalc_pos);
1024
1025 /* Free up any memory already allocated */
1026 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001027
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001028#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001029 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +00001030 * then try completing this word as a username. */
1031
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001032 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
Mark Whitley4e338752001-01-26 20:42:23 +00001033 matches = username_tab_completion(matchBuf, &num_matches);
Mark Whitley4e338752001-01-26 20:42:23 +00001034#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001035 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +00001036 * in the current working directory that matches. */
1037 if (!matches)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001038 matches =
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001039 exe_n_cwd_tab_completion(matchBuf,
1040 &num_matches, find_type);
1041 /* Remove duplicate found */
1042 if(matches) {
1043 int i, j;
1044 /* bubble */
1045 for(i=0; i<(num_matches-1); i++)
1046 for(j=i+1; j<num_matches; j++)
1047 if(matches[i]!=0 && matches[j]!=0 &&
1048 strcmp(matches[i], matches[j])==0) {
1049 free(matches[j]);
1050 matches[j]=0;
1051 }
1052 j=num_matches;
1053 num_matches = 0;
1054 for(i=0; i<j; i++)
1055 if(matches[i]) {
1056 if(!strcmp(matches[i], "./"))
1057 matches[i][1]=0;
1058 else if(!strcmp(matches[i], "../"))
1059 matches[i][2]=0;
1060 matches[num_matches++]=matches[i];
1061 }
1062 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001063 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001064 if (!matches || num_matches > 1) {
1065 char *tmp1;
1066
Mark Whitley4e338752001-01-26 20:42:23 +00001067 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001068 if (!matches)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001069 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001070 /* sort */
1071 qsort(matches, num_matches, sizeof(char *), match_compare);
1072
1073 /* find minimal match */
1074 tmp = xstrdup(matches[0]);
1075 for (tmp1 = tmp; *tmp1; tmp1++)
1076 for (len_found = 1; len_found < num_matches; len_found++)
1077 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
1078 *tmp1 = 0;
1079 break;
1080 }
1081 if (*tmp == 0) { /* have unique */
1082 free(tmp);
1083 return;
1084 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001085 } else { /* one match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001086 tmp = matches[0];
1087 /* for next completion current found */
1088 *lastWasTab = FALSE;
Mark Whitley4e338752001-01-26 20:42:23 +00001089 }
1090
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001091 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +00001092 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001093 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +00001094
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001095 /* before word for match */
1096 command_ps[cursor - recalc_pos] = 0;
1097 /* save tail line */
1098 strcpy(matchBuf, command_ps + cursor);
1099 /* add match */
1100 strcat(command_ps, tmp);
1101 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +00001102 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001103 /* back to begin word for match */
1104 input_backward(recalc_pos);
1105 /* new pos */
1106 recalc_pos = cursor + len_found;
1107 /* new len */
1108 len = strlen(command_ps);
1109 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +00001110 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +00001111 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001112 if (tmp != matches[0])
1113 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +00001114 } else {
1115 /* Ok -- the last char was a TAB. Since they
1116 * just hit TAB again, print a list of all the
1117 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001118 if (matches && num_matches > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001119 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +00001120
1121 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +00001122 goto_new_line();
Glenn L McGrath4d001292003-01-06 01:11:50 +00001123 showfiles(matches, num_matches);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001124 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +00001125 }
1126 }
1127}
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001128#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +00001129
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001130#if MAX_HISTORY >= 1
1131static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001132{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001133 if(command_ps[0] != 0 || history[cur_history] == 0) {
1134 free(history[cur_history]);
1135 history[cur_history] = xstrdup(command_ps);
1136 }
1137 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +00001138}
1139
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001140static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +00001141{
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001142 int ch = cur_history;
1143
1144 if (ch < n_history) {
1145 get_previous_history(); /* save the current history line */
1146 return (cur_history = ch+1);
1147 } else {
1148 beep();
1149 return 0;
1150 }
Erik Andersenf0657d32000-04-12 17:49:52 +00001151}
Robert Griebl350d26b2002-12-03 22:45:46 +00001152
Robert Griebl350d26b2002-12-03 22:45:46 +00001153#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001154extern void load_history ( const char *fromfile )
1155{
Robert Griebl350d26b2002-12-03 22:45:46 +00001156 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001157 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001158
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001159 /* cleanup old */
1160
1161 for(hi = n_history; hi > 0; ) {
1162 hi--;
1163 free ( history [hi] );
Robert Griebl350d26b2002-12-03 22:45:46 +00001164 }
1165
1166 if (( fp = fopen ( fromfile, "r" ))) {
Robert Griebl350d26b2002-12-03 22:45:46 +00001167
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001168 for ( hi = 0; hi < MAX_HISTORY; ) {
1169 char * hl = get_line_from_file(fp);
1170 int l;
1171
1172 if(!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +00001173 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001174 chomp(hl);
1175 l = strlen(hl);
1176 if(l >= BUFSIZ)
1177 hl[BUFSIZ-1] = 0;
1178 if(l == 0 || hl[0] == ' ') {
1179 free(hl);
1180 continue;
1181 }
1182 history [hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +00001183 }
1184 fclose ( fp );
1185 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001186 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +00001187}
1188
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001189extern void save_history ( const char *tofile )
Robert Griebl350d26b2002-12-03 22:45:46 +00001190{
Robert Griebl350d26b2002-12-03 22:45:46 +00001191 FILE *fp = fopen ( tofile, "w" );
1192
1193 if ( fp ) {
1194 int i;
1195
1196 for ( i = 0; i < n_history; i++ ) {
1197 fputs ( history [i], fp );
1198 fputc ( '\n', fp );
1199 }
1200 fclose ( fp );
1201 }
Robert Griebl350d26b2002-12-03 22:45:46 +00001202}
Glenn L McGrathfdbbb042002-12-09 11:10:40 +00001203#endif
Robert Griebl350d26b2002-12-03 22:45:46 +00001204
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001205#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001206
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001207enum {
1208 ESC = 27,
1209 DEL = 127,
1210};
1211
1212
Erik Andersen6273f652000-03-17 01:12:41 +00001213/*
1214 * This function is used to grab a character buffer
1215 * from the input file descriptor and allows you to
1216 * a string with full command editing (sortof like
1217 * a mini readline).
1218 *
1219 * The following standard commands are not implemented:
1220 * ESC-b -- Move back one word
1221 * ESC-f -- Move forward one word
1222 * ESC-d -- Delete back one word
1223 * ESC-h -- Delete forward one word
1224 * CTL-t -- Transpose two characters
1225 *
1226 * Furthermore, the "vi" command editing keys are not implemented.
1227 *
Erik Andersen6273f652000-03-17 01:12:41 +00001228 */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001229
Eric Andersen044228d2001-07-17 01:12:36 +00001230
1231int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001232{
1233
Erik Andersenc7c634b2000-03-19 05:28:55 +00001234 int break_out = 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001235 int lastWasTab = FALSE;
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001236 unsigned char c = 0;
Erik Andersen13456d12000-03-16 08:09:57 +00001237
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001238 /* prepare before init handlers */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001239 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001240 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001241 command_ps = command;
1242
Eric Andersen34506362001-08-02 05:02:46 +00001243 getTermSettings(0, (void *) &initial_settings);
1244 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1245 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1246 /* Turn off echoing and CTRL-C, so we can trap it */
1247 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001248#ifndef linux
Eric Andersen34506362001-08-02 05:02:46 +00001249 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1250 new_settings.c_cc[VMIN] = 1;
1251 new_settings.c_cc[VTIME] = 0;
1252 /* Turn off CTRL-C, so we can trap it */
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001253# ifndef _POSIX_VDISABLE
1254# define _POSIX_VDISABLE '\0'
1255# endif
Eric Andersen34506362001-08-02 05:02:46 +00001256 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001257#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001258 command[0] = 0;
1259
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001260 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001261 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001262
Eric Andersen6faae7d2001-02-16 20:09:17 +00001263 /* Now initialize things */
1264 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001265 /* Print out the command prompt */
1266 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001267
Erik Andersenc7c634b2000-03-19 05:28:55 +00001268 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +00001269
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001270 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001271
Eric Andersen7467c8d2001-07-12 20:26:32 +00001272 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001273 /* if we can't read input then exit */
1274 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001275
Erik Andersen13456d12000-03-16 08:09:57 +00001276 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001277 case '\n':
1278 case '\r':
1279 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001280 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001281 break_out = 1;
1282 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001283 case 1:
1284 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001285 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001286 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001287 case 2:
1288 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001289 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001290 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +00001291 case 3:
Eric Andersen86349772000-12-18 20:25:50 +00001292 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001293 goto_new_line();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001294 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001295 len = 0;
1296 lastWasTab = FALSE;
1297 put_prompt();
1298 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001299 case 4:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001300 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001301 * if the len=0 and no chars to delete */
1302 if (len == 0) {
Eric Andersened424db2001-04-23 15:28:28 +00001303prepare_to_die:
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001304#if !defined(CONFIG_ASH)
Mark Whitley4e338752001-01-26 20:42:23 +00001305 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001306 goto_new_line();
1307 /* cmdedit_reset_term() called in atexit */
1308 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001309#else
1310 break_out = -1; /* for control stoped jobs */
1311 break;
1312#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001313 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001314 input_delete();
Erik Andersenf0657d32000-04-12 17:49:52 +00001315 }
1316 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001317 case 5:
1318 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001319 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001320 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001321 case 6:
1322 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001323 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001324 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001325 case '\b':
1326 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +00001327 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001328 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001329 break;
1330 case '\t':
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001331#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001332 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001333#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001334 break;
Eric Andersen65a07302002-04-13 13:26:49 +00001335 case 11:
1336 /* Control-k -- clear to end of line */
1337 *(command + cursor) = 0;
1338 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001339 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001340 break;
1341 case 12:
Eric Andersen65a07302002-04-13 13:26:49 +00001342 /* Control-l -- clear screen */
Eric Andersen65a07302002-04-13 13:26:49 +00001343 printf("\033[H");
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001344 redraw(0, len-cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001345 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001346#if MAX_HISTORY >= 1
Erik Andersenf0657d32000-04-12 17:49:52 +00001347 case 14:
1348 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001349 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001350 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001351 break;
1352 case 16:
1353 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001354 if (cur_history > 0) {
1355 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001356 goto rewrite_line;
1357 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001358 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001359 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001360 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001361#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001362 case 21:
1363 /* Control-U -- Clear line before cursor */
1364 if (cursor) {
1365 strcpy(command, command + cursor);
1366 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001367 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001368 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001369 case ESC:{
1370 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001371 if (safe_read(0, &c, 1) < 1)
1372 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001373 /* different vt100 emulations */
1374 if (c == '[' || c == 'O') {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001375 if (safe_read(0, &c, 1) < 1)
1376 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001377 }
1378 switch (c) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001379#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001380 case '\t': /* Alt-Tab */
1381
1382 input_tab(&lastWasTab);
1383 break;
1384#endif
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001385#if MAX_HISTORY >= 1
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001386 case 'A':
1387 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001388 if (cur_history > 0) {
1389 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001390 goto rewrite_line;
1391 } else {
1392 beep();
1393 }
1394 break;
1395 case 'B':
1396 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001397 if (!get_next_history())
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001398 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001399 /* Rewrite the line with the selected history item */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001400rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001401 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001402 len = strlen(strcpy(command, history[cur_history]));
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001403 /* redraw and go to end line */
1404 redraw(cmdedit_y, 0);
1405 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001406#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001407 case 'C':
1408 /* Right Arrow -- Move forward one character */
1409 input_forward();
1410 break;
1411 case 'D':
1412 /* Left Arrow -- Move back one character */
1413 input_backward(1);
1414 break;
1415 case '3':
1416 /* Delete */
1417 input_delete();
1418 break;
1419 case '1':
1420 case 'H':
1421 /* Home (Ctrl-A) */
1422 input_backward(cursor);
1423 break;
1424 case '4':
1425 case 'F':
1426 /* End (Ctrl-E) */
1427 input_end();
1428 break;
1429 default:
1430 if (!(c >= '1' && c <= '9'))
1431 c = 0;
1432 beep();
1433 }
1434 if (c >= '1' && c <= '9')
1435 do
Eric Andersen7467c8d2001-07-12 20:26:32 +00001436 if (safe_read(0, &c, 1) < 1)
1437 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001438 while (c != '~');
1439 break;
1440 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001441
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001442 default: /* If it's regular input, do the normal thing */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001443#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001444 /* Control-V -- Add non-printable symbol */
1445 if (c == 22) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001446 if (safe_read(0, &c, 1) < 1)
1447 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001448 if (c == 0) {
1449 beep();
1450 break;
1451 }
1452 } else
1453#endif
Eric Andersene5dfced2001-04-09 22:48:12 +00001454 if (!Isprint(c)) /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001455 break;
1456
1457 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
1458 break;
1459
1460 len++;
1461
1462 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001463 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001464 *(command + cursor + 1) = 0;
1465 cmdedit_set_out_char(0);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001466 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001467 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001468
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001469 memmove(command + sc + 1, command + sc, len - sc);
1470 *(command + sc) = c;
1471 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001472 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001473 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001474 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001475 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001476 }
1477
Erik Andersenc7c634b2000-03-19 05:28:55 +00001478 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001479 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001480 if (break_out) /* Enter is the command terminator, no more input. */
1481 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001482
1483 if (c != '\t')
1484 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001485 }
1486
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001487 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001488 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001489
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001490#if MAX_HISTORY >= 1
Erik Andersenc7c634b2000-03-19 05:28:55 +00001491 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001492 /* cleanup may be saved current command line */
1493 free(history[MAX_HISTORY]);
1494 history[MAX_HISTORY] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001495 if (len) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001496 int i = n_history;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001497 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001498 if (i >= MAX_HISTORY) {
1499 free(history[0]);
1500 for(i = 0; i < (MAX_HISTORY-1); i++)
1501 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001502 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001503 history[i++] = xstrdup(command);
1504 cur_history = i;
1505 n_history = i;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001506#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001507 num_ok_lines++;
1508#endif
Erik Andersen6273f652000-03-17 01:12:41 +00001509 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001510#else /* MAX_HISTORY < 1 */
1511#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
1512 if (len) { /* no put empty line */
1513 num_ok_lines++;
1514 }
1515#endif
1516#endif /* MAX_HISTORY >= 1 */
Eric Andersen044228d2001-07-17 01:12:36 +00001517 if(break_out>0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001518 command[len++] = '\n'; /* set '\n' */
1519 command[len] = 0;
Eric Andersen044228d2001-07-17 01:12:36 +00001520 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001521#if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001522 input_tab(0); /* strong free */
1523#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001524#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001525 free(cmdedit_prompt);
1526#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001527 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001528 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001529}
1530
Eric Andersen7467c8d2001-07-12 20:26:32 +00001531
1532
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001533#endif /* CONFIG_FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001534
1535
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001536#ifdef TEST
1537
Eric Andersene5dfced2001-04-09 22:48:12 +00001538const char *applet_name = "debug stuff usage";
1539const char *memory_exhausted = "Memory exhausted";
1540
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001541#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001542#include <locale.h>
1543#endif
1544
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001545int main(int argc, char **argv)
1546{
1547 char buff[BUFSIZ];
1548 char *prompt =
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001549#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001550 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001551\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001552\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001553#else
1554 "% ";
1555#endif
1556
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001557#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001558 setlocale(LC_ALL, "");
1559#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001560 while(1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001561 int l;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001562 cmdedit_read_input(prompt, buff);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001563 l = strlen(buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001564 if(l==0)
1565 break;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001566 if(l > 0 && buff[l-1] == '\n')
1567 buff[l-1] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001568 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001569 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001570 printf("*** cmdedit_read_input() detect ^C\n");
1571 return 0;
1572}
1573
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001574#endif /* TEST */