Some enhancements I've been working on over the weekend,
-Erik
This commit is contained in:
parent
a2685735b1
commit
f0657d3229
@ -187,7 +187,7 @@
|
|||||||
//#define BB_FEATURE_SORT_REVERSE
|
//#define BB_FEATURE_SORT_REVERSE
|
||||||
//
|
//
|
||||||
// Enable command line editing in the shell
|
// Enable command line editing in the shell
|
||||||
#define BB_FEATURE_SH_COMMAND_EDITING
|
//#define BB_FEATURE_SH_COMMAND_EDITING
|
||||||
//
|
//
|
||||||
// Enable tab completion in the shell (not yet working very well)
|
// Enable tab completion in the shell (not yet working very well)
|
||||||
//#define BB_FEATURE_SH_TAB_COMPLETION
|
//#define BB_FEATURE_SH_TAB_COMPLETION
|
||||||
|
561
cmdedit.c
561
cmdedit.c
@ -53,9 +53,10 @@ static struct history *his_front = NULL; /* First element in command line list *
|
|||||||
static struct history *his_end = NULL; /* Last element in command line list */
|
static struct history *his_end = NULL; /* Last element in command line list */
|
||||||
static struct termio old_term, new_term; /* Current termio and the previous termio before starting ash */
|
static struct termio old_term, new_term; /* Current termio and the previous termio before starting ash */
|
||||||
|
|
||||||
|
static int cmdedit_termw = 80; /* actual terminal width */
|
||||||
|
static int cmdedit_scroll = 27; /* width of EOL scrolling region */
|
||||||
static int history_counter = 0; /* Number of commands in history list */
|
static int history_counter = 0; /* Number of commands in history list */
|
||||||
static int reset_term = 0; /* Set to true if the terminal needs to be reset upon exit */
|
static int reset_term = 0; /* Set to true if the terminal needs to be reset upon exit */
|
||||||
char *parsenextc; /* copy of parsefile->nextc */
|
|
||||||
|
|
||||||
struct history {
|
struct history {
|
||||||
char *s;
|
char *s;
|
||||||
@ -63,85 +64,42 @@ struct history {
|
|||||||
struct history *n;
|
struct history *n;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define xwrite write
|
||||||
|
|
||||||
/* Version of write which resumes after a signal is caught. */
|
void
|
||||||
int xwrite(int fd, char *buf, int nbytes)
|
cmdedit_setwidth(int w)
|
||||||
{
|
{
|
||||||
int ntry;
|
if (w > 20) {
|
||||||
int i;
|
cmdedit_termw = w;
|
||||||
int n;
|
cmdedit_scroll = w / 3;
|
||||||
|
} else {
|
||||||
n = nbytes;
|
errorMsg("\n*** Error: minimum screen width is 21\n");
|
||||||
ntry = 0;
|
|
||||||
for (;;) {
|
|
||||||
i = write(fd, buf, n);
|
|
||||||
if (i > 0) {
|
|
||||||
if ((n -= i) <= 0)
|
|
||||||
return nbytes;
|
|
||||||
buf += i;
|
|
||||||
ntry = 0;
|
|
||||||
} else if (i == 0) {
|
|
||||||
if (++ntry > 10)
|
|
||||||
return nbytes - n;
|
|
||||||
} else if (errno != EINTR) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Version of ioctl that retries after a signal is caught. */
|
|
||||||
int xioctl(int fd, unsigned long request, char *arg)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void cmdedit_reset_term(void)
|
void cmdedit_reset_term(void)
|
||||||
{
|
{
|
||||||
if (reset_term)
|
if (reset_term)
|
||||||
xioctl(fileno(stdin), TCSETA, (void *) &old_term);
|
ioctl(fileno(stdin), TCSETA, (void *) &old_term);
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepareToDie(int sig)
|
void clean_up_and_die(int sig)
|
||||||
{
|
{
|
||||||
cmdedit_reset_term();
|
cmdedit_reset_term();
|
||||||
fprintf(stdout, "\n");
|
fprintf(stdout, "\n");
|
||||||
exit(TRUE);
|
exit(TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Go to HOME position */
|
||||||
void input_home(int outputFd, int *cursor)
|
void input_home(int outputFd, int *cursor)
|
||||||
{ /* Command line input routines */
|
{
|
||||||
while (*cursor > 0) {
|
while (*cursor > 0) {
|
||||||
xwrite(outputFd, "\b", 1);
|
xwrite(outputFd, "\b", 1);
|
||||||
--*cursor;
|
--*cursor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Go to END position */
|
||||||
void input_delete(int outputFd, int cursor)
|
|
||||||
{
|
|
||||||
int j = 0;
|
|
||||||
|
|
||||||
memmove(parsenextc + cursor, parsenextc + cursor + 1,
|
|
||||||
BUFSIZ - cursor - 1);
|
|
||||||
for (j = cursor; j < (BUFSIZ - 1); j++) {
|
|
||||||
if (!*(parsenextc + j))
|
|
||||||
break;
|
|
||||||
else
|
|
||||||
xwrite(outputFd, (parsenextc + j), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
xwrite(outputFd, " \b", 2);
|
|
||||||
|
|
||||||
while (j-- > cursor)
|
|
||||||
xwrite(outputFd, "\b", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void input_end(int outputFd, int *cursor, int len)
|
void input_end(int outputFd, int *cursor, int len)
|
||||||
{
|
{
|
||||||
while (*cursor < len) {
|
while (*cursor < len) {
|
||||||
@ -150,22 +108,22 @@ void input_end(int outputFd, int *cursor, int len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Delete the char in back of the cursor */
|
||||||
void input_backspace(int outputFd, int *cursor, int *len)
|
void input_backspace(char* command, int outputFd, int *cursor, int *len)
|
||||||
{
|
{
|
||||||
int j = 0;
|
int j = 0;
|
||||||
|
|
||||||
if (*cursor > 0) {
|
if (*cursor > 0) {
|
||||||
xwrite(outputFd, "\b \b", 3);
|
xwrite(outputFd, "\b \b", 3);
|
||||||
--*cursor;
|
--*cursor;
|
||||||
memmove(parsenextc + *cursor, parsenextc + *cursor + 1,
|
memmove(command + *cursor, command + *cursor + 1,
|
||||||
BUFSIZ - *cursor + 1);
|
BUFSIZ - *cursor + 1);
|
||||||
|
|
||||||
for (j = *cursor; j < (BUFSIZ - 1); j++) {
|
for (j = *cursor; j < (BUFSIZ - 1); j++) {
|
||||||
if (!*(parsenextc + j))
|
if (!*(command + j))
|
||||||
break;
|
break;
|
||||||
else
|
else
|
||||||
xwrite(outputFd, (parsenextc + j), 1);
|
xwrite(outputFd, (command + j), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
xwrite(outputFd, " \b", 2);
|
xwrite(outputFd, " \b", 2);
|
||||||
@ -177,18 +135,61 @@ void input_backspace(int outputFd, int *cursor, int *len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef BB_FEATURE_SH_TAB_COMPLETION
|
/* Delete the char in front of the cursor */
|
||||||
|
void input_delete(char* command, int outputFd, int cursor, int *len)
|
||||||
|
{
|
||||||
|
int j = 0;
|
||||||
|
|
||||||
char** username_completion_matches(char* command, int *num_matches)
|
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)
|
||||||
{
|
{
|
||||||
char **matches = (char **) NULL;
|
char **matches = (char **) NULL;
|
||||||
*num_matches=0;
|
*num_matches=0;
|
||||||
fprintf(stderr, "\nin username_completion_matches\n");
|
fprintf(stderr, "\nin username_tab_completion\n");
|
||||||
return (matches);
|
return (matches);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
char** find_path_executable_n_cwd_matches(char* command, int *num_matches)
|
char** exe_n_cwd_tab_completion(char* command, int *num_matches)
|
||||||
{
|
{
|
||||||
char *dirName;
|
char *dirName;
|
||||||
char **matches = (char **) NULL;
|
char **matches = (char **) NULL;
|
||||||
@ -227,124 +228,9 @@ char** find_path_executable_n_cwd_matches(char* command, int *num_matches)
|
|||||||
|
|
||||||
return (matches);
|
return (matches);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
void input_tab(char* command, int outputFd, int *cursor, int *len)
|
||||||
* 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. :)
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|
||||||
char command[BUFSIZ])
|
|
||||||
{
|
{
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
memset(command, 0, sizeof(command));
|
|
||||||
parsenextc = command;
|
|
||||||
if (!reset_term) {
|
|
||||||
xioctl(inputFd, TCGETA, (void *) &old_term);
|
|
||||||
memcpy(&new_term, &old_term, sizeof(struct termio));
|
|
||||||
|
|
||||||
new_term.c_cc[VMIN] = 1;
|
|
||||||
new_term.c_cc[VTIME] = 0;
|
|
||||||
new_term.c_lflag &= ~ICANON; /* unbuffered input */
|
|
||||||
new_term.c_lflag &= ~ECHO;
|
|
||||||
xioctl(inputFd, TCSETA, (void *) &new_term);
|
|
||||||
reset_term = 1;
|
|
||||||
} else {
|
|
||||||
xioctl(inputFd, TCSETA, (void *) &new_term);
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(parsenextc, 0, BUFSIZ);
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
|
|
||||||
if ((ret = read(inputFd, &c, 1)) < 1)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
fprintf(stderr, "\n\nkey=%d (%c)\n\n", c, c);
|
|
||||||
/* Go to the next line */
|
|
||||||
xwrite(outputFd, "\n", 1);
|
|
||||||
/* Rewrite the prompt */
|
|
||||||
xwrite(outputFd, prompt, strlen(prompt));
|
|
||||||
/* Rewrite the command */
|
|
||||||
xwrite(outputFd, parsenextc, len);
|
|
||||||
|
|
||||||
switch (c) {
|
|
||||||
case 1:
|
|
||||||
/* Control-a -- Beginning of line */
|
|
||||||
input_home(outputFd, &cursor);
|
|
||||||
case 5:
|
|
||||||
/* Control-e -- End of line */
|
|
||||||
input_end(outputFd, &cursor, len);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
/* Control-b -- Move back one character */
|
|
||||||
if (cursor > 0) {
|
|
||||||
xwrite(outputFd, "\033[D", 3);
|
|
||||||
cursor--;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
/* Control-f -- Move forward one character */
|
|
||||||
if (cursor < len) {
|
|
||||||
xwrite(outputFd, "\033[C", 3);
|
|
||||||
cursor++;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
/* Control-d -- Delete one character */
|
|
||||||
if (cursor != len) {
|
|
||||||
input_delete(outputFd, cursor);
|
|
||||||
len--;
|
|
||||||
} else if (len == 0) {
|
|
||||||
prepareToDie(0);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 14:
|
|
||||||
/* Control-n -- Get next command */
|
|
||||||
if (hp && hp->n && hp->n->s) {
|
|
||||||
free(hp->s);
|
|
||||||
hp->s = strdup(parsenextc);
|
|
||||||
hp = hp->n;
|
|
||||||
goto hop;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 16:
|
|
||||||
/* Control-p -- Get previous command */
|
|
||||||
if (hp && hp->p) {
|
|
||||||
free(hp->s);
|
|
||||||
hp->s = strdup(parsenextc);
|
|
||||||
hp = hp->p;
|
|
||||||
goto hop;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case '\t':
|
|
||||||
#ifdef BB_FEATURE_SH_TAB_COMPLETION
|
|
||||||
{
|
|
||||||
/* Do TAB completion */
|
/* Do TAB completion */
|
||||||
static int num_matches=0;
|
static int num_matches=0;
|
||||||
static char **matches = (char **) NULL;
|
static char **matches = (char **) NULL;
|
||||||
@ -363,7 +249,7 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
/* Make a local copy of the string -- up
|
/* Make a local copy of the string -- up
|
||||||
* to the position of the cursor */
|
* to the position of the cursor */
|
||||||
matchBuf = (char *) calloc(BUFSIZ, sizeof(char));
|
matchBuf = (char *) calloc(BUFSIZ, sizeof(char));
|
||||||
strncpy(matchBuf, parsenextc, cursor);
|
strncpy(matchBuf, command, cursor);
|
||||||
tmp=matchBuf;
|
tmp=matchBuf;
|
||||||
|
|
||||||
/* skip past any command seperator tokens */
|
/* skip past any command seperator tokens */
|
||||||
@ -389,12 +275,12 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
|
|
||||||
/* FIXME -- this check is broken! */
|
/* FIXME -- this check is broken! */
|
||||||
if (*tmp == '~' && !strchr(tmp, '/'))
|
if (*tmp == '~' && !strchr(tmp, '/'))
|
||||||
matches = username_completion_matches(tmp, &num_matches);
|
matches = username_tab_completion(tmp, &num_matches);
|
||||||
|
|
||||||
/* Try to match any executable in our path and everything
|
/* Try to match any executable in our path and everything
|
||||||
* in the current working directory that matches. */
|
* in the current working directory that matches. */
|
||||||
if (!matches)
|
if (!matches)
|
||||||
matches = find_path_executable_n_cwd_matches(tmp, &num_matches);
|
matches = exe_n_cwd_tab_completion(tmp, &num_matches);
|
||||||
|
|
||||||
/* Don't leak memory */
|
/* Don't leak memory */
|
||||||
free( matchBuf);
|
free( matchBuf);
|
||||||
@ -402,8 +288,8 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
/* Did we find exactly one match? */
|
/* Did we find exactly one match? */
|
||||||
if (matches && num_matches==1) {
|
if (matches && num_matches==1) {
|
||||||
/* write out the matched command */
|
/* write out the matched command */
|
||||||
strncpy(parsenextc+pos, matches[0]+pos, strlen(matches[0])-pos);
|
strncpy(command+pos, matches[0]+pos, strlen(matches[0])-pos);
|
||||||
len=strlen(parsenextc);
|
len=strlen(command);
|
||||||
cursor=len;
|
cursor=len;
|
||||||
xwrite(outputFd, matches[0]+pos, strlen(matches[0])-pos);
|
xwrite(outputFd, matches[0]+pos, strlen(matches[0])-pos);
|
||||||
break;
|
break;
|
||||||
@ -432,127 +318,271 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
/* Rewrite the prompt */
|
/* Rewrite the prompt */
|
||||||
xwrite(outputFd, prompt, strlen(prompt));
|
xwrite(outputFd, prompt, strlen(prompt));
|
||||||
/* Rewrite the command */
|
/* Rewrite the command */
|
||||||
xwrite(outputFd, parsenextc, len);
|
xwrite(outputFd, command, len);
|
||||||
/* Put the cursor back to where it used to be */
|
/* Put the cursor back to where it used to be */
|
||||||
for (cursor=len; cursor > pos; cursor--)
|
for (cursor=len; cursor > pos; cursor--)
|
||||||
xwrite(outputFd, "\b", 1);
|
xwrite(outputFd, "\b", 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
#else
|
|
||||||
break;
|
|
||||||
#endif
|
#endif
|
||||||
case '\b':
|
|
||||||
case DEL:
|
void get_previous_history(struct history **hp, char* command)
|
||||||
/* Backspace */
|
{
|
||||||
input_backspace(outputFd, &cursor, &len);
|
free((*hp)->s);
|
||||||
break;
|
(*hp)->s = strdup(command);
|
||||||
|
*hp = (*hp)->p;
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_next_history(struct history **hp, char* command)
|
||||||
|
{
|
||||||
|
free((*hp)->s);
|
||||||
|
(*hp)->s = strdup(command);
|
||||||
|
*hp = (*hp)->n;
|
||||||
|
|
||||||
|
cmdedit_redraw( NULL, hp->s, -2, -2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
/* prompt : if !=NULL, print the prompt
|
||||||
|
* command: the command line to be displayed
|
||||||
|
* where : where to display changes from.
|
||||||
|
* -1 for no change, -2 for new line
|
||||||
|
* cursor : desired location for the cursor.
|
||||||
|
* -1 for Beginning of line.
|
||||||
|
* -2 for End of Line,
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
cmdedit_redraw(char* prompt, char* command, int where, int cursor)
|
||||||
|
{
|
||||||
|
static char* last_command;
|
||||||
|
int cmdedit_width;
|
||||||
|
|
||||||
|
if (where == -2) {
|
||||||
|
/* Rewrite the prompt and clean up static variables */
|
||||||
|
xwrite(outputFd, "\n", 1);
|
||||||
|
if (prompt) {
|
||||||
|
strcpy(last_command, prompt);
|
||||||
|
xwrite(outputFd, prompt, strlen(prompt));
|
||||||
|
} else {
|
||||||
|
last_command[0] = '\0';
|
||||||
|
xwrite(outputFd, "# ", 2);
|
||||||
|
}
|
||||||
|
cmdedit_width = cmdedit_termw - cmdedit_strlen(prompt);
|
||||||
|
} else if (strcmp(command, last_command) != 0) {
|
||||||
|
strcpy(last_command, prompt);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* erase old command from command line */
|
||||||
|
len = strlen(command)-strlen(last_command);
|
||||||
|
while (len>0)
|
||||||
|
input_backspace(command, outputFd, &cursor, &len);
|
||||||
|
input_home(outputFd, &cursor);
|
||||||
|
|
||||||
|
/* Rewrite the command */
|
||||||
|
xwrite(outputFd, command+where, len);
|
||||||
|
|
||||||
|
/* Put the where it is supposed to be */
|
||||||
|
for (cursor=len; cursor > where; cursor--)
|
||||||
|
xwrite(outputFd, "\b", 1);
|
||||||
|
|
||||||
|
/* write new command */
|
||||||
|
strcpy(command, hp->s);
|
||||||
|
len = strlen(hp->s);
|
||||||
|
xwrite(outputFd, command+where, len);
|
||||||
|
cursor = len;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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. :)
|
||||||
|
*/
|
||||||
|
extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
|
||||||
|
{
|
||||||
|
|
||||||
|
int inputFd=fileno(stdin);
|
||||||
|
int outputFd=fileno(stdout);
|
||||||
|
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;
|
||||||
|
|
||||||
|
memset(command, 0, sizeof(command));
|
||||||
|
if (!reset_term) {
|
||||||
|
ioctl(inputFd, TCGETA, (void *) &old_term);
|
||||||
|
memcpy(&new_term, &old_term, sizeof(struct termio));
|
||||||
|
|
||||||
|
new_term.c_cc[VMIN] = 1;
|
||||||
|
new_term.c_cc[VTIME] = 0;
|
||||||
|
new_term.c_lflag &= ~ICANON; /* unbuffered input */
|
||||||
|
new_term.c_lflag &= ~ECHO;
|
||||||
|
reset_term = 1;
|
||||||
|
ioctl(inputFd, TCSETA, (void *) &new_term);
|
||||||
|
} else {
|
||||||
|
ioctl(inputFd, TCSETA, (void *) &new_term);
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(command, 0, BUFSIZ);
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
|
||||||
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
switch (c) {
|
||||||
case '\n':
|
case '\n':
|
||||||
|
case '\r':
|
||||||
/* Enter */
|
/* Enter */
|
||||||
*(parsenextc + len++ + 1) = c;
|
*(command + len++ + 1) = c;
|
||||||
xwrite(outputFd, &c, 1);
|
xwrite(outputFd, &c, 1);
|
||||||
break_out = 1;
|
break_out = 1;
|
||||||
break;
|
break;
|
||||||
|
case 1:
|
||||||
|
/* Control-a -- Beginning of line */
|
||||||
|
input_home(outputFd, &cursor);
|
||||||
|
case 2:
|
||||||
|
/* Control-b -- Move back one character */
|
||||||
|
input_backward(outputFd, &cursor);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
/* Control-e -- End of line */
|
||||||
|
input_end(outputFd, &cursor, len);
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
/* Control-f -- Move forward one character */
|
||||||
|
input_forward(outputFd, &cursor, len);
|
||||||
|
break;
|
||||||
|
case '\b':
|
||||||
|
case DEL:
|
||||||
|
/* control-h and DEL */
|
||||||
|
input_backspace(command, outputFd, &cursor, &len);
|
||||||
|
break;
|
||||||
|
case '\t':
|
||||||
|
#ifdef BB_FEATURE_SH_TAB_COMPLETION
|
||||||
|
input_tab(command, outputFd, &cursor, &len);
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
case 14:
|
||||||
|
/* Control-n -- Get next command in history */
|
||||||
|
if (hp && hp->n && hp->n->s) {
|
||||||
|
get_next_history(&hp, command);
|
||||||
|
goto rewrite_line;
|
||||||
|
} else {
|
||||||
|
xwrite(outputFd, "\007", 1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 16:
|
||||||
|
/* Control-p -- Get previous command from history */
|
||||||
|
if (hp && hp->p) {
|
||||||
|
get_previous_history(&hp, command);
|
||||||
|
goto rewrite_line;
|
||||||
|
} else {
|
||||||
|
xwrite(outputFd, "\007", 1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case ESC:{
|
case ESC:{
|
||||||
/* escape sequence follows */
|
/* escape sequence follows */
|
||||||
if ((ret = read(inputFd, &c, 1)) < 1)
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
||||||
return ret;
|
return;
|
||||||
|
|
||||||
if (c == '[') { /* 91 */
|
if (c == '[') { /* 91 */
|
||||||
if ((ret = read(inputFd, &c, 1)) < 1)
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
||||||
return ret;
|
return;
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'A':
|
case 'A':
|
||||||
/* Up Arrow -- Get previous command */
|
/* Up Arrow -- Get previous command from history */
|
||||||
if (hp && hp->p) {
|
if (hp && hp->p) {
|
||||||
free(hp->s);
|
get_previous_history(&hp, command);
|
||||||
hp->s = strdup(parsenextc);
|
goto rewrite_line;
|
||||||
hp = hp->p;
|
} else {
|
||||||
goto hop;
|
xwrite(outputFd, "\007", 1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'B':
|
case 'B':
|
||||||
/* Down Arrow -- Get next command */
|
/* Down Arrow -- Get next command in history */
|
||||||
if (hp && hp->n && hp->n->s) {
|
if (hp && hp->n && hp->n->s) {
|
||||||
free(hp->s);
|
get_next_history(&hp, command);
|
||||||
hp->s = strdup(parsenextc);
|
goto rewrite_line;
|
||||||
hp = hp->n;
|
} else {
|
||||||
goto hop;
|
xwrite(outputFd, "\007", 1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* This is where we rewrite the line
|
/* Rewrite the line with the selected history item */
|
||||||
* using the selected history item */
|
rewrite_line:
|
||||||
hop:
|
/* erase old command from command line */
|
||||||
len = strlen(parsenextc);
|
len = strlen(command)-strlen(hp->s);
|
||||||
|
while (len>0)
|
||||||
|
input_backspace(command, outputFd, &cursor, &len);
|
||||||
|
input_home(outputFd, &cursor);
|
||||||
|
|
||||||
/* return to begining of line */
|
|
||||||
for (; cursor > 0; cursor--)
|
|
||||||
xwrite(outputFd, "\b", 1);
|
|
||||||
|
|
||||||
/* erase old command */
|
|
||||||
for (j = 0; j < len; j++)
|
|
||||||
xwrite(outputFd, " ", 1);
|
|
||||||
|
|
||||||
/* return to begining of line */
|
|
||||||
for (j = len; j > 0; j--)
|
|
||||||
xwrite(outputFd, "\b", 1);
|
|
||||||
|
|
||||||
memset(parsenextc, 0, BUFSIZ);
|
|
||||||
len = strlen(parsenextc);
|
|
||||||
/* write new command */
|
/* write new command */
|
||||||
strcpy(parsenextc, hp->s);
|
strcpy(command, hp->s);
|
||||||
len = strlen(hp->s);
|
len = strlen(hp->s);
|
||||||
xwrite(outputFd, parsenextc, len);
|
xwrite(outputFd, command, len);
|
||||||
cursor = len;
|
cursor = len;
|
||||||
break;
|
break;
|
||||||
case 'C':
|
case 'C':
|
||||||
/* Right Arrow -- Move forward one character */
|
/* Right Arrow -- Move forward one character */
|
||||||
if (cursor < len) {
|
input_forward(outputFd, &cursor, len);
|
||||||
xwrite(outputFd, "\033[C", 3);
|
|
||||||
cursor++;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'D':
|
case 'D':
|
||||||
/* Left Arrow -- Move back one character */
|
/* Left Arrow -- Move back one character */
|
||||||
if (cursor > 0) {
|
input_backward(outputFd, &cursor);
|
||||||
xwrite(outputFd, "\033[D", 3);
|
|
||||||
cursor--;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case '3':
|
case '3':
|
||||||
/* Delete */
|
/* Delete */
|
||||||
if (cursor != len) {
|
input_delete(command, outputFd, cursor, &len);
|
||||||
input_delete(outputFd, cursor);
|
|
||||||
len--;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//case '5': case '6': /* pgup/pgdown */
|
|
||||||
|
|
||||||
case '7':
|
|
||||||
/* rxvt home */
|
|
||||||
case '1':
|
case '1':
|
||||||
/* Home (Ctrl-A) */
|
/* Home (Ctrl-A) */
|
||||||
input_home(outputFd, &cursor);
|
input_home(outputFd, &cursor);
|
||||||
break;
|
break;
|
||||||
case '8':
|
|
||||||
/* rxvt END */
|
|
||||||
case '4':
|
case '4':
|
||||||
/* End (Ctrl-E) */
|
/* End (Ctrl-E) */
|
||||||
input_end(outputFd, &cursor, len);
|
input_end(outputFd, &cursor, len);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
xwrite(outputFd, "\007", 1);
|
||||||
}
|
}
|
||||||
if (c == '1' || c == '3' || c == '4')
|
if (c == '1' || c == '3' || c == '4')
|
||||||
if ((ret = read(inputFd, &c, 1)) < 1)
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
||||||
return ret; /* read 126 (~) */
|
return; /* read 126 (~) */
|
||||||
}
|
}
|
||||||
if (c == 'O') {
|
if (c == 'O') {
|
||||||
/* 79 */
|
/* 79 */
|
||||||
if ((ret = read(inputFd, &c, 1)) < 1)
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
||||||
return ret;
|
return;
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'H':
|
case 'H':
|
||||||
/* Home (xterm) */
|
/* Home (xterm) */
|
||||||
@ -562,6 +592,8 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
/* End (xterm) */
|
/* End (xterm) */
|
||||||
input_end(outputFd, &cursor, len);
|
input_end(outputFd, &cursor, len);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
xwrite(outputFd, "\007", 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c = 0;
|
c = 0;
|
||||||
@ -570,8 +602,9 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
|
|
||||||
default: /* If it's regular input, do the normal thing */
|
default: /* If it's regular input, do the normal thing */
|
||||||
|
|
||||||
if (!isprint(c)) /* Skip non-printable characters */
|
if (!isprint(c)) { /* Skip non-printable characters */
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
|
if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
|
||||||
break;
|
break;
|
||||||
@ -579,15 +612,15 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
len++;
|
len++;
|
||||||
|
|
||||||
if (cursor == (len - 1)) { /* Append if at the end of the line */
|
if (cursor == (len - 1)) { /* Append if at the end of the line */
|
||||||
*(parsenextc + cursor) = c;
|
*(command + cursor) = c;
|
||||||
} else { /* Insert otherwise */
|
} else { /* Insert otherwise */
|
||||||
memmove(parsenextc + cursor + 1, parsenextc + cursor,
|
memmove(command + cursor + 1, command + cursor,
|
||||||
len - cursor - 1);
|
len - cursor - 1);
|
||||||
|
|
||||||
*(parsenextc + cursor) = c;
|
*(command + cursor) = c;
|
||||||
|
|
||||||
for (j = cursor; j < len; j++)
|
for (j = cursor; j < len; j++)
|
||||||
xwrite(outputFd, parsenextc + j, 1);
|
xwrite(outputFd, command + j, 1);
|
||||||
for (; j > cursor; j--)
|
for (; j > cursor; j--)
|
||||||
xwrite(outputFd, "\033[D", 3);
|
xwrite(outputFd, "\033[D", 3);
|
||||||
}
|
}
|
||||||
@ -606,12 +639,12 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
}
|
}
|
||||||
|
|
||||||
nr = len + 1;
|
nr = len + 1;
|
||||||
xioctl(inputFd, TCSETA, (void *) &old_term);
|
ioctl(inputFd, TCSETA, (void *) &old_term);
|
||||||
reset_term = 0;
|
reset_term = 0;
|
||||||
|
|
||||||
|
|
||||||
/* Handle command history log */
|
/* Handle command history log */
|
||||||
if (*(parsenextc)) {
|
if (*(command)) {
|
||||||
|
|
||||||
struct history *h = his_end;
|
struct history *h = his_end;
|
||||||
|
|
||||||
@ -621,7 +654,7 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
h->n = malloc(sizeof(struct history));
|
h->n = malloc(sizeof(struct history));
|
||||||
|
|
||||||
h->p = NULL;
|
h->p = NULL;
|
||||||
h->s = strdup(parsenextc);
|
h->s = strdup(command);
|
||||||
h->n->p = h;
|
h->n->p = h;
|
||||||
h->n->n = NULL;
|
h->n->n = NULL;
|
||||||
h->n->s = NULL;
|
h->n->s = NULL;
|
||||||
@ -634,7 +667,7 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
h->n->p = h;
|
h->n->p = h;
|
||||||
h->n->n = NULL;
|
h->n->n = NULL;
|
||||||
h->n->s = NULL;
|
h->n->s = NULL;
|
||||||
h->s = strdup(parsenextc);
|
h->s = strdup(command);
|
||||||
his_end = h->n;
|
his_end = h->n;
|
||||||
|
|
||||||
/* After max history, remove the oldest command */
|
/* After max history, remove the oldest command */
|
||||||
@ -652,14 +685,14 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nr;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void cmdedit_init(void)
|
extern void cmdedit_init(void)
|
||||||
{
|
{
|
||||||
atexit(cmdedit_reset_term);
|
atexit(cmdedit_reset_term);
|
||||||
signal(SIGINT, prepareToDie);
|
signal(SIGINT, clean_up_and_die);
|
||||||
signal(SIGQUIT, prepareToDie);
|
signal(SIGQUIT, clean_up_and_die);
|
||||||
signal(SIGTERM, prepareToDie);
|
signal(SIGTERM, clean_up_and_die);
|
||||||
}
|
}
|
||||||
#endif /* BB_FEATURE_SH_COMMAND_EDITING */
|
#endif /* BB_FEATURE_SH_COMMAND_EDITING */
|
||||||
|
46
cmdedit.h
46
cmdedit.h
@ -1,17 +1,35 @@
|
|||||||
/*
|
#ifndef GETLINE_H
|
||||||
* Termios command line History and Editting for NetBSD sh (ash)
|
#define GETLINE_H
|
||||||
* Copyright (c) 1999
|
|
||||||
* Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
|
/* unix systems can #define POSIX to use termios, otherwise
|
||||||
* Etc: Dave Cinege <dcinege@psychosis.com>
|
* the bsd or sysv interface will be used
|
||||||
* Adjusted for busybox: Erik Andersen <andersee@debian.org>
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd, char command[BUFSIZ]);
|
#ifdef __STDC__
|
||||||
extern void cmdedit_init(void);
|
#include <stddef.h>
|
||||||
|
|
||||||
|
typedef size_t (*cmdedit_strwidth_proc)(char *);
|
||||||
|
|
||||||
|
void cmdedit_read_input(char* promptStr, char* command); /* read a line of input */
|
||||||
|
void cmdedit_setwidth(int); /* specify width of screen */
|
||||||
|
void cmdedit_histadd(char *); /* adds entries to hist */
|
||||||
|
void cmdedit_strwidth(cmdedit_strwidth_proc); /* to bind cmdedit_strlen */
|
||||||
|
|
||||||
|
extern int (*cmdedit_in_hook)(char *);
|
||||||
|
extern int (*cmdedit_out_hook)(char *);
|
||||||
|
extern int (*cmdedit_tab_hook)(char *, int, int *);
|
||||||
|
|
||||||
|
#else /* not __STDC__ */
|
||||||
|
|
||||||
|
void cmdedit_read_input(char* promptStr, char* command);
|
||||||
|
void cmdedit_setwidth();
|
||||||
|
void cmdedit_histadd();
|
||||||
|
void cmdedit_strwidth();
|
||||||
|
|
||||||
|
extern int (*cmdedit_in_hook)();
|
||||||
|
extern int (*cmdedit_out_hook)();
|
||||||
|
extern int (*cmdedit_tab_hook)();
|
||||||
|
|
||||||
|
#endif /* __STDC__ */
|
||||||
|
|
||||||
|
#endif /* GETLINE_H */
|
||||||
|
@ -220,12 +220,6 @@ extern pid_t* findPidByName( char* pidName);
|
|||||||
extern void *xmalloc (size_t size);
|
extern void *xmalloc (size_t size);
|
||||||
extern int find_real_root_device_name(char* name);
|
extern int find_real_root_device_name(char* name);
|
||||||
|
|
||||||
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
|
||||||
#include <stdio.h>
|
|
||||||
extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd, char command[BUFSIZ]);
|
|
||||||
extern void cmdedit_init(void);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined BB_INIT || defined BB_SYSLOGD
|
#if defined BB_INIT || defined BB_SYSLOGD
|
||||||
extern int device_open(char *device, int mode);
|
extern int device_open(char *device, int mode);
|
||||||
#endif
|
#endif
|
||||||
|
39
lash.c
39
lash.c
@ -37,6 +37,9 @@
|
|||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
||||||
|
#include "cmdedit.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
|
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
|
||||||
@ -132,6 +135,16 @@ static const char shell_usage[] =
|
|||||||
static char cwd[1024];
|
static char cwd[1024];
|
||||||
static char *prompt = "# ";
|
static char *prompt = "# ";
|
||||||
|
|
||||||
|
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
||||||
|
void win_changed(int sig)
|
||||||
|
{
|
||||||
|
struct winsize win = { 0, 0 };
|
||||||
|
ioctl(0, TIOCGWINSZ, &win);
|
||||||
|
if (win.ws_col > 0) {
|
||||||
|
cmdedit_setwidth( win.ws_col - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* built-in 'cd <path>' handler */
|
/* built-in 'cd <path>' handler */
|
||||||
@ -398,7 +411,7 @@ static int getCommand(FILE * source, char *command)
|
|||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
promptStr=(char*)malloc(sizeof(char)*(len+1));
|
promptStr=(char*)malloc(sizeof(char)*(len+1));
|
||||||
sprintf(promptStr, "%s %s", cwd, prompt);
|
sprintf(promptStr, "%s %s", cwd, prompt);
|
||||||
cmdedit_read_input(promptStr, fileno(stdin), fileno(stdout), command);
|
cmdedit_read_input(promptStr, command);
|
||||||
free( promptStr);
|
free( promptStr);
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
@ -696,7 +709,6 @@ static int parseCommand(char **commandPtr, struct job *job, int *isBg)
|
|||||||
strcpy(job->text, *commandPtr);
|
strcpy(job->text, *commandPtr);
|
||||||
} else {
|
} else {
|
||||||
/* This leaves any trailing spaces, which is a bit sloppy */
|
/* This leaves any trailing spaces, which is a bit sloppy */
|
||||||
|
|
||||||
count = returnCommand - *commandPtr;
|
count = returnCommand - *commandPtr;
|
||||||
job->text = malloc(count + 1);
|
job->text = malloc(count + 1);
|
||||||
strncpy(job->text, *commandPtr, count);
|
strncpy(job->text, *commandPtr, count);
|
||||||
@ -793,14 +805,12 @@ static int runCommand(struct job newJob, struct jobSet *jobList, int inBg)
|
|||||||
if (inBg) {
|
if (inBg) {
|
||||||
/* we don't wait for background jobs to return -- append it
|
/* we don't wait for background jobs to return -- append it
|
||||||
to the list of backgrounded jobs and leave it alone */
|
to the list of backgrounded jobs and leave it alone */
|
||||||
|
|
||||||
printf("[%d] %d\n", job->jobId,
|
printf("[%d] %d\n", job->jobId,
|
||||||
newJob.progs[newJob.numProgs - 1].pid);
|
newJob.progs[newJob.numProgs - 1].pid);
|
||||||
} else {
|
} else {
|
||||||
jobList->fg = job;
|
jobList->fg = job;
|
||||||
|
|
||||||
/* move the new process group into the foreground */
|
/* move the new process group into the foreground */
|
||||||
|
|
||||||
if (tcsetpgrp(0, newJob.pgrp))
|
if (tcsetpgrp(0, newJob.pgrp))
|
||||||
perror("tcsetpgrp");
|
perror("tcsetpgrp");
|
||||||
}
|
}
|
||||||
@ -938,29 +948,24 @@ int shell_main(int argc, char **argv)
|
|||||||
/* initialize the cwd */
|
/* initialize the cwd */
|
||||||
getcwd(cwd, sizeof(cwd));
|
getcwd(cwd, sizeof(cwd));
|
||||||
|
|
||||||
|
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
||||||
|
signal(SIGWINCH, win_changed);
|
||||||
|
win_changed(0);
|
||||||
|
#endif
|
||||||
|
|
||||||
//if (argv[0] && argv[0][0] == '-') {
|
//if (argv[0] && argv[0][0] == '-') {
|
||||||
// shell_source("/etc/profile");
|
// shell_source("/etc/profile");
|
||||||
//}
|
//}
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER,
|
fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER, BB_BT);
|
||||||
BB_BT);
|
fprintf(stdout, "Enter 'help' for a list of built-in commands.\n\n");
|
||||||
fprintf(stdout,
|
|
||||||
"Enter 'help' for a list of built-in commands.\n\n");
|
|
||||||
} else {
|
} else {
|
||||||
input = fopen(argv[1], "r");
|
input = fopen(argv[1], "r");
|
||||||
if (!input)
|
if (!input) {
|
||||||
fatalError("A: Couldn't open file '%s': %s\n", argv[1],
|
fatalError("A: Couldn't open file '%s': %s\n", argv[1],
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
// else
|
}
|
||||||
// fatalError("Got it.\n");
|
|
||||||
//exit(shell_source(argv[1]));
|
|
||||||
|
|
||||||
/* Set terminal IO to canonical mode, and save old term settings. */
|
|
||||||
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
|
||||||
cmdedit_init();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (busy_loop(input));
|
return (busy_loop(input));
|
||||||
|
39
sh.c
39
sh.c
@ -37,6 +37,9 @@
|
|||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
||||||
|
#include "cmdedit.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
|
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
|
||||||
@ -132,6 +135,16 @@ static const char shell_usage[] =
|
|||||||
static char cwd[1024];
|
static char cwd[1024];
|
||||||
static char *prompt = "# ";
|
static char *prompt = "# ";
|
||||||
|
|
||||||
|
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
||||||
|
void win_changed(int sig)
|
||||||
|
{
|
||||||
|
struct winsize win = { 0, 0 };
|
||||||
|
ioctl(0, TIOCGWINSZ, &win);
|
||||||
|
if (win.ws_col > 0) {
|
||||||
|
cmdedit_setwidth( win.ws_col - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* built-in 'cd <path>' handler */
|
/* built-in 'cd <path>' handler */
|
||||||
@ -398,7 +411,7 @@ static int getCommand(FILE * source, char *command)
|
|||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
promptStr=(char*)malloc(sizeof(char)*(len+1));
|
promptStr=(char*)malloc(sizeof(char)*(len+1));
|
||||||
sprintf(promptStr, "%s %s", cwd, prompt);
|
sprintf(promptStr, "%s %s", cwd, prompt);
|
||||||
cmdedit_read_input(promptStr, fileno(stdin), fileno(stdout), command);
|
cmdedit_read_input(promptStr, command);
|
||||||
free( promptStr);
|
free( promptStr);
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
@ -696,7 +709,6 @@ static int parseCommand(char **commandPtr, struct job *job, int *isBg)
|
|||||||
strcpy(job->text, *commandPtr);
|
strcpy(job->text, *commandPtr);
|
||||||
} else {
|
} else {
|
||||||
/* This leaves any trailing spaces, which is a bit sloppy */
|
/* This leaves any trailing spaces, which is a bit sloppy */
|
||||||
|
|
||||||
count = returnCommand - *commandPtr;
|
count = returnCommand - *commandPtr;
|
||||||
job->text = malloc(count + 1);
|
job->text = malloc(count + 1);
|
||||||
strncpy(job->text, *commandPtr, count);
|
strncpy(job->text, *commandPtr, count);
|
||||||
@ -793,14 +805,12 @@ static int runCommand(struct job newJob, struct jobSet *jobList, int inBg)
|
|||||||
if (inBg) {
|
if (inBg) {
|
||||||
/* we don't wait for background jobs to return -- append it
|
/* we don't wait for background jobs to return -- append it
|
||||||
to the list of backgrounded jobs and leave it alone */
|
to the list of backgrounded jobs and leave it alone */
|
||||||
|
|
||||||
printf("[%d] %d\n", job->jobId,
|
printf("[%d] %d\n", job->jobId,
|
||||||
newJob.progs[newJob.numProgs - 1].pid);
|
newJob.progs[newJob.numProgs - 1].pid);
|
||||||
} else {
|
} else {
|
||||||
jobList->fg = job;
|
jobList->fg = job;
|
||||||
|
|
||||||
/* move the new process group into the foreground */
|
/* move the new process group into the foreground */
|
||||||
|
|
||||||
if (tcsetpgrp(0, newJob.pgrp))
|
if (tcsetpgrp(0, newJob.pgrp))
|
||||||
perror("tcsetpgrp");
|
perror("tcsetpgrp");
|
||||||
}
|
}
|
||||||
@ -938,29 +948,24 @@ int shell_main(int argc, char **argv)
|
|||||||
/* initialize the cwd */
|
/* initialize the cwd */
|
||||||
getcwd(cwd, sizeof(cwd));
|
getcwd(cwd, sizeof(cwd));
|
||||||
|
|
||||||
|
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
||||||
|
signal(SIGWINCH, win_changed);
|
||||||
|
win_changed(0);
|
||||||
|
#endif
|
||||||
|
|
||||||
//if (argv[0] && argv[0][0] == '-') {
|
//if (argv[0] && argv[0][0] == '-') {
|
||||||
// shell_source("/etc/profile");
|
// shell_source("/etc/profile");
|
||||||
//}
|
//}
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER,
|
fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER, BB_BT);
|
||||||
BB_BT);
|
fprintf(stdout, "Enter 'help' for a list of built-in commands.\n\n");
|
||||||
fprintf(stdout,
|
|
||||||
"Enter 'help' for a list of built-in commands.\n\n");
|
|
||||||
} else {
|
} else {
|
||||||
input = fopen(argv[1], "r");
|
input = fopen(argv[1], "r");
|
||||||
if (!input)
|
if (!input) {
|
||||||
fatalError("A: Couldn't open file '%s': %s\n", argv[1],
|
fatalError("A: Couldn't open file '%s': %s\n", argv[1],
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
// else
|
}
|
||||||
// fatalError("Got it.\n");
|
|
||||||
//exit(shell_source(argv[1]));
|
|
||||||
|
|
||||||
/* Set terminal IO to canonical mode, and save old term settings. */
|
|
||||||
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
|
||||||
cmdedit_init();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (busy_loop(input));
|
return (busy_loop(input));
|
||||||
|
561
shell/cmdedit.c
561
shell/cmdedit.c
@ -53,9 +53,10 @@ static struct history *his_front = NULL; /* First element in command line list *
|
|||||||
static struct history *his_end = NULL; /* Last element in command line list */
|
static struct history *his_end = NULL; /* Last element in command line list */
|
||||||
static struct termio old_term, new_term; /* Current termio and the previous termio before starting ash */
|
static struct termio old_term, new_term; /* Current termio and the previous termio before starting ash */
|
||||||
|
|
||||||
|
static int cmdedit_termw = 80; /* actual terminal width */
|
||||||
|
static int cmdedit_scroll = 27; /* width of EOL scrolling region */
|
||||||
static int history_counter = 0; /* Number of commands in history list */
|
static int history_counter = 0; /* Number of commands in history list */
|
||||||
static int reset_term = 0; /* Set to true if the terminal needs to be reset upon exit */
|
static int reset_term = 0; /* Set to true if the terminal needs to be reset upon exit */
|
||||||
char *parsenextc; /* copy of parsefile->nextc */
|
|
||||||
|
|
||||||
struct history {
|
struct history {
|
||||||
char *s;
|
char *s;
|
||||||
@ -63,85 +64,42 @@ struct history {
|
|||||||
struct history *n;
|
struct history *n;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define xwrite write
|
||||||
|
|
||||||
/* Version of write which resumes after a signal is caught. */
|
void
|
||||||
int xwrite(int fd, char *buf, int nbytes)
|
cmdedit_setwidth(int w)
|
||||||
{
|
{
|
||||||
int ntry;
|
if (w > 20) {
|
||||||
int i;
|
cmdedit_termw = w;
|
||||||
int n;
|
cmdedit_scroll = w / 3;
|
||||||
|
} else {
|
||||||
n = nbytes;
|
errorMsg("\n*** Error: minimum screen width is 21\n");
|
||||||
ntry = 0;
|
|
||||||
for (;;) {
|
|
||||||
i = write(fd, buf, n);
|
|
||||||
if (i > 0) {
|
|
||||||
if ((n -= i) <= 0)
|
|
||||||
return nbytes;
|
|
||||||
buf += i;
|
|
||||||
ntry = 0;
|
|
||||||
} else if (i == 0) {
|
|
||||||
if (++ntry > 10)
|
|
||||||
return nbytes - n;
|
|
||||||
} else if (errno != EINTR) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Version of ioctl that retries after a signal is caught. */
|
|
||||||
int xioctl(int fd, unsigned long request, char *arg)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void cmdedit_reset_term(void)
|
void cmdedit_reset_term(void)
|
||||||
{
|
{
|
||||||
if (reset_term)
|
if (reset_term)
|
||||||
xioctl(fileno(stdin), TCSETA, (void *) &old_term);
|
ioctl(fileno(stdin), TCSETA, (void *) &old_term);
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepareToDie(int sig)
|
void clean_up_and_die(int sig)
|
||||||
{
|
{
|
||||||
cmdedit_reset_term();
|
cmdedit_reset_term();
|
||||||
fprintf(stdout, "\n");
|
fprintf(stdout, "\n");
|
||||||
exit(TRUE);
|
exit(TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Go to HOME position */
|
||||||
void input_home(int outputFd, int *cursor)
|
void input_home(int outputFd, int *cursor)
|
||||||
{ /* Command line input routines */
|
{
|
||||||
while (*cursor > 0) {
|
while (*cursor > 0) {
|
||||||
xwrite(outputFd, "\b", 1);
|
xwrite(outputFd, "\b", 1);
|
||||||
--*cursor;
|
--*cursor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Go to END position */
|
||||||
void input_delete(int outputFd, int cursor)
|
|
||||||
{
|
|
||||||
int j = 0;
|
|
||||||
|
|
||||||
memmove(parsenextc + cursor, parsenextc + cursor + 1,
|
|
||||||
BUFSIZ - cursor - 1);
|
|
||||||
for (j = cursor; j < (BUFSIZ - 1); j++) {
|
|
||||||
if (!*(parsenextc + j))
|
|
||||||
break;
|
|
||||||
else
|
|
||||||
xwrite(outputFd, (parsenextc + j), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
xwrite(outputFd, " \b", 2);
|
|
||||||
|
|
||||||
while (j-- > cursor)
|
|
||||||
xwrite(outputFd, "\b", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void input_end(int outputFd, int *cursor, int len)
|
void input_end(int outputFd, int *cursor, int len)
|
||||||
{
|
{
|
||||||
while (*cursor < len) {
|
while (*cursor < len) {
|
||||||
@ -150,22 +108,22 @@ void input_end(int outputFd, int *cursor, int len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Delete the char in back of the cursor */
|
||||||
void input_backspace(int outputFd, int *cursor, int *len)
|
void input_backspace(char* command, int outputFd, int *cursor, int *len)
|
||||||
{
|
{
|
||||||
int j = 0;
|
int j = 0;
|
||||||
|
|
||||||
if (*cursor > 0) {
|
if (*cursor > 0) {
|
||||||
xwrite(outputFd, "\b \b", 3);
|
xwrite(outputFd, "\b \b", 3);
|
||||||
--*cursor;
|
--*cursor;
|
||||||
memmove(parsenextc + *cursor, parsenextc + *cursor + 1,
|
memmove(command + *cursor, command + *cursor + 1,
|
||||||
BUFSIZ - *cursor + 1);
|
BUFSIZ - *cursor + 1);
|
||||||
|
|
||||||
for (j = *cursor; j < (BUFSIZ - 1); j++) {
|
for (j = *cursor; j < (BUFSIZ - 1); j++) {
|
||||||
if (!*(parsenextc + j))
|
if (!*(command + j))
|
||||||
break;
|
break;
|
||||||
else
|
else
|
||||||
xwrite(outputFd, (parsenextc + j), 1);
|
xwrite(outputFd, (command + j), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
xwrite(outputFd, " \b", 2);
|
xwrite(outputFd, " \b", 2);
|
||||||
@ -177,18 +135,61 @@ void input_backspace(int outputFd, int *cursor, int *len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef BB_FEATURE_SH_TAB_COMPLETION
|
/* Delete the char in front of the cursor */
|
||||||
|
void input_delete(char* command, int outputFd, int cursor, int *len)
|
||||||
|
{
|
||||||
|
int j = 0;
|
||||||
|
|
||||||
char** username_completion_matches(char* command, int *num_matches)
|
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)
|
||||||
{
|
{
|
||||||
char **matches = (char **) NULL;
|
char **matches = (char **) NULL;
|
||||||
*num_matches=0;
|
*num_matches=0;
|
||||||
fprintf(stderr, "\nin username_completion_matches\n");
|
fprintf(stderr, "\nin username_tab_completion\n");
|
||||||
return (matches);
|
return (matches);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
char** find_path_executable_n_cwd_matches(char* command, int *num_matches)
|
char** exe_n_cwd_tab_completion(char* command, int *num_matches)
|
||||||
{
|
{
|
||||||
char *dirName;
|
char *dirName;
|
||||||
char **matches = (char **) NULL;
|
char **matches = (char **) NULL;
|
||||||
@ -227,124 +228,9 @@ char** find_path_executable_n_cwd_matches(char* command, int *num_matches)
|
|||||||
|
|
||||||
return (matches);
|
return (matches);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
void input_tab(char* command, int outputFd, int *cursor, int *len)
|
||||||
* 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. :)
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|
||||||
char command[BUFSIZ])
|
|
||||||
{
|
{
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
memset(command, 0, sizeof(command));
|
|
||||||
parsenextc = command;
|
|
||||||
if (!reset_term) {
|
|
||||||
xioctl(inputFd, TCGETA, (void *) &old_term);
|
|
||||||
memcpy(&new_term, &old_term, sizeof(struct termio));
|
|
||||||
|
|
||||||
new_term.c_cc[VMIN] = 1;
|
|
||||||
new_term.c_cc[VTIME] = 0;
|
|
||||||
new_term.c_lflag &= ~ICANON; /* unbuffered input */
|
|
||||||
new_term.c_lflag &= ~ECHO;
|
|
||||||
xioctl(inputFd, TCSETA, (void *) &new_term);
|
|
||||||
reset_term = 1;
|
|
||||||
} else {
|
|
||||||
xioctl(inputFd, TCSETA, (void *) &new_term);
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(parsenextc, 0, BUFSIZ);
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
|
|
||||||
if ((ret = read(inputFd, &c, 1)) < 1)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
fprintf(stderr, "\n\nkey=%d (%c)\n\n", c, c);
|
|
||||||
/* Go to the next line */
|
|
||||||
xwrite(outputFd, "\n", 1);
|
|
||||||
/* Rewrite the prompt */
|
|
||||||
xwrite(outputFd, prompt, strlen(prompt));
|
|
||||||
/* Rewrite the command */
|
|
||||||
xwrite(outputFd, parsenextc, len);
|
|
||||||
|
|
||||||
switch (c) {
|
|
||||||
case 1:
|
|
||||||
/* Control-a -- Beginning of line */
|
|
||||||
input_home(outputFd, &cursor);
|
|
||||||
case 5:
|
|
||||||
/* Control-e -- End of line */
|
|
||||||
input_end(outputFd, &cursor, len);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
/* Control-b -- Move back one character */
|
|
||||||
if (cursor > 0) {
|
|
||||||
xwrite(outputFd, "\033[D", 3);
|
|
||||||
cursor--;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
/* Control-f -- Move forward one character */
|
|
||||||
if (cursor < len) {
|
|
||||||
xwrite(outputFd, "\033[C", 3);
|
|
||||||
cursor++;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
/* Control-d -- Delete one character */
|
|
||||||
if (cursor != len) {
|
|
||||||
input_delete(outputFd, cursor);
|
|
||||||
len--;
|
|
||||||
} else if (len == 0) {
|
|
||||||
prepareToDie(0);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 14:
|
|
||||||
/* Control-n -- Get next command */
|
|
||||||
if (hp && hp->n && hp->n->s) {
|
|
||||||
free(hp->s);
|
|
||||||
hp->s = strdup(parsenextc);
|
|
||||||
hp = hp->n;
|
|
||||||
goto hop;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 16:
|
|
||||||
/* Control-p -- Get previous command */
|
|
||||||
if (hp && hp->p) {
|
|
||||||
free(hp->s);
|
|
||||||
hp->s = strdup(parsenextc);
|
|
||||||
hp = hp->p;
|
|
||||||
goto hop;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case '\t':
|
|
||||||
#ifdef BB_FEATURE_SH_TAB_COMPLETION
|
|
||||||
{
|
|
||||||
/* Do TAB completion */
|
/* Do TAB completion */
|
||||||
static int num_matches=0;
|
static int num_matches=0;
|
||||||
static char **matches = (char **) NULL;
|
static char **matches = (char **) NULL;
|
||||||
@ -363,7 +249,7 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
/* Make a local copy of the string -- up
|
/* Make a local copy of the string -- up
|
||||||
* to the position of the cursor */
|
* to the position of the cursor */
|
||||||
matchBuf = (char *) calloc(BUFSIZ, sizeof(char));
|
matchBuf = (char *) calloc(BUFSIZ, sizeof(char));
|
||||||
strncpy(matchBuf, parsenextc, cursor);
|
strncpy(matchBuf, command, cursor);
|
||||||
tmp=matchBuf;
|
tmp=matchBuf;
|
||||||
|
|
||||||
/* skip past any command seperator tokens */
|
/* skip past any command seperator tokens */
|
||||||
@ -389,12 +275,12 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
|
|
||||||
/* FIXME -- this check is broken! */
|
/* FIXME -- this check is broken! */
|
||||||
if (*tmp == '~' && !strchr(tmp, '/'))
|
if (*tmp == '~' && !strchr(tmp, '/'))
|
||||||
matches = username_completion_matches(tmp, &num_matches);
|
matches = username_tab_completion(tmp, &num_matches);
|
||||||
|
|
||||||
/* Try to match any executable in our path and everything
|
/* Try to match any executable in our path and everything
|
||||||
* in the current working directory that matches. */
|
* in the current working directory that matches. */
|
||||||
if (!matches)
|
if (!matches)
|
||||||
matches = find_path_executable_n_cwd_matches(tmp, &num_matches);
|
matches = exe_n_cwd_tab_completion(tmp, &num_matches);
|
||||||
|
|
||||||
/* Don't leak memory */
|
/* Don't leak memory */
|
||||||
free( matchBuf);
|
free( matchBuf);
|
||||||
@ -402,8 +288,8 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
/* Did we find exactly one match? */
|
/* Did we find exactly one match? */
|
||||||
if (matches && num_matches==1) {
|
if (matches && num_matches==1) {
|
||||||
/* write out the matched command */
|
/* write out the matched command */
|
||||||
strncpy(parsenextc+pos, matches[0]+pos, strlen(matches[0])-pos);
|
strncpy(command+pos, matches[0]+pos, strlen(matches[0])-pos);
|
||||||
len=strlen(parsenextc);
|
len=strlen(command);
|
||||||
cursor=len;
|
cursor=len;
|
||||||
xwrite(outputFd, matches[0]+pos, strlen(matches[0])-pos);
|
xwrite(outputFd, matches[0]+pos, strlen(matches[0])-pos);
|
||||||
break;
|
break;
|
||||||
@ -432,127 +318,271 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
/* Rewrite the prompt */
|
/* Rewrite the prompt */
|
||||||
xwrite(outputFd, prompt, strlen(prompt));
|
xwrite(outputFd, prompt, strlen(prompt));
|
||||||
/* Rewrite the command */
|
/* Rewrite the command */
|
||||||
xwrite(outputFd, parsenextc, len);
|
xwrite(outputFd, command, len);
|
||||||
/* Put the cursor back to where it used to be */
|
/* Put the cursor back to where it used to be */
|
||||||
for (cursor=len; cursor > pos; cursor--)
|
for (cursor=len; cursor > pos; cursor--)
|
||||||
xwrite(outputFd, "\b", 1);
|
xwrite(outputFd, "\b", 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
#else
|
|
||||||
break;
|
|
||||||
#endif
|
#endif
|
||||||
case '\b':
|
|
||||||
case DEL:
|
void get_previous_history(struct history **hp, char* command)
|
||||||
/* Backspace */
|
{
|
||||||
input_backspace(outputFd, &cursor, &len);
|
free((*hp)->s);
|
||||||
break;
|
(*hp)->s = strdup(command);
|
||||||
|
*hp = (*hp)->p;
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_next_history(struct history **hp, char* command)
|
||||||
|
{
|
||||||
|
free((*hp)->s);
|
||||||
|
(*hp)->s = strdup(command);
|
||||||
|
*hp = (*hp)->n;
|
||||||
|
|
||||||
|
cmdedit_redraw( NULL, hp->s, -2, -2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
/* prompt : if !=NULL, print the prompt
|
||||||
|
* command: the command line to be displayed
|
||||||
|
* where : where to display changes from.
|
||||||
|
* -1 for no change, -2 for new line
|
||||||
|
* cursor : desired location for the cursor.
|
||||||
|
* -1 for Beginning of line.
|
||||||
|
* -2 for End of Line,
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
cmdedit_redraw(char* prompt, char* command, int where, int cursor)
|
||||||
|
{
|
||||||
|
static char* last_command;
|
||||||
|
int cmdedit_width;
|
||||||
|
|
||||||
|
if (where == -2) {
|
||||||
|
/* Rewrite the prompt and clean up static variables */
|
||||||
|
xwrite(outputFd, "\n", 1);
|
||||||
|
if (prompt) {
|
||||||
|
strcpy(last_command, prompt);
|
||||||
|
xwrite(outputFd, prompt, strlen(prompt));
|
||||||
|
} else {
|
||||||
|
last_command[0] = '\0';
|
||||||
|
xwrite(outputFd, "# ", 2);
|
||||||
|
}
|
||||||
|
cmdedit_width = cmdedit_termw - cmdedit_strlen(prompt);
|
||||||
|
} else if (strcmp(command, last_command) != 0) {
|
||||||
|
strcpy(last_command, prompt);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* erase old command from command line */
|
||||||
|
len = strlen(command)-strlen(last_command);
|
||||||
|
while (len>0)
|
||||||
|
input_backspace(command, outputFd, &cursor, &len);
|
||||||
|
input_home(outputFd, &cursor);
|
||||||
|
|
||||||
|
/* Rewrite the command */
|
||||||
|
xwrite(outputFd, command+where, len);
|
||||||
|
|
||||||
|
/* Put the where it is supposed to be */
|
||||||
|
for (cursor=len; cursor > where; cursor--)
|
||||||
|
xwrite(outputFd, "\b", 1);
|
||||||
|
|
||||||
|
/* write new command */
|
||||||
|
strcpy(command, hp->s);
|
||||||
|
len = strlen(hp->s);
|
||||||
|
xwrite(outputFd, command+where, len);
|
||||||
|
cursor = len;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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. :)
|
||||||
|
*/
|
||||||
|
extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
|
||||||
|
{
|
||||||
|
|
||||||
|
int inputFd=fileno(stdin);
|
||||||
|
int outputFd=fileno(stdout);
|
||||||
|
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;
|
||||||
|
|
||||||
|
memset(command, 0, sizeof(command));
|
||||||
|
if (!reset_term) {
|
||||||
|
ioctl(inputFd, TCGETA, (void *) &old_term);
|
||||||
|
memcpy(&new_term, &old_term, sizeof(struct termio));
|
||||||
|
|
||||||
|
new_term.c_cc[VMIN] = 1;
|
||||||
|
new_term.c_cc[VTIME] = 0;
|
||||||
|
new_term.c_lflag &= ~ICANON; /* unbuffered input */
|
||||||
|
new_term.c_lflag &= ~ECHO;
|
||||||
|
reset_term = 1;
|
||||||
|
ioctl(inputFd, TCSETA, (void *) &new_term);
|
||||||
|
} else {
|
||||||
|
ioctl(inputFd, TCSETA, (void *) &new_term);
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(command, 0, BUFSIZ);
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
|
||||||
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
switch (c) {
|
||||||
case '\n':
|
case '\n':
|
||||||
|
case '\r':
|
||||||
/* Enter */
|
/* Enter */
|
||||||
*(parsenextc + len++ + 1) = c;
|
*(command + len++ + 1) = c;
|
||||||
xwrite(outputFd, &c, 1);
|
xwrite(outputFd, &c, 1);
|
||||||
break_out = 1;
|
break_out = 1;
|
||||||
break;
|
break;
|
||||||
|
case 1:
|
||||||
|
/* Control-a -- Beginning of line */
|
||||||
|
input_home(outputFd, &cursor);
|
||||||
|
case 2:
|
||||||
|
/* Control-b -- Move back one character */
|
||||||
|
input_backward(outputFd, &cursor);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
/* Control-e -- End of line */
|
||||||
|
input_end(outputFd, &cursor, len);
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
/* Control-f -- Move forward one character */
|
||||||
|
input_forward(outputFd, &cursor, len);
|
||||||
|
break;
|
||||||
|
case '\b':
|
||||||
|
case DEL:
|
||||||
|
/* control-h and DEL */
|
||||||
|
input_backspace(command, outputFd, &cursor, &len);
|
||||||
|
break;
|
||||||
|
case '\t':
|
||||||
|
#ifdef BB_FEATURE_SH_TAB_COMPLETION
|
||||||
|
input_tab(command, outputFd, &cursor, &len);
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
case 14:
|
||||||
|
/* Control-n -- Get next command in history */
|
||||||
|
if (hp && hp->n && hp->n->s) {
|
||||||
|
get_next_history(&hp, command);
|
||||||
|
goto rewrite_line;
|
||||||
|
} else {
|
||||||
|
xwrite(outputFd, "\007", 1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 16:
|
||||||
|
/* Control-p -- Get previous command from history */
|
||||||
|
if (hp && hp->p) {
|
||||||
|
get_previous_history(&hp, command);
|
||||||
|
goto rewrite_line;
|
||||||
|
} else {
|
||||||
|
xwrite(outputFd, "\007", 1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case ESC:{
|
case ESC:{
|
||||||
/* escape sequence follows */
|
/* escape sequence follows */
|
||||||
if ((ret = read(inputFd, &c, 1)) < 1)
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
||||||
return ret;
|
return;
|
||||||
|
|
||||||
if (c == '[') { /* 91 */
|
if (c == '[') { /* 91 */
|
||||||
if ((ret = read(inputFd, &c, 1)) < 1)
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
||||||
return ret;
|
return;
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'A':
|
case 'A':
|
||||||
/* Up Arrow -- Get previous command */
|
/* Up Arrow -- Get previous command from history */
|
||||||
if (hp && hp->p) {
|
if (hp && hp->p) {
|
||||||
free(hp->s);
|
get_previous_history(&hp, command);
|
||||||
hp->s = strdup(parsenextc);
|
goto rewrite_line;
|
||||||
hp = hp->p;
|
} else {
|
||||||
goto hop;
|
xwrite(outputFd, "\007", 1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'B':
|
case 'B':
|
||||||
/* Down Arrow -- Get next command */
|
/* Down Arrow -- Get next command in history */
|
||||||
if (hp && hp->n && hp->n->s) {
|
if (hp && hp->n && hp->n->s) {
|
||||||
free(hp->s);
|
get_next_history(&hp, command);
|
||||||
hp->s = strdup(parsenextc);
|
goto rewrite_line;
|
||||||
hp = hp->n;
|
} else {
|
||||||
goto hop;
|
xwrite(outputFd, "\007", 1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* This is where we rewrite the line
|
/* Rewrite the line with the selected history item */
|
||||||
* using the selected history item */
|
rewrite_line:
|
||||||
hop:
|
/* erase old command from command line */
|
||||||
len = strlen(parsenextc);
|
len = strlen(command)-strlen(hp->s);
|
||||||
|
while (len>0)
|
||||||
|
input_backspace(command, outputFd, &cursor, &len);
|
||||||
|
input_home(outputFd, &cursor);
|
||||||
|
|
||||||
/* return to begining of line */
|
|
||||||
for (; cursor > 0; cursor--)
|
|
||||||
xwrite(outputFd, "\b", 1);
|
|
||||||
|
|
||||||
/* erase old command */
|
|
||||||
for (j = 0; j < len; j++)
|
|
||||||
xwrite(outputFd, " ", 1);
|
|
||||||
|
|
||||||
/* return to begining of line */
|
|
||||||
for (j = len; j > 0; j--)
|
|
||||||
xwrite(outputFd, "\b", 1);
|
|
||||||
|
|
||||||
memset(parsenextc, 0, BUFSIZ);
|
|
||||||
len = strlen(parsenextc);
|
|
||||||
/* write new command */
|
/* write new command */
|
||||||
strcpy(parsenextc, hp->s);
|
strcpy(command, hp->s);
|
||||||
len = strlen(hp->s);
|
len = strlen(hp->s);
|
||||||
xwrite(outputFd, parsenextc, len);
|
xwrite(outputFd, command, len);
|
||||||
cursor = len;
|
cursor = len;
|
||||||
break;
|
break;
|
||||||
case 'C':
|
case 'C':
|
||||||
/* Right Arrow -- Move forward one character */
|
/* Right Arrow -- Move forward one character */
|
||||||
if (cursor < len) {
|
input_forward(outputFd, &cursor, len);
|
||||||
xwrite(outputFd, "\033[C", 3);
|
|
||||||
cursor++;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'D':
|
case 'D':
|
||||||
/* Left Arrow -- Move back one character */
|
/* Left Arrow -- Move back one character */
|
||||||
if (cursor > 0) {
|
input_backward(outputFd, &cursor);
|
||||||
xwrite(outputFd, "\033[D", 3);
|
|
||||||
cursor--;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case '3':
|
case '3':
|
||||||
/* Delete */
|
/* Delete */
|
||||||
if (cursor != len) {
|
input_delete(command, outputFd, cursor, &len);
|
||||||
input_delete(outputFd, cursor);
|
|
||||||
len--;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//case '5': case '6': /* pgup/pgdown */
|
|
||||||
|
|
||||||
case '7':
|
|
||||||
/* rxvt home */
|
|
||||||
case '1':
|
case '1':
|
||||||
/* Home (Ctrl-A) */
|
/* Home (Ctrl-A) */
|
||||||
input_home(outputFd, &cursor);
|
input_home(outputFd, &cursor);
|
||||||
break;
|
break;
|
||||||
case '8':
|
|
||||||
/* rxvt END */
|
|
||||||
case '4':
|
case '4':
|
||||||
/* End (Ctrl-E) */
|
/* End (Ctrl-E) */
|
||||||
input_end(outputFd, &cursor, len);
|
input_end(outputFd, &cursor, len);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
xwrite(outputFd, "\007", 1);
|
||||||
}
|
}
|
||||||
if (c == '1' || c == '3' || c == '4')
|
if (c == '1' || c == '3' || c == '4')
|
||||||
if ((ret = read(inputFd, &c, 1)) < 1)
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
||||||
return ret; /* read 126 (~) */
|
return; /* read 126 (~) */
|
||||||
}
|
}
|
||||||
if (c == 'O') {
|
if (c == 'O') {
|
||||||
/* 79 */
|
/* 79 */
|
||||||
if ((ret = read(inputFd, &c, 1)) < 1)
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
||||||
return ret;
|
return;
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'H':
|
case 'H':
|
||||||
/* Home (xterm) */
|
/* Home (xterm) */
|
||||||
@ -562,6 +592,8 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
/* End (xterm) */
|
/* End (xterm) */
|
||||||
input_end(outputFd, &cursor, len);
|
input_end(outputFd, &cursor, len);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
xwrite(outputFd, "\007", 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c = 0;
|
c = 0;
|
||||||
@ -570,8 +602,9 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
|
|
||||||
default: /* If it's regular input, do the normal thing */
|
default: /* If it's regular input, do the normal thing */
|
||||||
|
|
||||||
if (!isprint(c)) /* Skip non-printable characters */
|
if (!isprint(c)) { /* Skip non-printable characters */
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
|
if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
|
||||||
break;
|
break;
|
||||||
@ -579,15 +612,15 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
len++;
|
len++;
|
||||||
|
|
||||||
if (cursor == (len - 1)) { /* Append if at the end of the line */
|
if (cursor == (len - 1)) { /* Append if at the end of the line */
|
||||||
*(parsenextc + cursor) = c;
|
*(command + cursor) = c;
|
||||||
} else { /* Insert otherwise */
|
} else { /* Insert otherwise */
|
||||||
memmove(parsenextc + cursor + 1, parsenextc + cursor,
|
memmove(command + cursor + 1, command + cursor,
|
||||||
len - cursor - 1);
|
len - cursor - 1);
|
||||||
|
|
||||||
*(parsenextc + cursor) = c;
|
*(command + cursor) = c;
|
||||||
|
|
||||||
for (j = cursor; j < len; j++)
|
for (j = cursor; j < len; j++)
|
||||||
xwrite(outputFd, parsenextc + j, 1);
|
xwrite(outputFd, command + j, 1);
|
||||||
for (; j > cursor; j--)
|
for (; j > cursor; j--)
|
||||||
xwrite(outputFd, "\033[D", 3);
|
xwrite(outputFd, "\033[D", 3);
|
||||||
}
|
}
|
||||||
@ -606,12 +639,12 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
}
|
}
|
||||||
|
|
||||||
nr = len + 1;
|
nr = len + 1;
|
||||||
xioctl(inputFd, TCSETA, (void *) &old_term);
|
ioctl(inputFd, TCSETA, (void *) &old_term);
|
||||||
reset_term = 0;
|
reset_term = 0;
|
||||||
|
|
||||||
|
|
||||||
/* Handle command history log */
|
/* Handle command history log */
|
||||||
if (*(parsenextc)) {
|
if (*(command)) {
|
||||||
|
|
||||||
struct history *h = his_end;
|
struct history *h = his_end;
|
||||||
|
|
||||||
@ -621,7 +654,7 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
h->n = malloc(sizeof(struct history));
|
h->n = malloc(sizeof(struct history));
|
||||||
|
|
||||||
h->p = NULL;
|
h->p = NULL;
|
||||||
h->s = strdup(parsenextc);
|
h->s = strdup(command);
|
||||||
h->n->p = h;
|
h->n->p = h;
|
||||||
h->n->n = NULL;
|
h->n->n = NULL;
|
||||||
h->n->s = NULL;
|
h->n->s = NULL;
|
||||||
@ -634,7 +667,7 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
h->n->p = h;
|
h->n->p = h;
|
||||||
h->n->n = NULL;
|
h->n->n = NULL;
|
||||||
h->n->s = NULL;
|
h->n->s = NULL;
|
||||||
h->s = strdup(parsenextc);
|
h->s = strdup(command);
|
||||||
his_end = h->n;
|
his_end = h->n;
|
||||||
|
|
||||||
/* After max history, remove the oldest command */
|
/* After max history, remove the oldest command */
|
||||||
@ -652,14 +685,14 @@ extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nr;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void cmdedit_init(void)
|
extern void cmdedit_init(void)
|
||||||
{
|
{
|
||||||
atexit(cmdedit_reset_term);
|
atexit(cmdedit_reset_term);
|
||||||
signal(SIGINT, prepareToDie);
|
signal(SIGINT, clean_up_and_die);
|
||||||
signal(SIGQUIT, prepareToDie);
|
signal(SIGQUIT, clean_up_and_die);
|
||||||
signal(SIGTERM, prepareToDie);
|
signal(SIGTERM, clean_up_and_die);
|
||||||
}
|
}
|
||||||
#endif /* BB_FEATURE_SH_COMMAND_EDITING */
|
#endif /* BB_FEATURE_SH_COMMAND_EDITING */
|
||||||
|
@ -1,17 +1,35 @@
|
|||||||
/*
|
#ifndef GETLINE_H
|
||||||
* Termios command line History and Editting for NetBSD sh (ash)
|
#define GETLINE_H
|
||||||
* Copyright (c) 1999
|
|
||||||
* Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
|
/* unix systems can #define POSIX to use termios, otherwise
|
||||||
* Etc: Dave Cinege <dcinege@psychosis.com>
|
* the bsd or sysv interface will be used
|
||||||
* Adjusted for busybox: Erik Andersen <andersee@debian.org>
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
extern int cmdedit_read_input(char* prompt, int inputFd, int outputFd, char command[BUFSIZ]);
|
#ifdef __STDC__
|
||||||
extern void cmdedit_init(void);
|
#include <stddef.h>
|
||||||
|
|
||||||
|
typedef size_t (*cmdedit_strwidth_proc)(char *);
|
||||||
|
|
||||||
|
void cmdedit_read_input(char* promptStr, char* command); /* read a line of input */
|
||||||
|
void cmdedit_setwidth(int); /* specify width of screen */
|
||||||
|
void cmdedit_histadd(char *); /* adds entries to hist */
|
||||||
|
void cmdedit_strwidth(cmdedit_strwidth_proc); /* to bind cmdedit_strlen */
|
||||||
|
|
||||||
|
extern int (*cmdedit_in_hook)(char *);
|
||||||
|
extern int (*cmdedit_out_hook)(char *);
|
||||||
|
extern int (*cmdedit_tab_hook)(char *, int, int *);
|
||||||
|
|
||||||
|
#else /* not __STDC__ */
|
||||||
|
|
||||||
|
void cmdedit_read_input(char* promptStr, char* command);
|
||||||
|
void cmdedit_setwidth();
|
||||||
|
void cmdedit_histadd();
|
||||||
|
void cmdedit_strwidth();
|
||||||
|
|
||||||
|
extern int (*cmdedit_in_hook)();
|
||||||
|
extern int (*cmdedit_out_hook)();
|
||||||
|
extern int (*cmdedit_tab_hook)();
|
||||||
|
|
||||||
|
#endif /* __STDC__ */
|
||||||
|
|
||||||
|
#endif /* GETLINE_H */
|
||||||
|
39
shell/lash.c
39
shell/lash.c
@ -37,6 +37,9 @@
|
|||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
||||||
|
#include "cmdedit.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
|
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
|
||||||
@ -132,6 +135,16 @@ static const char shell_usage[] =
|
|||||||
static char cwd[1024];
|
static char cwd[1024];
|
||||||
static char *prompt = "# ";
|
static char *prompt = "# ";
|
||||||
|
|
||||||
|
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
||||||
|
void win_changed(int sig)
|
||||||
|
{
|
||||||
|
struct winsize win = { 0, 0 };
|
||||||
|
ioctl(0, TIOCGWINSZ, &win);
|
||||||
|
if (win.ws_col > 0) {
|
||||||
|
cmdedit_setwidth( win.ws_col - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* built-in 'cd <path>' handler */
|
/* built-in 'cd <path>' handler */
|
||||||
@ -398,7 +411,7 @@ static int getCommand(FILE * source, char *command)
|
|||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
promptStr=(char*)malloc(sizeof(char)*(len+1));
|
promptStr=(char*)malloc(sizeof(char)*(len+1));
|
||||||
sprintf(promptStr, "%s %s", cwd, prompt);
|
sprintf(promptStr, "%s %s", cwd, prompt);
|
||||||
cmdedit_read_input(promptStr, fileno(stdin), fileno(stdout), command);
|
cmdedit_read_input(promptStr, command);
|
||||||
free( promptStr);
|
free( promptStr);
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
@ -696,7 +709,6 @@ static int parseCommand(char **commandPtr, struct job *job, int *isBg)
|
|||||||
strcpy(job->text, *commandPtr);
|
strcpy(job->text, *commandPtr);
|
||||||
} else {
|
} else {
|
||||||
/* This leaves any trailing spaces, which is a bit sloppy */
|
/* This leaves any trailing spaces, which is a bit sloppy */
|
||||||
|
|
||||||
count = returnCommand - *commandPtr;
|
count = returnCommand - *commandPtr;
|
||||||
job->text = malloc(count + 1);
|
job->text = malloc(count + 1);
|
||||||
strncpy(job->text, *commandPtr, count);
|
strncpy(job->text, *commandPtr, count);
|
||||||
@ -793,14 +805,12 @@ static int runCommand(struct job newJob, struct jobSet *jobList, int inBg)
|
|||||||
if (inBg) {
|
if (inBg) {
|
||||||
/* we don't wait for background jobs to return -- append it
|
/* we don't wait for background jobs to return -- append it
|
||||||
to the list of backgrounded jobs and leave it alone */
|
to the list of backgrounded jobs and leave it alone */
|
||||||
|
|
||||||
printf("[%d] %d\n", job->jobId,
|
printf("[%d] %d\n", job->jobId,
|
||||||
newJob.progs[newJob.numProgs - 1].pid);
|
newJob.progs[newJob.numProgs - 1].pid);
|
||||||
} else {
|
} else {
|
||||||
jobList->fg = job;
|
jobList->fg = job;
|
||||||
|
|
||||||
/* move the new process group into the foreground */
|
/* move the new process group into the foreground */
|
||||||
|
|
||||||
if (tcsetpgrp(0, newJob.pgrp))
|
if (tcsetpgrp(0, newJob.pgrp))
|
||||||
perror("tcsetpgrp");
|
perror("tcsetpgrp");
|
||||||
}
|
}
|
||||||
@ -938,29 +948,24 @@ int shell_main(int argc, char **argv)
|
|||||||
/* initialize the cwd */
|
/* initialize the cwd */
|
||||||
getcwd(cwd, sizeof(cwd));
|
getcwd(cwd, sizeof(cwd));
|
||||||
|
|
||||||
|
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
||||||
|
signal(SIGWINCH, win_changed);
|
||||||
|
win_changed(0);
|
||||||
|
#endif
|
||||||
|
|
||||||
//if (argv[0] && argv[0][0] == '-') {
|
//if (argv[0] && argv[0][0] == '-') {
|
||||||
// shell_source("/etc/profile");
|
// shell_source("/etc/profile");
|
||||||
//}
|
//}
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER,
|
fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER, BB_BT);
|
||||||
BB_BT);
|
fprintf(stdout, "Enter 'help' for a list of built-in commands.\n\n");
|
||||||
fprintf(stdout,
|
|
||||||
"Enter 'help' for a list of built-in commands.\n\n");
|
|
||||||
} else {
|
} else {
|
||||||
input = fopen(argv[1], "r");
|
input = fopen(argv[1], "r");
|
||||||
if (!input)
|
if (!input) {
|
||||||
fatalError("A: Couldn't open file '%s': %s\n", argv[1],
|
fatalError("A: Couldn't open file '%s': %s\n", argv[1],
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
// else
|
}
|
||||||
// fatalError("Got it.\n");
|
|
||||||
//exit(shell_source(argv[1]));
|
|
||||||
|
|
||||||
/* Set terminal IO to canonical mode, and save old term settings. */
|
|
||||||
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
|
||||||
cmdedit_init();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (busy_loop(input));
|
return (busy_loop(input));
|
||||||
|
Loading…
Reference in New Issue
Block a user