version 1.1.1.1, 2008/06/03 10:38:47 |
version 1.1.1.1.2.1, 2008/08/13 17:12:35 |
|
|
|
|
#include "sh.h" |
#include "sh.h" |
|
|
#define LINELEN 512 |
#define LINELEN 256 |
|
#define ARGMAX 32 |
|
|
#ifdef CMDBOX |
#ifdef CMDBOX |
extern const struct cmd_entry builtin_cmds[]; |
extern const struct cmd_entry builtin_cmds[]; |
|
|
extern int exec_cmd(int argc, char *argv[]); |
extern int exec_cmd(int argc, char *argv[]); |
extern const struct cmd_entry internal_cmds[]; |
extern const struct cmd_entry internal_cmds[]; |
|
|
static char *argv[20]; |
static char *args[ARGMAX]; |
|
|
/* |
/* |
* Read command string. |
|
*/ |
|
static char * |
|
read_line(char *line, int len) |
|
{ |
|
int c, index = 0; |
|
|
|
for (;;) { |
|
c = getchar(); |
|
switch (c) { |
|
case '\n': |
|
case EOF: |
|
line[index] = 0; |
|
return &line[index]; |
|
|
|
default: |
|
if (index > len) { |
|
fprintf(stderr, "Command line overflow\n"); |
|
return (char *)-1; |
|
} |
|
if (c < 0x80) { |
|
line[index] = (char)c; |
|
index++; |
|
} |
|
break; |
|
} |
|
} |
|
} |
|
|
|
/* |
|
* Find command from the specifid table. |
* Find command from the specifid table. |
* |
* |
* Returns 0 if command is processed. |
* Returns 0 if command is processed. |
|
|
parse_line(char *line) |
parse_line(char *line) |
{ |
{ |
char *p = line; |
char *p = line; |
int argc = 0; |
int i, argc; |
cmd_func_t cmd; |
cmd_func_t cmd; |
|
|
/* |
/* |
* Build argument list |
* Build argument list |
*/ |
*/ |
for (;;) { |
i = 0; |
/* Skip space */ |
do { |
|
/* Ignore whitespace at beginning */ |
while (*p && isspace((int)*p)) |
while (*p && isspace((int)*p)) |
p++; |
p++; |
if (*p == 0) |
if (*p == '\0') |
break; |
break; |
argv[argc++] = p; |
args[i] = p; |
|
i++; |
|
|
/* Skip word */ |
/* Skip word */ |
while (*p && !isspace((int)*p)) |
while (*p && !isspace((int)*p)) |
p++; |
p++; |
if (*p == 0) |
if (*p == '\0') |
break; |
break; |
*p++ = 0; |
*p++ = '\0'; |
|
} while (i < ARGMAX - 1); |
|
|
|
if (i == ARGMAX) { |
|
fprintf(stderr, "Too many args\n"); |
|
return; |
} |
} |
argv[argc] = NULL; |
args[i] = NULL; |
|
argc = i; |
|
|
/* |
/* |
* Dispatch command |
* Dispatch command |
*/ |
*/ |
if (argc) { |
if (argc > 0) { |
optind = 1; |
optind = 1; |
|
|
/* Run it as internal command */ |
/* Run it as internal command */ |
cmd = find_cmd(internal_cmds, argv[0]); |
cmd = find_cmd(internal_cmds, args[0]); |
if (cmd != NULL) { |
if (cmd != NULL) { |
if ((cmd)(argc, argv) != 0) { |
if ((*cmd)(argc, args) != 0) { |
fprintf(stderr, "%s: %s\n", argv[0], |
fprintf(stderr, "%s: %s\n", args[0], |
strerror(errno)); |
strerror(errno)); |
} |
} |
return; |
return; |
} |
} |
#ifdef CMDBOX |
#ifdef CMDBOX |
/* Run it as shell built-in command */ |
/* Run it as shell built-in command */ |
cmd = find_cmd(builtin_cmds, argv[0]); |
cmd = find_cmd(builtin_cmds, args[0]); |
if (cmd != NULL) { |
if (cmd != NULL) { |
exec_builtin(cmd, argc, argv); |
exec_builtin(cmd, argc, args); |
return; |
return; |
} |
} |
#endif |
#endif |
/* Next, run it as external command */ |
/* Next, run it as external command */ |
exec_cmd(argc, argv); |
exec_cmd(argc, args); |
} |
} |
} |
} |
|
|
|
/* |
|
* Read command string. |
|
*/ |
|
static char * |
|
read_line(char *line, int len, int fd) |
|
{ |
|
char *p, *end; |
|
int c; |
|
|
|
end = line + len; |
|
for (p = line; p < end; p++) { |
|
c = getchar(); |
|
if (c == EOF) { |
|
if (p == line) |
|
return NULL; |
|
*p = '\0'; |
|
return p; |
|
} |
|
if (c == '\n') { |
|
*p = '\0'; |
|
return p; |
|
} |
|
*p = (char)c; |
|
} |
|
fprintf(stderr, "Command line overflow\n"); |
|
return (char *)-1; |
|
} |
|
|
int |
int |
main(int dummy, char *argv[]) |
main(int argc, char **argv) |
{ |
{ |
static char line[LINELEN]; |
static char line[LINELEN]; |
static char cwd[PATH_MAX]; |
static char cwd[PATH_MAX]; |
|
|
signal(SIGINT, SIG_IGN); |
signal(SIGINT, SIG_IGN); |
signal(SIGQUIT, SIG_IGN); |
signal(SIGQUIT, SIG_IGN); |
signal(SIGTERM, SIG_IGN); |
signal(SIGTERM, SIG_IGN); |
|
signal(SIGTSTP, SIG_IGN); /* temporary... */ |
|
|
/* |
/* |
* Command loop |
* Command loop |
|
|
printf("\033[0m# "); |
printf("\033[0m# "); |
|
|
/* Read and parse user input */ |
/* Read and parse user input */ |
p = read_line(line, LINELEN); |
p = read_line(line, LINELEN, 0); |
if (p == NULL) |
if (p == NULL) |
break; |
break; |
if (p == (char *)-1) |
if (p == (char *)-1) |
|
|
while (*(p - 1) == '\\' && (p - 2) >= line |
while (*(p - 1) == '\\' && (p - 2) >= line |
&& *(p - 2) != '\\') { |
&& *(p - 2) != '\\') { |
*(p - 1) = ' '; |
*(p - 1) = ' '; |
p = read_line(p, LINELEN - (p - line)); |
p = read_line(p, LINELEN - (p - line), 0); |
if (p == NULL) |
if (p == NULL) |
break; |
break; |
if (p == (char *)-1) |
if (p == (char *)-1) |
|
|
} |
} |
parse_line(line); |
parse_line(line); |
} |
} |
|
printf("bye!\n"); |
|
exit(0); |
return 0; |
return 0; |
} |
} |