blob: 0de18e81f6bf4695e2840ce898959ca1999fccb0 [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);
Erik Andersen13456d12000-03-16 08:09:57 +0000118}
119
Erik Andersenf0657d32000-04-12 17:49:52 +0000120void clean_up_and_die(int sig)
Erik Andersen13456d12000-03-16 08:09:57 +0000121{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000122 cmdedit_reset_term();
123 fprintf(stdout, "\n");
Erik Andersen1d1d9502000-04-21 01:26:49 +0000124 if (sig!=SIGINT)
125 exit(TRUE);
Erik Andersen13456d12000-03-16 08:09:57 +0000126}
127
Erik Andersenf0657d32000-04-12 17:49:52 +0000128/* Go to HOME position */
Erik Andersen13456d12000-03-16 08:09:57 +0000129void input_home(int outputFd, int *cursor)
Erik Andersenf0657d32000-04-12 17:49:52 +0000130{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000131 while (*cursor > 0) {
132 xwrite(outputFd, "\b", 1);
133 --*cursor;
134 }
Erik Andersen13456d12000-03-16 08:09:57 +0000135}
136
Erik Andersenf0657d32000-04-12 17:49:52 +0000137/* Go to END position */
Erik Andersen13456d12000-03-16 08:09:57 +0000138void input_end(int outputFd, int *cursor, int len)
139{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000140 while (*cursor < len) {
141 xwrite(outputFd, "\033[C", 3);
142 ++*cursor;
143 }
Erik Andersen13456d12000-03-16 08:09:57 +0000144}
145
Erik Andersenf0657d32000-04-12 17:49:52 +0000146/* Delete the char in back of the cursor */
147void input_backspace(char* command, int outputFd, int *cursor, int *len)
Erik Andersen13456d12000-03-16 08:09:57 +0000148{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000149 int j = 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000150
Eric Andersen72965e32000-07-04 06:22:18 +0000151/* Debug crap */
152//fprintf(stderr, "\nerik: len=%d, cursor=%d, strlen(command)='%d'\n", *len, *cursor, strlen(command));
153//xwrite(outputFd, command, *len);
154//*cursor = *len;
155
156
Erik Andersenc7c634b2000-03-19 05:28:55 +0000157 if (*cursor > 0) {
158 xwrite(outputFd, "\b \b", 3);
159 --*cursor;
Erik Andersenf0657d32000-04-12 17:49:52 +0000160 memmove(command + *cursor, command + *cursor + 1,
Erik Andersenc7c634b2000-03-19 05:28:55 +0000161 BUFSIZ - *cursor + 1);
Erik Andersen13456d12000-03-16 08:09:57 +0000162
Erik Andersenc7c634b2000-03-19 05:28:55 +0000163 for (j = *cursor; j < (BUFSIZ - 1); j++) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000164 if (!*(command + j))
Erik Andersenc7c634b2000-03-19 05:28:55 +0000165 break;
166 else
Erik Andersenf0657d32000-04-12 17:49:52 +0000167 xwrite(outputFd, (command + j), 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000168 }
169
170 xwrite(outputFd, " \b", 2);
171
172 while (j-- > *cursor)
173 xwrite(outputFd, "\b", 1);
174
175 --*len;
Erik Andersen13456d12000-03-16 08:09:57 +0000176 }
Erik Andersen13456d12000-03-16 08:09:57 +0000177}
178
Erik Andersenf0657d32000-04-12 17:49:52 +0000179/* Delete the char in front of the cursor */
180void input_delete(char* command, int outputFd, int cursor, int *len)
181{
182 int j = 0;
Erik Andersena2685732000-04-09 18:27:46 +0000183
Erik Andersenf0657d32000-04-12 17:49:52 +0000184 if (cursor == *len)
185 return;
186
187 memmove(command + cursor, command + cursor + 1,
188 BUFSIZ - cursor - 1);
189 for (j = cursor; j < (BUFSIZ - 1); j++) {
190 if (!*(command + j))
191 break;
192 else
193 xwrite(outputFd, (command + j), 1);
194 }
195
196 xwrite(outputFd, " \b", 2);
197
198 while (j-- > cursor)
199 xwrite(outputFd, "\b", 1);
200 --*len;
201}
202
203/* Move forward one charactor */
204void input_forward(int outputFd, int *cursor, int len)
205{
206 if (*cursor < len) {
207 xwrite(outputFd, "\033[C", 3);
208 ++*cursor;
209 }
210}
211
212/* Move back one charactor */
213void input_backward(int outputFd, int *cursor)
214{
215 if (*cursor > 0) {
216 xwrite(outputFd, "\033[D", 3);
217 --*cursor;
218 }
219}
220
221
222
223#ifdef BB_FEATURE_SH_TAB_COMPLETION
224char** username_tab_completion(char* command, int *num_matches)
Erik Andersen6273f652000-03-17 01:12:41 +0000225{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000226 char **matches = (char **) NULL;
227 *num_matches=0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000228 fprintf(stderr, "\nin username_tab_completion\n");
Erik Andersenc7c634b2000-03-19 05:28:55 +0000229 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000230}
Erik Andersen1dbe3402000-03-19 10:46:06 +0000231
232#include <dirent.h>
Erik Andersenf0657d32000-04-12 17:49:52 +0000233char** exe_n_cwd_tab_completion(char* command, int *num_matches)
Erik Andersen6273f652000-03-17 01:12:41 +0000234{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000235 char *dirName;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000236 char **matches = (char **) NULL;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000237 DIR *dir;
238 struct dirent *next;
239
240 matches = malloc( sizeof(char*)*50);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000241
Erik Andersen1dbe3402000-03-19 10:46:06 +0000242 /* Stick a wildcard onto the command, for later use */
243 strcat( command, "*");
Erik Andersenc7c634b2000-03-19 05:28:55 +0000244
Erik Andersen1dbe3402000-03-19 10:46:06 +0000245 /* Now wall the current directory */
246 dirName = get_current_dir_name();
247 dir = opendir(dirName);
248 if (!dir) {
249 /* Don't print an error, just shut up and return */
250 *num_matches=0;
251 return (matches);
252 }
253 while ((next = readdir(dir)) != NULL) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000254
Erik Andersen1dbe3402000-03-19 10:46:06 +0000255 /* Some quick sanity checks */
256 if ((strcmp(next->d_name, "..") == 0)
257 || (strcmp(next->d_name, ".") == 0)) {
258 continue;
259 }
260 /* See if this matches */
261 if (check_wildcard_match(next->d_name, command) == TRUE) {
262 /* Cool, found a match. Add it to the list */
263 matches[*num_matches] = malloc(strlen(next->d_name)+1);
264 strcpy( matches[*num_matches], next->d_name);
265 ++*num_matches;
266 //matches = realloc( matches, sizeof(char*)*(*num_matches));
267 }
268 }
269
Erik Andersenc7c634b2000-03-19 05:28:55 +0000270 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000271}
Erik Andersenf0657d32000-04-12 17:49:52 +0000272
Erik Andersen1d1d9502000-04-21 01:26:49 +0000273void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000274{
275 /* Do TAB completion */
276 static int num_matches=0;
277 static char **matches = (char **) NULL;
278 int pos = cursor;
279
280
281 if (lastWasTab == FALSE) {
282 char *tmp, *tmp1, *matchBuf;
283
284 /* For now, we will not bother with trying to distinguish
285 * whether the cursor is in/at a command extression -- we
Eric Andersen74c66ad2000-06-16 19:57:44 +0000286 * will always try all possible matches. If you don't like
Erik Andersenf0657d32000-04-12 17:49:52 +0000287 * that then feel free to fix it.
288 */
289
290 /* Make a local copy of the string -- up
291 * to the position of the cursor */
292 matchBuf = (char *) calloc(BUFSIZ, sizeof(char));
293 strncpy(matchBuf, command, cursor);
294 tmp=matchBuf;
295
296 /* skip past any command seperator tokens */
297 while (*tmp && (tmp1=strpbrk(tmp, ";|&{(`")) != NULL) {
298 tmp=++tmp1;
299 /* skip any leading white space */
300 while (*tmp && isspace(*tmp))
301 ++tmp;
302 }
303
304 /* skip any leading white space */
305 while (*tmp && isspace(*tmp))
306 ++tmp;
307
308 /* Free up any memory already allocated */
309 if (matches) {
310 free(matches);
311 matches = (char **) NULL;
312 }
313
314 /* If the word starts with `~' and there is no slash in the word,
315 * then try completing this word as a username. */
316
317 /* FIXME -- this check is broken! */
318 if (*tmp == '~' && !strchr(tmp, '/'))
319 matches = username_tab_completion(tmp, &num_matches);
320
321 /* Try to match any executable in our path and everything
322 * in the current working directory that matches. */
323 if (!matches)
324 matches = exe_n_cwd_tab_completion(tmp, &num_matches);
325
326 /* Don't leak memory */
327 free( matchBuf);
328
329 /* Did we find exactly one match? */
330 if (matches && num_matches==1) {
331 /* write out the matched command */
332 strncpy(command+pos, matches[0]+pos, strlen(matches[0])-pos);
333 len=strlen(command);
334 cursor=len;
335 xwrite(outputFd, matches[0]+pos, strlen(matches[0])-pos);
336 break;
337 }
338 } else {
339 /* Ok -- the last char was a TAB. Since they
340 * just hit TAB again, print a list of all the
341 * available choices... */
342 if ( matches && num_matches>0 ) {
343 int i, col;
344
345 /* Go to the next line */
346 xwrite(outputFd, "\n", 1);
347 /* Print the list of matches */
348 for (i=0,col=0; i<num_matches; i++) {
349 char foo[17];
350 sprintf(foo, "%-14s ", matches[i]);
351 col += xwrite(outputFd, foo, strlen(foo));
352 if (col > 60 && matches[i+1] != NULL) {
353 xwrite(outputFd, "\n", 1);
354 col = 0;
355 }
356 }
357 /* Go to the next line */
358 xwrite(outputFd, "\n", 1);
359 /* Rewrite the prompt */
360 xwrite(outputFd, prompt, strlen(prompt));
361 /* Rewrite the command */
362 xwrite(outputFd, command, len);
363 /* Put the cursor back to where it used to be */
364 for (cursor=len; cursor > pos; cursor--)
365 xwrite(outputFd, "\b", 1);
366 }
367 }
368}
369#endif
370
371void get_previous_history(struct history **hp, char* command)
372{
Eric Andersen91a44002000-07-19 17:37:57 +0000373 if ((*hp)->s)
374 free((*hp)->s);
Erik Andersenf0657d32000-04-12 17:49:52 +0000375 (*hp)->s = strdup(command);
376 *hp = (*hp)->p;
377}
378
379void get_next_history(struct history **hp, char* command)
380{
Eric Andersen91a44002000-07-19 17:37:57 +0000381 if ((*hp)->s)
382 free((*hp)->s);
Erik Andersenf0657d32000-04-12 17:49:52 +0000383 (*hp)->s = strdup(command);
384 *hp = (*hp)->n;
Erik Andersenf0657d32000-04-12 17:49:52 +0000385}
386
Erik Andersen6273f652000-03-17 01:12:41 +0000387/*
388 * This function is used to grab a character buffer
389 * from the input file descriptor and allows you to
390 * a string with full command editing (sortof like
391 * a mini readline).
392 *
393 * The following standard commands are not implemented:
394 * ESC-b -- Move back one word
395 * ESC-f -- Move forward one word
396 * ESC-d -- Delete back one word
397 * ESC-h -- Delete forward one word
398 * CTL-t -- Transpose two characters
399 *
400 * Furthermore, the "vi" command editing keys are not implemented.
401 *
402 * TODO: implement TAB command completion. :)
Erik Andersen6273f652000-03-17 01:12:41 +0000403 */
Erik Andersenf0657d32000-04-12 17:49:52 +0000404extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +0000405{
406
Erik Andersenf0657d32000-04-12 17:49:52 +0000407 int inputFd=fileno(stdin);
408 int outputFd=fileno(stdout);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000409 int nr = 0;
410 int len = 0;
411 int j = 0;
412 int cursor = 0;
413 int break_out = 0;
414 int ret = 0;
415 int lastWasTab = FALSE;
416 char c = 0;
417 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +0000418
Erik Andersenc7c634b2000-03-19 05:28:55 +0000419 if (!reset_term) {
Erik Andersen1d1d9502000-04-21 01:26:49 +0000420
421 getTermSettings(inputFd, (void*) &initial_settings);
422 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
423 new_settings.c_cc[VMIN] = 1;
424 new_settings.c_cc[VTIME] = 0;
425 new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */
426 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
427 new_settings.c_lflag &= ~(ECHO|ECHOCTL|ECHONL); /* Turn off echoing */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000428 reset_term = 1;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000429 }
Erik Andersen1d1d9502000-04-21 01:26:49 +0000430 setTermSettings(inputFd, (void*) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +0000431
Erik Andersenf0657d32000-04-12 17:49:52 +0000432 memset(command, 0, BUFSIZ);
Erik Andersen13456d12000-03-16 08:09:57 +0000433
Erik Andersenc7c634b2000-03-19 05:28:55 +0000434 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +0000435
Erik Andersen13456d12000-03-16 08:09:57 +0000436 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000437 return;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000438 //fprintf(stderr, "got a '%c' (%d)\n", c, c);
Erik Andersenf3b3d172000-04-09 18:24:05 +0000439
Erik Andersen13456d12000-03-16 08:09:57 +0000440 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000441 case '\n':
442 case '\r':
443 /* Enter */
444 *(command + len++ + 1) = c;
445 xwrite(outputFd, &c, 1);
446 break_out = 1;
447 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000448 case 1:
449 /* Control-a -- Beginning of line */
450 input_home(outputFd, &cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000451 case 2:
452 /* Control-b -- Move back one character */
453 input_backward(outputFd, &cursor);
454 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000455 case 3:
456 /* Control-c -- leave the current line,
457 * and start over on the next line */
458
459 /* Go to the next line */
460 xwrite(outputFd, "\n", 1);
461
462 /* Rewrite the prompt */
463 xwrite(outputFd, prompt, strlen(prompt));
464
465 /* Reset the command string */
Eric Andersen4ac6cb52000-07-14 01:13:37 +0000466 memset(command, 0, BUFSIZ);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000467 len = cursor = 0;
468
469 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000470 case 4:
471 /* Control-d -- Delete one character, or exit
472 * if the len=0 and no chars to delete */
473 if (len == 0) {
474 xwrite(outputFd, "exit", 4);
475 clean_up_and_die(0);
476 } else {
477 input_delete(command, outputFd, cursor, &len);
478 }
479 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000480 case 5:
481 /* Control-e -- End of line */
482 input_end(outputFd, &cursor, len);
483 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000484 case 6:
485 /* Control-f -- Move forward one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000486 input_forward(outputFd, &cursor, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000487 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000488 case '\b':
489 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +0000490 /* Control-h and DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +0000491 input_backspace(command, outputFd, &cursor, &len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000492 break;
493 case '\t':
Erik Andersena2685732000-04-09 18:27:46 +0000494#ifdef BB_FEATURE_SH_TAB_COMPLETION
Erik Andersen1d1d9502000-04-21 01:26:49 +0000495 input_tab(command, prompt, outputFd, &cursor, &len);
Erik Andersena2685732000-04-09 18:27:46 +0000496#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +0000497 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000498 case 14:
499 /* Control-n -- Get next command in history */
500 if (hp && hp->n && hp->n->s) {
501 get_next_history(&hp, command);
502 goto rewrite_line;
503 } else {
504 xwrite(outputFd, "\007", 1);
505 }
506 break;
507 case 16:
508 /* Control-p -- Get previous command from history */
509 if (hp && hp->p) {
510 get_previous_history(&hp, command);
511 goto rewrite_line;
512 } else {
513 xwrite(outputFd, "\007", 1);
514 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000515 break;
516 case ESC:{
517 /* escape sequence follows */
518 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000519 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000520
521 if (c == '[') { /* 91 */
522 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000523 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000524
525 switch (c) {
526 case 'A':
Erik Andersenf0657d32000-04-12 17:49:52 +0000527 /* Up Arrow -- Get previous command from history */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000528 if (hp && hp->p) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000529 get_previous_history(&hp, command);
530 goto rewrite_line;
531 } else {
532 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000533 }
534 break;
535 case 'B':
Erik Andersenf0657d32000-04-12 17:49:52 +0000536 /* Down Arrow -- Get next command in history */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000537 if (hp && hp->n && hp->n->s) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000538 get_next_history(&hp, command);
539 goto rewrite_line;
540 } else {
541 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000542 }
543 break;
544
Erik Andersenf0657d32000-04-12 17:49:52 +0000545 /* Rewrite the line with the selected history item */
546 rewrite_line:
547 /* erase old command from command line */
548 len = strlen(command)-strlen(hp->s);
Eric Andersen72965e32000-07-04 06:22:18 +0000549
550 while (len>cursor)
551 input_delete(command, outputFd, cursor, &len);
552 while (cursor>0)
Erik Andersenf0657d32000-04-12 17:49:52 +0000553 input_backspace(command, outputFd, &cursor, &len);
554 input_home(outputFd, &cursor);
555
Erik Andersenc7c634b2000-03-19 05:28:55 +0000556 /* write new command */
Erik Andersenf0657d32000-04-12 17:49:52 +0000557 strcpy(command, hp->s);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000558 len = strlen(hp->s);
Erik Andersenf0657d32000-04-12 17:49:52 +0000559 xwrite(outputFd, command, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000560 cursor = len;
561 break;
562 case 'C':
563 /* Right Arrow -- Move forward one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000564 input_forward(outputFd, &cursor, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000565 break;
566 case 'D':
567 /* Left Arrow -- Move back one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000568 input_backward(outputFd, &cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000569 break;
570 case '3':
571 /* Delete */
Erik Andersenf0657d32000-04-12 17:49:52 +0000572 input_delete(command, outputFd, cursor, &len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000573 break;
574 case '1':
575 /* Home (Ctrl-A) */
576 input_home(outputFd, &cursor);
577 break;
578 case '4':
579 /* End (Ctrl-E) */
580 input_end(outputFd, &cursor, len);
581 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000582 default:
583 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000584 }
585 if (c == '1' || c == '3' || c == '4')
586 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000587 return; /* read 126 (~) */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000588 }
589 if (c == 'O') {
590 /* 79 */
591 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000592 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000593 switch (c) {
594 case 'H':
595 /* Home (xterm) */
596 input_home(outputFd, &cursor);
597 break;
598 case 'F':
599 /* End (xterm) */
600 input_end(outputFd, &cursor, len);
601 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000602 default:
603 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000604 }
605 }
606 c = 0;
607 break;
608 }
609
610 default: /* If it's regular input, do the normal thing */
611
Erik Andersenf0657d32000-04-12 17:49:52 +0000612 if (!isprint(c)) { /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000613 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000614 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000615
616 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
617 break;
618
619 len++;
620
621 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +0000622 *(command + cursor) = c;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000623 } else { /* Insert otherwise */
Erik Andersenf0657d32000-04-12 17:49:52 +0000624 memmove(command + cursor + 1, command + cursor,
Erik Andersenc7c634b2000-03-19 05:28:55 +0000625 len - cursor - 1);
626
Erik Andersenf0657d32000-04-12 17:49:52 +0000627 *(command + cursor) = c;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000628
629 for (j = cursor; j < len; j++)
Erik Andersenf0657d32000-04-12 17:49:52 +0000630 xwrite(outputFd, command + j, 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000631 for (; j > cursor; j--)
632 xwrite(outputFd, "\033[D", 3);
633 }
634
Erik Andersen13456d12000-03-16 08:09:57 +0000635 cursor++;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000636 xwrite(outputFd, &c, 1);
637 break;
Erik Andersen13456d12000-03-16 08:09:57 +0000638 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000639 if (c == '\t')
640 lastWasTab = TRUE;
641 else
642 lastWasTab = FALSE;
643
644 if (break_out) /* Enter is the command terminator, no more input. */
645 break;
646 }
647
648 nr = len + 1;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000649 setTermSettings(inputFd, (void *) &initial_settings);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000650 reset_term = 0;
651
652
653 /* Handle command history log */
Erik Andersenf0657d32000-04-12 17:49:52 +0000654 if (*(command)) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000655
656 struct history *h = his_end;
657
658 if (!h) {
Eric Andersen91a44002000-07-19 17:37:57 +0000659 /* No previous history -- this memory is never freed */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000660 h = his_front = malloc(sizeof(struct history));
661 h->n = malloc(sizeof(struct history));
662
663 h->p = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +0000664 h->s = strdup(command);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000665 h->n->p = h;
666 h->n->n = NULL;
667 h->n->s = NULL;
668 his_end = h->n;
669 history_counter++;
670 } else {
Eric Andersen91a44002000-07-19 17:37:57 +0000671 /* Add a new history command -- this memory is never freed */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000672 h->n = malloc(sizeof(struct history));
673
674 h->n->p = h;
675 h->n->n = NULL;
676 h->n->s = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +0000677 h->s = strdup(command);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000678 his_end = h->n;
679
680 /* After max history, remove the oldest command */
681 if (history_counter >= MAX_HISTORY) {
682
683 struct history *p = his_front->n;
684
685 p->p = NULL;
686 free(his_front->s);
687 free(his_front);
688 his_front = p;
689 } else {
690 history_counter++;
691 }
Erik Andersen13456d12000-03-16 08:09:57 +0000692 }
Erik Andersen6273f652000-03-17 01:12:41 +0000693 }
Erik Andersen13456d12000-03-16 08:09:57 +0000694
Erik Andersenf0657d32000-04-12 17:49:52 +0000695 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000696}
697
698extern void cmdedit_init(void)
699{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000700 atexit(cmdedit_reset_term);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000701 signal(SIGKILL, clean_up_and_die);
Erik Andersenf0657d32000-04-12 17:49:52 +0000702 signal(SIGINT, clean_up_and_die);
703 signal(SIGQUIT, clean_up_and_die);
704 signal(SIGTERM, clean_up_and_die);
Erik Andersen13456d12000-03-16 08:09:57 +0000705}
Erik Andersenc7c634b2000-03-19 05:28:55 +0000706#endif /* BB_FEATURE_SH_COMMAND_EDITING */