blob: 0ce64beebf130c00943866cda8edab6cefb2c910 [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
34#include "internal.h"
35#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 */
72struct 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 */
Erik Andersen13456d12000-03-16 08:09:57 +000087
88struct history {
Erik Andersenc7c634b2000-03-19 05:28:55 +000089 char *s;
90 struct history *p;
91 struct history *n;
Erik Andersen13456d12000-03-16 08:09:57 +000092};
93
Erik Andersenf0657d32000-04-12 17:49:52 +000094#define xwrite write
Erik Andersen13456d12000-03-16 08:09:57 +000095
Mark Whitley55380702000-07-13 17:20:23 +000096/*
97 * TODO: Someday we want to implement 'horizontal scrolling' of the
98 * command-line when the user has typed more than the current width. This
99 * would allow the user to see a 'window' of what he has typed.
100 */
Erik Andersenf0657d32000-04-12 17:49:52 +0000101void
102cmdedit_setwidth(int w)
Erik Andersen13456d12000-03-16 08:09:57 +0000103{
Erik Andersen61677fe2000-04-13 01:18:56 +0000104 if (w > 20) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000105 cmdedit_termw = w;
106 cmdedit_scroll = w / 3;
Erik Andersen61677fe2000-04-13 01:18:56 +0000107 } else {
Erik Andersenf0657d32000-04-12 17:49:52 +0000108 errorMsg("\n*** Error: minimum screen width is 21\n");
Erik Andersen61677fe2000-04-13 01:18:56 +0000109 }
Erik Andersen13456d12000-03-16 08:09:57 +0000110}
111
Erik Andersen61677fe2000-04-13 01:18:56 +0000112
Erik Andersen13456d12000-03-16 08:09:57 +0000113void cmdedit_reset_term(void)
114{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000115 if (reset_term)
Erik Andersena6c75222000-04-18 00:00:52 +0000116 /* sparc and other have broken termios support: use old termio handling. */
Erik Andersen1d1d9502000-04-21 01:26:49 +0000117 setTermSettings(fileno(stdin), (void*) &initial_settings);
Eric Andersenb040d4f2000-07-25 18:01:20 +0000118#ifdef BB_FEATURE_CLEAN_UP
119 if (his_front) {
120 struct history *n;
121 //while(his_front!=his_end) {
122 while(his_front!=his_end) {
123 n = his_front->n;
124 free(his_front->s);
125 free(his_front);
126 his_front=n;
127 }
128 }
129#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000130}
131
Erik Andersenf0657d32000-04-12 17:49:52 +0000132void clean_up_and_die(int sig)
Erik Andersen13456d12000-03-16 08:09:57 +0000133{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000134 cmdedit_reset_term();
135 fprintf(stdout, "\n");
Erik Andersen1d1d9502000-04-21 01:26:49 +0000136 if (sig!=SIGINT)
137 exit(TRUE);
Erik Andersen13456d12000-03-16 08:09:57 +0000138}
139
Erik Andersenf0657d32000-04-12 17:49:52 +0000140/* Go to HOME position */
Erik Andersen13456d12000-03-16 08:09:57 +0000141void input_home(int outputFd, int *cursor)
Erik Andersenf0657d32000-04-12 17:49:52 +0000142{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000143 while (*cursor > 0) {
144 xwrite(outputFd, "\b", 1);
145 --*cursor;
146 }
Erik Andersen13456d12000-03-16 08:09:57 +0000147}
148
Erik Andersenf0657d32000-04-12 17:49:52 +0000149/* Go to END position */
Erik Andersen13456d12000-03-16 08:09:57 +0000150void input_end(int outputFd, int *cursor, int len)
151{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000152 while (*cursor < len) {
153 xwrite(outputFd, "\033[C", 3);
154 ++*cursor;
155 }
Erik Andersen13456d12000-03-16 08:09:57 +0000156}
157
Erik Andersenf0657d32000-04-12 17:49:52 +0000158/* Delete the char in back of the cursor */
159void input_backspace(char* command, int outputFd, int *cursor, int *len)
Erik Andersen13456d12000-03-16 08:09:57 +0000160{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000161 int j = 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000162
Eric Andersen72965e32000-07-04 06:22:18 +0000163/* Debug crap */
164//fprintf(stderr, "\nerik: len=%d, cursor=%d, strlen(command)='%d'\n", *len, *cursor, strlen(command));
165//xwrite(outputFd, command, *len);
166//*cursor = *len;
167
168
Erik Andersenc7c634b2000-03-19 05:28:55 +0000169 if (*cursor > 0) {
170 xwrite(outputFd, "\b \b", 3);
171 --*cursor;
Erik Andersenf0657d32000-04-12 17:49:52 +0000172 memmove(command + *cursor, command + *cursor + 1,
Erik Andersenc7c634b2000-03-19 05:28:55 +0000173 BUFSIZ - *cursor + 1);
Erik Andersen13456d12000-03-16 08:09:57 +0000174
Erik Andersenc7c634b2000-03-19 05:28:55 +0000175 for (j = *cursor; j < (BUFSIZ - 1); j++) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000176 if (!*(command + j))
Erik Andersenc7c634b2000-03-19 05:28:55 +0000177 break;
178 else
Erik Andersenf0657d32000-04-12 17:49:52 +0000179 xwrite(outputFd, (command + j), 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000180 }
181
182 xwrite(outputFd, " \b", 2);
183
184 while (j-- > *cursor)
185 xwrite(outputFd, "\b", 1);
186
187 --*len;
Erik Andersen13456d12000-03-16 08:09:57 +0000188 }
Erik Andersen13456d12000-03-16 08:09:57 +0000189}
190
Erik Andersenf0657d32000-04-12 17:49:52 +0000191/* Delete the char in front of the cursor */
192void input_delete(char* command, int outputFd, int cursor, int *len)
193{
194 int j = 0;
Erik Andersena2685732000-04-09 18:27:46 +0000195
Erik Andersenf0657d32000-04-12 17:49:52 +0000196 if (cursor == *len)
197 return;
198
199 memmove(command + cursor, command + cursor + 1,
200 BUFSIZ - cursor - 1);
201 for (j = cursor; j < (BUFSIZ - 1); j++) {
202 if (!*(command + j))
203 break;
204 else
205 xwrite(outputFd, (command + j), 1);
206 }
207
208 xwrite(outputFd, " \b", 2);
209
210 while (j-- > cursor)
211 xwrite(outputFd, "\b", 1);
212 --*len;
213}
214
215/* Move forward one charactor */
216void input_forward(int outputFd, int *cursor, int len)
217{
218 if (*cursor < len) {
219 xwrite(outputFd, "\033[C", 3);
220 ++*cursor;
221 }
222}
223
224/* Move back one charactor */
225void input_backward(int outputFd, int *cursor)
226{
227 if (*cursor > 0) {
228 xwrite(outputFd, "\033[D", 3);
229 --*cursor;
230 }
231}
232
233
234
235#ifdef BB_FEATURE_SH_TAB_COMPLETION
236char** username_tab_completion(char* command, int *num_matches)
Erik Andersen6273f652000-03-17 01:12:41 +0000237{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000238 char **matches = (char **) NULL;
239 *num_matches=0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000240 fprintf(stderr, "\nin username_tab_completion\n");
Erik Andersenc7c634b2000-03-19 05:28:55 +0000241 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000242}
Erik Andersen1dbe3402000-03-19 10:46:06 +0000243
244#include <dirent.h>
Erik Andersenf0657d32000-04-12 17:49:52 +0000245char** exe_n_cwd_tab_completion(char* command, int *num_matches)
Erik Andersen6273f652000-03-17 01:12:41 +0000246{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000247 char *dirName;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000248 char **matches = (char **) NULL;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000249 DIR *dir;
250 struct dirent *next;
251
252 matches = malloc( sizeof(char*)*50);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000253
Erik Andersen1dbe3402000-03-19 10:46:06 +0000254 /* Stick a wildcard onto the command, for later use */
255 strcat( command, "*");
Erik Andersenc7c634b2000-03-19 05:28:55 +0000256
Erik Andersen1dbe3402000-03-19 10:46:06 +0000257 /* Now wall the current directory */
258 dirName = get_current_dir_name();
259 dir = opendir(dirName);
260 if (!dir) {
261 /* Don't print an error, just shut up and return */
262 *num_matches=0;
263 return (matches);
264 }
265 while ((next = readdir(dir)) != NULL) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000266
Erik Andersen1dbe3402000-03-19 10:46:06 +0000267 /* Some quick sanity checks */
268 if ((strcmp(next->d_name, "..") == 0)
269 || (strcmp(next->d_name, ".") == 0)) {
270 continue;
271 }
272 /* See if this matches */
273 if (check_wildcard_match(next->d_name, command) == TRUE) {
274 /* Cool, found a match. Add it to the list */
275 matches[*num_matches] = malloc(strlen(next->d_name)+1);
276 strcpy( matches[*num_matches], next->d_name);
277 ++*num_matches;
278 //matches = realloc( matches, sizeof(char*)*(*num_matches));
279 }
280 }
281
Erik Andersenc7c634b2000-03-19 05:28:55 +0000282 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000283}
Erik Andersenf0657d32000-04-12 17:49:52 +0000284
Erik Andersen1d1d9502000-04-21 01:26:49 +0000285void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000286{
287 /* Do TAB completion */
288 static int num_matches=0;
289 static char **matches = (char **) NULL;
290 int pos = cursor;
291
292
293 if (lastWasTab == FALSE) {
294 char *tmp, *tmp1, *matchBuf;
295
296 /* For now, we will not bother with trying to distinguish
297 * whether the cursor is in/at a command extression -- we
Eric Andersen74c66ad2000-06-16 19:57:44 +0000298 * will always try all possible matches. If you don't like
Erik Andersenf0657d32000-04-12 17:49:52 +0000299 * that then feel free to fix it.
300 */
301
302 /* Make a local copy of the string -- up
303 * to the position of the cursor */
304 matchBuf = (char *) calloc(BUFSIZ, sizeof(char));
305 strncpy(matchBuf, command, cursor);
306 tmp=matchBuf;
307
308 /* skip past any command seperator tokens */
309 while (*tmp && (tmp1=strpbrk(tmp, ";|&{(`")) != NULL) {
310 tmp=++tmp1;
311 /* skip any leading white space */
312 while (*tmp && isspace(*tmp))
313 ++tmp;
314 }
315
316 /* skip any leading white space */
317 while (*tmp && isspace(*tmp))
318 ++tmp;
319
320 /* Free up any memory already allocated */
321 if (matches) {
322 free(matches);
323 matches = (char **) NULL;
324 }
325
326 /* If the word starts with `~' and there is no slash in the word,
327 * then try completing this word as a username. */
328
329 /* FIXME -- this check is broken! */
330 if (*tmp == '~' && !strchr(tmp, '/'))
331 matches = username_tab_completion(tmp, &num_matches);
332
333 /* Try to match any executable in our path and everything
334 * in the current working directory that matches. */
335 if (!matches)
336 matches = exe_n_cwd_tab_completion(tmp, &num_matches);
337
338 /* Don't leak memory */
339 free( matchBuf);
340
341 /* Did we find exactly one match? */
342 if (matches && num_matches==1) {
343 /* write out the matched command */
344 strncpy(command+pos, matches[0]+pos, strlen(matches[0])-pos);
345 len=strlen(command);
346 cursor=len;
347 xwrite(outputFd, matches[0]+pos, strlen(matches[0])-pos);
348 break;
349 }
350 } else {
351 /* Ok -- the last char was a TAB. Since they
352 * just hit TAB again, print a list of all the
353 * available choices... */
354 if ( matches && num_matches>0 ) {
355 int i, col;
356
357 /* Go to the next line */
358 xwrite(outputFd, "\n", 1);
359 /* Print the list of matches */
360 for (i=0,col=0; i<num_matches; i++) {
361 char foo[17];
362 sprintf(foo, "%-14s ", matches[i]);
363 col += xwrite(outputFd, foo, strlen(foo));
364 if (col > 60 && matches[i+1] != NULL) {
365 xwrite(outputFd, "\n", 1);
366 col = 0;
367 }
368 }
369 /* Go to the next line */
370 xwrite(outputFd, "\n", 1);
371 /* Rewrite the prompt */
372 xwrite(outputFd, prompt, strlen(prompt));
373 /* Rewrite the command */
374 xwrite(outputFd, command, len);
375 /* Put the cursor back to where it used to be */
376 for (cursor=len; cursor > pos; cursor--)
377 xwrite(outputFd, "\b", 1);
378 }
379 }
380}
381#endif
382
383void get_previous_history(struct history **hp, char* command)
384{
Eric Andersen91a44002000-07-19 17:37:57 +0000385 if ((*hp)->s)
386 free((*hp)->s);
Erik Andersenf0657d32000-04-12 17:49:52 +0000387 (*hp)->s = strdup(command);
388 *hp = (*hp)->p;
389}
390
391void get_next_history(struct history **hp, char* command)
392{
Eric Andersen91a44002000-07-19 17:37:57 +0000393 if ((*hp)->s)
394 free((*hp)->s);
Erik Andersenf0657d32000-04-12 17:49:52 +0000395 (*hp)->s = strdup(command);
396 *hp = (*hp)->n;
Erik Andersenf0657d32000-04-12 17:49:52 +0000397}
398
Erik Andersen6273f652000-03-17 01:12:41 +0000399/*
400 * This function is used to grab a character buffer
401 * from the input file descriptor and allows you to
402 * a string with full command editing (sortof like
403 * a mini readline).
404 *
405 * The following standard commands are not implemented:
406 * ESC-b -- Move back one word
407 * ESC-f -- Move forward one word
408 * ESC-d -- Delete back one word
409 * ESC-h -- Delete forward one word
410 * CTL-t -- Transpose two characters
411 *
412 * Furthermore, the "vi" command editing keys are not implemented.
413 *
414 * TODO: implement TAB command completion. :)
Erik Andersen6273f652000-03-17 01:12:41 +0000415 */
Erik Andersenf0657d32000-04-12 17:49:52 +0000416extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +0000417{
418
Erik Andersenf0657d32000-04-12 17:49:52 +0000419 int inputFd=fileno(stdin);
420 int outputFd=fileno(stdout);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000421 int nr = 0;
422 int len = 0;
423 int j = 0;
424 int cursor = 0;
425 int break_out = 0;
426 int ret = 0;
427 int lastWasTab = FALSE;
428 char c = 0;
429 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +0000430
Erik Andersenc7c634b2000-03-19 05:28:55 +0000431 if (!reset_term) {
Erik Andersen1d1d9502000-04-21 01:26:49 +0000432
433 getTermSettings(inputFd, (void*) &initial_settings);
434 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
435 new_settings.c_cc[VMIN] = 1;
436 new_settings.c_cc[VTIME] = 0;
437 new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */
438 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
439 new_settings.c_lflag &= ~(ECHO|ECHOCTL|ECHONL); /* Turn off echoing */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000440 reset_term = 1;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000441 }
Erik Andersen1d1d9502000-04-21 01:26:49 +0000442 setTermSettings(inputFd, (void*) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +0000443
Erik Andersenf0657d32000-04-12 17:49:52 +0000444 memset(command, 0, BUFSIZ);
Erik Andersen13456d12000-03-16 08:09:57 +0000445
Erik Andersenc7c634b2000-03-19 05:28:55 +0000446 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +0000447
Erik Andersen13456d12000-03-16 08:09:57 +0000448 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000449 return;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000450 //fprintf(stderr, "got a '%c' (%d)\n", c, c);
Erik Andersenf3b3d172000-04-09 18:24:05 +0000451
Erik Andersen13456d12000-03-16 08:09:57 +0000452 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000453 case '\n':
454 case '\r':
455 /* Enter */
456 *(command + len++ + 1) = c;
457 xwrite(outputFd, &c, 1);
458 break_out = 1;
459 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000460 case 1:
461 /* Control-a -- Beginning of line */
462 input_home(outputFd, &cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000463 case 2:
464 /* Control-b -- Move back one character */
465 input_backward(outputFd, &cursor);
466 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000467 case 3:
468 /* Control-c -- leave the current line,
469 * and start over on the next line */
470
471 /* Go to the next line */
472 xwrite(outputFd, "\n", 1);
473
474 /* Rewrite the prompt */
475 xwrite(outputFd, prompt, strlen(prompt));
476
477 /* Reset the command string */
Eric Andersen4ac6cb52000-07-14 01:13:37 +0000478 memset(command, 0, BUFSIZ);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000479 len = cursor = 0;
480
481 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000482 case 4:
483 /* Control-d -- Delete one character, or exit
484 * if the len=0 and no chars to delete */
485 if (len == 0) {
486 xwrite(outputFd, "exit", 4);
487 clean_up_and_die(0);
488 } else {
489 input_delete(command, outputFd, cursor, &len);
490 }
491 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000492 case 5:
493 /* Control-e -- End of line */
494 input_end(outputFd, &cursor, len);
495 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000496 case 6:
497 /* Control-f -- Move forward one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000498 input_forward(outputFd, &cursor, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000499 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000500 case '\b':
501 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +0000502 /* Control-h and DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +0000503 input_backspace(command, outputFd, &cursor, &len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000504 break;
505 case '\t':
Erik Andersena2685732000-04-09 18:27:46 +0000506#ifdef BB_FEATURE_SH_TAB_COMPLETION
Erik Andersen1d1d9502000-04-21 01:26:49 +0000507 input_tab(command, prompt, outputFd, &cursor, &len);
Erik Andersena2685732000-04-09 18:27:46 +0000508#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +0000509 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000510 case 14:
511 /* Control-n -- Get next command in history */
512 if (hp && hp->n && hp->n->s) {
513 get_next_history(&hp, command);
514 goto rewrite_line;
515 } else {
516 xwrite(outputFd, "\007", 1);
517 }
518 break;
519 case 16:
520 /* Control-p -- Get previous command from history */
521 if (hp && hp->p) {
522 get_previous_history(&hp, command);
523 goto rewrite_line;
524 } else {
525 xwrite(outputFd, "\007", 1);
526 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000527 break;
528 case ESC:{
529 /* escape sequence follows */
530 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000531 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000532
533 if (c == '[') { /* 91 */
534 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000535 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000536
537 switch (c) {
538 case 'A':
Erik Andersenf0657d32000-04-12 17:49:52 +0000539 /* Up Arrow -- Get previous command from history */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000540 if (hp && hp->p) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000541 get_previous_history(&hp, command);
542 goto rewrite_line;
543 } else {
544 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000545 }
546 break;
547 case 'B':
Erik Andersenf0657d32000-04-12 17:49:52 +0000548 /* Down Arrow -- Get next command in history */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000549 if (hp && hp->n && hp->n->s) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000550 get_next_history(&hp, command);
551 goto rewrite_line;
552 } else {
553 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000554 }
555 break;
556
Erik Andersenf0657d32000-04-12 17:49:52 +0000557 /* Rewrite the line with the selected history item */
558 rewrite_line:
559 /* erase old command from command line */
560 len = strlen(command)-strlen(hp->s);
Eric Andersen72965e32000-07-04 06:22:18 +0000561
562 while (len>cursor)
563 input_delete(command, outputFd, cursor, &len);
564 while (cursor>0)
Erik Andersenf0657d32000-04-12 17:49:52 +0000565 input_backspace(command, outputFd, &cursor, &len);
566 input_home(outputFd, &cursor);
567
Erik Andersenc7c634b2000-03-19 05:28:55 +0000568 /* write new command */
Erik Andersenf0657d32000-04-12 17:49:52 +0000569 strcpy(command, hp->s);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000570 len = strlen(hp->s);
Erik Andersenf0657d32000-04-12 17:49:52 +0000571 xwrite(outputFd, command, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000572 cursor = len;
573 break;
574 case 'C':
575 /* Right Arrow -- Move forward one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000576 input_forward(outputFd, &cursor, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000577 break;
578 case 'D':
579 /* Left Arrow -- Move back one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000580 input_backward(outputFd, &cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000581 break;
582 case '3':
583 /* Delete */
Erik Andersenf0657d32000-04-12 17:49:52 +0000584 input_delete(command, outputFd, cursor, &len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000585 break;
586 case '1':
587 /* Home (Ctrl-A) */
588 input_home(outputFd, &cursor);
589 break;
590 case '4':
591 /* End (Ctrl-E) */
592 input_end(outputFd, &cursor, len);
593 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000594 default:
595 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000596 }
597 if (c == '1' || c == '3' || c == '4')
598 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000599 return; /* read 126 (~) */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000600 }
601 if (c == 'O') {
602 /* 79 */
603 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000604 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000605 switch (c) {
606 case 'H':
607 /* Home (xterm) */
608 input_home(outputFd, &cursor);
609 break;
610 case 'F':
611 /* End (xterm) */
612 input_end(outputFd, &cursor, len);
613 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000614 default:
615 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000616 }
617 }
618 c = 0;
619 break;
620 }
621
622 default: /* If it's regular input, do the normal thing */
623
Erik Andersenf0657d32000-04-12 17:49:52 +0000624 if (!isprint(c)) { /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000625 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000626 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000627
628 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
629 break;
630
631 len++;
632
633 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +0000634 *(command + cursor) = c;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000635 } else { /* Insert otherwise */
Erik Andersenf0657d32000-04-12 17:49:52 +0000636 memmove(command + cursor + 1, command + cursor,
Erik Andersenc7c634b2000-03-19 05:28:55 +0000637 len - cursor - 1);
638
Erik Andersenf0657d32000-04-12 17:49:52 +0000639 *(command + cursor) = c;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000640
641 for (j = cursor; j < len; j++)
Erik Andersenf0657d32000-04-12 17:49:52 +0000642 xwrite(outputFd, command + j, 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000643 for (; j > cursor; j--)
644 xwrite(outputFd, "\033[D", 3);
645 }
646
Erik Andersen13456d12000-03-16 08:09:57 +0000647 cursor++;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000648 xwrite(outputFd, &c, 1);
649 break;
Erik Andersen13456d12000-03-16 08:09:57 +0000650 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000651 if (c == '\t')
652 lastWasTab = TRUE;
653 else
654 lastWasTab = FALSE;
655
656 if (break_out) /* Enter is the command terminator, no more input. */
657 break;
658 }
659
660 nr = len + 1;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000661 setTermSettings(inputFd, (void *) &initial_settings);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000662 reset_term = 0;
663
664
665 /* Handle command history log */
Erik Andersenf0657d32000-04-12 17:49:52 +0000666 if (*(command)) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000667
668 struct history *h = his_end;
669
670 if (!h) {
Eric Andersen91a44002000-07-19 17:37:57 +0000671 /* No previous history -- this memory is never freed */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000672 h = his_front = malloc(sizeof(struct history));
673 h->n = malloc(sizeof(struct history));
674
675 h->p = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +0000676 h->s = strdup(command);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000677 h->n->p = h;
678 h->n->n = NULL;
679 h->n->s = NULL;
680 his_end = h->n;
681 history_counter++;
682 } else {
Eric Andersen91a44002000-07-19 17:37:57 +0000683 /* Add a new history command -- this memory is never freed */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000684 h->n = malloc(sizeof(struct history));
685
686 h->n->p = h;
687 h->n->n = NULL;
688 h->n->s = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +0000689 h->s = strdup(command);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000690 his_end = h->n;
691
692 /* After max history, remove the oldest command */
693 if (history_counter >= MAX_HISTORY) {
694
695 struct history *p = his_front->n;
696
697 p->p = NULL;
698 free(his_front->s);
699 free(his_front);
700 his_front = p;
701 } else {
702 history_counter++;
703 }
Erik Andersen13456d12000-03-16 08:09:57 +0000704 }
Erik Andersen6273f652000-03-17 01:12:41 +0000705 }
Erik Andersen13456d12000-03-16 08:09:57 +0000706
Erik Andersenf0657d32000-04-12 17:49:52 +0000707 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000708}
709
710extern void cmdedit_init(void)
711{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000712 atexit(cmdedit_reset_term);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000713 signal(SIGKILL, clean_up_and_die);
Erik Andersenf0657d32000-04-12 17:49:52 +0000714 signal(SIGINT, clean_up_and_die);
715 signal(SIGQUIT, clean_up_and_die);
716 signal(SIGTERM, clean_up_and_die);
Erik Andersen13456d12000-03-16 08:09:57 +0000717}
Erik Andersenc7c634b2000-03-19 05:28:55 +0000718#endif /* BB_FEATURE_SH_COMMAND_EDITING */