Clean up the nasty blunder I'd made of how different behaviors happen.

-Erik
This commit is contained in:
Eric Andersen 2000-07-28 17:08:36 +00:00
parent 6150df320a
commit 6a4c33c5c0
3 changed files with 48 additions and 42 deletions

30
lash.c
View File

@ -1383,7 +1383,7 @@ void free_memory(void)
int shell_main(int argc_l, char **argv_l) int shell_main(int argc_l, char **argv_l)
{ {
int opt; int opt, interactive=FALSE;
FILE *input = stdin; FILE *input = stdin;
argc = argc_l; argc = argc_l;
argv = argv_l; argv = argv_l;
@ -1407,7 +1407,7 @@ int shell_main(int argc_l, char **argv_l)
local_pending_command = realloc(local_pending_command, local_pending_command = realloc(local_pending_command,
strlen(local_pending_command) + strlen(argv[optind])); strlen(local_pending_command) + strlen(argv[optind]));
if (local_pending_command==NULL) if (local_pending_command==NULL)
fatalError("sh: command too long\n"); fatalError("command too long\n");
} }
strcat(local_pending_command, argv[optind]); strcat(local_pending_command, argv[optind]);
if ( (optind + 1) < argc) if ( (optind + 1) < argc)
@ -1419,29 +1419,31 @@ int shell_main(int argc_l, char **argv_l)
showXtrace = TRUE; showXtrace = TRUE;
break; break;
#endif #endif
case 'i':
interactive = TRUE;
break;
default: default:
usage(shell_usage); usage(shell_usage);
} }
} }
/* A shell is interactive if the `-i' flag was given, or if all of
* the following conditions are met:
if (optind<1 && input == stdin) { * no -c command
* no arguments remaining or the -s flag given
* standard input is a terminal
* standard output is a terminal
* Refer to Posix.2, the description of the `sh' utility. */
if (interactive==TRUE || ( argv[optind]==NULL && input==stdin && isatty(fileno(stdin)) && isatty(fileno(stdout)))) {
fprintf(stdout, "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
/* Looks like they want an interactive shell */ /* Looks like they want an interactive shell */
fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER, BB_BT); fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER, 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 if (1==(argc-optind)) { } else if (local_pending_command==NULL) {
fprintf(stdout, "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
input = fopen(argv[optind], "r"); input = fopen(argv[optind], "r");
if (!input) { if (!input) {
fatalError("%s: %s\n", argv[optind], strerror(errno)); fatalError("%s: %s\n", argv[optind], strerror(errno));
} }
} else {
char *oldpath, *newpath;
oldpath = getenv("PATH");
newpath=(char*)xmalloc(strlen(oldpath)+12);
snprintf(newpath, strlen(oldpath)+9, "PATH=./:%s", oldpath);
putenv(newpath);
execvp(argv[optind], argv+optind);
fatalError("%s: %s\n", argv[optind], strerror(errno));
} }
/* initialize the cwd -- this is never freed...*/ /* initialize the cwd -- this is never freed...*/

30
sh.c
View File

@ -1383,7 +1383,7 @@ void free_memory(void)
int shell_main(int argc_l, char **argv_l) int shell_main(int argc_l, char **argv_l)
{ {
int opt; int opt, interactive=FALSE;
FILE *input = stdin; FILE *input = stdin;
argc = argc_l; argc = argc_l;
argv = argv_l; argv = argv_l;
@ -1407,7 +1407,7 @@ int shell_main(int argc_l, char **argv_l)
local_pending_command = realloc(local_pending_command, local_pending_command = realloc(local_pending_command,
strlen(local_pending_command) + strlen(argv[optind])); strlen(local_pending_command) + strlen(argv[optind]));
if (local_pending_command==NULL) if (local_pending_command==NULL)
fatalError("sh: command too long\n"); fatalError("command too long\n");
} }
strcat(local_pending_command, argv[optind]); strcat(local_pending_command, argv[optind]);
if ( (optind + 1) < argc) if ( (optind + 1) < argc)
@ -1419,29 +1419,31 @@ int shell_main(int argc_l, char **argv_l)
showXtrace = TRUE; showXtrace = TRUE;
break; break;
#endif #endif
case 'i':
interactive = TRUE;
break;
default: default:
usage(shell_usage); usage(shell_usage);
} }
} }
/* A shell is interactive if the `-i' flag was given, or if all of
* the following conditions are met:
if (optind<1 && input == stdin) { * no -c command
* no arguments remaining or the -s flag given
* standard input is a terminal
* standard output is a terminal
* Refer to Posix.2, the description of the `sh' utility. */
if (interactive==TRUE || ( argv[optind]==NULL && input==stdin && isatty(fileno(stdin)) && isatty(fileno(stdout)))) {
fprintf(stdout, "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
/* Looks like they want an interactive shell */ /* Looks like they want an interactive shell */
fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER, BB_BT); fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER, 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 if (1==(argc-optind)) { } else if (local_pending_command==NULL) {
fprintf(stdout, "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
input = fopen(argv[optind], "r"); input = fopen(argv[optind], "r");
if (!input) { if (!input) {
fatalError("%s: %s\n", argv[optind], strerror(errno)); fatalError("%s: %s\n", argv[optind], strerror(errno));
} }
} else {
char *oldpath, *newpath;
oldpath = getenv("PATH");
newpath=(char*)xmalloc(strlen(oldpath)+12);
snprintf(newpath, strlen(oldpath)+9, "PATH=./:%s", oldpath);
putenv(newpath);
execvp(argv[optind], argv+optind);
fatalError("%s: %s\n", argv[optind], strerror(errno));
} }
/* initialize the cwd -- this is never freed...*/ /* initialize the cwd -- this is never freed...*/

View File

@ -1383,7 +1383,7 @@ void free_memory(void)
int shell_main(int argc_l, char **argv_l) int shell_main(int argc_l, char **argv_l)
{ {
int opt; int opt, interactive=FALSE;
FILE *input = stdin; FILE *input = stdin;
argc = argc_l; argc = argc_l;
argv = argv_l; argv = argv_l;
@ -1407,7 +1407,7 @@ int shell_main(int argc_l, char **argv_l)
local_pending_command = realloc(local_pending_command, local_pending_command = realloc(local_pending_command,
strlen(local_pending_command) + strlen(argv[optind])); strlen(local_pending_command) + strlen(argv[optind]));
if (local_pending_command==NULL) if (local_pending_command==NULL)
fatalError("sh: command too long\n"); fatalError("command too long\n");
} }
strcat(local_pending_command, argv[optind]); strcat(local_pending_command, argv[optind]);
if ( (optind + 1) < argc) if ( (optind + 1) < argc)
@ -1419,29 +1419,31 @@ int shell_main(int argc_l, char **argv_l)
showXtrace = TRUE; showXtrace = TRUE;
break; break;
#endif #endif
case 'i':
interactive = TRUE;
break;
default: default:
usage(shell_usage); usage(shell_usage);
} }
} }
/* A shell is interactive if the `-i' flag was given, or if all of
* the following conditions are met:
if (optind<1 && input == stdin) { * no -c command
* no arguments remaining or the -s flag given
* standard input is a terminal
* standard output is a terminal
* Refer to Posix.2, the description of the `sh' utility. */
if (interactive==TRUE || ( argv[optind]==NULL && input==stdin && isatty(fileno(stdin)) && isatty(fileno(stdout)))) {
fprintf(stdout, "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
/* Looks like they want an interactive shell */ /* Looks like they want an interactive shell */
fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER, BB_BT); fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER, 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 if (1==(argc-optind)) { } else if (local_pending_command==NULL) {
fprintf(stdout, "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
input = fopen(argv[optind], "r"); input = fopen(argv[optind], "r");
if (!input) { if (!input) {
fatalError("%s: %s\n", argv[optind], strerror(errno)); fatalError("%s: %s\n", argv[optind], strerror(errno));
} }
} else {
char *oldpath, *newpath;
oldpath = getenv("PATH");
newpath=(char*)xmalloc(strlen(oldpath)+12);
snprintf(newpath, strlen(oldpath)+9, "PATH=./:%s", oldpath);
putenv(newpath);
execvp(argv[optind], argv+optind);
fatalError("%s: %s\n", argv[optind], strerror(errno));
} }
/* initialize the cwd -- this is never freed...*/ /* initialize the cwd -- this is never freed...*/