Update the lash shell (hopefully the last time...) so things like

echo "foo   bar" and echo -n "foo\t\\\\\tbar" work as expected.
Merge prompt printing work from Vladimir.
 -Erik
This commit is contained in:
Eric Andersen 2001-03-13 22:57:56 +00:00
parent 798ab301c7
commit b3d6e2df95
9 changed files with 569 additions and 371 deletions

View File

@ -241,7 +241,14 @@
// //
// Enable command line editing in the shell. // Enable command line editing in the shell.
// Only relevant if BB_SH is enabled. // Only relevant if BB_SH is enabled.
#define BB_FEATURE_SH_COMMAND_EDITING #define BB_FEATURE_COMMAND_EDITING
//
// Enable tab completion in the shell. This is now working quite nicely.
// This feature adds a bit over 4k. Only relevant if BB_SH is enabled.
#define BB_FEATURE_COMMAND_TAB_COMPLETION
//
// Attempts to match usernames in a ~-prefixed path
//#define BB_FEATURE_COMMAND_USERNAME_COMPLETION
// //
//Allow the shell to invoke all the compiled in BusyBox applets as if they //Allow the shell to invoke all the compiled in BusyBox applets as if they
//were shell builtins. Nice for staticly linking an emergency rescue shell, //were shell builtins. Nice for staticly linking an emergency rescue shell,
@ -258,21 +265,11 @@
// Only relevant if BB_SH is enabled. // Only relevant if BB_SH is enabled.
#define BB_FEATURE_SH_APPLETS_ALWAYS_WIN #define BB_FEATURE_SH_APPLETS_ALWAYS_WIN
// //
// Enable tab completion in the shell. This is now working quite nicely.
// This feature adds a bit over 4k. Only relevant if BB_SH is enabled.
//#define BB_FEATURE_SH_TAB_COMPLETION
//
// Enable a simpler shell prompt of the form "path #"
// instead of the default "[username@hostname path]#"
//
// Some deeply embedded systems don't have usernames or even hostnames, // Some deeply embedded systems don't have usernames or even hostnames,
// and the default prompt can look rather hideous on them. Uncomment // and the default prompt can look rather hideous on them. Uncomment
// this option for a simpler, path-only prompt (which was the default until // this option for a simpler, path-only prompt (which was the default until
// around BusyBox-0.48): // around BusyBox-0.48):
#define BB_FEATURE_SH_SIMPLE_PROMPT #define BB_FEATURE_SIMPLE_PROMPT
//
// Attempts to match usernames in a ~-prefixed path
//#define BB_FEATURE_SH_USERNAME_COMPLETION
// //
//Turn on extra fbset options //Turn on extra fbset options
//#define BB_FEATURE_FBSET_FANCY //#define BB_FEATURE_FBSET_FANCY
@ -355,7 +352,7 @@
#undef BB_MTAB #undef BB_MTAB
#endif #endif
// //
#if defined BB_FEATURE_SH_COMMAND_EDITING && defined BB_SH #if defined BB_FEATURE_COMMAND_EDITING && defined BB_SH
#define BB_CMDEDIT #define BB_CMDEDIT
#endif #endif
// //
@ -406,7 +403,7 @@
#define BB_NFSMOUNT #define BB_NFSMOUNT
#endif #endif
// //
#if defined BB_FEATURE_SH_COMMAND_EDITING #if defined BB_FEATURE_COMMAND_EDITING
#ifndef BB_FEATURE_USE_TERMIOS #ifndef BB_FEATURE_USE_TERMIOS
#define BB_FEATURE_USE_TERMIOS #define BB_FEATURE_USE_TERMIOS
#endif #endif

View File

@ -299,6 +299,7 @@ busybox.links: busybox.mkll Config.h applets.h
- $(SHELL) $^ >$@ - $(SHELL) $^ >$@
nfsmount.o cmdedit.o: %.o: %.h nfsmount.o cmdedit.o: %.o: %.h
sh.o: cmdedit.h
$(OBJECTS): %.o: %.c Config.h busybox.h applets.h Makefile $(OBJECTS): %.o: %.c Config.h busybox.h applets.h Makefile
$(CC) -I- $(CFLAGS) -I. $(patsubst %,-I%,$(subst :, ,$(BB_SRC_DIR))) -c $< -o $*.o $(CC) -I- $(CFLAGS) -I. $(patsubst %,-I%,$(subst :, ,$(BB_SRC_DIR))) -c $< -o $*.o

171
cmdedit.c
View File

@ -27,6 +27,7 @@
Small bugs (simple effect): Small bugs (simple effect):
- not true viewing if terminal size (x*y symbols) less - not true viewing if terminal size (x*y symbols) less
size (prompt + editor`s line + 2 symbols) size (prompt + editor`s line + 2 symbols)
- not true viewing if length prompt less terminal width
*/ */
@ -50,29 +51,36 @@
#else #else
#define BB_FEATURE_SH_COMMAND_EDITING #define BB_FEATURE_COMMAND_EDITING
#define BB_FEATURE_SH_TAB_COMPLETION #define BB_FEATURE_COMMAND_TAB_COMPLETION
#define BB_FEATURE_SH_USERNAME_COMPLETION #define BB_FEATURE_COMMAND_USERNAME_COMPLETION
#define BB_FEATURE_NONPRINTABLE_INVERSE_PUT #define BB_FEATURE_NONPRINTABLE_INVERSE_PUT
#define BB_FEATURE_BASH_STYLE_PROMT #undef BB_FEATURE_SIMPLE_PROMPT
#define BB_FEATURE_CLEAN_UP #define BB_FEATURE_CLEAN_UP
#define D(x) x #define D(x) x
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#endif /* TEST */ #endif /* TEST */
#ifdef BB_FEATURE_SH_TAB_COMPLETION #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#endif #endif
#ifdef BB_FEATURE_SH_COMMAND_EDITING #ifdef BB_FEATURE_COMMAND_EDITING
#ifndef BB_FEATURE_SH_TAB_COMPLETION #ifndef BB_FEATURE_COMMAND_TAB_COMPLETION
#undef BB_FEATURE_SH_USERNAME_COMPLETION #undef BB_FEATURE_COMMAND_USERNAME_COMPLETION
#endif #endif
#if defined(BB_FEATURE_SH_USERNAME_COMPLETION) || defined(BB_FEATURE_BASH_STYLE_PROMT) #if defined(BB_FEATURE_COMMAND_USERNAME_COMPLETION) || !defined(BB_FEATURE_SIMPLE_PROMPT)
#define BB_FEATURE_GETUSERNAME_AND_HOMEDIR #define BB_FEATURE_GETUSERNAME_AND_HOMEDIR
#endif #endif
@ -154,26 +162,26 @@ static
volatile int handlers_sets = 0; /* Set next bites: */ volatile int handlers_sets = 0; /* Set next bites: */
enum { enum {
SET_ATEXIT = 1, /* when atexit() has been called and SET_ATEXIT = 1, /* when atexit() has been called
get euid,uid,gid to fast compare */ and get euid,uid,gid to fast compare */
SET_TERM_HANDLERS = 2, /* set many terminates signal handlers */ SET_TERM_HANDLERS = 2, /* set many terminates signal handlers */
SET_WCHG_HANDLERS = 4, /* winchg signal handler */ SET_WCHG_HANDLERS = 4, /* winchg signal handler */
SET_RESET_TERM = 8, /* if the terminal needs to be reset upon exit */ SET_RESET_TERM = 8, /* if the terminal needs to be reset upon exit */
}; };
static int cmdedit_x; /* real x terminal position */ static int cmdedit_x; /* real x terminal position */
static int cmdedit_y; /* pseudoreal y terminal position */ static int cmdedit_y; /* pseudoreal y terminal position */
static int cmdedit_prmt_len; /* lenght prompt without colores string */ static int cmdedit_prmt_len; /* lenght prompt without colores string */
static int cursor; /* required global for signal handler */ static int cursor; /* required global for signal handler */
static int len; /* --- "" - - "" - -"- --""-- --""--- */ static int len; /* --- "" - - "" - -"- --""-- --""--- */
static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */ static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
static static
#ifndef BB_FEATURE_BASH_STYLE_PROMT #ifdef BB_FEATURE_SIMPLE_PROMPT
const const
#endif #endif
char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */ char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
/* Link into lash to reset context to 0 on ^C and such */ /* Link into lash to reset context to 0 on ^C and such */
extern unsigned int shell_context; extern unsigned int shell_context;
@ -185,13 +193,13 @@ static char *home_pwd_buf = "";
static int my_euid; static int my_euid;
#endif #endif
#ifdef BB_FEATURE_BASH_STYLE_PROMT #ifndef BB_FEATURE_SIMPLE_PROMPT
static char *hostname_buf = ""; static char *hostname_buf = "";
static int num_ok_lines = 1; static int num_ok_lines = 1;
#endif #endif
#ifdef BB_FEATURE_SH_TAB_COMPLETION #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR #ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
static int my_euid; static int my_euid;
@ -200,7 +208,7 @@ static int my_euid;
static int my_uid; static int my_uid;
static int my_gid; static int my_gid;
#endif /* BB_FEATURE_SH_TAB_COMPLETION */ #endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
static void cmdedit_setwidth(int w, int redraw_flg); static void cmdedit_setwidth(int w, int redraw_flg);
@ -245,7 +253,6 @@ static void cmdedit_reset_term(void)
if (his_front) { if (his_front) {
struct history *n; struct history *n;
//while(his_front!=his_end) {
while (his_front != his_end) { while (his_front != his_end) {
n = his_front->n; n = his_front->n;
free(his_front->s); free(his_front->s);
@ -355,25 +362,27 @@ static void put_prompt(void)
cursor = 0; cursor = 0;
} }
#ifdef BB_FEATURE_BASH_STYLE_PROMT #ifdef BB_FEATURE_SIMPLE_PROMPT
static void static void parse_prompt(const char *prmt_ptr)
add_to_prompt(char **prmt_mem_ptr, int *alm, int *prmt_len,
const char *addb)
{ {
int l = strlen(addb); cmdedit_prompt = prmt_ptr;
cmdedit_prmt_len = strlen(prmt_ptr);
*prmt_len += l; put_prompt();
}
#else
static void add_to_prompt(char **prmt_mem_ptr, int *alm,
int *prmt_len, const char *addb)
{
*prmt_len += strlen(addb);
if (*alm < (*prmt_len) + 1) { if (*alm < (*prmt_len) + 1) {
*alm = (*prmt_len) + 1; *alm = (*prmt_len) + 1;
*prmt_mem_ptr = xrealloc(*prmt_mem_ptr, *alm); *prmt_mem_ptr = xrealloc(*prmt_mem_ptr, *alm);
} }
strcat(*prmt_mem_ptr, addb); strcat(*prmt_mem_ptr, addb);
} }
#endif
static void parse_prompt(const char *prmt_ptr) static void parse_prompt(const char *prmt_ptr)
{ {
#ifdef BB_FEATURE_BASH_STYLE_PROMT
int alm = strlen(prmt_ptr) + 1; /* supposedly require memory */ int alm = strlen(prmt_ptr) + 1; /* supposedly require memory */
int prmt_len = 0; int prmt_len = 0;
int sub_len = 0; int sub_len = 0;
@ -394,9 +403,11 @@ static void parse_prompt(const char *prmt_ptr)
break; break;
prmt_ptr++; prmt_ptr++;
switch (c) { switch (c) {
#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
case 'u': case 'u':
add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, user_buf); add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, user_buf);
continue; continue;
#endif
case 'h': case 'h':
if (hostname_buf[0] == 0) { if (hostname_buf[0] == 0) {
hostname_buf = xcalloc(256, 1); hostname_buf = xcalloc(256, 1);
@ -415,6 +426,7 @@ static void parse_prompt(const char *prmt_ptr)
case '$': case '$':
c = my_euid == 0 ? '#' : '$'; c = my_euid == 0 ? '#' : '$';
break; break;
#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
case 'w': case 'w':
if (pwd_buf[0] == 0) { if (pwd_buf[0] == 0) {
int l; int l;
@ -429,6 +441,7 @@ static void parse_prompt(const char *prmt_ptr)
} }
add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf); add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf);
continue; continue;
#endif
case '!': case '!':
snprintf(buf, sizeof(buf), "%d", num_ok_lines); snprintf(buf, sizeof(buf), "%d", num_ok_lines);
add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf); add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf);
@ -485,20 +498,21 @@ static void parse_prompt(const char *prmt_ptr)
if (flg_not_length == ']') if (flg_not_length == ']')
sub_len++; sub_len++;
} }
#if 0
cmdedit_prmt_len = prmt_len - sub_len; cmdedit_prmt_len = prmt_len - sub_len;
cmdedit_prompt = prmt_mem_ptr;
#else
cmdedit_prompt = prmt_ptr; cmdedit_prompt = prmt_ptr;
cmdedit_prmt_len = strlen(prmt_ptr);
#endif #endif
cmdedit_prompt = prmt_mem_ptr;
cmdedit_prmt_len = strlen(cmdedit_prompt);
put_prompt(); put_prompt();
} }
#endif
/* draw promt, editor line, and clear tail */ /* draw promt, editor line, and clear tail */
static void redraw(int y, int back_cursor) static void redraw(int y, int back_cursor)
{ {
if (y > 0) /* up to start y */ if (y > 0) /* up to start y */
printf("\033[%dA", y); printf("\033[%dA", y);
cmdedit_y = 0; /* new quasireal y */ cmdedit_y = 0; /* new quasireal y */
putchar('\r'); putchar('\r');
@ -518,7 +532,7 @@ static void input_delete(void)
strcpy(command_ps + j, command_ps + j + 1); strcpy(command_ps + j, command_ps + j + 1);
len--; len--;
input_end(); /* rewtite new line */ input_end(); /* rewtite new line */
cmdedit_set_out_char(0); /* destroy end char */ cmdedit_set_out_char(0); /* destroy end char */
input_backward(cursor - j); /* back to old pos cursor */ input_backward(cursor - j); /* back to old pos cursor */
} }
@ -545,7 +559,7 @@ static void clean_up_and_die(int sig)
{ {
goto_new_line(); goto_new_line();
if (sig != SIGINT) if (sig != SIGINT)
exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */ exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */
cmdedit_reset_term(); cmdedit_reset_term();
} }
@ -556,7 +570,6 @@ static void cmdedit_setwidth(int w, int redraw_flg)
cmdedit_termw = cmdedit_termw % w; cmdedit_termw = cmdedit_termw % w;
} }
if (w > cmdedit_termw) { if (w > cmdedit_termw) {
cmdedit_termw = w; cmdedit_termw = w;
if (redraw_flg) { if (redraw_flg) {
@ -590,14 +603,14 @@ extern void cmdedit_init(void)
} }
#endif #endif
#ifdef BB_FEATURE_SH_TAB_COMPLETION #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR #ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
my_euid = geteuid(); my_euid = geteuid();
#endif #endif
my_uid = getuid(); my_uid = getuid();
my_gid = getgid(); my_gid = getgid();
#endif /* BB_FEATURE_SH_TAB_COMPLETION */ #endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
handlers_sets |= SET_ATEXIT; handlers_sets |= SET_ATEXIT;
atexit(cmdedit_reset_term); /* be sure to do this only once */ atexit(cmdedit_reset_term); /* be sure to do this only once */
} }
@ -612,7 +625,7 @@ extern void cmdedit_init(void)
} }
#ifdef BB_FEATURE_SH_TAB_COMPLETION #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
static int is_execute(const struct stat *st) static int is_execute(const struct stat *st)
{ {
@ -623,7 +636,7 @@ static int is_execute(const struct stat *st)
return FALSE; return FALSE;
} }
#ifdef BB_FEATURE_SH_USERNAME_COMPLETION #ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
static char **username_tab_completion(char *ud, int *num_matches) static char **username_tab_completion(char *ud, int *num_matches)
{ {
@ -632,19 +645,19 @@ static char **username_tab_completion(char *ud, int *num_matches)
char *temp; char *temp;
ud++; /* ~user/... to user/... */ ud++; /* ~user/... to user/... */
userlen = strlen(ud); userlen = strlen(ud);
if (num_matches == 0) { /* "~/..." or "~user/..." */ if (num_matches == 0) { /* "~/..." or "~user/..." */
char *sav_ud = ud - 1; char *sav_ud = ud - 1;
char *home = 0; char *home = 0;
if (*ud == '/') { /* "~/..." */ if (*ud == '/') { /* "~/..." */
home = home_pwd_buf; home = home_pwd_buf;
} else { } else {
/* "~user/..." */ /* "~user/..." */
temp = strchr(ud, '/'); temp = strchr(ud, '/');
*temp = 0; /* ~user\0 */ *temp = 0; /* ~user\0 */
entry = getpwnam(ud); entry = getpwnam(ud);
*temp = '/'; /* restore ~user/... */ *temp = '/'; /* restore ~user/... */
ud = temp; ud = temp;
@ -660,7 +673,7 @@ static char **username_tab_completion(char *ud, int *num_matches)
strcpy(sav_ud, temp2); strcpy(sav_ud, temp2);
} }
} }
return 0; /* void, result save to argument :-) */ return 0; /* void, result save to argument :-) */
} else { } else {
/* "~[^/]*" */ /* "~[^/]*" */
char **matches = (char **) NULL; char **matches = (char **) NULL;
@ -685,7 +698,7 @@ static char **username_tab_completion(char *ud, int *num_matches)
return (matches); return (matches);
} }
} }
#endif /* BB_FEATURE_SH_USERNAME_COMPLETION */ #endif /* BB_FEATURE_COMMAND_USERNAME_COMPLETION */
enum { enum {
FIND_EXE_ONLY = 0, FIND_EXE_ONLY = 0,
@ -710,11 +723,11 @@ static int path_parse(char ***p, int flags)
npth = 0; npth = 0;
for (;;) { for (;;) {
npth++; /* count words is + 1 count ':' */ npth++; /* count words is + 1 count ':' */
tmp = strchr(tmp, ':'); tmp = strchr(tmp, ':');
if (tmp) { if (tmp) {
if (*++tmp == 0) if (*++tmp == 0)
break; /* :<empty> */ break; /* :<empty> */
} else } else
break; break;
} }
@ -723,7 +736,7 @@ static int path_parse(char ***p, int flags)
tmp = pth; tmp = pth;
(*p)[0] = xstrdup(tmp); (*p)[0] = xstrdup(tmp);
npth = 1; /* count words is + 1 count ':' */ npth = 1; /* count words is + 1 count ':' */
for (;;) { for (;;) {
tmp = strchr(tmp, ':'); tmp = strchr(tmp, ':');
@ -754,7 +767,7 @@ static char *add_quote_for_spec_chars(char *found)
} }
static char **exe_n_cwd_tab_completion(char *command, int *num_matches, static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
int type) int type)
{ {
char **matches = 0; char **matches = 0;
@ -782,7 +795,7 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
strcpy(dirbuf, command); strcpy(dirbuf, command);
/* set dir only */ /* set dir only */
dirbuf[(pfind - command) + 1] = 0; dirbuf[(pfind - command) + 1] = 0;
#ifdef BB_FEATURE_SH_USERNAME_COMPLETION #ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
if (dirbuf[0] == '~') /* ~/... or ~user/... */ if (dirbuf[0] == '~') /* ~/... or ~user/... */
username_tab_completion(dirbuf, 0); username_tab_completion(dirbuf, 0);
#endif #endif
@ -796,7 +809,7 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
for (i = 0; i < npaths; i++) { for (i = 0; i < npaths; i++) {
dir = opendir(paths[i]); dir = opendir(paths[i]);
if (!dir) /* Don't print an error */ if (!dir) /* Don't print an error */
continue; continue;
while ((next = readdir(dir)) != NULL) { while ((next = readdir(dir)) != NULL) {
@ -987,7 +1000,8 @@ static int find_match(char *matchBuf, int *len_with_quotes)
for (i = 0; int_buf[i]; i++) for (i = 0; int_buf[i]; i++)
if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') { if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
&& strncmp(&matchBuf[pos_buf[0]], "cd", 2) == 0) && matchBuf[pos_buf[0]]=='c'
&& matchBuf[pos_buf[1]]=='d' )
command_mode = FIND_DIR_ONLY; command_mode = FIND_DIR_ONLY;
else { else {
command_mode = FIND_FILE_ONLY; command_mode = FIND_FILE_ONLY;
@ -1065,7 +1079,7 @@ static void input_tab(int *lastWasTab)
/* Free up any memory already allocated */ /* Free up any memory already allocated */
input_tab(0); input_tab(0);
#ifdef BB_FEATURE_SH_USERNAME_COMPLETION #ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
/* If the word starts with `~' and there is no slash in the word, /* If the word starts with `~' and there is no slash in the word,
* then try completing this word as a username. */ * then try completing this word as a username. */
@ -1085,7 +1099,7 @@ static void input_tab(int *lastWasTab)
beep(); beep();
if (!matches) if (!matches)
return; /* not found */ return; /* not found */
/* sort */ /* sort */
qsort(matches, num_matches, sizeof(char *), match_compare); qsort(matches, num_matches, sizeof(char *), match_compare);
@ -1101,7 +1115,7 @@ static void input_tab(int *lastWasTab)
free(tmp); free(tmp);
return; return;
} }
} else { /* one match */ } else { /* one match */
tmp = matches[0]; tmp = matches[0];
/* for next completion current found */ /* for next completion current found */
*lastWasTab = FALSE; *lastWasTab = FALSE;
@ -1164,7 +1178,7 @@ static void input_tab(int *lastWasTab)
} }
} }
} }
#endif /* BB_FEATURE_SH_TAB_COMPLETION */ #endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
static void get_previous_history(struct history **hp, struct history *p) static void get_previous_history(struct history **hp, struct history *p)
{ {
@ -1201,6 +1215,7 @@ enum {
* Furthermore, the "vi" command editing keys are not implemented. * Furthermore, the "vi" command editing keys are not implemented.
* *
*/ */
extern void cmdedit_read_input(char *prompt, char command[BUFSIZ]) extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
{ {
@ -1212,7 +1227,7 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
struct history *hp = his_end; struct history *hp = his_end;
/* prepare before init handlers */ /* prepare before init handlers */
cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */ cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
len = 0; len = 0;
command_ps = command; command_ps = command;
@ -1223,9 +1238,11 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
new_settings.c_cc[VMIN] = 1; new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0; new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */ /* Turn off CTRL-C, so we can trap it */
new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
new_settings.c_lflag &= ~ICANON; /* unbuffered input */ new_settings.c_lflag &= ~ICANON; /* unbuffered input */
new_settings.c_lflag &= ~(ECHO | ECHOCTL | ECHONL); /* Turn off echoing */ /* Turn off echoing */
new_settings.c_lflag &= ~(ECHO | ECHOCTL | ECHONL);
} }
command[0] = 0; command[0] = 0;
@ -1295,7 +1312,7 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
input_backspace(); input_backspace();
break; break;
case '\t': case '\t':
#ifdef BB_FEATURE_SH_TAB_COMPLETION #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
input_tab(&lastWasTab); input_tab(&lastWasTab);
#endif #endif
break; break;
@ -1335,7 +1352,7 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
return; return;
} }
switch (c) { switch (c) {
#ifdef BB_FEATURE_SH_TAB_COMPLETION #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
case '\t': /* Alt-Tab */ case '\t': /* Alt-Tab */
input_tab(&lastWasTab); input_tab(&lastWasTab);
@ -1402,7 +1419,7 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
break; break;
} }
default: /* If it's regular input, do the normal thing */ default: /* If it's regular input, do the normal thing */
#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
/* Control-V -- Add non-printable symbol */ /* Control-V -- Add non-printable symbol */
if (c == 22) { if (c == 22) {
@ -1493,16 +1510,16 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
history_counter++; history_counter++;
} }
} }
#if defined(BB_FEATURE_BASH_STYLE_PROMT) #if !defined(BB_FEATURE_SIMPLE_PROMPT)
num_ok_lines++; num_ok_lines++;
#endif #endif
} }
command[len++] = '\n'; /* set '\n' */ command[len++] = '\n'; /* set '\n' */
command[len] = 0; command[len] = 0;
#if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_SH_TAB_COMPLETION) #if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_COMMAND_TAB_COMPLETION)
input_tab(0); /* strong free */ input_tab(0); /* strong free */
#endif #endif
#if defined(BB_FEATURE_BASH_STYLE_PROMT) #if !defined(BB_FEATURE_SIMPLE_PROMPT)
free(cmdedit_prompt); free(cmdedit_prompt);
#endif #endif
return; return;
@ -1523,7 +1540,7 @@ extern void cmdedit_terminate(void)
} }
} }
#endif /* BB_FEATURE_SH_COMMAND_EDITING */ #endif /* BB_FEATURE_COMMAND_EDITING */
#ifdef TEST #ifdef TEST
@ -1534,21 +1551,25 @@ int main(int argc, char **argv)
{ {
char buff[BUFSIZ]; char buff[BUFSIZ];
char *prompt = char *prompt =
#if defined(BB_FEATURE_BASH_STYLE_PROMT) #if !defined(BB_FEATURE_SIMPLE_PROMPT)
"\\[\\033[32;1m\\]\\u@\\[\\033[33;1m\\]\\h:\ "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \ \\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
\\!\\[\\033[36;1m\\]\\$ \\[\\033[0m\\]"; \\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
#else #else
"% "; "% ";
#endif #endif
shell_context = 1; shell_context = 1;
do { do {
int l;
cmdedit_read_input(prompt, buff); cmdedit_read_input(prompt, buff);
l = strlen(buff);
if(l > 0 && buff[l-1] == '\n')
buff[l-1] = 0;
printf("*** cmdedit_read_input() returned line =%s=\n", buff); printf("*** cmdedit_read_input() returned line =%s=\n", buff);
} while (shell_context); } while (shell_context);
printf("*** cmdedit_read_input() detect ^C\n"); printf("*** cmdedit_read_input() detect ^C\n");
return 0; return 0;
} }
#endif /* TEST */ #endif /* TEST */

View File

@ -1,13 +1,14 @@
#ifndef CMDEDIT_H #ifndef CMDEDIT_H
#define CMDEDIT_H #define CMDEDIT_H
#ifdef BB_FEATURE_SH_COMMAND_EDITING #ifdef BB_FEATURE_COMMAND_EDITING
#include <stddef.h> #include <stddef.h>
#include "busybox.h"
void cmdedit_init(void); void cmdedit_init(void);
void cmdedit_terminate(void); void cmdedit_terminate(void);
void cmdedit_read_input(char* promptStr, char* command); /* read a line of input */ void cmdedit_read_input(char* promptStr, char* command); /* read a line of input */
#endif /* BB_FEATURE_SH_COMMAND_EDITING */ #endif /* BB_FEATURE_COMMAND_EDITING */
#endif /* CMDEDIT_H */ #endif /* CMDEDIT_H */

184
lash.c
View File

@ -39,11 +39,15 @@
//#define BB_FEATURE_SH_BACKTICKS //#define BB_FEATURE_SH_BACKTICKS
// //
//If, then, else, etc. support.. This should now behave basically //If, then, else, etc. support.. This should now behave basically
//like any other Bourne shell... //like any other Bourne shell -- sortof...
#define BB_FEATURE_SH_IF_EXPRESSIONS #define BB_FEATURE_SH_IF_EXPRESSIONS
// //
/* This is currently a little broken... */ /* This is currently sortof broken, only for the brave... */
//#define HANDLE_CONTINUATION_CHARS #undef HANDLE_CONTINUATION_CHARS
//
/* This would be great -- if wordexp wouldn't strip all quoting
* out from the target strings... As is, a parser needs */
#undef BB_FEATURE_SH_WORDEXP
// //
//For debugging/development on the shell only... //For debugging/development on the shell only...
//#define DEBUG_SHELL //#define DEBUG_SHELL
@ -61,10 +65,9 @@
#include <unistd.h> #include <unistd.h>
#include <getopt.h> #include <getopt.h>
//#undef __GLIBC__ #undef BB_FEATURE_SH_WORDEXP
//#undef __UCLIBC__
#if ( (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) ) || defined (__UCLIBC__) #if BB_FEATURE_SH_WORDEXP
#include <wordexp.h> #include <wordexp.h>
#define expand_t wordexp_t #define expand_t wordexp_t
#undef BB_FEATURE_SH_BACKTICKS #undef BB_FEATURE_SH_BACKTICKS
@ -227,6 +230,10 @@ static int show_x_trace;
static char syntax_err[]="syntax error near unexpected token"; static char syntax_err[]="syntax error near unexpected token";
#endif #endif
static char *PS1;
static char *PS2;
#ifdef DEBUG_SHELL #ifdef DEBUG_SHELL
static inline void debug_printf(const char *format, ...) static inline void debug_printf(const char *format, ...)
{ {
@ -286,7 +293,7 @@ static int builtin_cd(struct child_prog *child)
else else
newdir = child->argv[1]; newdir = child->argv[1];
if (chdir(newdir)) { if (chdir(newdir)) {
printf("cd: %s: %s\n", newdir, strerror(errno)); printf("cd: %s: %m\n", newdir);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
getcwd(cwd, sizeof(char)*MAX_LINE); getcwd(cwd, sizeof(char)*MAX_LINE);
@ -425,13 +432,20 @@ static int builtin_pwd(struct child_prog *dummy)
static int builtin_export(struct child_prog *child) static int builtin_export(struct child_prog *child)
{ {
int res; int res;
char *v = child->argv[1];
if (child->argv[1] == NULL) { if (v == NULL) {
return (builtin_env(child)); return (builtin_env(child));
} }
res = putenv(child->argv[1]); res = putenv(v);
if (res) if (res)
fprintf(stderr, "export: %s\n", strerror(errno)); fprintf(stderr, "export: %m\n");
#ifndef BB_FEATURE_SIMPLE_PROMPT
if (strncmp(v, "PS1=", 4)==0)
PS1 = getenv("PS1");
else if (strncmp(v, "PS2=", 4)==0)
PS2 = getenv("PS2");
#endif
return (res); return (res);
} }
@ -461,7 +475,7 @@ static int builtin_read(struct child_prog *child)
if((s = strdup(string))) if((s = strdup(string)))
res = putenv(s); res = putenv(s);
if (res) if (res)
fprintf(stderr, "read: %s\n", strerror(errno)); fprintf(stderr, "read: %m\n");
} }
else else
fgets(string, sizeof(string), stdin); fgets(string, sizeof(string), stdin);
@ -759,8 +773,7 @@ static int setup_redirects(struct child_prog *prog, int squirrel[])
if (openfd < 0) { if (openfd < 0) {
/* this could get lost if stderr has been redirected, but /* this could get lost if stderr has been redirected, but
bash and ash both lose it as well (though zsh doesn't!) */ bash and ash both lose it as well (though zsh doesn't!) */
error_msg("error opening %s: %s", redir->filename, perror_msg("error opening %s", redir->filename);
strerror(errno));
return 1; return 1;
} }
@ -790,56 +803,39 @@ static void restore_redirects(int squirrel[])
} }
} }
#if defined(BB_FEATURE_SH_SIMPLE_PROMPT) static inline void cmdedit_set_initial_prompt(void)
static char* setup_prompt_string(int state)
{ {
char prompt_str[BUFSIZ]; #ifdef BB_FEATURE_SIMPLE_PROMPT
PS1 = NULL;
/* Set up the prompt */ PS2 = "> ";
if (state == 0) {
/* simple prompt */
sprintf(prompt_str, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
} else {
strcpy(prompt_str,"> ");
}
return(strdup(prompt_str)); /* Must free this memory */
}
#else #else
PS1 = getenv("PS1");
static char* setup_prompt_string(int state) if(PS1==0) {
{ PS1 = "\\w \\$ ";
char user[9],buf[255],*s;
char prompt[3];
char prompt_str[BUFSIZ];
/* Set up the prompt */
if (state == 0) {
/* get User Name and setup prompt */
strcpy(prompt,( geteuid() != 0 ) ? "$ ":"# ");
my_getpwuid(user, geteuid());
/* get HostName */
gethostname(buf, 255);
s = strchr(buf, '.');
if (s) {
*s = 0;
}
} else {
strcpy(prompt,"> ");
} }
PS2 = getenv("PS2");
if (state == 0) { if(PS2==0)
snprintf(prompt_str, BUFSIZ-1, "[%s@%s %s]%s", user, buf, PS2 = "> ";
get_last_path_component(cwd), prompt); #endif
} else {
sprintf(prompt_str, "%s", prompt);
}
return(strdup(prompt_str)); /* Must free this memory */
} }
static inline void setup_prompt_string(char **prompt_str)
{
#ifdef BB_FEATURE_SIMPLE_PROMPT
/* Set up the prompt */
if (shell_context == 0) {
if (PS1)
free(PS1);
PS1=xmalloc(strlen(cwd)+4);
sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
*prompt_str = PS1;
} else {
*prompt_str = PS2;
}
#else
*prompt_str = (shell_context==0)? PS1 : PS2;
#endif #endif
}
static int get_command(FILE * source, char *command) static int get_command(FILE * source, char *command)
{ {
@ -857,9 +853,9 @@ static int get_command(FILE * source, char *command)
} }
if (source == stdin) { if (source == stdin) {
prompt_str = setup_prompt_string(shell_context); setup_prompt_string(&prompt_str);
#ifdef BB_FEATURE_SH_COMMAND_EDITING #ifdef BB_FEATURE_COMMAND_EDITING
/* /*
** enable command line editing only while a command line ** enable command line editing only while a command line
** is actually being read; otherwise, we'll end up bequeathing ** is actually being read; otherwise, we'll end up bequeathing
@ -868,11 +864,9 @@ static int get_command(FILE * source, char *command)
*/ */
cmdedit_read_input(prompt_str, command); cmdedit_read_input(prompt_str, command);
cmdedit_terminate(); cmdedit_terminate();
free(prompt_str);
return 0; return 0;
#else #else
fputs(prompt_str, stdout); fputs(prompt_str, stdout);
free(prompt_str);
#endif #endif
} }
@ -910,11 +904,48 @@ static char* itoa(register int i)
} }
#endif #endif
#if defined BB_FEATURE_SH_ENVIRONMENT && ! defined BB_FEATURE_SH_WORDEXP
char * strsep_space( char *string, int * index)
{
char *token, *begin;
begin = string;
/* Short circuit the trivial case */
if ( !string || ! string[*index])
return NULL;
/* Find the end of the token. */
while( string && string[*index] && !isspace(string[*index]) ) {
(*index)++;
}
/* Find the end of any whitespace trailing behind
* the token and let that be part of the token */
while( string && string[*index] && isspace(string[*index]) ) {
(*index)++;
}
if (! string && *index==0) {
/* Nothing useful was found */
return NULL;
}
token = xmalloc(*index);
token[*index] = '\0';
strncpy(token, string, *index);
return token;
}
#endif
static int expand_arguments(char *command) static int expand_arguments(char *command)
{ {
#ifdef BB_FEATURE_SH_ENVIRONMENT #ifdef BB_FEATURE_SH_ENVIRONMENT
expand_t expand_result; expand_t expand_result;
char *src, *dst, *var; char *src, *dst, *var;
int index = 0;
int i=0, length, total_length=0, retval; int i=0, length, total_length=0, retval;
const char *out_of_space = "out of space during expansion"; const char *out_of_space = "out of space during expansion";
#endif #endif
@ -922,13 +953,23 @@ static int expand_arguments(char *command)
/* get rid of the terminating \n */ /* get rid of the terminating \n */
chomp(command); chomp(command);
/* Fix up escape sequences to be the Real Thing(tm) */
while( command && command[index]) {
if (command[index] == '\\') {
char *tmp = command+index+1;
command[index+1] = process_escape_sequence( &tmp );
memmove(command+index, command+index+1, strlen(command+index));
}
index++;
}
#ifdef BB_FEATURE_SH_ENVIRONMENT #ifdef BB_FEATURE_SH_ENVIRONMENT
#if ( (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) ) || defined (__UCLIBC__) #if BB_FEATURE_SH_WORDEXP
/* This first part uses wordexp() which is a wonderful C lib /* This first part uses wordexp() which is a wonderful C lib
* function which expands nearly everything. */ * function which expands nearly everything. */
retval = wordexp (command, &expand_result, 0); retval = wordexp (command, &expand_result, WRDE_SHOWERR);
if (retval == WRDE_NOSPACE) { if (retval == WRDE_NOSPACE) {
/* Mem may have been allocated... */ /* Mem may have been allocated... */
wordfree (&expand_result); wordfree (&expand_result);
@ -970,15 +1011,17 @@ static int expand_arguments(char *command)
{ {
int flags = GLOB_NOCHECK|GLOB_BRACE|GLOB_TILDE; int flags = GLOB_NOCHECK|GLOB_BRACE|GLOB_TILDE;
char * tmpcmd, *cmd, *cmd_copy; char *tmpcmd, *cmd, *cmd_copy;
/* We need a clean copy, so strsep can mess up the copy while /* We need a clean copy, so strsep can mess up the copy while
* we write stuff into the original (in a minute) */ * we write stuff into the original (in a minute) */
cmd = cmd_copy = strdup(command); cmd = cmd_copy = strdup(command);
*command = '\0'; *command = '\0';
for (tmpcmd = cmd; (tmpcmd = strsep(&cmd, " \t")) != NULL;) { for (index = 0, tmpcmd = cmd;
(tmpcmd = strsep_space(cmd, &index)) != NULL; cmd += index, index=0) {
if (*tmpcmd == '\0') if (*tmpcmd == '\0')
break; break;
retval = glob(tmpcmd, flags, NULL, &expand_result); retval = glob(tmpcmd, flags, NULL, &expand_result);
free(tmpcmd); /* Free mem allocated by strsep_space */
if (retval == GLOB_NOSPACE) { if (retval == GLOB_NOSPACE) {
/* Mem may have been allocated... */ /* Mem may have been allocated... */
globfree (&expand_result); globfree (&expand_result);
@ -1711,7 +1754,8 @@ static int busy_loop(FILE * input)
if (!parse_command(&next_command, &newjob, &inbg) && if (!parse_command(&next_command, &newjob, &inbg) &&
newjob.num_progs) { newjob.num_progs) {
int pipefds[2] = {-1,-1}; int pipefds[2] = {-1,-1};
debug_printf( "job=%p being fed to run_command by busy_loop()'\n", &newjob); debug_printf( "job=%p fed to run_command by busy_loop()'\n",
&newjob);
run_command(&newjob, inbg, pipefds); run_command(&newjob, inbg, pipefds);
} }
else { else {
@ -1879,5 +1923,13 @@ int shell_main(int argc_l, char **argv_l)
atexit(free_memory); atexit(free_memory);
#endif #endif
#ifdef BB_FEATURE_COMMAND_EDITING
cmdedit_set_initial_prompt();
#else
PS1 = NULL;
PS2 = "> ";
#endif
return (busy_loop(input)); return (busy_loop(input));
} }

184
sh.c
View File

@ -39,11 +39,15 @@
//#define BB_FEATURE_SH_BACKTICKS //#define BB_FEATURE_SH_BACKTICKS
// //
//If, then, else, etc. support.. This should now behave basically //If, then, else, etc. support.. This should now behave basically
//like any other Bourne shell... //like any other Bourne shell -- sortof...
#define BB_FEATURE_SH_IF_EXPRESSIONS #define BB_FEATURE_SH_IF_EXPRESSIONS
// //
/* This is currently a little broken... */ /* This is currently sortof broken, only for the brave... */
//#define HANDLE_CONTINUATION_CHARS #undef HANDLE_CONTINUATION_CHARS
//
/* This would be great -- if wordexp wouldn't strip all quoting
* out from the target strings... As is, a parser needs */
#undef BB_FEATURE_SH_WORDEXP
// //
//For debugging/development on the shell only... //For debugging/development on the shell only...
//#define DEBUG_SHELL //#define DEBUG_SHELL
@ -61,10 +65,9 @@
#include <unistd.h> #include <unistd.h>
#include <getopt.h> #include <getopt.h>
//#undef __GLIBC__ #undef BB_FEATURE_SH_WORDEXP
//#undef __UCLIBC__
#if ( (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) ) || defined (__UCLIBC__) #if BB_FEATURE_SH_WORDEXP
#include <wordexp.h> #include <wordexp.h>
#define expand_t wordexp_t #define expand_t wordexp_t
#undef BB_FEATURE_SH_BACKTICKS #undef BB_FEATURE_SH_BACKTICKS
@ -227,6 +230,10 @@ static int show_x_trace;
static char syntax_err[]="syntax error near unexpected token"; static char syntax_err[]="syntax error near unexpected token";
#endif #endif
static char *PS1;
static char *PS2;
#ifdef DEBUG_SHELL #ifdef DEBUG_SHELL
static inline void debug_printf(const char *format, ...) static inline void debug_printf(const char *format, ...)
{ {
@ -286,7 +293,7 @@ static int builtin_cd(struct child_prog *child)
else else
newdir = child->argv[1]; newdir = child->argv[1];
if (chdir(newdir)) { if (chdir(newdir)) {
printf("cd: %s: %s\n", newdir, strerror(errno)); printf("cd: %s: %m\n", newdir);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
getcwd(cwd, sizeof(char)*MAX_LINE); getcwd(cwd, sizeof(char)*MAX_LINE);
@ -425,13 +432,20 @@ static int builtin_pwd(struct child_prog *dummy)
static int builtin_export(struct child_prog *child) static int builtin_export(struct child_prog *child)
{ {
int res; int res;
char *v = child->argv[1];
if (child->argv[1] == NULL) { if (v == NULL) {
return (builtin_env(child)); return (builtin_env(child));
} }
res = putenv(child->argv[1]); res = putenv(v);
if (res) if (res)
fprintf(stderr, "export: %s\n", strerror(errno)); fprintf(stderr, "export: %m\n");
#ifndef BB_FEATURE_SIMPLE_PROMPT
if (strncmp(v, "PS1=", 4)==0)
PS1 = getenv("PS1");
else if (strncmp(v, "PS2=", 4)==0)
PS2 = getenv("PS2");
#endif
return (res); return (res);
} }
@ -461,7 +475,7 @@ static int builtin_read(struct child_prog *child)
if((s = strdup(string))) if((s = strdup(string)))
res = putenv(s); res = putenv(s);
if (res) if (res)
fprintf(stderr, "read: %s\n", strerror(errno)); fprintf(stderr, "read: %m\n");
} }
else else
fgets(string, sizeof(string), stdin); fgets(string, sizeof(string), stdin);
@ -759,8 +773,7 @@ static int setup_redirects(struct child_prog *prog, int squirrel[])
if (openfd < 0) { if (openfd < 0) {
/* this could get lost if stderr has been redirected, but /* this could get lost if stderr has been redirected, but
bash and ash both lose it as well (though zsh doesn't!) */ bash and ash both lose it as well (though zsh doesn't!) */
error_msg("error opening %s: %s", redir->filename, perror_msg("error opening %s", redir->filename);
strerror(errno));
return 1; return 1;
} }
@ -790,56 +803,39 @@ static void restore_redirects(int squirrel[])
} }
} }
#if defined(BB_FEATURE_SH_SIMPLE_PROMPT) static inline void cmdedit_set_initial_prompt(void)
static char* setup_prompt_string(int state)
{ {
char prompt_str[BUFSIZ]; #ifdef BB_FEATURE_SIMPLE_PROMPT
PS1 = NULL;
/* Set up the prompt */ PS2 = "> ";
if (state == 0) {
/* simple prompt */
sprintf(prompt_str, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
} else {
strcpy(prompt_str,"> ");
}
return(strdup(prompt_str)); /* Must free this memory */
}
#else #else
PS1 = getenv("PS1");
static char* setup_prompt_string(int state) if(PS1==0) {
{ PS1 = "\\w \\$ ";
char user[9],buf[255],*s;
char prompt[3];
char prompt_str[BUFSIZ];
/* Set up the prompt */
if (state == 0) {
/* get User Name and setup prompt */
strcpy(prompt,( geteuid() != 0 ) ? "$ ":"# ");
my_getpwuid(user, geteuid());
/* get HostName */
gethostname(buf, 255);
s = strchr(buf, '.');
if (s) {
*s = 0;
}
} else {
strcpy(prompt,"> ");
} }
PS2 = getenv("PS2");
if (state == 0) { if(PS2==0)
snprintf(prompt_str, BUFSIZ-1, "[%s@%s %s]%s", user, buf, PS2 = "> ";
get_last_path_component(cwd), prompt); #endif
} else {
sprintf(prompt_str, "%s", prompt);
}
return(strdup(prompt_str)); /* Must free this memory */
} }
static inline void setup_prompt_string(char **prompt_str)
{
#ifdef BB_FEATURE_SIMPLE_PROMPT
/* Set up the prompt */
if (shell_context == 0) {
if (PS1)
free(PS1);
PS1=xmalloc(strlen(cwd)+4);
sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
*prompt_str = PS1;
} else {
*prompt_str = PS2;
}
#else
*prompt_str = (shell_context==0)? PS1 : PS2;
#endif #endif
}
static int get_command(FILE * source, char *command) static int get_command(FILE * source, char *command)
{ {
@ -857,9 +853,9 @@ static int get_command(FILE * source, char *command)
} }
if (source == stdin) { if (source == stdin) {
prompt_str = setup_prompt_string(shell_context); setup_prompt_string(&prompt_str);
#ifdef BB_FEATURE_SH_COMMAND_EDITING #ifdef BB_FEATURE_COMMAND_EDITING
/* /*
** enable command line editing only while a command line ** enable command line editing only while a command line
** is actually being read; otherwise, we'll end up bequeathing ** is actually being read; otherwise, we'll end up bequeathing
@ -868,11 +864,9 @@ static int get_command(FILE * source, char *command)
*/ */
cmdedit_read_input(prompt_str, command); cmdedit_read_input(prompt_str, command);
cmdedit_terminate(); cmdedit_terminate();
free(prompt_str);
return 0; return 0;
#else #else
fputs(prompt_str, stdout); fputs(prompt_str, stdout);
free(prompt_str);
#endif #endif
} }
@ -910,11 +904,48 @@ static char* itoa(register int i)
} }
#endif #endif
#if defined BB_FEATURE_SH_ENVIRONMENT && ! defined BB_FEATURE_SH_WORDEXP
char * strsep_space( char *string, int * index)
{
char *token, *begin;
begin = string;
/* Short circuit the trivial case */
if ( !string || ! string[*index])
return NULL;
/* Find the end of the token. */
while( string && string[*index] && !isspace(string[*index]) ) {
(*index)++;
}
/* Find the end of any whitespace trailing behind
* the token and let that be part of the token */
while( string && string[*index] && isspace(string[*index]) ) {
(*index)++;
}
if (! string && *index==0) {
/* Nothing useful was found */
return NULL;
}
token = xmalloc(*index);
token[*index] = '\0';
strncpy(token, string, *index);
return token;
}
#endif
static int expand_arguments(char *command) static int expand_arguments(char *command)
{ {
#ifdef BB_FEATURE_SH_ENVIRONMENT #ifdef BB_FEATURE_SH_ENVIRONMENT
expand_t expand_result; expand_t expand_result;
char *src, *dst, *var; char *src, *dst, *var;
int index = 0;
int i=0, length, total_length=0, retval; int i=0, length, total_length=0, retval;
const char *out_of_space = "out of space during expansion"; const char *out_of_space = "out of space during expansion";
#endif #endif
@ -922,13 +953,23 @@ static int expand_arguments(char *command)
/* get rid of the terminating \n */ /* get rid of the terminating \n */
chomp(command); chomp(command);
/* Fix up escape sequences to be the Real Thing(tm) */
while( command && command[index]) {
if (command[index] == '\\') {
char *tmp = command+index+1;
command[index+1] = process_escape_sequence( &tmp );
memmove(command+index, command+index+1, strlen(command+index));
}
index++;
}
#ifdef BB_FEATURE_SH_ENVIRONMENT #ifdef BB_FEATURE_SH_ENVIRONMENT
#if ( (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) ) || defined (__UCLIBC__) #if BB_FEATURE_SH_WORDEXP
/* This first part uses wordexp() which is a wonderful C lib /* This first part uses wordexp() which is a wonderful C lib
* function which expands nearly everything. */ * function which expands nearly everything. */
retval = wordexp (command, &expand_result, 0); retval = wordexp (command, &expand_result, WRDE_SHOWERR);
if (retval == WRDE_NOSPACE) { if (retval == WRDE_NOSPACE) {
/* Mem may have been allocated... */ /* Mem may have been allocated... */
wordfree (&expand_result); wordfree (&expand_result);
@ -970,15 +1011,17 @@ static int expand_arguments(char *command)
{ {
int flags = GLOB_NOCHECK|GLOB_BRACE|GLOB_TILDE; int flags = GLOB_NOCHECK|GLOB_BRACE|GLOB_TILDE;
char * tmpcmd, *cmd, *cmd_copy; char *tmpcmd, *cmd, *cmd_copy;
/* We need a clean copy, so strsep can mess up the copy while /* We need a clean copy, so strsep can mess up the copy while
* we write stuff into the original (in a minute) */ * we write stuff into the original (in a minute) */
cmd = cmd_copy = strdup(command); cmd = cmd_copy = strdup(command);
*command = '\0'; *command = '\0';
for (tmpcmd = cmd; (tmpcmd = strsep(&cmd, " \t")) != NULL;) { for (index = 0, tmpcmd = cmd;
(tmpcmd = strsep_space(cmd, &index)) != NULL; cmd += index, index=0) {
if (*tmpcmd == '\0') if (*tmpcmd == '\0')
break; break;
retval = glob(tmpcmd, flags, NULL, &expand_result); retval = glob(tmpcmd, flags, NULL, &expand_result);
free(tmpcmd); /* Free mem allocated by strsep_space */
if (retval == GLOB_NOSPACE) { if (retval == GLOB_NOSPACE) {
/* Mem may have been allocated... */ /* Mem may have been allocated... */
globfree (&expand_result); globfree (&expand_result);
@ -1711,7 +1754,8 @@ static int busy_loop(FILE * input)
if (!parse_command(&next_command, &newjob, &inbg) && if (!parse_command(&next_command, &newjob, &inbg) &&
newjob.num_progs) { newjob.num_progs) {
int pipefds[2] = {-1,-1}; int pipefds[2] = {-1,-1};
debug_printf( "job=%p being fed to run_command by busy_loop()'\n", &newjob); debug_printf( "job=%p fed to run_command by busy_loop()'\n",
&newjob);
run_command(&newjob, inbg, pipefds); run_command(&newjob, inbg, pipefds);
} }
else { else {
@ -1879,5 +1923,13 @@ int shell_main(int argc_l, char **argv_l)
atexit(free_memory); atexit(free_memory);
#endif #endif
#ifdef BB_FEATURE_COMMAND_EDITING
cmdedit_set_initial_prompt();
#else
PS1 = NULL;
PS2 = "> ";
#endif
return (busy_loop(input)); return (busy_loop(input));
} }

View File

@ -27,6 +27,7 @@
Small bugs (simple effect): Small bugs (simple effect):
- not true viewing if terminal size (x*y symbols) less - not true viewing if terminal size (x*y symbols) less
size (prompt + editor`s line + 2 symbols) size (prompt + editor`s line + 2 symbols)
- not true viewing if length prompt less terminal width
*/ */
@ -50,29 +51,36 @@
#else #else
#define BB_FEATURE_SH_COMMAND_EDITING #define BB_FEATURE_COMMAND_EDITING
#define BB_FEATURE_SH_TAB_COMPLETION #define BB_FEATURE_COMMAND_TAB_COMPLETION
#define BB_FEATURE_SH_USERNAME_COMPLETION #define BB_FEATURE_COMMAND_USERNAME_COMPLETION
#define BB_FEATURE_NONPRINTABLE_INVERSE_PUT #define BB_FEATURE_NONPRINTABLE_INVERSE_PUT
#define BB_FEATURE_BASH_STYLE_PROMT #undef BB_FEATURE_SIMPLE_PROMPT
#define BB_FEATURE_CLEAN_UP #define BB_FEATURE_CLEAN_UP
#define D(x) x #define D(x) x
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#endif /* TEST */ #endif /* TEST */
#ifdef BB_FEATURE_SH_TAB_COMPLETION #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#endif #endif
#ifdef BB_FEATURE_SH_COMMAND_EDITING #ifdef BB_FEATURE_COMMAND_EDITING
#ifndef BB_FEATURE_SH_TAB_COMPLETION #ifndef BB_FEATURE_COMMAND_TAB_COMPLETION
#undef BB_FEATURE_SH_USERNAME_COMPLETION #undef BB_FEATURE_COMMAND_USERNAME_COMPLETION
#endif #endif
#if defined(BB_FEATURE_SH_USERNAME_COMPLETION) || defined(BB_FEATURE_BASH_STYLE_PROMT) #if defined(BB_FEATURE_COMMAND_USERNAME_COMPLETION) || !defined(BB_FEATURE_SIMPLE_PROMPT)
#define BB_FEATURE_GETUSERNAME_AND_HOMEDIR #define BB_FEATURE_GETUSERNAME_AND_HOMEDIR
#endif #endif
@ -154,26 +162,26 @@ static
volatile int handlers_sets = 0; /* Set next bites: */ volatile int handlers_sets = 0; /* Set next bites: */
enum { enum {
SET_ATEXIT = 1, /* when atexit() has been called and SET_ATEXIT = 1, /* when atexit() has been called
get euid,uid,gid to fast compare */ and get euid,uid,gid to fast compare */
SET_TERM_HANDLERS = 2, /* set many terminates signal handlers */ SET_TERM_HANDLERS = 2, /* set many terminates signal handlers */
SET_WCHG_HANDLERS = 4, /* winchg signal handler */ SET_WCHG_HANDLERS = 4, /* winchg signal handler */
SET_RESET_TERM = 8, /* if the terminal needs to be reset upon exit */ SET_RESET_TERM = 8, /* if the terminal needs to be reset upon exit */
}; };
static int cmdedit_x; /* real x terminal position */ static int cmdedit_x; /* real x terminal position */
static int cmdedit_y; /* pseudoreal y terminal position */ static int cmdedit_y; /* pseudoreal y terminal position */
static int cmdedit_prmt_len; /* lenght prompt without colores string */ static int cmdedit_prmt_len; /* lenght prompt without colores string */
static int cursor; /* required global for signal handler */ static int cursor; /* required global for signal handler */
static int len; /* --- "" - - "" - -"- --""-- --""--- */ static int len; /* --- "" - - "" - -"- --""-- --""--- */
static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */ static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
static static
#ifndef BB_FEATURE_BASH_STYLE_PROMT #ifdef BB_FEATURE_SIMPLE_PROMPT
const const
#endif #endif
char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */ char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
/* Link into lash to reset context to 0 on ^C and such */ /* Link into lash to reset context to 0 on ^C and such */
extern unsigned int shell_context; extern unsigned int shell_context;
@ -185,13 +193,13 @@ static char *home_pwd_buf = "";
static int my_euid; static int my_euid;
#endif #endif
#ifdef BB_FEATURE_BASH_STYLE_PROMT #ifndef BB_FEATURE_SIMPLE_PROMPT
static char *hostname_buf = ""; static char *hostname_buf = "";
static int num_ok_lines = 1; static int num_ok_lines = 1;
#endif #endif
#ifdef BB_FEATURE_SH_TAB_COMPLETION #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR #ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
static int my_euid; static int my_euid;
@ -200,7 +208,7 @@ static int my_euid;
static int my_uid; static int my_uid;
static int my_gid; static int my_gid;
#endif /* BB_FEATURE_SH_TAB_COMPLETION */ #endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
static void cmdedit_setwidth(int w, int redraw_flg); static void cmdedit_setwidth(int w, int redraw_flg);
@ -245,7 +253,6 @@ static void cmdedit_reset_term(void)
if (his_front) { if (his_front) {
struct history *n; struct history *n;
//while(his_front!=his_end) {
while (his_front != his_end) { while (his_front != his_end) {
n = his_front->n; n = his_front->n;
free(his_front->s); free(his_front->s);
@ -355,25 +362,27 @@ static void put_prompt(void)
cursor = 0; cursor = 0;
} }
#ifdef BB_FEATURE_BASH_STYLE_PROMT #ifdef BB_FEATURE_SIMPLE_PROMPT
static void static void parse_prompt(const char *prmt_ptr)
add_to_prompt(char **prmt_mem_ptr, int *alm, int *prmt_len,
const char *addb)
{ {
int l = strlen(addb); cmdedit_prompt = prmt_ptr;
cmdedit_prmt_len = strlen(prmt_ptr);
*prmt_len += l; put_prompt();
}
#else
static void add_to_prompt(char **prmt_mem_ptr, int *alm,
int *prmt_len, const char *addb)
{
*prmt_len += strlen(addb);
if (*alm < (*prmt_len) + 1) { if (*alm < (*prmt_len) + 1) {
*alm = (*prmt_len) + 1; *alm = (*prmt_len) + 1;
*prmt_mem_ptr = xrealloc(*prmt_mem_ptr, *alm); *prmt_mem_ptr = xrealloc(*prmt_mem_ptr, *alm);
} }
strcat(*prmt_mem_ptr, addb); strcat(*prmt_mem_ptr, addb);
} }
#endif
static void parse_prompt(const char *prmt_ptr) static void parse_prompt(const char *prmt_ptr)
{ {
#ifdef BB_FEATURE_BASH_STYLE_PROMT
int alm = strlen(prmt_ptr) + 1; /* supposedly require memory */ int alm = strlen(prmt_ptr) + 1; /* supposedly require memory */
int prmt_len = 0; int prmt_len = 0;
int sub_len = 0; int sub_len = 0;
@ -394,9 +403,11 @@ static void parse_prompt(const char *prmt_ptr)
break; break;
prmt_ptr++; prmt_ptr++;
switch (c) { switch (c) {
#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
case 'u': case 'u':
add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, user_buf); add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, user_buf);
continue; continue;
#endif
case 'h': case 'h':
if (hostname_buf[0] == 0) { if (hostname_buf[0] == 0) {
hostname_buf = xcalloc(256, 1); hostname_buf = xcalloc(256, 1);
@ -415,6 +426,7 @@ static void parse_prompt(const char *prmt_ptr)
case '$': case '$':
c = my_euid == 0 ? '#' : '$'; c = my_euid == 0 ? '#' : '$';
break; break;
#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
case 'w': case 'w':
if (pwd_buf[0] == 0) { if (pwd_buf[0] == 0) {
int l; int l;
@ -429,6 +441,7 @@ static void parse_prompt(const char *prmt_ptr)
} }
add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf); add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf);
continue; continue;
#endif
case '!': case '!':
snprintf(buf, sizeof(buf), "%d", num_ok_lines); snprintf(buf, sizeof(buf), "%d", num_ok_lines);
add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf); add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf);
@ -485,20 +498,21 @@ static void parse_prompt(const char *prmt_ptr)
if (flg_not_length == ']') if (flg_not_length == ']')
sub_len++; sub_len++;
} }
#if 0
cmdedit_prmt_len = prmt_len - sub_len; cmdedit_prmt_len = prmt_len - sub_len;
cmdedit_prompt = prmt_mem_ptr;
#else
cmdedit_prompt = prmt_ptr; cmdedit_prompt = prmt_ptr;
cmdedit_prmt_len = strlen(prmt_ptr);
#endif #endif
cmdedit_prompt = prmt_mem_ptr;
cmdedit_prmt_len = strlen(cmdedit_prompt);
put_prompt(); put_prompt();
} }
#endif
/* draw promt, editor line, and clear tail */ /* draw promt, editor line, and clear tail */
static void redraw(int y, int back_cursor) static void redraw(int y, int back_cursor)
{ {
if (y > 0) /* up to start y */ if (y > 0) /* up to start y */
printf("\033[%dA", y); printf("\033[%dA", y);
cmdedit_y = 0; /* new quasireal y */ cmdedit_y = 0; /* new quasireal y */
putchar('\r'); putchar('\r');
@ -518,7 +532,7 @@ static void input_delete(void)
strcpy(command_ps + j, command_ps + j + 1); strcpy(command_ps + j, command_ps + j + 1);
len--; len--;
input_end(); /* rewtite new line */ input_end(); /* rewtite new line */
cmdedit_set_out_char(0); /* destroy end char */ cmdedit_set_out_char(0); /* destroy end char */
input_backward(cursor - j); /* back to old pos cursor */ input_backward(cursor - j); /* back to old pos cursor */
} }
@ -545,7 +559,7 @@ static void clean_up_and_die(int sig)
{ {
goto_new_line(); goto_new_line();
if (sig != SIGINT) if (sig != SIGINT)
exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */ exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */
cmdedit_reset_term(); cmdedit_reset_term();
} }
@ -556,7 +570,6 @@ static void cmdedit_setwidth(int w, int redraw_flg)
cmdedit_termw = cmdedit_termw % w; cmdedit_termw = cmdedit_termw % w;
} }
if (w > cmdedit_termw) { if (w > cmdedit_termw) {
cmdedit_termw = w; cmdedit_termw = w;
if (redraw_flg) { if (redraw_flg) {
@ -590,14 +603,14 @@ extern void cmdedit_init(void)
} }
#endif #endif
#ifdef BB_FEATURE_SH_TAB_COMPLETION #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
#ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR #ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
my_euid = geteuid(); my_euid = geteuid();
#endif #endif
my_uid = getuid(); my_uid = getuid();
my_gid = getgid(); my_gid = getgid();
#endif /* BB_FEATURE_SH_TAB_COMPLETION */ #endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
handlers_sets |= SET_ATEXIT; handlers_sets |= SET_ATEXIT;
atexit(cmdedit_reset_term); /* be sure to do this only once */ atexit(cmdedit_reset_term); /* be sure to do this only once */
} }
@ -612,7 +625,7 @@ extern void cmdedit_init(void)
} }
#ifdef BB_FEATURE_SH_TAB_COMPLETION #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
static int is_execute(const struct stat *st) static int is_execute(const struct stat *st)
{ {
@ -623,7 +636,7 @@ static int is_execute(const struct stat *st)
return FALSE; return FALSE;
} }
#ifdef BB_FEATURE_SH_USERNAME_COMPLETION #ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
static char **username_tab_completion(char *ud, int *num_matches) static char **username_tab_completion(char *ud, int *num_matches)
{ {
@ -632,19 +645,19 @@ static char **username_tab_completion(char *ud, int *num_matches)
char *temp; char *temp;
ud++; /* ~user/... to user/... */ ud++; /* ~user/... to user/... */
userlen = strlen(ud); userlen = strlen(ud);
if (num_matches == 0) { /* "~/..." or "~user/..." */ if (num_matches == 0) { /* "~/..." or "~user/..." */
char *sav_ud = ud - 1; char *sav_ud = ud - 1;
char *home = 0; char *home = 0;
if (*ud == '/') { /* "~/..." */ if (*ud == '/') { /* "~/..." */
home = home_pwd_buf; home = home_pwd_buf;
} else { } else {
/* "~user/..." */ /* "~user/..." */
temp = strchr(ud, '/'); temp = strchr(ud, '/');
*temp = 0; /* ~user\0 */ *temp = 0; /* ~user\0 */
entry = getpwnam(ud); entry = getpwnam(ud);
*temp = '/'; /* restore ~user/... */ *temp = '/'; /* restore ~user/... */
ud = temp; ud = temp;
@ -660,7 +673,7 @@ static char **username_tab_completion(char *ud, int *num_matches)
strcpy(sav_ud, temp2); strcpy(sav_ud, temp2);
} }
} }
return 0; /* void, result save to argument :-) */ return 0; /* void, result save to argument :-) */
} else { } else {
/* "~[^/]*" */ /* "~[^/]*" */
char **matches = (char **) NULL; char **matches = (char **) NULL;
@ -685,7 +698,7 @@ static char **username_tab_completion(char *ud, int *num_matches)
return (matches); return (matches);
} }
} }
#endif /* BB_FEATURE_SH_USERNAME_COMPLETION */ #endif /* BB_FEATURE_COMMAND_USERNAME_COMPLETION */
enum { enum {
FIND_EXE_ONLY = 0, FIND_EXE_ONLY = 0,
@ -710,11 +723,11 @@ static int path_parse(char ***p, int flags)
npth = 0; npth = 0;
for (;;) { for (;;) {
npth++; /* count words is + 1 count ':' */ npth++; /* count words is + 1 count ':' */
tmp = strchr(tmp, ':'); tmp = strchr(tmp, ':');
if (tmp) { if (tmp) {
if (*++tmp == 0) if (*++tmp == 0)
break; /* :<empty> */ break; /* :<empty> */
} else } else
break; break;
} }
@ -723,7 +736,7 @@ static int path_parse(char ***p, int flags)
tmp = pth; tmp = pth;
(*p)[0] = xstrdup(tmp); (*p)[0] = xstrdup(tmp);
npth = 1; /* count words is + 1 count ':' */ npth = 1; /* count words is + 1 count ':' */
for (;;) { for (;;) {
tmp = strchr(tmp, ':'); tmp = strchr(tmp, ':');
@ -754,7 +767,7 @@ static char *add_quote_for_spec_chars(char *found)
} }
static char **exe_n_cwd_tab_completion(char *command, int *num_matches, static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
int type) int type)
{ {
char **matches = 0; char **matches = 0;
@ -782,7 +795,7 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
strcpy(dirbuf, command); strcpy(dirbuf, command);
/* set dir only */ /* set dir only */
dirbuf[(pfind - command) + 1] = 0; dirbuf[(pfind - command) + 1] = 0;
#ifdef BB_FEATURE_SH_USERNAME_COMPLETION #ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
if (dirbuf[0] == '~') /* ~/... or ~user/... */ if (dirbuf[0] == '~') /* ~/... or ~user/... */
username_tab_completion(dirbuf, 0); username_tab_completion(dirbuf, 0);
#endif #endif
@ -796,7 +809,7 @@ static char **exe_n_cwd_tab_completion(char *command, int *num_matches,
for (i = 0; i < npaths; i++) { for (i = 0; i < npaths; i++) {
dir = opendir(paths[i]); dir = opendir(paths[i]);
if (!dir) /* Don't print an error */ if (!dir) /* Don't print an error */
continue; continue;
while ((next = readdir(dir)) != NULL) { while ((next = readdir(dir)) != NULL) {
@ -987,7 +1000,8 @@ static int find_match(char *matchBuf, int *len_with_quotes)
for (i = 0; int_buf[i]; i++) for (i = 0; int_buf[i]; i++)
if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') { if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
&& strncmp(&matchBuf[pos_buf[0]], "cd", 2) == 0) && matchBuf[pos_buf[0]]=='c'
&& matchBuf[pos_buf[1]]=='d' )
command_mode = FIND_DIR_ONLY; command_mode = FIND_DIR_ONLY;
else { else {
command_mode = FIND_FILE_ONLY; command_mode = FIND_FILE_ONLY;
@ -1065,7 +1079,7 @@ static void input_tab(int *lastWasTab)
/* Free up any memory already allocated */ /* Free up any memory already allocated */
input_tab(0); input_tab(0);
#ifdef BB_FEATURE_SH_USERNAME_COMPLETION #ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION
/* If the word starts with `~' and there is no slash in the word, /* If the word starts with `~' and there is no slash in the word,
* then try completing this word as a username. */ * then try completing this word as a username. */
@ -1085,7 +1099,7 @@ static void input_tab(int *lastWasTab)
beep(); beep();
if (!matches) if (!matches)
return; /* not found */ return; /* not found */
/* sort */ /* sort */
qsort(matches, num_matches, sizeof(char *), match_compare); qsort(matches, num_matches, sizeof(char *), match_compare);
@ -1101,7 +1115,7 @@ static void input_tab(int *lastWasTab)
free(tmp); free(tmp);
return; return;
} }
} else { /* one match */ } else { /* one match */
tmp = matches[0]; tmp = matches[0];
/* for next completion current found */ /* for next completion current found */
*lastWasTab = FALSE; *lastWasTab = FALSE;
@ -1164,7 +1178,7 @@ static void input_tab(int *lastWasTab)
} }
} }
} }
#endif /* BB_FEATURE_SH_TAB_COMPLETION */ #endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */
static void get_previous_history(struct history **hp, struct history *p) static void get_previous_history(struct history **hp, struct history *p)
{ {
@ -1201,6 +1215,7 @@ enum {
* Furthermore, the "vi" command editing keys are not implemented. * Furthermore, the "vi" command editing keys are not implemented.
* *
*/ */
extern void cmdedit_read_input(char *prompt, char command[BUFSIZ]) extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
{ {
@ -1212,7 +1227,7 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
struct history *hp = his_end; struct history *hp = his_end;
/* prepare before init handlers */ /* prepare before init handlers */
cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */ cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
len = 0; len = 0;
command_ps = command; command_ps = command;
@ -1223,9 +1238,11 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
new_settings.c_cc[VMIN] = 1; new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0; new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */ /* Turn off CTRL-C, so we can trap it */
new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
new_settings.c_lflag &= ~ICANON; /* unbuffered input */ new_settings.c_lflag &= ~ICANON; /* unbuffered input */
new_settings.c_lflag &= ~(ECHO | ECHOCTL | ECHONL); /* Turn off echoing */ /* Turn off echoing */
new_settings.c_lflag &= ~(ECHO | ECHOCTL | ECHONL);
} }
command[0] = 0; command[0] = 0;
@ -1295,7 +1312,7 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
input_backspace(); input_backspace();
break; break;
case '\t': case '\t':
#ifdef BB_FEATURE_SH_TAB_COMPLETION #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
input_tab(&lastWasTab); input_tab(&lastWasTab);
#endif #endif
break; break;
@ -1335,7 +1352,7 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
return; return;
} }
switch (c) { switch (c) {
#ifdef BB_FEATURE_SH_TAB_COMPLETION #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
case '\t': /* Alt-Tab */ case '\t': /* Alt-Tab */
input_tab(&lastWasTab); input_tab(&lastWasTab);
@ -1402,7 +1419,7 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
break; break;
} }
default: /* If it's regular input, do the normal thing */ default: /* If it's regular input, do the normal thing */
#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
/* Control-V -- Add non-printable symbol */ /* Control-V -- Add non-printable symbol */
if (c == 22) { if (c == 22) {
@ -1493,16 +1510,16 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
history_counter++; history_counter++;
} }
} }
#if defined(BB_FEATURE_BASH_STYLE_PROMT) #if !defined(BB_FEATURE_SIMPLE_PROMPT)
num_ok_lines++; num_ok_lines++;
#endif #endif
} }
command[len++] = '\n'; /* set '\n' */ command[len++] = '\n'; /* set '\n' */
command[len] = 0; command[len] = 0;
#if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_SH_TAB_COMPLETION) #if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_COMMAND_TAB_COMPLETION)
input_tab(0); /* strong free */ input_tab(0); /* strong free */
#endif #endif
#if defined(BB_FEATURE_BASH_STYLE_PROMT) #if !defined(BB_FEATURE_SIMPLE_PROMPT)
free(cmdedit_prompt); free(cmdedit_prompt);
#endif #endif
return; return;
@ -1523,7 +1540,7 @@ extern void cmdedit_terminate(void)
} }
} }
#endif /* BB_FEATURE_SH_COMMAND_EDITING */ #endif /* BB_FEATURE_COMMAND_EDITING */
#ifdef TEST #ifdef TEST
@ -1534,21 +1551,25 @@ int main(int argc, char **argv)
{ {
char buff[BUFSIZ]; char buff[BUFSIZ];
char *prompt = char *prompt =
#if defined(BB_FEATURE_BASH_STYLE_PROMT) #if !defined(BB_FEATURE_SIMPLE_PROMPT)
"\\[\\033[32;1m\\]\\u@\\[\\033[33;1m\\]\\h:\ "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \ \\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
\\!\\[\\033[36;1m\\]\\$ \\[\\033[0m\\]"; \\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
#else #else
"% "; "% ";
#endif #endif
shell_context = 1; shell_context = 1;
do { do {
int l;
cmdedit_read_input(prompt, buff); cmdedit_read_input(prompt, buff);
l = strlen(buff);
if(l > 0 && buff[l-1] == '\n')
buff[l-1] = 0;
printf("*** cmdedit_read_input() returned line =%s=\n", buff); printf("*** cmdedit_read_input() returned line =%s=\n", buff);
} while (shell_context); } while (shell_context);
printf("*** cmdedit_read_input() detect ^C\n"); printf("*** cmdedit_read_input() detect ^C\n");
return 0; return 0;
} }
#endif /* TEST */ #endif /* TEST */

View File

@ -1,13 +1,14 @@
#ifndef CMDEDIT_H #ifndef CMDEDIT_H
#define CMDEDIT_H #define CMDEDIT_H
#ifdef BB_FEATURE_SH_COMMAND_EDITING #ifdef BB_FEATURE_COMMAND_EDITING
#include <stddef.h> #include <stddef.h>
#include "busybox.h"
void cmdedit_init(void); void cmdedit_init(void);
void cmdedit_terminate(void); void cmdedit_terminate(void);
void cmdedit_read_input(char* promptStr, char* command); /* read a line of input */ void cmdedit_read_input(char* promptStr, char* command); /* read a line of input */
#endif /* BB_FEATURE_SH_COMMAND_EDITING */ #endif /* BB_FEATURE_COMMAND_EDITING */
#endif /* CMDEDIT_H */ #endif /* CMDEDIT_H */

View File

@ -39,11 +39,15 @@
//#define BB_FEATURE_SH_BACKTICKS //#define BB_FEATURE_SH_BACKTICKS
// //
//If, then, else, etc. support.. This should now behave basically //If, then, else, etc. support.. This should now behave basically
//like any other Bourne shell... //like any other Bourne shell -- sortof...
#define BB_FEATURE_SH_IF_EXPRESSIONS #define BB_FEATURE_SH_IF_EXPRESSIONS
// //
/* This is currently a little broken... */ /* This is currently sortof broken, only for the brave... */
//#define HANDLE_CONTINUATION_CHARS #undef HANDLE_CONTINUATION_CHARS
//
/* This would be great -- if wordexp wouldn't strip all quoting
* out from the target strings... As is, a parser needs */
#undef BB_FEATURE_SH_WORDEXP
// //
//For debugging/development on the shell only... //For debugging/development on the shell only...
//#define DEBUG_SHELL //#define DEBUG_SHELL
@ -61,10 +65,9 @@
#include <unistd.h> #include <unistd.h>
#include <getopt.h> #include <getopt.h>
//#undef __GLIBC__ #undef BB_FEATURE_SH_WORDEXP
//#undef __UCLIBC__
#if ( (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) ) || defined (__UCLIBC__) #if BB_FEATURE_SH_WORDEXP
#include <wordexp.h> #include <wordexp.h>
#define expand_t wordexp_t #define expand_t wordexp_t
#undef BB_FEATURE_SH_BACKTICKS #undef BB_FEATURE_SH_BACKTICKS
@ -227,6 +230,10 @@ static int show_x_trace;
static char syntax_err[]="syntax error near unexpected token"; static char syntax_err[]="syntax error near unexpected token";
#endif #endif
static char *PS1;
static char *PS2;
#ifdef DEBUG_SHELL #ifdef DEBUG_SHELL
static inline void debug_printf(const char *format, ...) static inline void debug_printf(const char *format, ...)
{ {
@ -286,7 +293,7 @@ static int builtin_cd(struct child_prog *child)
else else
newdir = child->argv[1]; newdir = child->argv[1];
if (chdir(newdir)) { if (chdir(newdir)) {
printf("cd: %s: %s\n", newdir, strerror(errno)); printf("cd: %s: %m\n", newdir);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
getcwd(cwd, sizeof(char)*MAX_LINE); getcwd(cwd, sizeof(char)*MAX_LINE);
@ -425,13 +432,20 @@ static int builtin_pwd(struct child_prog *dummy)
static int builtin_export(struct child_prog *child) static int builtin_export(struct child_prog *child)
{ {
int res; int res;
char *v = child->argv[1];
if (child->argv[1] == NULL) { if (v == NULL) {
return (builtin_env(child)); return (builtin_env(child));
} }
res = putenv(child->argv[1]); res = putenv(v);
if (res) if (res)
fprintf(stderr, "export: %s\n", strerror(errno)); fprintf(stderr, "export: %m\n");
#ifndef BB_FEATURE_SIMPLE_PROMPT
if (strncmp(v, "PS1=", 4)==0)
PS1 = getenv("PS1");
else if (strncmp(v, "PS2=", 4)==0)
PS2 = getenv("PS2");
#endif
return (res); return (res);
} }
@ -461,7 +475,7 @@ static int builtin_read(struct child_prog *child)
if((s = strdup(string))) if((s = strdup(string)))
res = putenv(s); res = putenv(s);
if (res) if (res)
fprintf(stderr, "read: %s\n", strerror(errno)); fprintf(stderr, "read: %m\n");
} }
else else
fgets(string, sizeof(string), stdin); fgets(string, sizeof(string), stdin);
@ -759,8 +773,7 @@ static int setup_redirects(struct child_prog *prog, int squirrel[])
if (openfd < 0) { if (openfd < 0) {
/* this could get lost if stderr has been redirected, but /* this could get lost if stderr has been redirected, but
bash and ash both lose it as well (though zsh doesn't!) */ bash and ash both lose it as well (though zsh doesn't!) */
error_msg("error opening %s: %s", redir->filename, perror_msg("error opening %s", redir->filename);
strerror(errno));
return 1; return 1;
} }
@ -790,56 +803,39 @@ static void restore_redirects(int squirrel[])
} }
} }
#if defined(BB_FEATURE_SH_SIMPLE_PROMPT) static inline void cmdedit_set_initial_prompt(void)
static char* setup_prompt_string(int state)
{ {
char prompt_str[BUFSIZ]; #ifdef BB_FEATURE_SIMPLE_PROMPT
PS1 = NULL;
/* Set up the prompt */ PS2 = "> ";
if (state == 0) {
/* simple prompt */
sprintf(prompt_str, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
} else {
strcpy(prompt_str,"> ");
}
return(strdup(prompt_str)); /* Must free this memory */
}
#else #else
PS1 = getenv("PS1");
static char* setup_prompt_string(int state) if(PS1==0) {
{ PS1 = "\\w \\$ ";
char user[9],buf[255],*s;
char prompt[3];
char prompt_str[BUFSIZ];
/* Set up the prompt */
if (state == 0) {
/* get User Name and setup prompt */
strcpy(prompt,( geteuid() != 0 ) ? "$ ":"# ");
my_getpwuid(user, geteuid());
/* get HostName */
gethostname(buf, 255);
s = strchr(buf, '.');
if (s) {
*s = 0;
}
} else {
strcpy(prompt,"> ");
} }
PS2 = getenv("PS2");
if (state == 0) { if(PS2==0)
snprintf(prompt_str, BUFSIZ-1, "[%s@%s %s]%s", user, buf, PS2 = "> ";
get_last_path_component(cwd), prompt); #endif
} else {
sprintf(prompt_str, "%s", prompt);
}
return(strdup(prompt_str)); /* Must free this memory */
} }
static inline void setup_prompt_string(char **prompt_str)
{
#ifdef BB_FEATURE_SIMPLE_PROMPT
/* Set up the prompt */
if (shell_context == 0) {
if (PS1)
free(PS1);
PS1=xmalloc(strlen(cwd)+4);
sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
*prompt_str = PS1;
} else {
*prompt_str = PS2;
}
#else
*prompt_str = (shell_context==0)? PS1 : PS2;
#endif #endif
}
static int get_command(FILE * source, char *command) static int get_command(FILE * source, char *command)
{ {
@ -857,9 +853,9 @@ static int get_command(FILE * source, char *command)
} }
if (source == stdin) { if (source == stdin) {
prompt_str = setup_prompt_string(shell_context); setup_prompt_string(&prompt_str);
#ifdef BB_FEATURE_SH_COMMAND_EDITING #ifdef BB_FEATURE_COMMAND_EDITING
/* /*
** enable command line editing only while a command line ** enable command line editing only while a command line
** is actually being read; otherwise, we'll end up bequeathing ** is actually being read; otherwise, we'll end up bequeathing
@ -868,11 +864,9 @@ static int get_command(FILE * source, char *command)
*/ */
cmdedit_read_input(prompt_str, command); cmdedit_read_input(prompt_str, command);
cmdedit_terminate(); cmdedit_terminate();
free(prompt_str);
return 0; return 0;
#else #else
fputs(prompt_str, stdout); fputs(prompt_str, stdout);
free(prompt_str);
#endif #endif
} }
@ -910,11 +904,48 @@ static char* itoa(register int i)
} }
#endif #endif
#if defined BB_FEATURE_SH_ENVIRONMENT && ! defined BB_FEATURE_SH_WORDEXP
char * strsep_space( char *string, int * index)
{
char *token, *begin;
begin = string;
/* Short circuit the trivial case */
if ( !string || ! string[*index])
return NULL;
/* Find the end of the token. */
while( string && string[*index] && !isspace(string[*index]) ) {
(*index)++;
}
/* Find the end of any whitespace trailing behind
* the token and let that be part of the token */
while( string && string[*index] && isspace(string[*index]) ) {
(*index)++;
}
if (! string && *index==0) {
/* Nothing useful was found */
return NULL;
}
token = xmalloc(*index);
token[*index] = '\0';
strncpy(token, string, *index);
return token;
}
#endif
static int expand_arguments(char *command) static int expand_arguments(char *command)
{ {
#ifdef BB_FEATURE_SH_ENVIRONMENT #ifdef BB_FEATURE_SH_ENVIRONMENT
expand_t expand_result; expand_t expand_result;
char *src, *dst, *var; char *src, *dst, *var;
int index = 0;
int i=0, length, total_length=0, retval; int i=0, length, total_length=0, retval;
const char *out_of_space = "out of space during expansion"; const char *out_of_space = "out of space during expansion";
#endif #endif
@ -922,13 +953,23 @@ static int expand_arguments(char *command)
/* get rid of the terminating \n */ /* get rid of the terminating \n */
chomp(command); chomp(command);
/* Fix up escape sequences to be the Real Thing(tm) */
while( command && command[index]) {
if (command[index] == '\\') {
char *tmp = command+index+1;
command[index+1] = process_escape_sequence( &tmp );
memmove(command+index, command+index+1, strlen(command+index));
}
index++;
}
#ifdef BB_FEATURE_SH_ENVIRONMENT #ifdef BB_FEATURE_SH_ENVIRONMENT
#if ( (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) ) || defined (__UCLIBC__) #if BB_FEATURE_SH_WORDEXP
/* This first part uses wordexp() which is a wonderful C lib /* This first part uses wordexp() which is a wonderful C lib
* function which expands nearly everything. */ * function which expands nearly everything. */
retval = wordexp (command, &expand_result, 0); retval = wordexp (command, &expand_result, WRDE_SHOWERR);
if (retval == WRDE_NOSPACE) { if (retval == WRDE_NOSPACE) {
/* Mem may have been allocated... */ /* Mem may have been allocated... */
wordfree (&expand_result); wordfree (&expand_result);
@ -970,15 +1011,17 @@ static int expand_arguments(char *command)
{ {
int flags = GLOB_NOCHECK|GLOB_BRACE|GLOB_TILDE; int flags = GLOB_NOCHECK|GLOB_BRACE|GLOB_TILDE;
char * tmpcmd, *cmd, *cmd_copy; char *tmpcmd, *cmd, *cmd_copy;
/* We need a clean copy, so strsep can mess up the copy while /* We need a clean copy, so strsep can mess up the copy while
* we write stuff into the original (in a minute) */ * we write stuff into the original (in a minute) */
cmd = cmd_copy = strdup(command); cmd = cmd_copy = strdup(command);
*command = '\0'; *command = '\0';
for (tmpcmd = cmd; (tmpcmd = strsep(&cmd, " \t")) != NULL;) { for (index = 0, tmpcmd = cmd;
(tmpcmd = strsep_space(cmd, &index)) != NULL; cmd += index, index=0) {
if (*tmpcmd == '\0') if (*tmpcmd == '\0')
break; break;
retval = glob(tmpcmd, flags, NULL, &expand_result); retval = glob(tmpcmd, flags, NULL, &expand_result);
free(tmpcmd); /* Free mem allocated by strsep_space */
if (retval == GLOB_NOSPACE) { if (retval == GLOB_NOSPACE) {
/* Mem may have been allocated... */ /* Mem may have been allocated... */
globfree (&expand_result); globfree (&expand_result);
@ -1711,7 +1754,8 @@ static int busy_loop(FILE * input)
if (!parse_command(&next_command, &newjob, &inbg) && if (!parse_command(&next_command, &newjob, &inbg) &&
newjob.num_progs) { newjob.num_progs) {
int pipefds[2] = {-1,-1}; int pipefds[2] = {-1,-1};
debug_printf( "job=%p being fed to run_command by busy_loop()'\n", &newjob); debug_printf( "job=%p fed to run_command by busy_loop()'\n",
&newjob);
run_command(&newjob, inbg, pipefds); run_command(&newjob, inbg, pipefds);
} }
else { else {
@ -1879,5 +1923,13 @@ int shell_main(int argc_l, char **argv_l)
atexit(free_memory); atexit(free_memory);
#endif #endif
#ifdef BB_FEATURE_COMMAND_EDITING
cmdedit_set_initial_prompt();
#else
PS1 = NULL;
PS2 = "> ";
#endif
return (busy_loop(input)); return (busy_loop(input));
} }