blob: 0c9bda760375ad35ab86ea60bf1ddedf86443434 [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 */
Erik Andersenf0657d32000-04-12 17:49:52 +0000108void
109cmdedit_setwidth(int w)
Erik Andersen13456d12000-03-16 08:09:57 +0000110{
Erik Andersen61677fe2000-04-13 01:18:56 +0000111 if (w > 20) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000112 cmdedit_termw = w;
113 cmdedit_scroll = w / 3;
Erik Andersen61677fe2000-04-13 01:18:56 +0000114 } else {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000115 error_msg("\n*** Error: minimum screen width is 21\n");
Erik Andersen61677fe2000-04-13 01:18:56 +0000116 }
Erik Andersen13456d12000-03-16 08:09:57 +0000117}
118
Erik Andersen61677fe2000-04-13 01:18:56 +0000119
Erik Andersen13456d12000-03-16 08:09:57 +0000120void cmdedit_reset_term(void)
121{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000122 if (reset_term)
Erik Andersena6c75222000-04-18 00:00:52 +0000123 /* sparc and other have broken termios support: use old termio handling. */
Erik Andersen1d1d9502000-04-21 01:26:49 +0000124 setTermSettings(fileno(stdin), (void*) &initial_settings);
Eric Andersenb040d4f2000-07-25 18:01:20 +0000125#ifdef BB_FEATURE_CLEAN_UP
126 if (his_front) {
127 struct history *n;
128 //while(his_front!=his_end) {
129 while(his_front!=his_end) {
130 n = his_front->n;
131 free(his_front->s);
132 free(his_front);
133 his_front=n;
134 }
135 }
136#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000137}
138
Erik Andersenf0657d32000-04-12 17:49:52 +0000139void clean_up_and_die(int sig)
Erik Andersen13456d12000-03-16 08:09:57 +0000140{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000141 cmdedit_reset_term();
142 fprintf(stdout, "\n");
Erik Andersen1d1d9502000-04-21 01:26:49 +0000143 if (sig!=SIGINT)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000144 exit(EXIT_SUCCESS);
Erik Andersen13456d12000-03-16 08:09:57 +0000145}
146
Erik Andersenf0657d32000-04-12 17:49:52 +0000147/* Go to HOME position */
Erik Andersen13456d12000-03-16 08:09:57 +0000148void input_home(int outputFd, int *cursor)
Erik Andersenf0657d32000-04-12 17:49:52 +0000149{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000150 while (*cursor > 0) {
151 xwrite(outputFd, "\b", 1);
152 --*cursor;
153 }
Erik Andersen13456d12000-03-16 08:09:57 +0000154}
155
Erik Andersenf0657d32000-04-12 17:49:52 +0000156/* Go to END position */
Erik Andersen13456d12000-03-16 08:09:57 +0000157void input_end(int outputFd, int *cursor, int len)
158{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000159 while (*cursor < len) {
160 xwrite(outputFd, "\033[C", 3);
161 ++*cursor;
162 }
Erik Andersen13456d12000-03-16 08:09:57 +0000163}
164
Erik Andersenf0657d32000-04-12 17:49:52 +0000165/* Delete the char in back of the cursor */
166void input_backspace(char* command, int outputFd, int *cursor, int *len)
Erik Andersen13456d12000-03-16 08:09:57 +0000167{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000168 int j = 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000169
Eric Andersen72965e32000-07-04 06:22:18 +0000170/* Debug crap */
171//fprintf(stderr, "\nerik: len=%d, cursor=%d, strlen(command)='%d'\n", *len, *cursor, strlen(command));
172//xwrite(outputFd, command, *len);
173//*cursor = *len;
174
175
Erik Andersenc7c634b2000-03-19 05:28:55 +0000176 if (*cursor > 0) {
177 xwrite(outputFd, "\b \b", 3);
178 --*cursor;
Erik Andersenf0657d32000-04-12 17:49:52 +0000179 memmove(command + *cursor, command + *cursor + 1,
Erik Andersenc7c634b2000-03-19 05:28:55 +0000180 BUFSIZ - *cursor + 1);
Erik Andersen13456d12000-03-16 08:09:57 +0000181
Erik Andersenc7c634b2000-03-19 05:28:55 +0000182 for (j = *cursor; j < (BUFSIZ - 1); j++) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000183 if (!*(command + j))
Erik Andersenc7c634b2000-03-19 05:28:55 +0000184 break;
185 else
Erik Andersenf0657d32000-04-12 17:49:52 +0000186 xwrite(outputFd, (command + j), 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000187 }
188
189 xwrite(outputFd, " \b", 2);
190
191 while (j-- > *cursor)
192 xwrite(outputFd, "\b", 1);
193
194 --*len;
Erik Andersen13456d12000-03-16 08:09:57 +0000195 }
Erik Andersen13456d12000-03-16 08:09:57 +0000196}
197
Erik Andersenf0657d32000-04-12 17:49:52 +0000198/* Delete the char in front of the cursor */
199void input_delete(char* command, int outputFd, int cursor, int *len)
200{
201 int j = 0;
Erik Andersena2685732000-04-09 18:27:46 +0000202
Erik Andersenf0657d32000-04-12 17:49:52 +0000203 if (cursor == *len)
204 return;
205
206 memmove(command + cursor, command + cursor + 1,
207 BUFSIZ - cursor - 1);
208 for (j = cursor; j < (BUFSIZ - 1); j++) {
209 if (!*(command + j))
210 break;
211 else
212 xwrite(outputFd, (command + j), 1);
213 }
214
215 xwrite(outputFd, " \b", 2);
216
217 while (j-- > cursor)
218 xwrite(outputFd, "\b", 1);
219 --*len;
220}
221
222/* Move forward one charactor */
223void input_forward(int outputFd, int *cursor, int len)
224{
225 if (*cursor < len) {
226 xwrite(outputFd, "\033[C", 3);
227 ++*cursor;
228 }
229}
230
231/* Move back one charactor */
232void input_backward(int outputFd, int *cursor)
233{
234 if (*cursor > 0) {
235 xwrite(outputFd, "\033[D", 3);
236 --*cursor;
237 }
238}
239
240
241
242#ifdef BB_FEATURE_SH_TAB_COMPLETION
243char** username_tab_completion(char* command, int *num_matches)
Erik Andersen6273f652000-03-17 01:12:41 +0000244{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000245 char **matches = (char **) NULL;
246 *num_matches=0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000247 fprintf(stderr, "\nin username_tab_completion\n");
Erik Andersenc7c634b2000-03-19 05:28:55 +0000248 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000249}
Erik Andersen1dbe3402000-03-19 10:46:06 +0000250
251#include <dirent.h>
Erik Andersenf0657d32000-04-12 17:49:52 +0000252char** exe_n_cwd_tab_completion(char* command, int *num_matches)
Erik Andersen6273f652000-03-17 01:12:41 +0000253{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000254 char *dirName;
Matt Kraai322ae932000-09-13 02:46:14 +0000255 char **matches;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000256 DIR *dir;
257 struct dirent *next;
258
Matt Kraai322ae932000-09-13 02:46:14 +0000259 matches = xmalloc( sizeof(char*)*50);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000260
Erik Andersen1dbe3402000-03-19 10:46:06 +0000261 /* Stick a wildcard onto the command, for later use */
262 strcat( command, "*");
Erik Andersenc7c634b2000-03-19 05:28:55 +0000263
Erik Andersen1dbe3402000-03-19 10:46:06 +0000264 /* Now wall the current directory */
265 dirName = get_current_dir_name();
266 dir = opendir(dirName);
267 if (!dir) {
268 /* Don't print an error, just shut up and return */
269 *num_matches=0;
270 return (matches);
271 }
272 while ((next = readdir(dir)) != NULL) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000273
Erik Andersen1dbe3402000-03-19 10:46:06 +0000274 /* Some quick sanity checks */
275 if ((strcmp(next->d_name, "..") == 0)
276 || (strcmp(next->d_name, ".") == 0)) {
277 continue;
278 }
279 /* See if this matches */
280 if (check_wildcard_match(next->d_name, command) == TRUE) {
281 /* Cool, found a match. Add it to the list */
Matt Kraai322ae932000-09-13 02:46:14 +0000282 matches[*num_matches] = xmalloc(strlen(next->d_name)+1);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000283 strcpy( matches[*num_matches], next->d_name);
284 ++*num_matches;
285 //matches = realloc( matches, sizeof(char*)*(*num_matches));
286 }
287 }
288
Erik Andersenc7c634b2000-03-19 05:28:55 +0000289 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000290}
Erik Andersenf0657d32000-04-12 17:49:52 +0000291
Eric Andersena75466e2000-11-02 17:02:26 +0000292void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len, int lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000293{
294 /* Do TAB completion */
295 static int num_matches=0;
296 static char **matches = (char **) NULL;
Eric Andersena75466e2000-11-02 17:02:26 +0000297 int pos = *cursor;
Erik Andersenf0657d32000-04-12 17:49:52 +0000298
299
300 if (lastWasTab == FALSE) {
301 char *tmp, *tmp1, *matchBuf;
302
303 /* For now, we will not bother with trying to distinguish
304 * whether the cursor is in/at a command extression -- we
Eric Andersen74c66ad2000-06-16 19:57:44 +0000305 * will always try all possible matches. If you don't like
Erik Andersenf0657d32000-04-12 17:49:52 +0000306 * that then feel free to fix it.
307 */
308
309 /* Make a local copy of the string -- up
310 * to the position of the cursor */
Matt Kraai322ae932000-09-13 02:46:14 +0000311 matchBuf = (char *) xcalloc(BUFSIZ, sizeof(char));
Eric Andersena75466e2000-11-02 17:02:26 +0000312 strncpy(matchBuf, command, *cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000313 tmp=matchBuf;
314
315 /* skip past any command seperator tokens */
316 while (*tmp && (tmp1=strpbrk(tmp, ";|&{(`")) != NULL) {
317 tmp=++tmp1;
318 /* skip any leading white space */
319 while (*tmp && isspace(*tmp))
320 ++tmp;
321 }
322
323 /* skip any leading white space */
324 while (*tmp && isspace(*tmp))
325 ++tmp;
326
327 /* Free up any memory already allocated */
328 if (matches) {
329 free(matches);
330 matches = (char **) NULL;
331 }
332
333 /* If the word starts with `~' and there is no slash in the word,
334 * then try completing this word as a username. */
335
336 /* FIXME -- this check is broken! */
337 if (*tmp == '~' && !strchr(tmp, '/'))
338 matches = username_tab_completion(tmp, &num_matches);
339
340 /* Try to match any executable in our path and everything
341 * in the current working directory that matches. */
342 if (!matches)
343 matches = exe_n_cwd_tab_completion(tmp, &num_matches);
344
345 /* Don't leak memory */
346 free( matchBuf);
347
348 /* Did we find exactly one match? */
349 if (matches && num_matches==1) {
350 /* write out the matched command */
351 strncpy(command+pos, matches[0]+pos, strlen(matches[0])-pos);
Eric Andersena75466e2000-11-02 17:02:26 +0000352 *len=strlen(command);
353 *cursor=*len;
Erik Andersenf0657d32000-04-12 17:49:52 +0000354 xwrite(outputFd, matches[0]+pos, strlen(matches[0])-pos);
Eric Andersena75466e2000-11-02 17:02:26 +0000355 return;
Erik Andersenf0657d32000-04-12 17:49:52 +0000356 }
357 } else {
358 /* Ok -- the last char was a TAB. Since they
359 * just hit TAB again, print a list of all the
360 * available choices... */
361 if ( matches && num_matches>0 ) {
362 int i, col;
363
364 /* Go to the next line */
365 xwrite(outputFd, "\n", 1);
366 /* Print the list of matches */
367 for (i=0,col=0; i<num_matches; i++) {
368 char foo[17];
369 sprintf(foo, "%-14s ", matches[i]);
370 col += xwrite(outputFd, foo, strlen(foo));
371 if (col > 60 && matches[i+1] != NULL) {
372 xwrite(outputFd, "\n", 1);
373 col = 0;
374 }
375 }
376 /* Go to the next line */
377 xwrite(outputFd, "\n", 1);
378 /* Rewrite the prompt */
379 xwrite(outputFd, prompt, strlen(prompt));
380 /* Rewrite the command */
Eric Andersena75466e2000-11-02 17:02:26 +0000381 xwrite(outputFd, command, *len);
Erik Andersenf0657d32000-04-12 17:49:52 +0000382 /* Put the cursor back to where it used to be */
Eric Andersena75466e2000-11-02 17:02:26 +0000383 for (cursor=len; *cursor > pos; cursor--)
Erik Andersenf0657d32000-04-12 17:49:52 +0000384 xwrite(outputFd, "\b", 1);
385 }
386 }
387}
388#endif
389
390void get_previous_history(struct history **hp, char* command)
391{
Eric Andersen91a44002000-07-19 17:37:57 +0000392 if ((*hp)->s)
393 free((*hp)->s);
Erik Andersenf0657d32000-04-12 17:49:52 +0000394 (*hp)->s = strdup(command);
395 *hp = (*hp)->p;
396}
397
398void get_next_history(struct history **hp, char* command)
399{
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)->n;
Erik Andersenf0657d32000-04-12 17:49:52 +0000404}
405
Erik Andersen6273f652000-03-17 01:12:41 +0000406/*
407 * This function is used to grab a character buffer
408 * from the input file descriptor and allows you to
409 * a string with full command editing (sortof like
410 * a mini readline).
411 *
412 * The following standard commands are not implemented:
413 * ESC-b -- Move back one word
414 * ESC-f -- Move forward one word
415 * ESC-d -- Delete back one word
416 * ESC-h -- Delete forward one word
417 * CTL-t -- Transpose two characters
418 *
419 * Furthermore, the "vi" command editing keys are not implemented.
420 *
421 * TODO: implement TAB command completion. :)
Erik Andersen6273f652000-03-17 01:12:41 +0000422 */
Erik Andersenf0657d32000-04-12 17:49:52 +0000423extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +0000424{
425
Erik Andersenf0657d32000-04-12 17:49:52 +0000426 int inputFd=fileno(stdin);
427 int outputFd=fileno(stdout);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000428 int nr = 0;
429 int len = 0;
430 int j = 0;
431 int cursor = 0;
432 int break_out = 0;
433 int ret = 0;
434 int lastWasTab = FALSE;
435 char c = 0;
436 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +0000437
Erik Andersenc7c634b2000-03-19 05:28:55 +0000438 if (!reset_term) {
Erik Andersen1d1d9502000-04-21 01:26:49 +0000439
440 getTermSettings(inputFd, (void*) &initial_settings);
441 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
442 new_settings.c_cc[VMIN] = 1;
443 new_settings.c_cc[VTIME] = 0;
444 new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */
445 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
446 new_settings.c_lflag &= ~(ECHO|ECHOCTL|ECHONL); /* Turn off echoing */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000447 reset_term = 1;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000448 }
Erik Andersen1d1d9502000-04-21 01:26:49 +0000449 setTermSettings(inputFd, (void*) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +0000450
Erik Andersenf0657d32000-04-12 17:49:52 +0000451 memset(command, 0, BUFSIZ);
Erik Andersen13456d12000-03-16 08:09:57 +0000452
Erik Andersenc7c634b2000-03-19 05:28:55 +0000453 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +0000454
Erik Andersen13456d12000-03-16 08:09:57 +0000455 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000456 return;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000457 //fprintf(stderr, "got a '%c' (%d)\n", c, c);
Erik Andersenf3b3d172000-04-09 18:24:05 +0000458
Erik Andersen13456d12000-03-16 08:09:57 +0000459 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000460 case '\n':
461 case '\r':
462 /* Enter */
463 *(command + len++ + 1) = c;
464 xwrite(outputFd, &c, 1);
465 break_out = 1;
466 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000467 case 1:
468 /* Control-a -- Beginning of line */
469 input_home(outputFd, &cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000470 case 2:
471 /* Control-b -- Move back one character */
472 input_backward(outputFd, &cursor);
473 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000474 case 3:
Eric Andersen86349772000-12-18 20:25:50 +0000475 /* Control-c -- stop gathering input */
476
477 /* Link into lash to reset context to 0 on ^C and such */
478 shell_context = 0;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000479
480 /* Go to the next line */
481 xwrite(outputFd, "\n", 1);
482
Eric Andersen86349772000-12-18 20:25:50 +0000483#if 0
Erik Andersen1d1d9502000-04-21 01:26:49 +0000484 /* Rewrite the prompt */
485 xwrite(outputFd, prompt, strlen(prompt));
486
487 /* Reset the command string */
Eric Andersen4ac6cb52000-07-14 01:13:37 +0000488 memset(command, 0, BUFSIZ);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000489 len = cursor = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000490#endif
491 return;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000492
Erik Andersenf0657d32000-04-12 17:49:52 +0000493 case 4:
494 /* Control-d -- Delete one character, or exit
495 * if the len=0 and no chars to delete */
496 if (len == 0) {
497 xwrite(outputFd, "exit", 4);
498 clean_up_and_die(0);
499 } else {
500 input_delete(command, outputFd, cursor, &len);
501 }
502 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000503 case 5:
504 /* Control-e -- End of line */
505 input_end(outputFd, &cursor, len);
506 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000507 case 6:
508 /* Control-f -- Move forward one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000509 input_forward(outputFd, &cursor, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000510 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000511 case '\b':
512 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +0000513 /* Control-h and DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +0000514 input_backspace(command, outputFd, &cursor, &len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000515 break;
516 case '\t':
Erik Andersena2685732000-04-09 18:27:46 +0000517#ifdef BB_FEATURE_SH_TAB_COMPLETION
Eric Andersena75466e2000-11-02 17:02:26 +0000518 input_tab(command, prompt, outputFd, &cursor,
519&len, lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +0000520#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +0000521 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000522 case 14:
523 /* Control-n -- Get next command in history */
524 if (hp && hp->n && hp->n->s) {
525 get_next_history(&hp, command);
526 goto rewrite_line;
527 } else {
528 xwrite(outputFd, "\007", 1);
529 }
530 break;
531 case 16:
532 /* Control-p -- Get previous command from history */
533 if (hp && hp->p) {
534 get_previous_history(&hp, command);
535 goto rewrite_line;
536 } else {
537 xwrite(outputFd, "\007", 1);
538 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000539 break;
540 case ESC:{
541 /* escape sequence follows */
542 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000543 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000544
545 if (c == '[') { /* 91 */
546 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000547 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000548
549 switch (c) {
550 case 'A':
Erik Andersenf0657d32000-04-12 17:49:52 +0000551 /* Up Arrow -- Get previous command from history */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000552 if (hp && hp->p) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000553 get_previous_history(&hp, command);
554 goto rewrite_line;
555 } else {
556 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000557 }
558 break;
559 case 'B':
Erik Andersenf0657d32000-04-12 17:49:52 +0000560 /* Down Arrow -- Get next command in history */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000561 if (hp && hp->n && hp->n->s) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000562 get_next_history(&hp, command);
563 goto rewrite_line;
564 } else {
565 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000566 }
567 break;
568
Erik Andersenf0657d32000-04-12 17:49:52 +0000569 /* Rewrite the line with the selected history item */
570 rewrite_line:
571 /* erase old command from command line */
572 len = strlen(command)-strlen(hp->s);
Eric Andersen72965e32000-07-04 06:22:18 +0000573
574 while (len>cursor)
575 input_delete(command, outputFd, cursor, &len);
576 while (cursor>0)
Erik Andersenf0657d32000-04-12 17:49:52 +0000577 input_backspace(command, outputFd, &cursor, &len);
578 input_home(outputFd, &cursor);
579
Erik Andersenc7c634b2000-03-19 05:28:55 +0000580 /* write new command */
Erik Andersenf0657d32000-04-12 17:49:52 +0000581 strcpy(command, hp->s);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000582 len = strlen(hp->s);
Erik Andersenf0657d32000-04-12 17:49:52 +0000583 xwrite(outputFd, command, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000584 cursor = len;
585 break;
586 case 'C':
587 /* Right Arrow -- Move forward one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000588 input_forward(outputFd, &cursor, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000589 break;
590 case 'D':
591 /* Left Arrow -- Move back one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000592 input_backward(outputFd, &cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000593 break;
594 case '3':
595 /* Delete */
Erik Andersenf0657d32000-04-12 17:49:52 +0000596 input_delete(command, outputFd, cursor, &len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000597 break;
598 case '1':
599 /* Home (Ctrl-A) */
600 input_home(outputFd, &cursor);
601 break;
602 case '4':
603 /* End (Ctrl-E) */
604 input_end(outputFd, &cursor, len);
605 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000606 default:
607 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000608 }
609 if (c == '1' || c == '3' || c == '4')
610 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000611 return; /* read 126 (~) */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000612 }
613 if (c == 'O') {
614 /* 79 */
615 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000616 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000617 switch (c) {
618 case 'H':
619 /* Home (xterm) */
620 input_home(outputFd, &cursor);
621 break;
622 case 'F':
623 /* End (xterm) */
624 input_end(outputFd, &cursor, len);
625 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000626 default:
627 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000628 }
629 }
630 c = 0;
631 break;
632 }
633
634 default: /* If it's regular input, do the normal thing */
635
Erik Andersenf0657d32000-04-12 17:49:52 +0000636 if (!isprint(c)) { /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000637 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000638 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000639
640 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
641 break;
642
643 len++;
644
645 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +0000646 *(command + cursor) = c;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000647 } else { /* Insert otherwise */
Erik Andersenf0657d32000-04-12 17:49:52 +0000648 memmove(command + cursor + 1, command + cursor,
Erik Andersenc7c634b2000-03-19 05:28:55 +0000649 len - cursor - 1);
650
Erik Andersenf0657d32000-04-12 17:49:52 +0000651 *(command + cursor) = c;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000652
653 for (j = cursor; j < len; j++)
Erik Andersenf0657d32000-04-12 17:49:52 +0000654 xwrite(outputFd, command + j, 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000655 for (; j > cursor; j--)
656 xwrite(outputFd, "\033[D", 3);
657 }
658
Erik Andersen13456d12000-03-16 08:09:57 +0000659 cursor++;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000660 xwrite(outputFd, &c, 1);
661 break;
Erik Andersen13456d12000-03-16 08:09:57 +0000662 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000663 if (c == '\t')
664 lastWasTab = TRUE;
665 else
666 lastWasTab = FALSE;
667
668 if (break_out) /* Enter is the command terminator, no more input. */
669 break;
670 }
671
672 nr = len + 1;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000673 setTermSettings(inputFd, (void *) &initial_settings);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000674 reset_term = 0;
675
676
677 /* Handle command history log */
Erik Andersenf0657d32000-04-12 17:49:52 +0000678 if (*(command)) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000679
680 struct history *h = his_end;
681
682 if (!h) {
Eric Andersen91a44002000-07-19 17:37:57 +0000683 /* No previous history -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +0000684 h = his_front = xmalloc(sizeof(struct history));
685 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +0000686
687 h->p = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +0000688 h->s = strdup(command);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000689 h->n->p = h;
690 h->n->n = NULL;
691 h->n->s = NULL;
692 his_end = h->n;
693 history_counter++;
694 } else {
Eric Andersen91a44002000-07-19 17:37:57 +0000695 /* Add a new history command -- this memory is never freed */
Matt Kraai322ae932000-09-13 02:46:14 +0000696 h->n = xmalloc(sizeof(struct history));
Erik Andersenc7c634b2000-03-19 05:28:55 +0000697
698 h->n->p = h;
699 h->n->n = NULL;
700 h->n->s = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +0000701 h->s = strdup(command);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000702 his_end = h->n;
703
704 /* After max history, remove the oldest command */
705 if (history_counter >= MAX_HISTORY) {
706
707 struct history *p = his_front->n;
708
709 p->p = NULL;
710 free(his_front->s);
711 free(his_front);
712 his_front = p;
713 } else {
714 history_counter++;
715 }
Erik Andersen13456d12000-03-16 08:09:57 +0000716 }
Erik Andersen6273f652000-03-17 01:12:41 +0000717 }
Erik Andersen13456d12000-03-16 08:09:57 +0000718
Erik Andersenf0657d32000-04-12 17:49:52 +0000719 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000720}
721
722extern void cmdedit_init(void)
723{
Eric Andersen501c88b2000-07-28 15:14:45 +0000724 if(exithandler_set == 0) {
725 atexit(cmdedit_reset_term); /* be sure to do this only once */
726 exithandler_set = 1;
727 }
Erik Andersen1d1d9502000-04-21 01:26:49 +0000728 signal(SIGKILL, clean_up_and_die);
Erik Andersenf0657d32000-04-12 17:49:52 +0000729 signal(SIGINT, clean_up_and_die);
730 signal(SIGQUIT, clean_up_and_die);
731 signal(SIGTERM, clean_up_and_die);
Erik Andersen13456d12000-03-16 08:09:57 +0000732}
Eric Andersen501c88b2000-07-28 15:14:45 +0000733
734/*
735** Undo the effects of cmdedit_init() as good as we can:
736** I am not aware of a way to revoke an atexit() handler,
737** but, fortunately, our particular handler can be made
738** a no-op by setting reset_term = 0.
739*/
740extern void cmdedit_terminate(void)
741{
742 cmdedit_reset_term();
743 reset_term = 0;
744 signal(SIGKILL, SIG_DFL);
745 signal(SIGINT, SIG_DFL);
746 signal(SIGQUIT, SIG_DFL);
747 signal(SIGTERM, SIG_DFL);
748}
749
750
751
Erik Andersenc7c634b2000-03-19 05:28:55 +0000752#endif /* BB_FEATURE_SH_COMMAND_EDITING */