blob: 22831b10c7c96763e2511fb6994fb141080616b8 [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
Erik Andersenf0657d32000-04-12 17:49:52 +000096void
97cmdedit_setwidth(int w)
Erik Andersen13456d12000-03-16 08:09:57 +000098{
Erik Andersen61677fe2000-04-13 01:18:56 +000099 if (w > 20) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000100 cmdedit_termw = w;
101 cmdedit_scroll = w / 3;
Erik Andersen61677fe2000-04-13 01:18:56 +0000102 } else {
Erik Andersenf0657d32000-04-12 17:49:52 +0000103 errorMsg("\n*** Error: minimum screen width is 21\n");
Erik Andersen61677fe2000-04-13 01:18:56 +0000104 }
Erik Andersen13456d12000-03-16 08:09:57 +0000105}
106
Erik Andersen61677fe2000-04-13 01:18:56 +0000107
Erik Andersen13456d12000-03-16 08:09:57 +0000108void cmdedit_reset_term(void)
109{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000110 if (reset_term)
Erik Andersena6c75222000-04-18 00:00:52 +0000111 /* sparc and other have broken termios support: use old termio handling. */
Erik Andersen1d1d9502000-04-21 01:26:49 +0000112 setTermSettings(fileno(stdin), (void*) &initial_settings);
Erik Andersen13456d12000-03-16 08:09:57 +0000113}
114
Erik Andersenf0657d32000-04-12 17:49:52 +0000115void clean_up_and_die(int sig)
Erik Andersen13456d12000-03-16 08:09:57 +0000116{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000117 cmdedit_reset_term();
118 fprintf(stdout, "\n");
Erik Andersen1d1d9502000-04-21 01:26:49 +0000119 if (sig!=SIGINT)
120 exit(TRUE);
Erik Andersen13456d12000-03-16 08:09:57 +0000121}
122
Erik Andersenf0657d32000-04-12 17:49:52 +0000123/* Go to HOME position */
Erik Andersen13456d12000-03-16 08:09:57 +0000124void input_home(int outputFd, int *cursor)
Erik Andersenf0657d32000-04-12 17:49:52 +0000125{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000126 while (*cursor > 0) {
127 xwrite(outputFd, "\b", 1);
128 --*cursor;
129 }
Erik Andersen13456d12000-03-16 08:09:57 +0000130}
131
Erik Andersenf0657d32000-04-12 17:49:52 +0000132/* Go to END position */
Erik Andersen13456d12000-03-16 08:09:57 +0000133void input_end(int outputFd, int *cursor, int len)
134{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000135 while (*cursor < len) {
136 xwrite(outputFd, "\033[C", 3);
137 ++*cursor;
138 }
Erik Andersen13456d12000-03-16 08:09:57 +0000139}
140
Erik Andersenf0657d32000-04-12 17:49:52 +0000141/* Delete the char in back of the cursor */
142void input_backspace(char* command, int outputFd, int *cursor, int *len)
Erik Andersen13456d12000-03-16 08:09:57 +0000143{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000144 int j = 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000145
Erik Andersenc7c634b2000-03-19 05:28:55 +0000146 if (*cursor > 0) {
147 xwrite(outputFd, "\b \b", 3);
148 --*cursor;
Erik Andersenf0657d32000-04-12 17:49:52 +0000149 memmove(command + *cursor, command + *cursor + 1,
Erik Andersenc7c634b2000-03-19 05:28:55 +0000150 BUFSIZ - *cursor + 1);
Erik Andersen13456d12000-03-16 08:09:57 +0000151
Erik Andersenc7c634b2000-03-19 05:28:55 +0000152 for (j = *cursor; j < (BUFSIZ - 1); j++) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000153 if (!*(command + j))
Erik Andersenc7c634b2000-03-19 05:28:55 +0000154 break;
155 else
Erik Andersenf0657d32000-04-12 17:49:52 +0000156 xwrite(outputFd, (command + j), 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000157 }
158
159 xwrite(outputFd, " \b", 2);
160
161 while (j-- > *cursor)
162 xwrite(outputFd, "\b", 1);
163
164 --*len;
Erik Andersen13456d12000-03-16 08:09:57 +0000165 }
Erik Andersen13456d12000-03-16 08:09:57 +0000166}
167
Erik Andersenf0657d32000-04-12 17:49:52 +0000168/* Delete the char in front of the cursor */
169void input_delete(char* command, int outputFd, int cursor, int *len)
170{
171 int j = 0;
Erik Andersena2685732000-04-09 18:27:46 +0000172
Erik Andersenf0657d32000-04-12 17:49:52 +0000173 if (cursor == *len)
174 return;
175
176 memmove(command + cursor, command + cursor + 1,
177 BUFSIZ - cursor - 1);
178 for (j = cursor; j < (BUFSIZ - 1); j++) {
179 if (!*(command + j))
180 break;
181 else
182 xwrite(outputFd, (command + j), 1);
183 }
184
185 xwrite(outputFd, " \b", 2);
186
187 while (j-- > cursor)
188 xwrite(outputFd, "\b", 1);
189 --*len;
190}
191
192/* Move forward one charactor */
193void input_forward(int outputFd, int *cursor, int len)
194{
195 if (*cursor < len) {
196 xwrite(outputFd, "\033[C", 3);
197 ++*cursor;
198 }
199}
200
201/* Move back one charactor */
202void input_backward(int outputFd, int *cursor)
203{
204 if (*cursor > 0) {
205 xwrite(outputFd, "\033[D", 3);
206 --*cursor;
207 }
208}
209
210
211
212#ifdef BB_FEATURE_SH_TAB_COMPLETION
213char** username_tab_completion(char* command, int *num_matches)
Erik Andersen6273f652000-03-17 01:12:41 +0000214{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000215 char **matches = (char **) NULL;
216 *num_matches=0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000217 fprintf(stderr, "\nin username_tab_completion\n");
Erik Andersenc7c634b2000-03-19 05:28:55 +0000218 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000219}
Erik Andersen1dbe3402000-03-19 10:46:06 +0000220
221#include <dirent.h>
Erik Andersenf0657d32000-04-12 17:49:52 +0000222char** exe_n_cwd_tab_completion(char* command, int *num_matches)
Erik Andersen6273f652000-03-17 01:12:41 +0000223{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000224 char *dirName;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000225 char **matches = (char **) NULL;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000226 DIR *dir;
227 struct dirent *next;
228
229 matches = malloc( sizeof(char*)*50);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000230
Erik Andersen1dbe3402000-03-19 10:46:06 +0000231 /* Stick a wildcard onto the command, for later use */
232 strcat( command, "*");
Erik Andersenc7c634b2000-03-19 05:28:55 +0000233
Erik Andersen1dbe3402000-03-19 10:46:06 +0000234 /* Now wall the current directory */
235 dirName = get_current_dir_name();
236 dir = opendir(dirName);
237 if (!dir) {
238 /* Don't print an error, just shut up and return */
239 *num_matches=0;
240 return (matches);
241 }
242 while ((next = readdir(dir)) != NULL) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000243
Erik Andersen1dbe3402000-03-19 10:46:06 +0000244 /* Some quick sanity checks */
245 if ((strcmp(next->d_name, "..") == 0)
246 || (strcmp(next->d_name, ".") == 0)) {
247 continue;
248 }
249 /* See if this matches */
250 if (check_wildcard_match(next->d_name, command) == TRUE) {
251 /* Cool, found a match. Add it to the list */
252 matches[*num_matches] = malloc(strlen(next->d_name)+1);
253 strcpy( matches[*num_matches], next->d_name);
254 ++*num_matches;
255 //matches = realloc( matches, sizeof(char*)*(*num_matches));
256 }
257 }
258
Erik Andersenc7c634b2000-03-19 05:28:55 +0000259 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000260}
Erik Andersenf0657d32000-04-12 17:49:52 +0000261
Erik Andersen1d1d9502000-04-21 01:26:49 +0000262void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000263{
264 /* Do TAB completion */
265 static int num_matches=0;
266 static char **matches = (char **) NULL;
267 int pos = cursor;
268
269
270 if (lastWasTab == FALSE) {
271 char *tmp, *tmp1, *matchBuf;
272
273 /* For now, we will not bother with trying to distinguish
274 * whether the cursor is in/at a command extression -- we
275 * will always try all possable matches. If you don't like
276 * that then feel free to fix it.
277 */
278
279 /* Make a local copy of the string -- up
280 * to the position of the cursor */
281 matchBuf = (char *) calloc(BUFSIZ, sizeof(char));
282 strncpy(matchBuf, command, cursor);
283 tmp=matchBuf;
284
285 /* skip past any command seperator tokens */
286 while (*tmp && (tmp1=strpbrk(tmp, ";|&{(`")) != NULL) {
287 tmp=++tmp1;
288 /* skip any leading white space */
289 while (*tmp && isspace(*tmp))
290 ++tmp;
291 }
292
293 /* skip any leading white space */
294 while (*tmp && isspace(*tmp))
295 ++tmp;
296
297 /* Free up any memory already allocated */
298 if (matches) {
299 free(matches);
300 matches = (char **) NULL;
301 }
302
303 /* If the word starts with `~' and there is no slash in the word,
304 * then try completing this word as a username. */
305
306 /* FIXME -- this check is broken! */
307 if (*tmp == '~' && !strchr(tmp, '/'))
308 matches = username_tab_completion(tmp, &num_matches);
309
310 /* Try to match any executable in our path and everything
311 * in the current working directory that matches. */
312 if (!matches)
313 matches = exe_n_cwd_tab_completion(tmp, &num_matches);
314
315 /* Don't leak memory */
316 free( matchBuf);
317
318 /* Did we find exactly one match? */
319 if (matches && num_matches==1) {
320 /* write out the matched command */
321 strncpy(command+pos, matches[0]+pos, strlen(matches[0])-pos);
322 len=strlen(command);
323 cursor=len;
324 xwrite(outputFd, matches[0]+pos, strlen(matches[0])-pos);
325 break;
326 }
327 } else {
328 /* Ok -- the last char was a TAB. Since they
329 * just hit TAB again, print a list of all the
330 * available choices... */
331 if ( matches && num_matches>0 ) {
332 int i, col;
333
334 /* Go to the next line */
335 xwrite(outputFd, "\n", 1);
336 /* Print the list of matches */
337 for (i=0,col=0; i<num_matches; i++) {
338 char foo[17];
339 sprintf(foo, "%-14s ", matches[i]);
340 col += xwrite(outputFd, foo, strlen(foo));
341 if (col > 60 && matches[i+1] != NULL) {
342 xwrite(outputFd, "\n", 1);
343 col = 0;
344 }
345 }
346 /* Go to the next line */
347 xwrite(outputFd, "\n", 1);
348 /* Rewrite the prompt */
349 xwrite(outputFd, prompt, strlen(prompt));
350 /* Rewrite the command */
351 xwrite(outputFd, command, len);
352 /* Put the cursor back to where it used to be */
353 for (cursor=len; cursor > pos; cursor--)
354 xwrite(outputFd, "\b", 1);
355 }
356 }
357}
358#endif
359
360void get_previous_history(struct history **hp, char* command)
361{
362 free((*hp)->s);
363 (*hp)->s = strdup(command);
364 *hp = (*hp)->p;
365}
366
367void get_next_history(struct history **hp, char* command)
368{
369 free((*hp)->s);
370 (*hp)->s = strdup(command);
371 *hp = (*hp)->n;
Erik Andersenf0657d32000-04-12 17:49:52 +0000372}
373
Erik Andersen6273f652000-03-17 01:12:41 +0000374/*
375 * This function is used to grab a character buffer
376 * from the input file descriptor and allows you to
377 * a string with full command editing (sortof like
378 * a mini readline).
379 *
380 * The following standard commands are not implemented:
381 * ESC-b -- Move back one word
382 * ESC-f -- Move forward one word
383 * ESC-d -- Delete back one word
384 * ESC-h -- Delete forward one word
385 * CTL-t -- Transpose two characters
386 *
387 * Furthermore, the "vi" command editing keys are not implemented.
388 *
389 * TODO: implement TAB command completion. :)
Erik Andersen6273f652000-03-17 01:12:41 +0000390 */
Erik Andersenf0657d32000-04-12 17:49:52 +0000391extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +0000392{
393
Erik Andersenf0657d32000-04-12 17:49:52 +0000394 int inputFd=fileno(stdin);
395 int outputFd=fileno(stdout);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000396 int nr = 0;
397 int len = 0;
398 int j = 0;
399 int cursor = 0;
400 int break_out = 0;
401 int ret = 0;
402 int lastWasTab = FALSE;
403 char c = 0;
404 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +0000405
Erik Andersenc7c634b2000-03-19 05:28:55 +0000406 memset(command, 0, sizeof(command));
Erik Andersenc7c634b2000-03-19 05:28:55 +0000407 if (!reset_term) {
Erik Andersen1d1d9502000-04-21 01:26:49 +0000408
409 getTermSettings(inputFd, (void*) &initial_settings);
410 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
411 new_settings.c_cc[VMIN] = 1;
412 new_settings.c_cc[VTIME] = 0;
413 new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */
414 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
415 new_settings.c_lflag &= ~(ECHO|ECHOCTL|ECHONL); /* Turn off echoing */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000416 reset_term = 1;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000417 }
Erik Andersen1d1d9502000-04-21 01:26:49 +0000418 setTermSettings(inputFd, (void*) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +0000419
Erik Andersenf0657d32000-04-12 17:49:52 +0000420 memset(command, 0, BUFSIZ);
Erik Andersen13456d12000-03-16 08:09:57 +0000421
Erik Andersenc7c634b2000-03-19 05:28:55 +0000422 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +0000423
Erik Andersen13456d12000-03-16 08:09:57 +0000424 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000425 return;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000426 //fprintf(stderr, "got a '%c' (%d)\n", c, c);
Erik Andersenf3b3d172000-04-09 18:24:05 +0000427
Erik Andersen13456d12000-03-16 08:09:57 +0000428 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000429 case '\n':
430 case '\r':
431 /* Enter */
432 *(command + len++ + 1) = c;
433 xwrite(outputFd, &c, 1);
434 break_out = 1;
435 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000436 case 1:
437 /* Control-a -- Beginning of line */
438 input_home(outputFd, &cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000439 case 2:
440 /* Control-b -- Move back one character */
441 input_backward(outputFd, &cursor);
442 break;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000443 case 3:
444 /* Control-c -- leave the current line,
445 * and start over on the next line */
446
447 /* Go to the next line */
448 xwrite(outputFd, "\n", 1);
449
450 /* Rewrite the prompt */
451 xwrite(outputFd, prompt, strlen(prompt));
452
453 /* Reset the command string */
454 memset(command, 0, sizeof(command));
455 len = cursor = 0;
456
457 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000458 case 4:
459 /* Control-d -- Delete one character, or exit
460 * if the len=0 and no chars to delete */
461 if (len == 0) {
462 xwrite(outputFd, "exit", 4);
463 clean_up_and_die(0);
464 } else {
465 input_delete(command, outputFd, cursor, &len);
466 }
467 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000468 case 5:
469 /* Control-e -- End of line */
470 input_end(outputFd, &cursor, len);
471 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000472 case 6:
473 /* Control-f -- Move forward one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000474 input_forward(outputFd, &cursor, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000475 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000476 case '\b':
477 case DEL:
Erik Andersen1d1d9502000-04-21 01:26:49 +0000478 /* Control-h and DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +0000479 input_backspace(command, outputFd, &cursor, &len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000480 break;
481 case '\t':
Erik Andersena2685732000-04-09 18:27:46 +0000482#ifdef BB_FEATURE_SH_TAB_COMPLETION
Erik Andersen1d1d9502000-04-21 01:26:49 +0000483 input_tab(command, prompt, outputFd, &cursor, &len);
Erik Andersena2685732000-04-09 18:27:46 +0000484#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +0000485 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000486 case 14:
487 /* Control-n -- Get next command in history */
488 if (hp && hp->n && hp->n->s) {
489 get_next_history(&hp, command);
490 goto rewrite_line;
491 } else {
492 xwrite(outputFd, "\007", 1);
493 }
494 break;
495 case 16:
496 /* Control-p -- Get previous command from history */
497 if (hp && hp->p) {
498 get_previous_history(&hp, command);
499 goto rewrite_line;
500 } else {
501 xwrite(outputFd, "\007", 1);
502 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000503 break;
504 case ESC:{
505 /* escape sequence follows */
506 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000507 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000508
509 if (c == '[') { /* 91 */
510 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000511 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000512
513 switch (c) {
514 case 'A':
Erik Andersenf0657d32000-04-12 17:49:52 +0000515 /* Up Arrow -- Get previous command from history */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000516 if (hp && hp->p) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000517 get_previous_history(&hp, command);
518 goto rewrite_line;
519 } else {
520 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000521 }
522 break;
523 case 'B':
Erik Andersenf0657d32000-04-12 17:49:52 +0000524 /* Down Arrow -- Get next command in history */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000525 if (hp && hp->n && hp->n->s) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000526 get_next_history(&hp, command);
527 goto rewrite_line;
528 } else {
529 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000530 }
531 break;
532
Erik Andersenf0657d32000-04-12 17:49:52 +0000533 /* Rewrite the line with the selected history item */
534 rewrite_line:
535 /* erase old command from command line */
536 len = strlen(command)-strlen(hp->s);
537 while (len>0)
538 input_backspace(command, outputFd, &cursor, &len);
539 input_home(outputFd, &cursor);
540
Erik Andersenc7c634b2000-03-19 05:28:55 +0000541 /* write new command */
Erik Andersenf0657d32000-04-12 17:49:52 +0000542 strcpy(command, hp->s);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000543 len = strlen(hp->s);
Erik Andersenf0657d32000-04-12 17:49:52 +0000544 xwrite(outputFd, command, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000545 cursor = len;
546 break;
547 case 'C':
548 /* Right Arrow -- Move forward one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000549 input_forward(outputFd, &cursor, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000550 break;
551 case 'D':
552 /* Left Arrow -- Move back one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000553 input_backward(outputFd, &cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000554 break;
555 case '3':
556 /* Delete */
Erik Andersenf0657d32000-04-12 17:49:52 +0000557 input_delete(command, outputFd, cursor, &len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000558 break;
559 case '1':
560 /* Home (Ctrl-A) */
561 input_home(outputFd, &cursor);
562 break;
563 case '4':
564 /* End (Ctrl-E) */
565 input_end(outputFd, &cursor, len);
566 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000567 default:
568 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000569 }
570 if (c == '1' || c == '3' || c == '4')
571 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000572 return; /* read 126 (~) */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000573 }
574 if (c == 'O') {
575 /* 79 */
576 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000577 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000578 switch (c) {
579 case 'H':
580 /* Home (xterm) */
581 input_home(outputFd, &cursor);
582 break;
583 case 'F':
584 /* End (xterm) */
585 input_end(outputFd, &cursor, len);
586 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000587 default:
588 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000589 }
590 }
591 c = 0;
592 break;
593 }
594
595 default: /* If it's regular input, do the normal thing */
596
Erik Andersenf0657d32000-04-12 17:49:52 +0000597 if (!isprint(c)) { /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000598 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000599 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000600
601 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
602 break;
603
604 len++;
605
606 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +0000607 *(command + cursor) = c;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000608 } else { /* Insert otherwise */
Erik Andersenf0657d32000-04-12 17:49:52 +0000609 memmove(command + cursor + 1, command + cursor,
Erik Andersenc7c634b2000-03-19 05:28:55 +0000610 len - cursor - 1);
611
Erik Andersenf0657d32000-04-12 17:49:52 +0000612 *(command + cursor) = c;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000613
614 for (j = cursor; j < len; j++)
Erik Andersenf0657d32000-04-12 17:49:52 +0000615 xwrite(outputFd, command + j, 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000616 for (; j > cursor; j--)
617 xwrite(outputFd, "\033[D", 3);
618 }
619
Erik Andersen13456d12000-03-16 08:09:57 +0000620 cursor++;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000621 xwrite(outputFd, &c, 1);
622 break;
Erik Andersen13456d12000-03-16 08:09:57 +0000623 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000624 if (c == '\t')
625 lastWasTab = TRUE;
626 else
627 lastWasTab = FALSE;
628
629 if (break_out) /* Enter is the command terminator, no more input. */
630 break;
631 }
632
633 nr = len + 1;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000634 setTermSettings(inputFd, (void *) &initial_settings);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000635 reset_term = 0;
636
637
638 /* Handle command history log */
Erik Andersenf0657d32000-04-12 17:49:52 +0000639 if (*(command)) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000640
641 struct history *h = his_end;
642
643 if (!h) {
644 /* No previous history */
645 h = his_front = malloc(sizeof(struct history));
646 h->n = malloc(sizeof(struct history));
647
648 h->p = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +0000649 h->s = strdup(command);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000650 h->n->p = h;
651 h->n->n = NULL;
652 h->n->s = NULL;
653 his_end = h->n;
654 history_counter++;
655 } else {
656 /* Add a new history command */
657 h->n = malloc(sizeof(struct history));
658
659 h->n->p = h;
660 h->n->n = NULL;
661 h->n->s = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +0000662 h->s = strdup(command);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000663 his_end = h->n;
664
665 /* After max history, remove the oldest command */
666 if (history_counter >= MAX_HISTORY) {
667
668 struct history *p = his_front->n;
669
670 p->p = NULL;
671 free(his_front->s);
672 free(his_front);
673 his_front = p;
674 } else {
675 history_counter++;
676 }
Erik Andersen13456d12000-03-16 08:09:57 +0000677 }
Erik Andersen6273f652000-03-17 01:12:41 +0000678 }
Erik Andersen13456d12000-03-16 08:09:57 +0000679
Erik Andersenf0657d32000-04-12 17:49:52 +0000680 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000681}
682
683extern void cmdedit_init(void)
684{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000685 atexit(cmdedit_reset_term);
Erik Andersen1d1d9502000-04-21 01:26:49 +0000686 signal(SIGKILL, clean_up_and_die);
Erik Andersenf0657d32000-04-12 17:49:52 +0000687 signal(SIGINT, clean_up_and_die);
688 signal(SIGQUIT, clean_up_and_die);
689 signal(SIGTERM, clean_up_and_die);
Erik Andersen13456d12000-03-16 08:09:57 +0000690}
Erik Andersenc7c634b2000-03-19 05:28:55 +0000691#endif /* BB_FEATURE_SH_COMMAND_EDITING */