blob: ebc6b969653b89b5110dfc563c15eeed0ec098e7 [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>
42#include <termio.h>
43#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 */
56static struct termio old_term, new_term; /* Current termio and the previous termio before starting ash */
57
Erik Andersenf0657d32000-04-12 17:49:52 +000058static int cmdedit_termw = 80; /* actual terminal width */
59static int cmdedit_scroll = 27; /* width of EOL scrolling region */
Erik Andersen13456d12000-03-16 08:09:57 +000060static int history_counter = 0; /* Number of commands in history list */
Erik Andersenc7c634b2000-03-19 05:28:55 +000061static int reset_term = 0; /* Set to true if the terminal needs to be reset upon exit */
Erik Andersen13456d12000-03-16 08:09:57 +000062
63struct history {
Erik Andersenc7c634b2000-03-19 05:28:55 +000064 char *s;
65 struct history *p;
66 struct history *n;
Erik Andersen13456d12000-03-16 08:09:57 +000067};
68
Erik Andersenf0657d32000-04-12 17:49:52 +000069#define xwrite write
Erik Andersen13456d12000-03-16 08:09:57 +000070
Erik Andersenf0657d32000-04-12 17:49:52 +000071void
72cmdedit_setwidth(int w)
Erik Andersen13456d12000-03-16 08:09:57 +000073{
Erik Andersen61677fe2000-04-13 01:18:56 +000074 if (w > 20) {
Erik Andersenf0657d32000-04-12 17:49:52 +000075 cmdedit_termw = w;
76 cmdedit_scroll = w / 3;
Erik Andersen61677fe2000-04-13 01:18:56 +000077 } else {
Erik Andersenf0657d32000-04-12 17:49:52 +000078 errorMsg("\n*** Error: minimum screen width is 21\n");
Erik Andersen61677fe2000-04-13 01:18:56 +000079 }
Erik Andersen13456d12000-03-16 08:09:57 +000080}
81
Erik Andersen61677fe2000-04-13 01:18:56 +000082
Erik Andersen13456d12000-03-16 08:09:57 +000083void cmdedit_reset_term(void)
84{
Erik Andersenc7c634b2000-03-19 05:28:55 +000085 if (reset_term)
Erik Andersena6c75222000-04-18 00:00:52 +000086 /* sparc and other have broken termios support: use old termio handling. */
Erik Andersenf0657d32000-04-12 17:49:52 +000087 ioctl(fileno(stdin), TCSETA, (void *) &old_term);
Erik Andersen13456d12000-03-16 08:09:57 +000088}
89
Erik Andersenf0657d32000-04-12 17:49:52 +000090void clean_up_and_die(int sig)
Erik Andersen13456d12000-03-16 08:09:57 +000091{
Erik Andersenc7c634b2000-03-19 05:28:55 +000092 cmdedit_reset_term();
93 fprintf(stdout, "\n");
94 exit(TRUE);
Erik Andersen13456d12000-03-16 08:09:57 +000095}
96
Erik Andersenf0657d32000-04-12 17:49:52 +000097/* Go to HOME position */
Erik Andersen13456d12000-03-16 08:09:57 +000098void input_home(int outputFd, int *cursor)
Erik Andersenf0657d32000-04-12 17:49:52 +000099{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000100 while (*cursor > 0) {
101 xwrite(outputFd, "\b", 1);
102 --*cursor;
103 }
Erik Andersen13456d12000-03-16 08:09:57 +0000104}
105
Erik Andersenf0657d32000-04-12 17:49:52 +0000106/* Go to END position */
Erik Andersen13456d12000-03-16 08:09:57 +0000107void input_end(int outputFd, int *cursor, int len)
108{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000109 while (*cursor < len) {
110 xwrite(outputFd, "\033[C", 3);
111 ++*cursor;
112 }
Erik Andersen13456d12000-03-16 08:09:57 +0000113}
114
Erik Andersenf0657d32000-04-12 17:49:52 +0000115/* Delete the char in back of the cursor */
116void input_backspace(char* command, int outputFd, int *cursor, int *len)
Erik Andersen13456d12000-03-16 08:09:57 +0000117{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000118 int j = 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000119
Erik Andersenc7c634b2000-03-19 05:28:55 +0000120 if (*cursor > 0) {
121 xwrite(outputFd, "\b \b", 3);
122 --*cursor;
Erik Andersenf0657d32000-04-12 17:49:52 +0000123 memmove(command + *cursor, command + *cursor + 1,
Erik Andersenc7c634b2000-03-19 05:28:55 +0000124 BUFSIZ - *cursor + 1);
Erik Andersen13456d12000-03-16 08:09:57 +0000125
Erik Andersenc7c634b2000-03-19 05:28:55 +0000126 for (j = *cursor; j < (BUFSIZ - 1); j++) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000127 if (!*(command + j))
Erik Andersenc7c634b2000-03-19 05:28:55 +0000128 break;
129 else
Erik Andersenf0657d32000-04-12 17:49:52 +0000130 xwrite(outputFd, (command + j), 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000131 }
132
133 xwrite(outputFd, " \b", 2);
134
135 while (j-- > *cursor)
136 xwrite(outputFd, "\b", 1);
137
138 --*len;
Erik Andersen13456d12000-03-16 08:09:57 +0000139 }
Erik Andersen13456d12000-03-16 08:09:57 +0000140}
141
Erik Andersenf0657d32000-04-12 17:49:52 +0000142/* Delete the char in front of the cursor */
143void input_delete(char* command, int outputFd, int cursor, int *len)
144{
145 int j = 0;
Erik Andersena2685732000-04-09 18:27:46 +0000146
Erik Andersenf0657d32000-04-12 17:49:52 +0000147 if (cursor == *len)
148 return;
149
150 memmove(command + cursor, command + cursor + 1,
151 BUFSIZ - cursor - 1);
152 for (j = cursor; j < (BUFSIZ - 1); j++) {
153 if (!*(command + j))
154 break;
155 else
156 xwrite(outputFd, (command + j), 1);
157 }
158
159 xwrite(outputFd, " \b", 2);
160
161 while (j-- > cursor)
162 xwrite(outputFd, "\b", 1);
163 --*len;
164}
165
166/* Move forward one charactor */
167void input_forward(int outputFd, int *cursor, int len)
168{
169 if (*cursor < len) {
170 xwrite(outputFd, "\033[C", 3);
171 ++*cursor;
172 }
173}
174
175/* Move back one charactor */
176void input_backward(int outputFd, int *cursor)
177{
178 if (*cursor > 0) {
179 xwrite(outputFd, "\033[D", 3);
180 --*cursor;
181 }
182}
183
184
185
186#ifdef BB_FEATURE_SH_TAB_COMPLETION
187char** username_tab_completion(char* command, int *num_matches)
Erik Andersen6273f652000-03-17 01:12:41 +0000188{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000189 char **matches = (char **) NULL;
190 *num_matches=0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000191 fprintf(stderr, "\nin username_tab_completion\n");
Erik Andersenc7c634b2000-03-19 05:28:55 +0000192 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000193}
Erik Andersen1dbe3402000-03-19 10:46:06 +0000194
195#include <dirent.h>
Erik Andersenf0657d32000-04-12 17:49:52 +0000196char** exe_n_cwd_tab_completion(char* command, int *num_matches)
Erik Andersen6273f652000-03-17 01:12:41 +0000197{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000198 char *dirName;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000199 char **matches = (char **) NULL;
Erik Andersen1dbe3402000-03-19 10:46:06 +0000200 DIR *dir;
201 struct dirent *next;
202
203 matches = malloc( sizeof(char*)*50);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000204
Erik Andersen1dbe3402000-03-19 10:46:06 +0000205 /* Stick a wildcard onto the command, for later use */
206 strcat( command, "*");
Erik Andersenc7c634b2000-03-19 05:28:55 +0000207
Erik Andersen1dbe3402000-03-19 10:46:06 +0000208 /* Now wall the current directory */
209 dirName = get_current_dir_name();
210 dir = opendir(dirName);
211 if (!dir) {
212 /* Don't print an error, just shut up and return */
213 *num_matches=0;
214 return (matches);
215 }
216 while ((next = readdir(dir)) != NULL) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000217
Erik Andersen1dbe3402000-03-19 10:46:06 +0000218 /* Some quick sanity checks */
219 if ((strcmp(next->d_name, "..") == 0)
220 || (strcmp(next->d_name, ".") == 0)) {
221 continue;
222 }
223 /* See if this matches */
224 if (check_wildcard_match(next->d_name, command) == TRUE) {
225 /* Cool, found a match. Add it to the list */
226 matches[*num_matches] = malloc(strlen(next->d_name)+1);
227 strcpy( matches[*num_matches], next->d_name);
228 ++*num_matches;
229 //matches = realloc( matches, sizeof(char*)*(*num_matches));
230 }
231 }
232
Erik Andersenc7c634b2000-03-19 05:28:55 +0000233 return (matches);
Erik Andersen6273f652000-03-17 01:12:41 +0000234}
Erik Andersenf0657d32000-04-12 17:49:52 +0000235
236void input_tab(char* command, int outputFd, int *cursor, int *len)
237{
238 /* Do TAB completion */
239 static int num_matches=0;
240 static char **matches = (char **) NULL;
241 int pos = cursor;
242
243
244 if (lastWasTab == FALSE) {
245 char *tmp, *tmp1, *matchBuf;
246
247 /* For now, we will not bother with trying to distinguish
248 * whether the cursor is in/at a command extression -- we
249 * will always try all possable matches. If you don't like
250 * that then feel free to fix it.
251 */
252
253 /* Make a local copy of the string -- up
254 * to the position of the cursor */
255 matchBuf = (char *) calloc(BUFSIZ, sizeof(char));
256 strncpy(matchBuf, command, cursor);
257 tmp=matchBuf;
258
259 /* skip past any command seperator tokens */
260 while (*tmp && (tmp1=strpbrk(tmp, ";|&{(`")) != NULL) {
261 tmp=++tmp1;
262 /* skip any leading white space */
263 while (*tmp && isspace(*tmp))
264 ++tmp;
265 }
266
267 /* skip any leading white space */
268 while (*tmp && isspace(*tmp))
269 ++tmp;
270
271 /* Free up any memory already allocated */
272 if (matches) {
273 free(matches);
274 matches = (char **) NULL;
275 }
276
277 /* If the word starts with `~' and there is no slash in the word,
278 * then try completing this word as a username. */
279
280 /* FIXME -- this check is broken! */
281 if (*tmp == '~' && !strchr(tmp, '/'))
282 matches = username_tab_completion(tmp, &num_matches);
283
284 /* Try to match any executable in our path and everything
285 * in the current working directory that matches. */
286 if (!matches)
287 matches = exe_n_cwd_tab_completion(tmp, &num_matches);
288
289 /* Don't leak memory */
290 free( matchBuf);
291
292 /* Did we find exactly one match? */
293 if (matches && num_matches==1) {
294 /* write out the matched command */
295 strncpy(command+pos, matches[0]+pos, strlen(matches[0])-pos);
296 len=strlen(command);
297 cursor=len;
298 xwrite(outputFd, matches[0]+pos, strlen(matches[0])-pos);
299 break;
300 }
301 } else {
302 /* Ok -- the last char was a TAB. Since they
303 * just hit TAB again, print a list of all the
304 * available choices... */
305 if ( matches && num_matches>0 ) {
306 int i, col;
307
308 /* Go to the next line */
309 xwrite(outputFd, "\n", 1);
310 /* Print the list of matches */
311 for (i=0,col=0; i<num_matches; i++) {
312 char foo[17];
313 sprintf(foo, "%-14s ", matches[i]);
314 col += xwrite(outputFd, foo, strlen(foo));
315 if (col > 60 && matches[i+1] != NULL) {
316 xwrite(outputFd, "\n", 1);
317 col = 0;
318 }
319 }
320 /* Go to the next line */
321 xwrite(outputFd, "\n", 1);
322 /* Rewrite the prompt */
323 xwrite(outputFd, prompt, strlen(prompt));
324 /* Rewrite the command */
325 xwrite(outputFd, command, len);
326 /* Put the cursor back to where it used to be */
327 for (cursor=len; cursor > pos; cursor--)
328 xwrite(outputFd, "\b", 1);
329 }
330 }
331}
332#endif
333
334void get_previous_history(struct history **hp, char* command)
335{
336 free((*hp)->s);
337 (*hp)->s = strdup(command);
338 *hp = (*hp)->p;
339}
340
341void get_next_history(struct history **hp, char* command)
342{
343 free((*hp)->s);
344 (*hp)->s = strdup(command);
345 *hp = (*hp)->n;
Erik Andersenf0657d32000-04-12 17:49:52 +0000346}
347
Erik Andersen6273f652000-03-17 01:12:41 +0000348/*
349 * This function is used to grab a character buffer
350 * from the input file descriptor and allows you to
351 * a string with full command editing (sortof like
352 * a mini readline).
353 *
354 * The following standard commands are not implemented:
355 * ESC-b -- Move back one word
356 * ESC-f -- Move forward one word
357 * ESC-d -- Delete back one word
358 * ESC-h -- Delete forward one word
359 * CTL-t -- Transpose two characters
360 *
361 * Furthermore, the "vi" command editing keys are not implemented.
362 *
363 * TODO: implement TAB command completion. :)
Erik Andersen6273f652000-03-17 01:12:41 +0000364 */
Erik Andersenf0657d32000-04-12 17:49:52 +0000365extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +0000366{
367
Erik Andersenf0657d32000-04-12 17:49:52 +0000368 int inputFd=fileno(stdin);
369 int outputFd=fileno(stdout);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000370 int nr = 0;
371 int len = 0;
372 int j = 0;
373 int cursor = 0;
374 int break_out = 0;
375 int ret = 0;
376 int lastWasTab = FALSE;
377 char c = 0;
378 struct history *hp = his_end;
Erik Andersen13456d12000-03-16 08:09:57 +0000379
Erik Andersenc7c634b2000-03-19 05:28:55 +0000380 memset(command, 0, sizeof(command));
Erik Andersenc7c634b2000-03-19 05:28:55 +0000381 if (!reset_term) {
Erik Andersena6c75222000-04-18 00:00:52 +0000382 /* sparc and other have broken termios support: use old termio handling. */
Erik Andersenf0657d32000-04-12 17:49:52 +0000383 ioctl(inputFd, TCGETA, (void *) &old_term);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000384 memcpy(&new_term, &old_term, sizeof(struct termio));
Erik Andersen13456d12000-03-16 08:09:57 +0000385
Erik Andersenc7c634b2000-03-19 05:28:55 +0000386 new_term.c_cc[VMIN] = 1;
387 new_term.c_cc[VTIME] = 0;
388 new_term.c_lflag &= ~ICANON; /* unbuffered input */
389 new_term.c_lflag &= ~ECHO;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000390 reset_term = 1;
Erik Andersenf0657d32000-04-12 17:49:52 +0000391 ioctl(inputFd, TCSETA, (void *) &new_term);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000392 } else {
Erik Andersenf0657d32000-04-12 17:49:52 +0000393 ioctl(inputFd, TCSETA, (void *) &new_term);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000394 }
Erik Andersen13456d12000-03-16 08:09:57 +0000395
Erik Andersenf0657d32000-04-12 17:49:52 +0000396 memset(command, 0, BUFSIZ);
Erik Andersen13456d12000-03-16 08:09:57 +0000397
Erik Andersenc7c634b2000-03-19 05:28:55 +0000398 while (1) {
Erik Andersen6273f652000-03-17 01:12:41 +0000399
Erik Andersen13456d12000-03-16 08:09:57 +0000400 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000401 return;
Erik Andersenf3b3d172000-04-09 18:24:05 +0000402
Erik Andersen13456d12000-03-16 08:09:57 +0000403 switch (c) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000404 case '\n':
405 case '\r':
406 /* Enter */
407 *(command + len++ + 1) = c;
408 xwrite(outputFd, &c, 1);
409 break_out = 1;
410 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000411 case 1:
412 /* Control-a -- Beginning of line */
413 input_home(outputFd, &cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000414 case 2:
415 /* Control-b -- Move back one character */
416 input_backward(outputFd, &cursor);
417 break;
418 case 4:
419 /* Control-d -- Delete one character, or exit
420 * if the len=0 and no chars to delete */
421 if (len == 0) {
422 xwrite(outputFd, "exit", 4);
423 clean_up_and_die(0);
424 } else {
425 input_delete(command, outputFd, cursor, &len);
426 }
427 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000428 case 5:
429 /* Control-e -- End of line */
430 input_end(outputFd, &cursor, len);
431 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000432 case 6:
433 /* Control-f -- Move forward one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000434 input_forward(outputFd, &cursor, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000435 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000436 case '\b':
437 case DEL:
438 /* control-h and DEL */
439 input_backspace(command, outputFd, &cursor, &len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000440 break;
441 case '\t':
Erik Andersena2685732000-04-09 18:27:46 +0000442#ifdef BB_FEATURE_SH_TAB_COMPLETION
Erik Andersenf0657d32000-04-12 17:49:52 +0000443 input_tab(command, outputFd, &cursor, &len);
Erik Andersena2685732000-04-09 18:27:46 +0000444#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +0000445 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000446 case 14:
447 /* Control-n -- Get next command in history */
448 if (hp && hp->n && hp->n->s) {
449 get_next_history(&hp, command);
450 goto rewrite_line;
451 } else {
452 xwrite(outputFd, "\007", 1);
453 }
454 break;
455 case 16:
456 /* Control-p -- Get previous command from history */
457 if (hp && hp->p) {
458 get_previous_history(&hp, command);
459 goto rewrite_line;
460 } else {
461 xwrite(outputFd, "\007", 1);
462 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000463 break;
464 case ESC:{
465 /* escape sequence follows */
466 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000467 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000468
469 if (c == '[') { /* 91 */
470 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000471 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000472
473 switch (c) {
474 case 'A':
Erik Andersenf0657d32000-04-12 17:49:52 +0000475 /* Up Arrow -- Get previous command from history */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000476 if (hp && hp->p) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000477 get_previous_history(&hp, command);
478 goto rewrite_line;
479 } else {
480 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000481 }
482 break;
483 case 'B':
Erik Andersenf0657d32000-04-12 17:49:52 +0000484 /* Down Arrow -- Get next command in history */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000485 if (hp && hp->n && hp->n->s) {
Erik Andersenf0657d32000-04-12 17:49:52 +0000486 get_next_history(&hp, command);
487 goto rewrite_line;
488 } else {
489 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000490 }
491 break;
492
Erik Andersenf0657d32000-04-12 17:49:52 +0000493 /* Rewrite the line with the selected history item */
494 rewrite_line:
495 /* erase old command from command line */
496 len = strlen(command)-strlen(hp->s);
497 while (len>0)
498 input_backspace(command, outputFd, &cursor, &len);
499 input_home(outputFd, &cursor);
500
Erik Andersenc7c634b2000-03-19 05:28:55 +0000501 /* write new command */
Erik Andersenf0657d32000-04-12 17:49:52 +0000502 strcpy(command, hp->s);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000503 len = strlen(hp->s);
Erik Andersenf0657d32000-04-12 17:49:52 +0000504 xwrite(outputFd, command, len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000505 cursor = len;
506 break;
507 case 'C':
508 /* Right Arrow -- 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;
511 case 'D':
512 /* Left Arrow -- Move back one character */
Erik Andersenf0657d32000-04-12 17:49:52 +0000513 input_backward(outputFd, &cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000514 break;
515 case '3':
516 /* Delete */
Erik Andersenf0657d32000-04-12 17:49:52 +0000517 input_delete(command, outputFd, cursor, &len);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000518 break;
519 case '1':
520 /* Home (Ctrl-A) */
521 input_home(outputFd, &cursor);
522 break;
523 case '4':
524 /* End (Ctrl-E) */
525 input_end(outputFd, &cursor, len);
526 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000527 default:
528 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000529 }
530 if (c == '1' || c == '3' || c == '4')
531 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000532 return; /* read 126 (~) */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000533 }
534 if (c == 'O') {
535 /* 79 */
536 if ((ret = read(inputFd, &c, 1)) < 1)
Erik Andersenf0657d32000-04-12 17:49:52 +0000537 return;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000538 switch (c) {
539 case 'H':
540 /* Home (xterm) */
541 input_home(outputFd, &cursor);
542 break;
543 case 'F':
544 /* End (xterm) */
545 input_end(outputFd, &cursor, len);
546 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000547 default:
548 xwrite(outputFd, "\007", 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000549 }
550 }
551 c = 0;
552 break;
553 }
554
555 default: /* If it's regular input, do the normal thing */
556
Erik Andersenf0657d32000-04-12 17:49:52 +0000557 if (!isprint(c)) { /* Skip non-printable characters */
Erik Andersenc7c634b2000-03-19 05:28:55 +0000558 break;
Erik Andersenf0657d32000-04-12 17:49:52 +0000559 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000560
561 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
562 break;
563
564 len++;
565
566 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +0000567 *(command + cursor) = c;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000568 } else { /* Insert otherwise */
Erik Andersenf0657d32000-04-12 17:49:52 +0000569 memmove(command + cursor + 1, command + cursor,
Erik Andersenc7c634b2000-03-19 05:28:55 +0000570 len - cursor - 1);
571
Erik Andersenf0657d32000-04-12 17:49:52 +0000572 *(command + cursor) = c;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000573
574 for (j = cursor; j < len; j++)
Erik Andersenf0657d32000-04-12 17:49:52 +0000575 xwrite(outputFd, command + j, 1);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000576 for (; j > cursor; j--)
577 xwrite(outputFd, "\033[D", 3);
578 }
579
Erik Andersen13456d12000-03-16 08:09:57 +0000580 cursor++;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000581 xwrite(outputFd, &c, 1);
582 break;
Erik Andersen13456d12000-03-16 08:09:57 +0000583 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000584 if (c == '\t')
585 lastWasTab = TRUE;
586 else
587 lastWasTab = FALSE;
588
589 if (break_out) /* Enter is the command terminator, no more input. */
590 break;
591 }
592
593 nr = len + 1;
Erik Andersena6c75222000-04-18 00:00:52 +0000594 /* sparc and other have broken termios support: use old termio handling. */
Erik Andersenf0657d32000-04-12 17:49:52 +0000595 ioctl(inputFd, TCSETA, (void *) &old_term);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000596 reset_term = 0;
597
598
599 /* Handle command history log */
Erik Andersenf0657d32000-04-12 17:49:52 +0000600 if (*(command)) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000601
602 struct history *h = his_end;
603
604 if (!h) {
605 /* No previous history */
606 h = his_front = malloc(sizeof(struct history));
607 h->n = malloc(sizeof(struct history));
608
609 h->p = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +0000610 h->s = strdup(command);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000611 h->n->p = h;
612 h->n->n = NULL;
613 h->n->s = NULL;
614 his_end = h->n;
615 history_counter++;
616 } else {
617 /* Add a new history command */
618 h->n = malloc(sizeof(struct history));
619
620 h->n->p = h;
621 h->n->n = NULL;
622 h->n->s = NULL;
Erik Andersenf0657d32000-04-12 17:49:52 +0000623 h->s = strdup(command);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000624 his_end = h->n;
625
626 /* After max history, remove the oldest command */
627 if (history_counter >= MAX_HISTORY) {
628
629 struct history *p = his_front->n;
630
631 p->p = NULL;
632 free(his_front->s);
633 free(his_front);
634 his_front = p;
635 } else {
636 history_counter++;
637 }
Erik Andersen13456d12000-03-16 08:09:57 +0000638 }
Erik Andersen6273f652000-03-17 01:12:41 +0000639 }
Erik Andersen13456d12000-03-16 08:09:57 +0000640
Erik Andersenf0657d32000-04-12 17:49:52 +0000641 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000642}
643
644extern void cmdedit_init(void)
645{
Erik Andersenc7c634b2000-03-19 05:28:55 +0000646 atexit(cmdedit_reset_term);
Erik Andersenf0657d32000-04-12 17:49:52 +0000647 signal(SIGINT, clean_up_and_die);
648 signal(SIGQUIT, clean_up_and_die);
649 signal(SIGTERM, clean_up_and_die);
Erik Andersen13456d12000-03-16 08:09:57 +0000650}
Erik Andersenc7c634b2000-03-19 05:28:55 +0000651#endif /* BB_FEATURE_SH_COMMAND_EDITING */