blob: 722a36a505614e93b73d3443f69d9e3266b17633 [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/*
Erik Andersen61677fe2000-04-13 01:18:56 +00003 * Termios command line History and Editting, originally
4 * intended for NetBSD sh (ash)
Erik Andersen13456d12000-03-16 08:09:57 +00005 * Copyright (c) 1999
6 * Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
7 * Etc: Dave Cinege <dcinege@psychosis.com>
Erik Andersen61677fe2000-04-13 01:18:56 +00008 * Majorly adjusted/re-written for busybox:
9 * Erik Andersen <andersee@debian.org>
Erik Andersen13456d12000-03-16 08:09:57 +000010 *
11 * You may use this code as you wish, so long as the original author(s)
12 * are attributed in any redistributions of the source code.
13 * This code is 'as is' with no warranty.
14 * This code may safely be consumed by a BSD or GPL license.
15 *
16 * v 0.5 19990328 Initial release
17 *
18 * Future plans: Simple file and path name completion. (like BASH)
19 *
20 */
21
22/*
23 Usage and Known bugs:
24 Terminal key codes are not extensive, and more will probably
25 need to be added. This version was created on Debian GNU/Linux 2.x.
26 Delete, Backspace, Home, End, and the arrow keys were tested
27 to work in an Xterm and console. Ctrl-A also works as Home.
28 Ctrl-E also works as End. The binary size increase is <3K.
29
30 Editting will not display correctly for lines greater then the
31 terminal width. (more then one line.) However, history will.
32 */
33
Eric Andersen3570a342000-09-25 21:45:58 +000034#include "busybox.h"
Erik Andersen13456d12000-03-16 08:09:57 +000035#ifdef BB_FEATURE_SH_COMMAND_EDITING
36
37#include <stdio.h>
38#include <errno.h>
39#include <unistd.h>
40#include <stdlib.h>
41#include <string.h>
Erik Andersen1d1d9502000-04-21 01:26:49 +000042#include <sys/ioctl.h>
Erik Andersen13456d12000-03-16 08:09:57 +000043#include <ctype.h>
44#include <signal.h>
45
Erik Andersen13456d12000-03-16 08:09:57 +000046
Erik Andersenc7c634b2000-03-19 05:28:55 +000047#define MAX_HISTORY 15 /* Maximum length of the linked list for the command line history */
Erik Andersen13456d12000-03-16 08:09:57 +000048
49#define ESC 27
50#define DEL 127
Erik Andersen6273f652000-03-17 01:12:41 +000051#define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0)
52#define whitespace(c) (((c) == ' ') || ((c) == '\t'))
Erik Andersen13456d12000-03-16 08:09:57 +000053
54static struct history *his_front = NULL; /* First element in command line list */
55static struct history *his_end = NULL; /* Last element in command line list */
Erik Andersen1d1d9502000-04-21 01:26:49 +000056
57/* ED: sparc termios is broken: revert back to old termio handling. */
58#ifdef BB_FEATURE_USE_TERMIOS
59
60#if #cpu(sparc)
61# include <termio.h>
62# define termios termio
63# define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp)
64# define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp)
65#else
66# include <termios.h>
67# define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
68# define getTermSettings(fd,argp) tcgetattr(fd, argp);
69#endif
70
71/* Current termio and the previous termio before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +000072static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000073
74
75#ifndef _POSIX_VDISABLE
76#define _POSIX_VDISABLE '\0'
77#endif
78
Erik Andersen1d1d9502000-04-21 01:26:49 +000079#endif
80
81
Erik Andersen13456d12000-03-16 08:09:57 +000082
Erik Andersenf0657d32000-04-12 17:49:52 +000083static int cmdedit_termw = 80; /* actual terminal width */
84static int cmdedit_scroll = 27; /* width of EOL scrolling region */
Erik Andersen13456d12000-03-16 08:09:57 +000085static int history_counter = 0; /* Number of commands in history list */
Erik Andersenc7c634b2000-03-19 05:28:55 +000086static int reset_term = 0; /* Set to true if the terminal needs to be reset upon exit */
Eric Andersen501c88b2000-07-28 15:14:45 +000087static int exithandler_set = 0; /* Set to true when atexit() has been called */
Eric Andersen86349772000-12-18 20:25:50 +000088
89
90/* Link into lash to reset context to 0
91 * on ^C and such */
92extern unsigned int shell_context;
93
Erik Andersen13456d12000-03-16 08:09:57 +000094
95struct history {
Erik Andersenc7c634b2000-03-19 05:28:55 +000096 char *s;
97 struct history *p;
98 struct history *n;
Erik Andersen13456d12000-03-16 08:09:57 +000099};
100
Erik Andersenf0657d32000-04-12 17:49:52 +0000101#define xwrite write
Erik Andersen13456d12000-03-16 08:09:57 +0000102
Mark Whitley55380702000-07-13 17:20:23 +0000103/*
104 * TODO: Someday we want to implement 'horizontal scrolling' of the
105 * command-line when the user has typed more than the current width. This
106 * would allow the user to see a 'window' of what he has typed.
107 */
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000108static void cmdedit_setwidth(int w)
Erik Andersen13456d12000-03-16 08:09:57 +0000109{
Erik Andersen61677fe2000-04-13 01:18:56 +0000110 if (w > 20) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000111 cmdedit_termw = w;
112 cmdedit_scroll = w / 3;
Erik Andersen61677fe2000-04-13 01:18:56 +0000113 } else {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000114 error_msg("\n*** Error: minimum screen width is 21\n");
Erik Andersen61677fe2000-04-13 01:18:56 +0000115 }
Erik Andersen13456d12000-03-16 08:09:57 +0000116}
117
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000118static void win_changed(int junk)
119{
120 struct winsize win = { 0, 0, 0, 0 };
121 ioctl(0, TIOCGWINSZ, &win);
122 if (win.ws_col > 0) {
123 cmdedit_setwidth( win.ws_col - 1);
124 }
125}
Erik Andersen61677fe2000-04-13 01:18:56 +0000126
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000127
128static void cmdedit_reset_term(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000129{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000130 if (reset_term)
Erik Andersena6c75222000-04-18 00:00:52 +0000131 /* sparc and other have broken termios support: use old termio handling. */
Erik Andersen1d1d9502000-04-21 01:26:49 +0000132 setTermSettings(fileno(stdin), (void*) &initial_settings);
Eric Andersenb040d4f2000-07-25 18:01:20 +0000133#ifdef BB_FEATURE_CLEAN_UP
134 if (his_front) {
135 struct history *n;
136 //while(his_front!=his_end) {
137 while(his_front!=his_end) {
138 n = his_front->n;
139 free(his_front->s);
140 free(his_front);
141 his_front=n;
142 }
143 }
144#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000145}
146
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000147static void clean_up_and_die(int sig)
Erik Andersen13456d12000-03-16 08:09:57 +0000148{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000149 cmdedit_reset_term();
Matt Kraai12f417e2001-01-18 02:57:08 +0000150 printf("\n");
Erik Andersen1d1d9502000-04-21 01:26:49 +0000151 if (sig!=SIGINT)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000152 exit(EXIT_SUCCESS);
Erik Andersen13456d12000-03-16 08:09:57 +0000153}
154
Erik Andersenf0657d32000-04-12 17:49:52 +0000155/* Go to HOME position */
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000156static void input_home(int outputFd, int *cursor)
Erik Andersenf0657d32000-04-12 17:49:52 +0000157{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000158 while (*cursor > 0) {
159 xwrite(outputFd, "\b", 1);
160 --*cursor;
161 }
Erik Andersen13456d12000-03-16 08:09:57 +0000162}
163
Erik Andersenf0657d32000-04-12 17:49:52 +0000164/* Go to END position */
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000165static void input_end(int outputFd, int *cursor, int len)
Erik Andersen13456d12000-03-16 08:09:57 +0000166{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000167 while (*cursor < len) {
168 xwrite(outputFd, "\033[C", 3);
169 ++*cursor;
170 }
Erik Andersen13456d12000-03-16 08:09:57 +0000171}
172
Erik Andersenf0657d32000-04-12 17:49:52 +0000173/* Delete the char in back of the cursor */
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000174static void input_backspace(char* command, int outputFd, int *cursor, int *len)
Erik Andersen13456d12000-03-16 08:09:57 +0000175{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000176 int j = 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000177
Eric Andersen72965e32000-07-04 06:22:18 +0000178/* Debug crap */
179//fprintf(stderr, "\nerik: len=%d, cursor=%d, strlen(command)='%d'\n", *len, *cursor, strlen(command));
180//xwrite(outputFd, command, *len);
181//*cursor = *len;
182
183
Erik Andersenc7c634b2000-03-19 05:28:55 +0000184 if (*cursor > 0) {
185 xwrite(outputFd, "\b \b", 3);
186 --*cursor;
Erik Andersenf0657d32000-04-12 17:49:52 +0000187 memmove(command + *cursor, command + *cursor + 1,
Erik Andersenc7c634b2000-03-19 05:28:55 +0000188 BUFSIZ - *cursor + 1);
Erik Andersen13456d12000-03-16 08:09:57 +0000189
Erik Andersenc7c634b2000-03-19 05:28:55 +0000190 for (j = *cursor; j < (BUFSIZ - 1); j++) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000191 if (!*(command + j))
Erik Andersenc7c634b2000-03-19 05:28:55 +0000192 break;
193 else
Erik Andersenf0657d32000-04-12 17:49:52 +0000194 xwrite(outputFd, (command + j), 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000195 }
196
197 xwrite(outputFd, " \b", 2);
198
199 while (j-- > *cursor)
200 xwrite(outputFd, "\b", 1);
201
202 --*len;
Erik Andersen13456d12000-03-16 08:09:57 +0000203 }
Erik Andersen13456d12000-03-16 08:09:57 +0000204}
205
Erik Andersenf0657d32000-04-12 17:49:52 +0000206/* Delete the char in front of the cursor */
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000207static void input_delete(char* command, int outputFd, int cursor, int *len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000208{
209 int j = 0;
Erik Andersena2685732000-04-09 18:27:46 +0000210
Erik Andersenf0657d32000-04-12 17:49:52 +0000211 if (cursor == *len)
212 return;
213
214 memmove(command + cursor, command + cursor + 1,
215 BUFSIZ - cursor - 1);
216 for (j = cursor; j < (BUFSIZ - 1); j++) {
217 if (!*(command + j))
218 break;
219 else
220 xwrite(outputFd, (command + j), 1);
221 }
222
223 xwrite(outputFd, " \b", 2);
224
225 while (j-- > cursor)
226 xwrite(outputFd, "\b", 1);
227 --*len;
228}
229
230/* Move forward one charactor */
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000231static void input_forward(int outputFd, int *cursor, int len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000232{
233 if (*cursor < len) {
234 xwrite(outputFd, "\033[C", 3);
235 ++*cursor;
236 }
237}
238
239/* Move back one charactor */
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000240static void input_backward(int outputFd, int *cursor)
Erik Andersenf0657d32000-04-12 17:49:52 +0000241{
242 if (*cursor > 0) {
243 xwrite(outputFd, "\033[D", 3);
244 --*cursor;
245 }
246}
247
248
249
250#ifdef BB_FEATURE_SH_TAB_COMPLETION
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000251static char** username_tab_completion(char* command, int *num_matches)
Erik Andersen6273f652000-03-17 01:12:41 +0000252{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000253 char **matches = (char **) NULL;
254 *num_matches=0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000255 fprintf(stderr, "\nin username_tab_completion\n");
Erik Andersenc7c634b2000-03-19 05:28:55 +0000256 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000257}
Erik Andersen1dbe3402000-03-19 10:46:06 +0000258
259#include <dirent.h>
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000260static char** exe_n_cwd_tab_completion(char* command, int *num_matches)
Erik Andersen6273f652000-03-17 01:12:41 +0000261{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000262 char *dirName;
Matt Kraai322ae932000-09-13 02:46:14 +0000263 char **matches;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000264 DIR *dir;
265 struct dirent *next;
266
Matt Kraai322ae932000-09-13 02:46:14 +0000267 matches = xmalloc( sizeof(char*)*50);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000268
Erik Andersen1dbe3402000-03-19 10:46:06 +0000269 /* Stick a wildcard onto the command, for later use */
270 strcat( command, "*");
Erik Andersenc7c634b2000-03-19 05:28:55 +0000271
Erik Andersen1dbe3402000-03-19 10:46:06 +0000272 /* Now wall the current directory */
273 dirName = get_current_dir_name();
274 dir = opendir(dirName);
275 if (!dir) {
276 /* Don't print an error, just shut up and return */
277 *num_matches=0;
278 return (matches);
279 }
280 while ((next = readdir(dir)) != NULL) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000281
Erik Andersen1dbe3402000-03-19 10:46:06 +0000282 /* Some quick sanity checks */
283 if ((strcmp(next->d_name, "..") == 0)
284 || (strcmp(next->d_name, ".") == 0)) {
285 continue;
286 }
287 /* See if this matches */
288 if (check_wildcard_match(next->d_name, command) == TRUE) {
289 /* Cool, found a match. Add it to the list */
Matt Kraai322ae932000-09-13 02:46:14 +0000290 matches[*num_matches] = xmalloc(strlen(next->d_name)+1);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000291 strcpy( matches[*num_matches], next->d_name);
292 ++*num_matches;
293 //matches = realloc( matches, sizeof(char*)*(*num_matches));
294 }
295 }
296
Erik Andersenc7c634b2000-03-19 05:28:55 +0000297 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000298}
Erik Andersenf0657d32000-04-12 17:49:52 +0000299
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000300static void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len, int lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000301{
302 /* Do TAB completion */
303 static int num_matches=0;
304 static char **matches = (char **) NULL;
Eric Andersena75466e2000-11-02 17:02:26 +0000305 int pos = *cursor;
Erik Andersenf0657d32000-04-12 17:49:52 +0000306
307
308 if (lastWasTab == FALSE) {
309 char *tmp, *tmp1, *matchBuf;
310
311 /* For now, we will not bother with trying to distinguish
312 * whether the cursor is in/at a command extression -- we
Eric Andersen74c66ad2000-06-16 19:57:44 +0000313 * will always try all possible matches. If you don't like
Erik Andersenf0657d32000-04-12 17:49:52 +0000314 * that then feel free to fix it.
315 */
316
317 /* Make a local copy of the string -- up
318 * to the position of the cursor */
Matt Kraai322ae932000-09-13 02:46:14 +0000319 matchBuf = (char *) xcalloc(BUFSIZ, sizeof(char));
Eric Andersena75466e2000-11-02 17:02:26 +0000320 strncpy(matchBuf, command, *cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000321 tmp=matchBuf;
322
323 /* skip past any command seperator tokens */
324 while (*tmp && (tmp1=strpbrk(tmp, ";|&{(`")) != NULL) {
325 tmp=++tmp1;
326 /* skip any leading white space */
327 while (*tmp && isspace(*tmp))
328 ++tmp;
329 }
330
331 /* skip any leading white space */
332 while (*tmp && isspace(*tmp))
333 ++tmp;
334
335 /* Free up any memory already allocated */
336 if (matches) {
337 free(matches);
338 matches = (char **) NULL;
339 }
340
341 /* If the word starts with `~' and there is no slash in the word,
342 * then try completing this word as a username. */
343
344 /* FIXME -- this check is broken! */
345 if (*tmp == '~' && !strchr(tmp, '/'))
346 matches = username_tab_completion(tmp, &num_matches);
347
348 /* Try to match any executable in our path and everything
349 * in the current working directory that matches. */
350 if (!matches)
351 matches = exe_n_cwd_tab_completion(tmp, &num_matches);
352
353 /* Don't leak memory */
354 free( matchBuf);
355
356 /* Did we find exactly one match? */
357 if (matches && num_matches==1) {
358 /* write out the matched command */
359 strncpy(command+pos, matches[0]+pos, strlen(matches[0])-pos);
Eric Andersena75466e2000-11-02 17:02:26 +0000360 *len=strlen(command);
361 *cursor=*len;
Erik Andersenf0657d32000-04-12 17:49:52 +0000362 xwrite(outputFd, matches[0]+pos, strlen(matches[0])-pos);
Eric Andersena75466e2000-11-02 17:02:26 +0000363 return;
Erik Andersenf0657d32000-04-12 17:49:52 +0000364 }
365 } else {
366 /* Ok -- the last char was a TAB. Since they
367 * just hit TAB again, print a list of all the
368 * available choices... */
369 if ( matches && num_matches>0 ) {
370 int i, col;
371
372 /* Go to the next line */
373 xwrite(outputFd, "\n", 1);
374 /* Print the list of matches */
375 for (i=0,col=0; i<num_matches; i++) {
376 char foo[17];
377 sprintf(foo, "%-14s ", matches[i]);
378 col += xwrite(outputFd, foo, strlen(foo));
379 if (col > 60 && matches[i+1] != NULL) {
380 xwrite(outputFd, "\n", 1);
381 col = 0;
382 }
383 }
384 /* Go to the next line */
385 xwrite(outputFd, "\n", 1);
386 /* Rewrite the prompt */
387 xwrite(outputFd, prompt, strlen(prompt));
388 /* Rewrite the command */
Eric Andersena75466e2000-11-02 17:02:26 +0000389 xwrite(outputFd, command, *len);
Erik Andersenf0657d32000-04-12 17:49:52 +0000390 /* Put the cursor back to where it used to be */
Eric Andersena75466e2000-11-02 17:02:26 +0000391 for (cursor=len; *cursor > pos; cursor--)
Erik Andersenf0657d32000-04-12 17:49:52 +0000392 xwrite(outputFd, "\b", 1);
393 }
394 }
395}
396#endif
397
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000398static void get_previous_history(struct history **hp, char* command)
Erik Andersenf0657d32000-04-12 17:49:52 +0000399{
Eric Andersen91a44002000-07-19 17:37:57 +0000400 if ((*hp)->s)
401 free((*hp)->s);
Erik Andersenf0657d32000-04-12 17:49:52 +0000402 (*hp)->s = strdup(command);
403 *hp = (*hp)->p;
404}
405
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000406static void get_next_history(struct history **hp, char* command)
Erik Andersenf0657d32000-04-12 17:49:52 +0000407{
Eric Andersen91a44002000-07-19 17:37:57 +0000408 if ((*hp)->s)
409 free((*hp)->s);
Erik Andersenf0657d32000-04-12 17:49:52 +0000410 (*hp)->s = strdup(command);
411 *hp = (*hp)->n;
Erik Andersenf0657d32000-04-12 17:49:52 +0000412}
413
Erik Andersen6273f652000-03-17 01:12:41 +0000414/*
415 * This function is used to grab a character buffer
416 * from the input file descriptor and allows you to
417 * a string with full command editing (sortof like
418 * a mini readline).
419 *
420 * The following standard commands are not implemented:
421 * ESC-b -- Move back one word
422 * ESC-f -- Move forward one word
423 * ESC-d -- Delete back one word
424 * ESC-h -- Delete forward one word
425 * CTL-t -- Transpose two characters
426 *
427 * Furthermore, the "vi" command editing keys are not implemented.
428 *
429 * TODO: implement TAB command completion. :)
Erik Andersen6273f652000-03-17 01:12:41 +0000430 */
Erik Andersenf0657d32000-04-12 17:49:52 +0000431extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +0000432{
433
Erik Andersenf0657d32000-04-12 17:49:52 +0000434 int inputFd=fileno(stdin);
435 int outputFd=fileno(stdout);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000436 int nr = 0;
437 int len = 0;
438 int j = 0;
439 int cursor = 0;
440 int break_out = 0;
441 int ret = 0;
442 int lastWasTab = FALSE;
443 char c = 0;
444 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +0000445
Erik Andersenc7c634b2000-03-19 05:28:55 +0000446 if (!reset_term) {
Erik Andersen1d1d9502000-04-21 01:26:49 +0000447
448 getTermSettings(inputFd, (void*) &initial_settings);
449 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
450 new_settings.c_cc[VMIN] = 1;
451 new_settings.c_cc[VTIME] = 0;
452 new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */
453 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
454 new_settings.c_lflag &= ~(ECHO|ECHOCTL|ECHONL); /* Turn off echoing */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000455 reset_term = 1;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000456 }
Erik Andersen1d1d9502000-04-21 01:26:49 +0000457 setTermSettings(inputFd, (void*) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +0000458
Erik Andersenf0657d32000-04-12 17:49:52 +0000459 memset(command, 0, BUFSIZ);
Erik Andersen13456d12000-03-16 08:09:57 +0000460
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000461 /* Print out the command prompt */
462 xwrite(outputFd, prompt, strlen(prompt));
463
Erik Andersenc7c634b2000-03-19 05:28:55 +0000464 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +0000465
Erik Andersen13456d12000-03-16 08:09:57 +0000466 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000467 return;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000468 //fprintf(stderr, "got a '%c' (%d)\n", c, c);
Erik Andersenf3b3d172000-04-09 18:24:05 +0000469
Erik Andersen13456d12000-03-16 08:09:57 +0000470 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000471 case '\n':
472 case '\r':
473 /* Enter */
474 *(command + len++ + 1) = c;
475 xwrite(outputFd, &c, 1);
476 break_out = 1;
477 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000478 case 1:
479 /* Control-a -- Beginning of line */
480 input_home(outputFd, &cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000481 case 2:
482 /* Control-b -- Move back one character */
483 input_backward(outputFd, &cursor);
484 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000485 case 3:
Eric Andersen86349772000-12-18 20:25:50 +0000486 /* Control-c -- stop gathering input */
487
488 /* Link into lash to reset context to 0 on ^C and such */
489 shell_context = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000490
491 /* Go to the next line */
492 xwrite(outputFd, "\n", 1);
493
Eric Andersen86349772000-12-18 20:25:50 +0000494#if 0
Erik Andersen1d1d9502000-04-21 01:26:49 +0000495 /* Rewrite the prompt */
496 xwrite(outputFd, prompt, strlen(prompt));
497
498 /* Reset the command string */
Eric Andersen4ac6cb52000-07-14 01:13:37 +0000499 memset(command, 0, BUFSIZ);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000500 len = cursor = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000501#endif
502 return;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000503
Erik Andersenf0657d32000-04-12 17:49:52 +0000504 case 4:
505 /* Control-d -- Delete one character, or exit
506 * if the len=0 and no chars to delete */
507 if (len == 0) {
508 xwrite(outputFd, "exit", 4);
509 clean_up_and_die(0);
510 } else {
511 input_delete(command, outputFd, cursor, &len);
512 }
513 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000514 case 5:
515 /* Control-e -- End of line */
516 input_end(outputFd, &cursor, len);
517 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000518 case 6:
519 /* Control-f -- Move forward one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000520 input_forward(outputFd, &cursor, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000521 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000522 case '\b':
523 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +0000524 /* Control-h and DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +0000525 input_backspace(command, outputFd, &cursor, &len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000526 break;
527 case '\t':
Erik Andersena2685732000-04-09 18:27:46 +0000528#ifdef BB_FEATURE_SH_TAB_COMPLETION
Eric Andersena75466e2000-11-02 17:02:26 +0000529 input_tab(command, prompt, outputFd, &cursor,
530&len, lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +0000531#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +0000532 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000533 case 14:
534 /* Control-n -- Get next command in history */
535 if (hp && hp->n && hp->n->s) {
536 get_next_history(&hp, command);
537 goto rewrite_line;
538 } else {
539 xwrite(outputFd, "\007", 1);
540 }
541 break;
542 case 16:
543 /* Control-p -- Get previous command from history */
544 if (hp && hp->p) {
545 get_previous_history(&hp, command);
546 goto rewrite_line;
547 } else {
548 xwrite(outputFd, "\007", 1);
549 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000550 break;
551 case ESC:{
552 /* escape sequence follows */
553 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000554 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000555
556 if (c == '[') { /* 91 */
557 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000558 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000559
560 switch (c) {
561 case 'A':
Erik Andersenf0657d32000-04-12 17:49:52 +0000562 /* Up Arrow -- Get previous command from history */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000563 if (hp && hp->p) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000564 get_previous_history(&hp, command);
565 goto rewrite_line;
566 } else {
567 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000568 }
569 break;
570 case 'B':
Erik Andersenf0657d32000-04-12 17:49:52 +0000571 /* Down Arrow -- Get next command in history */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000572 if (hp && hp->n && hp->n->s) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000573 get_next_history(&hp, command);
574 goto rewrite_line;
575 } else {
576 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000577 }
578 break;
579
Erik Andersenf0657d32000-04-12 17:49:52 +0000580 /* Rewrite the line with the selected history item */
581 rewrite_line:
582 /* erase old command from command line */
583 len = strlen(command)-strlen(hp->s);
Eric Andersen72965e32000-07-04 06:22:18 +0000584
585 while (len>cursor)
586 input_delete(command, outputFd, cursor, &len);
587 while (cursor>0)
Erik Andersenf0657d32000-04-12 17:49:52 +0000588 input_backspace(command, outputFd, &cursor, &len);
589 input_home(outputFd, &cursor);
590
Erik Andersenc7c634b2000-03-19 05:28:55 +0000591 /* write new command */
Erik Andersenf0657d32000-04-12 17:49:52 +0000592 strcpy(command, hp->s);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000593 len = strlen(hp->s);
Erik Andersenf0657d32000-04-12 17:49:52 +0000594 xwrite(outputFd, command, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000595 cursor = len;
596 break;
597 case 'C':
598 /* Right Arrow -- Move forward one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000599 input_forward(outputFd, &cursor, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000600 break;
601 case 'D':
602 /* Left Arrow -- Move back one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000603 input_backward(outputFd, &cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000604 break;
605 case '3':
606 /* Delete */
Erik Andersenf0657d32000-04-12 17:49:52 +0000607 input_delete(command, outputFd, cursor, &len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000608 break;
609 case '1':
610 /* Home (Ctrl-A) */
611 input_home(outputFd, &cursor);
612 break;
613 case '4':
614 /* End (Ctrl-E) */
615 input_end(outputFd, &cursor, len);
616 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000617 default:
618 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000619 }
620 if (c == '1' || c == '3' || c == '4')
621 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000622 return; /* read 126 (~) */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000623 }
624 if (c == 'O') {
625 /* 79 */
626 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000627 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000628 switch (c) {
629 case 'H':
630 /* Home (xterm) */
631 input_home(outputFd, &cursor);
632 break;
633 case 'F':
634 /* End (xterm) */
635 input_end(outputFd, &cursor, len);
636 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000637 default:
638 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000639 }
640 }
641 c = 0;
642 break;
643 }
644
645 default: /* If it's regular input, do the normal thing */
646
Erik Andersenf0657d32000-04-12 17:49:52 +0000647 if (!isprint(c)) { /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000648 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000649 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000650
651 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
652 break;
653
654 len++;
655
656 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +0000657 *(command + cursor) = c;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000658 } else { /* Insert otherwise */
Erik Andersenf0657d32000-04-12 17:49:52 +0000659 memmove(command + cursor + 1, command + cursor,
Erik Andersenc7c634b2000-03-19 05:28:55 +0000660 len - cursor - 1);
661
Erik Andersenf0657d32000-04-12 17:49:52 +0000662 *(command + cursor) = c;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000663
664 for (j = cursor; j < len; j++)
Erik Andersenf0657d32000-04-12 17:49:52 +0000665 xwrite(outputFd, command + j, 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000666 for (; j > cursor; j--)
667 xwrite(outputFd, "\033[D", 3);
668 }
669
Erik Andersen13456d12000-03-16 08:09:57 +0000670 cursor++;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000671 xwrite(outputFd, &c, 1);
672 break;
Erik Andersen13456d12000-03-16 08:09:57 +0000673 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000674 if (c == '\t')
675 lastWasTab = TRUE;
676 else
677 lastWasTab = FALSE;
678
679 if (break_out) /* Enter is the command terminator, no more input. */
680 break;
681 }
682
683 nr = len + 1;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000684 setTermSettings(inputFd, (void *) &initial_settings);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000685 reset_term = 0;
686
687
688 /* Handle command history log */
Erik Andersenf0657d32000-04-12 17:49:52 +0000689 if (*(command)) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000690
691 struct history *h = his_end;
692
693 if (!h) {
Eric Andersen91a44002000-07-19 17:37:57 +0000694 /* No previous history -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +0000695 h = his_front = xmalloc(sizeof(struct history));
696 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +0000697
698 h->p = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +0000699 h->s = strdup(command);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000700 h->n->p = h;
701 h->n->n = NULL;
702 h->n->s = NULL;
703 his_end = h->n;
704 history_counter++;
705 } else {
Eric Andersen91a44002000-07-19 17:37:57 +0000706 /* Add a new history command -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +0000707 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +0000708
709 h->n->p = h;
710 h->n->n = NULL;
711 h->n->s = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +0000712 h->s = strdup(command);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000713 his_end = h->n;
714
715 /* After max history, remove the oldest command */
716 if (history_counter >= MAX_HISTORY) {
717
718 struct history *p = his_front->n;
719
720 p->p = NULL;
721 free(his_front->s);
722 free(his_front);
723 his_front = p;
724 } else {
725 history_counter++;
726 }
Erik Andersen13456d12000-03-16 08:09:57 +0000727 }
Erik Andersen6273f652000-03-17 01:12:41 +0000728 }
Erik Andersen13456d12000-03-16 08:09:57 +0000729
Erik Andersenf0657d32000-04-12 17:49:52 +0000730 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000731}
732
733extern void cmdedit_init(void)
734{
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000735 win_changed(0);
736 signal(SIGWINCH, win_changed);
737
Eric Andersen501c88b2000-07-28 15:14:45 +0000738 if(exithandler_set == 0) {
739 atexit(cmdedit_reset_term); /* be sure to do this only once */
740 exithandler_set = 1;
741 }
Erik Andersen1d1d9502000-04-21 01:26:49 +0000742 signal(SIGKILL, clean_up_and_die);
Erik Andersenf0657d32000-04-12 17:49:52 +0000743 signal(SIGINT, clean_up_and_die);
744 signal(SIGQUIT, clean_up_and_die);
745 signal(SIGTERM, clean_up_and_die);
Erik Andersen13456d12000-03-16 08:09:57 +0000746}
Eric Andersen501c88b2000-07-28 15:14:45 +0000747
748/*
749** Undo the effects of cmdedit_init() as good as we can:
750** I am not aware of a way to revoke an atexit() handler,
751** but, fortunately, our particular handler can be made
752** a no-op by setting reset_term = 0.
753*/
754extern void cmdedit_terminate(void)
755{
756 cmdedit_reset_term();
757 reset_term = 0;
758 signal(SIGKILL, SIG_DFL);
759 signal(SIGINT, SIG_DFL);
760 signal(SIGQUIT, SIG_DFL);
761 signal(SIGTERM, SIG_DFL);
Eric Andersenb3dc3b82001-01-04 11:08:45 +0000762 signal(SIGWINCH, SIG_DFL);
Eric Andersen501c88b2000-07-28 15:14:45 +0000763}
764
765
766
Erik Andersenc7c634b2000-03-19 05:28:55 +0000767#endif /* BB_FEATURE_SH_COMMAND_EDITING */