2000-03-19 05:28:55 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2000-03-16 08:09:57 +00:00
|
|
|
/*
|
2000-04-13 01:18:56 +00:00
|
|
|
* Termios command line History and Editting, originally
|
|
|
|
* intended for NetBSD sh (ash)
|
2000-03-16 08:09:57 +00:00
|
|
|
* Copyright (c) 1999
|
|
|
|
* Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
|
|
|
|
* Etc: Dave Cinege <dcinege@psychosis.com>
|
2000-04-13 01:18:56 +00:00
|
|
|
* Majorly adjusted/re-written for busybox:
|
|
|
|
* Erik Andersen <andersee@debian.org>
|
2000-03-16 08:09:57 +00:00
|
|
|
*
|
|
|
|
* You may use this code as you wish, so long as the original author(s)
|
|
|
|
* are attributed in any redistributions of the source code.
|
|
|
|
* This code is 'as is' with no warranty.
|
|
|
|
* This code may safely be consumed by a BSD or GPL license.
|
|
|
|
*
|
|
|
|
* v 0.5 19990328 Initial release
|
|
|
|
*
|
|
|
|
* Future plans: Simple file and path name completion. (like BASH)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Usage and Known bugs:
|
|
|
|
Terminal key codes are not extensive, and more will probably
|
|
|
|
need to be added. This version was created on Debian GNU/Linux 2.x.
|
|
|
|
Delete, Backspace, Home, End, and the arrow keys were tested
|
|
|
|
to work in an Xterm and console. Ctrl-A also works as Home.
|
|
|
|
Ctrl-E also works as End. The binary size increase is <3K.
|
|
|
|
|
|
|
|
Editting will not display correctly for lines greater then the
|
|
|
|
terminal width. (more then one line.) However, history will.
|
|
|
|
*/
|
|
|
|
|
2000-09-25 21:45:58 +00:00
|
|
|
#include "busybox.h"
|
2000-03-16 08:09:57 +00:00
|
|
|
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2000-04-21 01:26:49 +00:00
|
|
|
#include <sys/ioctl.h>
|
2000-03-16 08:09:57 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
#define MAX_HISTORY 15 /* Maximum length of the linked list for the command line history */
|
2000-03-16 08:09:57 +00:00
|
|
|
|
|
|
|
#define ESC 27
|
|
|
|
#define DEL 127
|
2000-03-17 01:12:41 +00:00
|
|
|
#define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0)
|
|
|
|
#define whitespace(c) (((c) == ' ') || ((c) == '\t'))
|
2000-03-16 08:09:57 +00:00
|
|
|
|
|
|
|
static struct history *his_front = NULL; /* First element in command line list */
|
|
|
|
static struct history *his_end = NULL; /* Last element in command line list */
|
2000-04-21 01:26:49 +00:00
|
|
|
|
|
|
|
/* ED: sparc termios is broken: revert back to old termio handling. */
|
|
|
|
#ifdef BB_FEATURE_USE_TERMIOS
|
|
|
|
|
|
|
|
#if #cpu(sparc)
|
|
|
|
# include <termio.h>
|
|
|
|
# define termios termio
|
|
|
|
# define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp)
|
|
|
|
# define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp)
|
|
|
|
#else
|
|
|
|
# include <termios.h>
|
|
|
|
# define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
|
|
|
|
# define getTermSettings(fd,argp) tcgetattr(fd, argp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Current termio and the previous termio before starting sh */
|
2000-11-07 06:52:13 +00:00
|
|
|
static struct termios initial_settings, new_settings;
|
2000-05-20 00:40:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef _POSIX_VDISABLE
|
|
|
|
#define _POSIX_VDISABLE '\0'
|
|
|
|
#endif
|
|
|
|
|
2000-04-21 01:26:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2000-03-16 08:09:57 +00:00
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
static int cmdedit_termw = 80; /* actual terminal width */
|
|
|
|
static int cmdedit_scroll = 27; /* width of EOL scrolling region */
|
2000-03-16 08:09:57 +00:00
|
|
|
static int history_counter = 0; /* Number of commands in history list */
|
2000-03-19 05:28:55 +00:00
|
|
|
static int reset_term = 0; /* Set to true if the terminal needs to be reset upon exit */
|
2000-07-28 15:14:45 +00:00
|
|
|
static int exithandler_set = 0; /* Set to true when atexit() has been called */
|
2000-03-16 08:09:57 +00:00
|
|
|
|
|
|
|
struct history {
|
2000-03-19 05:28:55 +00:00
|
|
|
char *s;
|
|
|
|
struct history *p;
|
|
|
|
struct history *n;
|
2000-03-16 08:09:57 +00:00
|
|
|
};
|
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
#define xwrite write
|
2000-03-16 08:09:57 +00:00
|
|
|
|
2000-07-13 17:20:23 +00:00
|
|
|
/*
|
|
|
|
* TODO: Someday we want to implement 'horizontal scrolling' of the
|
|
|
|
* command-line when the user has typed more than the current width. This
|
|
|
|
* would allow the user to see a 'window' of what he has typed.
|
|
|
|
*/
|
2000-04-12 17:49:52 +00:00
|
|
|
void
|
|
|
|
cmdedit_setwidth(int w)
|
2000-03-16 08:09:57 +00:00
|
|
|
{
|
2000-04-13 01:18:56 +00:00
|
|
|
if (w > 20) {
|
2000-04-12 17:49:52 +00:00
|
|
|
cmdedit_termw = w;
|
|
|
|
cmdedit_scroll = w / 3;
|
2000-04-13 01:18:56 +00:00
|
|
|
} else {
|
2000-04-12 17:49:52 +00:00
|
|
|
errorMsg("\n*** Error: minimum screen width is 21\n");
|
2000-04-13 01:18:56 +00:00
|
|
|
}
|
2000-03-16 08:09:57 +00:00
|
|
|
}
|
|
|
|
|
2000-04-13 01:18:56 +00:00
|
|
|
|
2000-03-16 08:09:57 +00:00
|
|
|
void cmdedit_reset_term(void)
|
|
|
|
{
|
2000-03-19 05:28:55 +00:00
|
|
|
if (reset_term)
|
2000-04-18 00:00:52 +00:00
|
|
|
/* sparc and other have broken termios support: use old termio handling. */
|
2000-04-21 01:26:49 +00:00
|
|
|
setTermSettings(fileno(stdin), (void*) &initial_settings);
|
2000-07-25 18:01:20 +00:00
|
|
|
#ifdef BB_FEATURE_CLEAN_UP
|
|
|
|
if (his_front) {
|
|
|
|
struct history *n;
|
|
|
|
//while(his_front!=his_end) {
|
|
|
|
while(his_front!=his_end) {
|
|
|
|
n = his_front->n;
|
|
|
|
free(his_front->s);
|
|
|
|
free(his_front);
|
|
|
|
his_front=n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2000-03-16 08:09:57 +00:00
|
|
|
}
|
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
void clean_up_and_die(int sig)
|
2000-03-16 08:09:57 +00:00
|
|
|
{
|
2000-03-19 05:28:55 +00:00
|
|
|
cmdedit_reset_term();
|
|
|
|
fprintf(stdout, "\n");
|
2000-04-21 01:26:49 +00:00
|
|
|
if (sig!=SIGINT)
|
2000-12-01 02:55:13 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2000-03-16 08:09:57 +00:00
|
|
|
}
|
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
/* Go to HOME position */
|
2000-03-16 08:09:57 +00:00
|
|
|
void input_home(int outputFd, int *cursor)
|
2000-04-12 17:49:52 +00:00
|
|
|
{
|
2000-03-19 05:28:55 +00:00
|
|
|
while (*cursor > 0) {
|
|
|
|
xwrite(outputFd, "\b", 1);
|
|
|
|
--*cursor;
|
|
|
|
}
|
2000-03-16 08:09:57 +00:00
|
|
|
}
|
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
/* Go to END position */
|
2000-03-16 08:09:57 +00:00
|
|
|
void input_end(int outputFd, int *cursor, int len)
|
|
|
|
{
|
2000-03-19 05:28:55 +00:00
|
|
|
while (*cursor < len) {
|
|
|
|
xwrite(outputFd, "\033[C", 3);
|
|
|
|
++*cursor;
|
|
|
|
}
|
2000-03-16 08:09:57 +00:00
|
|
|
}
|
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
/* Delete the char in back of the cursor */
|
|
|
|
void input_backspace(char* command, int outputFd, int *cursor, int *len)
|
2000-03-16 08:09:57 +00:00
|
|
|
{
|
2000-03-19 05:28:55 +00:00
|
|
|
int j = 0;
|
|
|
|
|
2000-07-04 06:22:18 +00:00
|
|
|
/* Debug crap */
|
|
|
|
//fprintf(stderr, "\nerik: len=%d, cursor=%d, strlen(command)='%d'\n", *len, *cursor, strlen(command));
|
|
|
|
//xwrite(outputFd, command, *len);
|
|
|
|
//*cursor = *len;
|
|
|
|
|
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
if (*cursor > 0) {
|
|
|
|
xwrite(outputFd, "\b \b", 3);
|
|
|
|
--*cursor;
|
2000-04-12 17:49:52 +00:00
|
|
|
memmove(command + *cursor, command + *cursor + 1,
|
2000-03-19 05:28:55 +00:00
|
|
|
BUFSIZ - *cursor + 1);
|
|
|
|
|
|
|
|
for (j = *cursor; j < (BUFSIZ - 1); j++) {
|
2000-04-12 17:49:52 +00:00
|
|
|
if (!*(command + j))
|
2000-03-19 05:28:55 +00:00
|
|
|
break;
|
|
|
|
else
|
2000-04-12 17:49:52 +00:00
|
|
|
xwrite(outputFd, (command + j), 1);
|
2000-03-19 05:28:55 +00:00
|
|
|
}
|
2000-03-16 08:09:57 +00:00
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
xwrite(outputFd, " \b", 2);
|
2000-03-16 08:09:57 +00:00
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
while (j-- > *cursor)
|
|
|
|
xwrite(outputFd, "\b", 1);
|
2000-03-16 08:09:57 +00:00
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
--*len;
|
|
|
|
}
|
2000-03-16 08:09:57 +00:00
|
|
|
}
|
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
/* Delete the char in front of the cursor */
|
|
|
|
void input_delete(char* command, int outputFd, int cursor, int *len)
|
|
|
|
{
|
|
|
|
int j = 0;
|
2000-04-09 18:27:46 +00:00
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
if (cursor == *len)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memmove(command + cursor, command + cursor + 1,
|
|
|
|
BUFSIZ - cursor - 1);
|
|
|
|
for (j = cursor; j < (BUFSIZ - 1); j++) {
|
|
|
|
if (!*(command + j))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
xwrite(outputFd, (command + j), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
xwrite(outputFd, " \b", 2);
|
|
|
|
|
|
|
|
while (j-- > cursor)
|
|
|
|
xwrite(outputFd, "\b", 1);
|
|
|
|
--*len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move forward one charactor */
|
|
|
|
void input_forward(int outputFd, int *cursor, int len)
|
|
|
|
{
|
|
|
|
if (*cursor < len) {
|
|
|
|
xwrite(outputFd, "\033[C", 3);
|
|
|
|
++*cursor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move back one charactor */
|
|
|
|
void input_backward(int outputFd, int *cursor)
|
|
|
|
{
|
|
|
|
if (*cursor > 0) {
|
|
|
|
xwrite(outputFd, "\033[D", 3);
|
|
|
|
--*cursor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef BB_FEATURE_SH_TAB_COMPLETION
|
|
|
|
char** username_tab_completion(char* command, int *num_matches)
|
2000-03-17 01:12:41 +00:00
|
|
|
{
|
2000-03-19 05:28:55 +00:00
|
|
|
char **matches = (char **) NULL;
|
|
|
|
*num_matches=0;
|
2000-04-12 17:49:52 +00:00
|
|
|
fprintf(stderr, "\nin username_tab_completion\n");
|
2000-03-19 05:28:55 +00:00
|
|
|
return (matches);
|
2000-03-17 01:12:41 +00:00
|
|
|
}
|
2000-03-19 10:46:06 +00:00
|
|
|
|
|
|
|
#include <dirent.h>
|
2000-04-12 17:49:52 +00:00
|
|
|
char** exe_n_cwd_tab_completion(char* command, int *num_matches)
|
2000-03-17 01:12:41 +00:00
|
|
|
{
|
2000-03-19 10:46:06 +00:00
|
|
|
char *dirName;
|
2000-09-13 02:46:14 +00:00
|
|
|
char **matches;
|
2000-03-19 10:46:06 +00:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *next;
|
|
|
|
|
2000-09-13 02:46:14 +00:00
|
|
|
matches = xmalloc( sizeof(char*)*50);
|
2000-03-19 10:46:06 +00:00
|
|
|
|
|
|
|
/* Stick a wildcard onto the command, for later use */
|
|
|
|
strcat( command, "*");
|
|
|
|
|
|
|
|
/* Now wall the current directory */
|
|
|
|
dirName = get_current_dir_name();
|
|
|
|
dir = opendir(dirName);
|
|
|
|
if (!dir) {
|
|
|
|
/* Don't print an error, just shut up and return */
|
|
|
|
*num_matches=0;
|
|
|
|
return (matches);
|
|
|
|
}
|
|
|
|
while ((next = readdir(dir)) != NULL) {
|
|
|
|
|
|
|
|
/* Some quick sanity checks */
|
|
|
|
if ((strcmp(next->d_name, "..") == 0)
|
|
|
|
|| (strcmp(next->d_name, ".") == 0)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* See if this matches */
|
|
|
|
if (check_wildcard_match(next->d_name, command) == TRUE) {
|
|
|
|
/* Cool, found a match. Add it to the list */
|
2000-09-13 02:46:14 +00:00
|
|
|
matches[*num_matches] = xmalloc(strlen(next->d_name)+1);
|
2000-03-19 10:46:06 +00:00
|
|
|
strcpy( matches[*num_matches], next->d_name);
|
|
|
|
++*num_matches;
|
|
|
|
//matches = realloc( matches, sizeof(char*)*(*num_matches));
|
|
|
|
}
|
|
|
|
}
|
2000-03-19 05:28:55 +00:00
|
|
|
|
|
|
|
return (matches);
|
2000-03-17 01:12:41 +00:00
|
|
|
}
|
2000-04-12 17:49:52 +00:00
|
|
|
|
2000-11-02 17:02:26 +00:00
|
|
|
void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len, int lastWasTab)
|
2000-04-12 17:49:52 +00:00
|
|
|
{
|
|
|
|
/* Do TAB completion */
|
|
|
|
static int num_matches=0;
|
|
|
|
static char **matches = (char **) NULL;
|
2000-11-02 17:02:26 +00:00
|
|
|
int pos = *cursor;
|
2000-04-12 17:49:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (lastWasTab == FALSE) {
|
|
|
|
char *tmp, *tmp1, *matchBuf;
|
|
|
|
|
|
|
|
/* For now, we will not bother with trying to distinguish
|
|
|
|
* whether the cursor is in/at a command extression -- we
|
2000-06-16 19:57:44 +00:00
|
|
|
* will always try all possible matches. If you don't like
|
2000-04-12 17:49:52 +00:00
|
|
|
* that then feel free to fix it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Make a local copy of the string -- up
|
|
|
|
* to the position of the cursor */
|
2000-09-13 02:46:14 +00:00
|
|
|
matchBuf = (char *) xcalloc(BUFSIZ, sizeof(char));
|
2000-11-02 17:02:26 +00:00
|
|
|
strncpy(matchBuf, command, *cursor);
|
2000-04-12 17:49:52 +00:00
|
|
|
tmp=matchBuf;
|
|
|
|
|
|
|
|
/* skip past any command seperator tokens */
|
|
|
|
while (*tmp && (tmp1=strpbrk(tmp, ";|&{(`")) != NULL) {
|
|
|
|
tmp=++tmp1;
|
|
|
|
/* skip any leading white space */
|
|
|
|
while (*tmp && isspace(*tmp))
|
|
|
|
++tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip any leading white space */
|
|
|
|
while (*tmp && isspace(*tmp))
|
|
|
|
++tmp;
|
|
|
|
|
|
|
|
/* Free up any memory already allocated */
|
|
|
|
if (matches) {
|
|
|
|
free(matches);
|
|
|
|
matches = (char **) NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the word starts with `~' and there is no slash in the word,
|
|
|
|
* then try completing this word as a username. */
|
|
|
|
|
|
|
|
/* FIXME -- this check is broken! */
|
|
|
|
if (*tmp == '~' && !strchr(tmp, '/'))
|
|
|
|
matches = username_tab_completion(tmp, &num_matches);
|
|
|
|
|
|
|
|
/* Try to match any executable in our path and everything
|
|
|
|
* in the current working directory that matches. */
|
|
|
|
if (!matches)
|
|
|
|
matches = exe_n_cwd_tab_completion(tmp, &num_matches);
|
|
|
|
|
|
|
|
/* Don't leak memory */
|
|
|
|
free( matchBuf);
|
|
|
|
|
|
|
|
/* Did we find exactly one match? */
|
|
|
|
if (matches && num_matches==1) {
|
|
|
|
/* write out the matched command */
|
|
|
|
strncpy(command+pos, matches[0]+pos, strlen(matches[0])-pos);
|
2000-11-02 17:02:26 +00:00
|
|
|
*len=strlen(command);
|
|
|
|
*cursor=*len;
|
2000-04-12 17:49:52 +00:00
|
|
|
xwrite(outputFd, matches[0]+pos, strlen(matches[0])-pos);
|
2000-11-02 17:02:26 +00:00
|
|
|
return;
|
2000-04-12 17:49:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Ok -- the last char was a TAB. Since they
|
|
|
|
* just hit TAB again, print a list of all the
|
|
|
|
* available choices... */
|
|
|
|
if ( matches && num_matches>0 ) {
|
|
|
|
int i, col;
|
|
|
|
|
|
|
|
/* Go to the next line */
|
|
|
|
xwrite(outputFd, "\n", 1);
|
|
|
|
/* Print the list of matches */
|
|
|
|
for (i=0,col=0; i<num_matches; i++) {
|
|
|
|
char foo[17];
|
|
|
|
sprintf(foo, "%-14s ", matches[i]);
|
|
|
|
col += xwrite(outputFd, foo, strlen(foo));
|
|
|
|
if (col > 60 && matches[i+1] != NULL) {
|
|
|
|
xwrite(outputFd, "\n", 1);
|
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Go to the next line */
|
|
|
|
xwrite(outputFd, "\n", 1);
|
|
|
|
/* Rewrite the prompt */
|
|
|
|
xwrite(outputFd, prompt, strlen(prompt));
|
|
|
|
/* Rewrite the command */
|
2000-11-02 17:02:26 +00:00
|
|
|
xwrite(outputFd, command, *len);
|
2000-04-12 17:49:52 +00:00
|
|
|
/* Put the cursor back to where it used to be */
|
2000-11-02 17:02:26 +00:00
|
|
|
for (cursor=len; *cursor > pos; cursor--)
|
2000-04-12 17:49:52 +00:00
|
|
|
xwrite(outputFd, "\b", 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void get_previous_history(struct history **hp, char* command)
|
|
|
|
{
|
2000-07-19 17:37:57 +00:00
|
|
|
if ((*hp)->s)
|
|
|
|
free((*hp)->s);
|
2000-04-12 17:49:52 +00:00
|
|
|
(*hp)->s = strdup(command);
|
|
|
|
*hp = (*hp)->p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_next_history(struct history **hp, char* command)
|
|
|
|
{
|
2000-07-19 17:37:57 +00:00
|
|
|
if ((*hp)->s)
|
|
|
|
free((*hp)->s);
|
2000-04-12 17:49:52 +00:00
|
|
|
(*hp)->s = strdup(command);
|
|
|
|
*hp = (*hp)->n;
|
|
|
|
}
|
|
|
|
|
2000-03-17 01:12:41 +00:00
|
|
|
/*
|
|
|
|
* This function is used to grab a character buffer
|
|
|
|
* from the input file descriptor and allows you to
|
|
|
|
* a string with full command editing (sortof like
|
|
|
|
* a mini readline).
|
|
|
|
*
|
|
|
|
* The following standard commands are not implemented:
|
|
|
|
* ESC-b -- Move back one word
|
|
|
|
* ESC-f -- Move forward one word
|
|
|
|
* ESC-d -- Delete back one word
|
|
|
|
* ESC-h -- Delete forward one word
|
|
|
|
* CTL-t -- Transpose two characters
|
|
|
|
*
|
|
|
|
* Furthermore, the "vi" command editing keys are not implemented.
|
|
|
|
*
|
|
|
|
* TODO: implement TAB command completion. :)
|
|
|
|
*/
|
2000-04-12 17:49:52 +00:00
|
|
|
extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
|
2000-03-16 08:09:57 +00:00
|
|
|
{
|
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
int inputFd=fileno(stdin);
|
|
|
|
int outputFd=fileno(stdout);
|
2000-03-19 05:28:55 +00:00
|
|
|
int nr = 0;
|
|
|
|
int len = 0;
|
|
|
|
int j = 0;
|
|
|
|
int cursor = 0;
|
|
|
|
int break_out = 0;
|
|
|
|
int ret = 0;
|
|
|
|
int lastWasTab = FALSE;
|
|
|
|
char c = 0;
|
|
|
|
struct history *hp = his_end;
|
|
|
|
|
|
|
|
if (!reset_term) {
|
2000-04-21 01:26:49 +00:00
|
|
|
|
|
|
|
getTermSettings(inputFd, (void*) &initial_settings);
|
|
|
|
memcpy(&new_settings, &initial_settings, sizeof(struct termios));
|
|
|
|
new_settings.c_cc[VMIN] = 1;
|
|
|
|
new_settings.c_cc[VTIME] = 0;
|
|
|
|
new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */
|
|
|
|
new_settings.c_lflag &= ~ICANON; /* unbuffered input */
|
|
|
|
new_settings.c_lflag &= ~(ECHO|ECHOCTL|ECHONL); /* Turn off echoing */
|
2000-03-19 05:28:55 +00:00
|
|
|
reset_term = 1;
|
|
|
|
}
|
2000-04-21 01:26:49 +00:00
|
|
|
setTermSettings(inputFd, (void*) &new_settings);
|
2000-03-16 08:09:57 +00:00
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
memset(command, 0, BUFSIZ);
|
2000-03-16 08:09:57 +00:00
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
while (1) {
|
2000-03-16 08:09:57 +00:00
|
|
|
|
|
|
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
2000-04-12 17:49:52 +00:00
|
|
|
return;
|
2000-04-21 01:26:49 +00:00
|
|
|
//fprintf(stderr, "got a '%c' (%d)\n", c, c);
|
2000-04-09 18:24:05 +00:00
|
|
|
|
2000-03-16 08:09:57 +00:00
|
|
|
switch (c) {
|
2000-04-12 17:49:52 +00:00
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
/* Enter */
|
|
|
|
*(command + len++ + 1) = c;
|
|
|
|
xwrite(outputFd, &c, 1);
|
|
|
|
break_out = 1;
|
|
|
|
break;
|
2000-03-19 05:28:55 +00:00
|
|
|
case 1:
|
|
|
|
/* Control-a -- Beginning of line */
|
|
|
|
input_home(outputFd, &cursor);
|
|
|
|
case 2:
|
|
|
|
/* Control-b -- Move back one character */
|
2000-04-12 17:49:52 +00:00
|
|
|
input_backward(outputFd, &cursor);
|
2000-04-21 01:26:49 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
/* Control-c -- leave the current line,
|
|
|
|
* and start over on the next line */
|
|
|
|
|
|
|
|
/* Go to the next line */
|
|
|
|
xwrite(outputFd, "\n", 1);
|
|
|
|
|
|
|
|
/* Rewrite the prompt */
|
|
|
|
xwrite(outputFd, prompt, strlen(prompt));
|
|
|
|
|
|
|
|
/* Reset the command string */
|
2000-07-14 01:13:37 +00:00
|
|
|
memset(command, 0, BUFSIZ);
|
2000-04-21 01:26:49 +00:00
|
|
|
len = cursor = 0;
|
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
/* Control-d -- Delete one character, or exit
|
|
|
|
* if the len=0 and no chars to delete */
|
|
|
|
if (len == 0) {
|
|
|
|
xwrite(outputFd, "exit", 4);
|
|
|
|
clean_up_and_die(0);
|
|
|
|
} else {
|
|
|
|
input_delete(command, outputFd, cursor, &len);
|
2000-03-19 05:28:55 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-04-12 17:49:52 +00:00
|
|
|
case 5:
|
|
|
|
/* Control-e -- End of line */
|
|
|
|
input_end(outputFd, &cursor, len);
|
|
|
|
break;
|
2000-03-19 05:28:55 +00:00
|
|
|
case 6:
|
|
|
|
/* Control-f -- Move forward one character */
|
2000-04-12 17:49:52 +00:00
|
|
|
input_forward(outputFd, &cursor, len);
|
2000-03-19 05:28:55 +00:00
|
|
|
break;
|
2000-04-12 17:49:52 +00:00
|
|
|
case '\b':
|
|
|
|
case DEL:
|
2000-04-21 01:26:49 +00:00
|
|
|
/* Control-h and DEL */
|
2000-04-12 17:49:52 +00:00
|
|
|
input_backspace(command, outputFd, &cursor, &len);
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
#ifdef BB_FEATURE_SH_TAB_COMPLETION
|
2000-11-02 17:02:26 +00:00
|
|
|
input_tab(command, prompt, outputFd, &cursor,
|
|
|
|
&len, lastWasTab);
|
2000-04-12 17:49:52 +00:00
|
|
|
#endif
|
2000-03-19 05:28:55 +00:00
|
|
|
break;
|
|
|
|
case 14:
|
2000-04-12 17:49:52 +00:00
|
|
|
/* Control-n -- Get next command in history */
|
2000-03-19 05:28:55 +00:00
|
|
|
if (hp && hp->n && hp->n->s) {
|
2000-04-12 17:49:52 +00:00
|
|
|
get_next_history(&hp, command);
|
|
|
|
goto rewrite_line;
|
|
|
|
} else {
|
|
|
|
xwrite(outputFd, "\007", 1);
|
2000-03-19 05:28:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 16:
|
2000-04-12 17:49:52 +00:00
|
|
|
/* Control-p -- Get previous command from history */
|
2000-03-19 05:28:55 +00:00
|
|
|
if (hp && hp->p) {
|
2000-04-12 17:49:52 +00:00
|
|
|
get_previous_history(&hp, command);
|
|
|
|
goto rewrite_line;
|
|
|
|
} else {
|
|
|
|
xwrite(outputFd, "\007", 1);
|
2000-03-19 05:28:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ESC:{
|
|
|
|
/* escape sequence follows */
|
|
|
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
2000-04-12 17:49:52 +00:00
|
|
|
return;
|
2000-03-19 05:28:55 +00:00
|
|
|
|
|
|
|
if (c == '[') { /* 91 */
|
|
|
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
2000-04-12 17:49:52 +00:00
|
|
|
return;
|
2000-03-19 05:28:55 +00:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'A':
|
2000-04-12 17:49:52 +00:00
|
|
|
/* Up Arrow -- Get previous command from history */
|
2000-03-19 05:28:55 +00:00
|
|
|
if (hp && hp->p) {
|
2000-04-12 17:49:52 +00:00
|
|
|
get_previous_history(&hp, command);
|
|
|
|
goto rewrite_line;
|
|
|
|
} else {
|
|
|
|
xwrite(outputFd, "\007", 1);
|
2000-03-19 05:28:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'B':
|
2000-04-12 17:49:52 +00:00
|
|
|
/* Down Arrow -- Get next command in history */
|
2000-03-19 05:28:55 +00:00
|
|
|
if (hp && hp->n && hp->n->s) {
|
2000-04-12 17:49:52 +00:00
|
|
|
get_next_history(&hp, command);
|
|
|
|
goto rewrite_line;
|
|
|
|
} else {
|
|
|
|
xwrite(outputFd, "\007", 1);
|
2000-03-19 05:28:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
/* Rewrite the line with the selected history item */
|
|
|
|
rewrite_line:
|
|
|
|
/* erase old command from command line */
|
|
|
|
len = strlen(command)-strlen(hp->s);
|
2000-07-04 06:22:18 +00:00
|
|
|
|
|
|
|
while (len>cursor)
|
|
|
|
input_delete(command, outputFd, cursor, &len);
|
|
|
|
while (cursor>0)
|
2000-04-12 17:49:52 +00:00
|
|
|
input_backspace(command, outputFd, &cursor, &len);
|
|
|
|
input_home(outputFd, &cursor);
|
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
/* write new command */
|
2000-04-12 17:49:52 +00:00
|
|
|
strcpy(command, hp->s);
|
2000-03-19 05:28:55 +00:00
|
|
|
len = strlen(hp->s);
|
2000-04-12 17:49:52 +00:00
|
|
|
xwrite(outputFd, command, len);
|
2000-03-19 05:28:55 +00:00
|
|
|
cursor = len;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
/* Right Arrow -- Move forward one character */
|
2000-04-12 17:49:52 +00:00
|
|
|
input_forward(outputFd, &cursor, len);
|
2000-03-19 05:28:55 +00:00
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
/* Left Arrow -- Move back one character */
|
2000-04-12 17:49:52 +00:00
|
|
|
input_backward(outputFd, &cursor);
|
2000-03-19 05:28:55 +00:00
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
/* Delete */
|
2000-04-12 17:49:52 +00:00
|
|
|
input_delete(command, outputFd, cursor, &len);
|
2000-03-19 05:28:55 +00:00
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
/* Home (Ctrl-A) */
|
|
|
|
input_home(outputFd, &cursor);
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
/* End (Ctrl-E) */
|
|
|
|
input_end(outputFd, &cursor, len);
|
|
|
|
break;
|
2000-04-12 17:49:52 +00:00
|
|
|
default:
|
|
|
|
xwrite(outputFd, "\007", 1);
|
2000-03-19 05:28:55 +00:00
|
|
|
}
|
|
|
|
if (c == '1' || c == '3' || c == '4')
|
|
|
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
2000-04-12 17:49:52 +00:00
|
|
|
return; /* read 126 (~) */
|
2000-03-19 05:28:55 +00:00
|
|
|
}
|
|
|
|
if (c == 'O') {
|
|
|
|
/* 79 */
|
|
|
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
2000-04-12 17:49:52 +00:00
|
|
|
return;
|
2000-03-19 05:28:55 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'H':
|
|
|
|
/* Home (xterm) */
|
|
|
|
input_home(outputFd, &cursor);
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
/* End (xterm) */
|
|
|
|
input_end(outputFd, &cursor, len);
|
|
|
|
break;
|
2000-04-12 17:49:52 +00:00
|
|
|
default:
|
|
|
|
xwrite(outputFd, "\007", 1);
|
2000-03-19 05:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: /* If it's regular input, do the normal thing */
|
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
if (!isprint(c)) { /* Skip non-printable characters */
|
2000-03-19 05:28:55 +00:00
|
|
|
break;
|
2000-04-12 17:49:52 +00:00
|
|
|
}
|
2000-03-19 05:28:55 +00:00
|
|
|
|
|
|
|
if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
|
|
|
|
break;
|
|
|
|
|
|
|
|
len++;
|
|
|
|
|
|
|
|
if (cursor == (len - 1)) { /* Append if at the end of the line */
|
2000-04-12 17:49:52 +00:00
|
|
|
*(command + cursor) = c;
|
2000-03-19 05:28:55 +00:00
|
|
|
} else { /* Insert otherwise */
|
2000-04-12 17:49:52 +00:00
|
|
|
memmove(command + cursor + 1, command + cursor,
|
2000-03-19 05:28:55 +00:00
|
|
|
len - cursor - 1);
|
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
*(command + cursor) = c;
|
2000-03-19 05:28:55 +00:00
|
|
|
|
|
|
|
for (j = cursor; j < len; j++)
|
2000-04-12 17:49:52 +00:00
|
|
|
xwrite(outputFd, command + j, 1);
|
2000-03-19 05:28:55 +00:00
|
|
|
for (; j > cursor; j--)
|
|
|
|
xwrite(outputFd, "\033[D", 3);
|
|
|
|
}
|
2000-03-16 08:09:57 +00:00
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
cursor++;
|
|
|
|
xwrite(outputFd, &c, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c == '\t')
|
|
|
|
lastWasTab = TRUE;
|
|
|
|
else
|
|
|
|
lastWasTab = FALSE;
|
2000-03-16 08:09:57 +00:00
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
if (break_out) /* Enter is the command terminator, no more input. */
|
|
|
|
break;
|
|
|
|
}
|
2000-03-16 08:09:57 +00:00
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
nr = len + 1;
|
2000-04-21 01:26:49 +00:00
|
|
|
setTermSettings(inputFd, (void *) &initial_settings);
|
2000-03-19 05:28:55 +00:00
|
|
|
reset_term = 0;
|
2000-03-16 08:09:57 +00:00
|
|
|
|
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
/* Handle command history log */
|
2000-04-12 17:49:52 +00:00
|
|
|
if (*(command)) {
|
2000-03-16 08:09:57 +00:00
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
struct history *h = his_end;
|
2000-03-16 08:09:57 +00:00
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
if (!h) {
|
2000-07-19 17:37:57 +00:00
|
|
|
/* No previous history -- this memory is never freed */
|
2000-09-13 02:46:14 +00:00
|
|
|
h = his_front = xmalloc(sizeof(struct history));
|
|
|
|
h->n = xmalloc(sizeof(struct history));
|
2000-03-16 08:09:57 +00:00
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
h->p = NULL;
|
2000-04-12 17:49:52 +00:00
|
|
|
h->s = strdup(command);
|
2000-03-19 05:28:55 +00:00
|
|
|
h->n->p = h;
|
|
|
|
h->n->n = NULL;
|
|
|
|
h->n->s = NULL;
|
|
|
|
his_end = h->n;
|
|
|
|
history_counter++;
|
|
|
|
} else {
|
2000-07-19 17:37:57 +00:00
|
|
|
/* Add a new history command -- this memory is never freed */
|
2000-09-13 02:46:14 +00:00
|
|
|
h->n = xmalloc(sizeof(struct history));
|
2000-03-19 05:28:55 +00:00
|
|
|
|
|
|
|
h->n->p = h;
|
|
|
|
h->n->n = NULL;
|
|
|
|
h->n->s = NULL;
|
2000-04-12 17:49:52 +00:00
|
|
|
h->s = strdup(command);
|
2000-03-19 05:28:55 +00:00
|
|
|
his_end = h->n;
|
|
|
|
|
|
|
|
/* After max history, remove the oldest command */
|
|
|
|
if (history_counter >= MAX_HISTORY) {
|
|
|
|
|
|
|
|
struct history *p = his_front->n;
|
|
|
|
|
|
|
|
p->p = NULL;
|
|
|
|
free(his_front->s);
|
|
|
|
free(his_front);
|
|
|
|
his_front = p;
|
|
|
|
} else {
|
|
|
|
history_counter++;
|
|
|
|
}
|
|
|
|
}
|
2000-03-16 08:09:57 +00:00
|
|
|
}
|
|
|
|
|
2000-04-12 17:49:52 +00:00
|
|
|
return;
|
2000-03-16 08:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern void cmdedit_init(void)
|
|
|
|
{
|
2000-07-28 15:14:45 +00:00
|
|
|
if(exithandler_set == 0) {
|
|
|
|
atexit(cmdedit_reset_term); /* be sure to do this only once */
|
|
|
|
exithandler_set = 1;
|
|
|
|
}
|
2000-04-21 01:26:49 +00:00
|
|
|
signal(SIGKILL, clean_up_and_die);
|
2000-04-12 17:49:52 +00:00
|
|
|
signal(SIGINT, clean_up_and_die);
|
|
|
|
signal(SIGQUIT, clean_up_and_die);
|
|
|
|
signal(SIGTERM, clean_up_and_die);
|
2000-03-16 08:09:57 +00:00
|
|
|
}
|
2000-07-28 15:14:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Undo the effects of cmdedit_init() as good as we can:
|
|
|
|
** I am not aware of a way to revoke an atexit() handler,
|
|
|
|
** but, fortunately, our particular handler can be made
|
|
|
|
** a no-op by setting reset_term = 0.
|
|
|
|
*/
|
|
|
|
extern void cmdedit_terminate(void)
|
|
|
|
{
|
|
|
|
cmdedit_reset_term();
|
|
|
|
reset_term = 0;
|
|
|
|
signal(SIGKILL, SIG_DFL);
|
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
signal(SIGQUIT, SIG_DFL);
|
|
|
|
signal(SIGTERM, SIG_DFL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-03-19 05:28:55 +00:00
|
|
|
#endif /* BB_FEATURE_SH_COMMAND_EDITING */
|