Another sh.c patch from Larry Doolittle. This makes redirects work properly
with non-forking shell builtins. Especially helpful for "read". This patch also beautifies builtin_fg_bg, clarifies the problems with run_command_predicate, makes if/then/else support the default, and corrects the sense of the BB_FEATURE_SH_ENVIRONMENT comment.
This commit is contained in:
parent
16fdbe8b27
commit
e9f07fb6e8
108
lash.c
108
lash.c
@ -26,7 +26,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
//
|
//
|
||||||
//This works pretty well now, and is not on by default.
|
//This works pretty well now, and is now on by default.
|
||||||
#define BB_FEATURE_SH_ENVIRONMENT
|
#define BB_FEATURE_SH_ENVIRONMENT
|
||||||
//
|
//
|
||||||
//Backtick support has some problems, use at your own risk!
|
//Backtick support has some problems, use at your own risk!
|
||||||
@ -34,7 +34,7 @@
|
|||||||
//
|
//
|
||||||
//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...
|
||||||
//#define BB_FEATURE_SH_IF_EXPRESSIONS
|
#define BB_FEATURE_SH_IF_EXPRESSIONS
|
||||||
//
|
//
|
||||||
/* This is currently a little broken... */
|
/* This is currently a little broken... */
|
||||||
//#define HANDLE_CONTINUATION_CHARS
|
//#define HANDLE_CONTINUATION_CHARS
|
||||||
@ -316,22 +316,23 @@ static int builtin_fg_bg(struct child_prog *child)
|
|||||||
int i, jobNum;
|
int i, jobNum;
|
||||||
struct job *job=NULL;
|
struct job *job=NULL;
|
||||||
|
|
||||||
|
if (!child->argv[1] || child->argv[2]) {
|
||||||
|
error_msg("%s: exactly one argument is expected\n",
|
||||||
|
child->argv[0]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
if (!child->argv[1] || child->argv[2]) {
|
if (sscanf(child->argv[1], "%%%d", &jobNum) != 1) {
|
||||||
error_msg("%s: exactly one argument is expected\n",
|
error_msg("%s: bad argument '%s'\n",
|
||||||
child->argv[0]);
|
child->argv[0], child->argv[1]);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
if (sscanf(child->argv[1], "%%%d", &jobNum) != 1) {
|
|
||||||
error_msg("%s: bad argument '%s'\n",
|
for (job = child->family->job_list->head; job; job = job->next) {
|
||||||
child->argv[0], child->argv[1]);
|
if (job->jobid == jobNum) {
|
||||||
return EXIT_FAILURE;
|
break;
|
||||||
}
|
|
||||||
for (job = child->family->job_list->head; job; job = job->next) {
|
|
||||||
if (job->jobid == jobNum) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!job) {
|
if (!job) {
|
||||||
error_msg("%s: unknown job %d\n",
|
error_msg("%s: unknown job %d\n",
|
||||||
@ -524,7 +525,7 @@ static int builtin_else(struct child_prog *child)
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd->job_context |= ELSE_EXP_CONTEXT;
|
cmd->job_context |= ELSE_EXP_CONTEXT;
|
||||||
debug_printf("job=%p builtin_else set job context to %x\n", child->family, cmd->job_context);
|
debug_printf("job=%p builtin_else set job context to %x\n", cmd, cmd->job_context);
|
||||||
|
|
||||||
/* Now run the 'else' command */
|
/* Now run the 'else' command */
|
||||||
debug_printf( "'else' now running '%s'\n", charptr1);
|
debug_printf( "'else' now running '%s'\n", charptr1);
|
||||||
@ -583,12 +584,13 @@ static int builtin_unset(struct child_prog *child)
|
|||||||
|
|
||||||
#ifdef BB_FEATURE_SH_IF_EXPRESSIONS
|
#ifdef BB_FEATURE_SH_IF_EXPRESSIONS
|
||||||
/* currently used by if/then/else.
|
/* currently used by if/then/else.
|
||||||
* Needlessly (?) forks and reparses the command line.
|
*
|
||||||
* But pseudo_exec on the pre-parsed args doesn't have the
|
* Reparsing the command line for this purpose is gross,
|
||||||
* "fork, stick around until the child exits, and find it's return code"
|
* incorrect, and fundamentally unfixable; in particular,
|
||||||
* functionality. The fork is not needed if the predicate is
|
* think about what happens with command substitution.
|
||||||
* non-forking builtin, and maybe not even if it's a forking builtin.
|
* We really need to pull out the run, wait, return status
|
||||||
* applets pretty clearly need the fork.
|
* functionality out of busy_loop so we can child->argv++
|
||||||
|
* and use that, without going back through parse_command.
|
||||||
*/
|
*/
|
||||||
static int run_command_predicate(char *cmd)
|
static int run_command_predicate(char *cmd)
|
||||||
{
|
{
|
||||||
@ -684,7 +686,9 @@ static void checkjobs(struct jobset *job_list)
|
|||||||
perror("waitpid");
|
perror("waitpid");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int setup_redirects(struct child_prog *prog)
|
/* squirrel != NULL means we squirrel away copies of stdin, stdout,
|
||||||
|
* and stderr if they are redirected. */
|
||||||
|
static int setup_redirects(struct child_prog *prog, int squirrel[])
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int openfd;
|
int openfd;
|
||||||
@ -714,6 +718,9 @@ static int setup_redirects(struct child_prog *prog)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (openfd != redir->fd) {
|
if (openfd != redir->fd) {
|
||||||
|
if (squirrel && redir->fd < 3) {
|
||||||
|
squirrel[redir->fd] = dup(redir->fd);
|
||||||
|
}
|
||||||
dup2(openfd, redir->fd);
|
dup2(openfd, redir->fd);
|
||||||
close(openfd);
|
close(openfd);
|
||||||
}
|
}
|
||||||
@ -722,6 +729,19 @@ static int setup_redirects(struct child_prog *prog)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void restore_redirects(int squirrel[])
|
||||||
|
{
|
||||||
|
int i, fd;
|
||||||
|
for (i=0; i<3; i++) {
|
||||||
|
fd = squirrel[i];
|
||||||
|
if (fd != -1) {
|
||||||
|
/* No error checking. I sure wouldn't know what
|
||||||
|
* to do with an error if I found one! */
|
||||||
|
dup2(fd, i);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int get_command(FILE * source, char *command)
|
static int get_command(FILE * source, char *command)
|
||||||
{
|
{
|
||||||
@ -1304,11 +1324,18 @@ static int pseudo_exec(struct child_prog *child)
|
|||||||
struct BB_applet search_applet, *applet;
|
struct BB_applet search_applet, *applet;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Check if the command matches any of the forking builtins.
|
/* Check if the command matches any of the non-forking builtins.
|
||||||
* XXX It would probably be wise to check for non-forking builtins
|
* Depending on context, this might be redundant. But it's
|
||||||
* here as well, since in some context the non-forking path
|
* easier to waste a few CPU cycles than it is to figure out
|
||||||
* is disabled or bypassed. See comment in run_command.
|
* if this is one of those cases.
|
||||||
*/
|
*/
|
||||||
|
for (x = bltins; x->cmd; x++) {
|
||||||
|
if (strcmp(child->argv[0], x->cmd) == 0 ) {
|
||||||
|
exit(x->function(child));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if the command matches any of the forking builtins. */
|
||||||
for (x = bltins_forking; x->cmd; x++) {
|
for (x = bltins_forking; x->cmd; x++) {
|
||||||
if (strcmp(child->argv[0], x->cmd) == 0) {
|
if (strcmp(child->argv[0], x->cmd) == 0) {
|
||||||
applet_name=x->cmd;
|
applet_name=x->cmd;
|
||||||
@ -1435,15 +1462,22 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2])
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Check if the command matches any non-forking builtins.
|
/* Check if the command matches any non-forking builtins,
|
||||||
* XXX should probably skip this test, and fork anyway, if
|
* but only if this is a simple command.
|
||||||
* there redirects of some kind demand forking to work right.
|
* Non-forking builtins within pipes have to fork anyway,
|
||||||
* pseudo_exec would then need to handle the non-forking command
|
* and are handled in pseudo_exec. "echo foo | read bar"
|
||||||
* in a forked context.
|
* is doomed to failure, and doesn't work on bash, either.
|
||||||
*/
|
*/
|
||||||
for (x = bltins; x->cmd; x++) {
|
if (newjob->num_progs == 1) {
|
||||||
if (strcmp(child->argv[0], x->cmd) == 0 ) {
|
for (x = bltins; x->cmd; x++) {
|
||||||
return(x->function(child));
|
if (strcmp(child->argv[0], x->cmd) == 0 ) {
|
||||||
|
int squirrel[] = {-1, -1, -1};
|
||||||
|
int rcode;
|
||||||
|
setup_redirects(child, squirrel);
|
||||||
|
rcode = x->function(child);
|
||||||
|
restore_redirects(squirrel);
|
||||||
|
return rcode;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1466,7 +1500,7 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2])
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* explicit redirects override pipes */
|
/* explicit redirects override pipes */
|
||||||
setup_redirects(child);
|
setup_redirects(child,NULL);
|
||||||
|
|
||||||
pseudo_exec(child);
|
pseudo_exec(child);
|
||||||
}
|
}
|
||||||
|
108
sh.c
108
sh.c
@ -26,7 +26,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
//
|
//
|
||||||
//This works pretty well now, and is not on by default.
|
//This works pretty well now, and is now on by default.
|
||||||
#define BB_FEATURE_SH_ENVIRONMENT
|
#define BB_FEATURE_SH_ENVIRONMENT
|
||||||
//
|
//
|
||||||
//Backtick support has some problems, use at your own risk!
|
//Backtick support has some problems, use at your own risk!
|
||||||
@ -34,7 +34,7 @@
|
|||||||
//
|
//
|
||||||
//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...
|
||||||
//#define BB_FEATURE_SH_IF_EXPRESSIONS
|
#define BB_FEATURE_SH_IF_EXPRESSIONS
|
||||||
//
|
//
|
||||||
/* This is currently a little broken... */
|
/* This is currently a little broken... */
|
||||||
//#define HANDLE_CONTINUATION_CHARS
|
//#define HANDLE_CONTINUATION_CHARS
|
||||||
@ -316,22 +316,23 @@ static int builtin_fg_bg(struct child_prog *child)
|
|||||||
int i, jobNum;
|
int i, jobNum;
|
||||||
struct job *job=NULL;
|
struct job *job=NULL;
|
||||||
|
|
||||||
|
if (!child->argv[1] || child->argv[2]) {
|
||||||
|
error_msg("%s: exactly one argument is expected\n",
|
||||||
|
child->argv[0]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
if (!child->argv[1] || child->argv[2]) {
|
if (sscanf(child->argv[1], "%%%d", &jobNum) != 1) {
|
||||||
error_msg("%s: exactly one argument is expected\n",
|
error_msg("%s: bad argument '%s'\n",
|
||||||
child->argv[0]);
|
child->argv[0], child->argv[1]);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
if (sscanf(child->argv[1], "%%%d", &jobNum) != 1) {
|
|
||||||
error_msg("%s: bad argument '%s'\n",
|
for (job = child->family->job_list->head; job; job = job->next) {
|
||||||
child->argv[0], child->argv[1]);
|
if (job->jobid == jobNum) {
|
||||||
return EXIT_FAILURE;
|
break;
|
||||||
}
|
|
||||||
for (job = child->family->job_list->head; job; job = job->next) {
|
|
||||||
if (job->jobid == jobNum) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!job) {
|
if (!job) {
|
||||||
error_msg("%s: unknown job %d\n",
|
error_msg("%s: unknown job %d\n",
|
||||||
@ -524,7 +525,7 @@ static int builtin_else(struct child_prog *child)
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd->job_context |= ELSE_EXP_CONTEXT;
|
cmd->job_context |= ELSE_EXP_CONTEXT;
|
||||||
debug_printf("job=%p builtin_else set job context to %x\n", child->family, cmd->job_context);
|
debug_printf("job=%p builtin_else set job context to %x\n", cmd, cmd->job_context);
|
||||||
|
|
||||||
/* Now run the 'else' command */
|
/* Now run the 'else' command */
|
||||||
debug_printf( "'else' now running '%s'\n", charptr1);
|
debug_printf( "'else' now running '%s'\n", charptr1);
|
||||||
@ -583,12 +584,13 @@ static int builtin_unset(struct child_prog *child)
|
|||||||
|
|
||||||
#ifdef BB_FEATURE_SH_IF_EXPRESSIONS
|
#ifdef BB_FEATURE_SH_IF_EXPRESSIONS
|
||||||
/* currently used by if/then/else.
|
/* currently used by if/then/else.
|
||||||
* Needlessly (?) forks and reparses the command line.
|
*
|
||||||
* But pseudo_exec on the pre-parsed args doesn't have the
|
* Reparsing the command line for this purpose is gross,
|
||||||
* "fork, stick around until the child exits, and find it's return code"
|
* incorrect, and fundamentally unfixable; in particular,
|
||||||
* functionality. The fork is not needed if the predicate is
|
* think about what happens with command substitution.
|
||||||
* non-forking builtin, and maybe not even if it's a forking builtin.
|
* We really need to pull out the run, wait, return status
|
||||||
* applets pretty clearly need the fork.
|
* functionality out of busy_loop so we can child->argv++
|
||||||
|
* and use that, without going back through parse_command.
|
||||||
*/
|
*/
|
||||||
static int run_command_predicate(char *cmd)
|
static int run_command_predicate(char *cmd)
|
||||||
{
|
{
|
||||||
@ -684,7 +686,9 @@ static void checkjobs(struct jobset *job_list)
|
|||||||
perror("waitpid");
|
perror("waitpid");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int setup_redirects(struct child_prog *prog)
|
/* squirrel != NULL means we squirrel away copies of stdin, stdout,
|
||||||
|
* and stderr if they are redirected. */
|
||||||
|
static int setup_redirects(struct child_prog *prog, int squirrel[])
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int openfd;
|
int openfd;
|
||||||
@ -714,6 +718,9 @@ static int setup_redirects(struct child_prog *prog)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (openfd != redir->fd) {
|
if (openfd != redir->fd) {
|
||||||
|
if (squirrel && redir->fd < 3) {
|
||||||
|
squirrel[redir->fd] = dup(redir->fd);
|
||||||
|
}
|
||||||
dup2(openfd, redir->fd);
|
dup2(openfd, redir->fd);
|
||||||
close(openfd);
|
close(openfd);
|
||||||
}
|
}
|
||||||
@ -722,6 +729,19 @@ static int setup_redirects(struct child_prog *prog)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void restore_redirects(int squirrel[])
|
||||||
|
{
|
||||||
|
int i, fd;
|
||||||
|
for (i=0; i<3; i++) {
|
||||||
|
fd = squirrel[i];
|
||||||
|
if (fd != -1) {
|
||||||
|
/* No error checking. I sure wouldn't know what
|
||||||
|
* to do with an error if I found one! */
|
||||||
|
dup2(fd, i);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int get_command(FILE * source, char *command)
|
static int get_command(FILE * source, char *command)
|
||||||
{
|
{
|
||||||
@ -1304,11 +1324,18 @@ static int pseudo_exec(struct child_prog *child)
|
|||||||
struct BB_applet search_applet, *applet;
|
struct BB_applet search_applet, *applet;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Check if the command matches any of the forking builtins.
|
/* Check if the command matches any of the non-forking builtins.
|
||||||
* XXX It would probably be wise to check for non-forking builtins
|
* Depending on context, this might be redundant. But it's
|
||||||
* here as well, since in some context the non-forking path
|
* easier to waste a few CPU cycles than it is to figure out
|
||||||
* is disabled or bypassed. See comment in run_command.
|
* if this is one of those cases.
|
||||||
*/
|
*/
|
||||||
|
for (x = bltins; x->cmd; x++) {
|
||||||
|
if (strcmp(child->argv[0], x->cmd) == 0 ) {
|
||||||
|
exit(x->function(child));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if the command matches any of the forking builtins. */
|
||||||
for (x = bltins_forking; x->cmd; x++) {
|
for (x = bltins_forking; x->cmd; x++) {
|
||||||
if (strcmp(child->argv[0], x->cmd) == 0) {
|
if (strcmp(child->argv[0], x->cmd) == 0) {
|
||||||
applet_name=x->cmd;
|
applet_name=x->cmd;
|
||||||
@ -1435,15 +1462,22 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2])
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Check if the command matches any non-forking builtins.
|
/* Check if the command matches any non-forking builtins,
|
||||||
* XXX should probably skip this test, and fork anyway, if
|
* but only if this is a simple command.
|
||||||
* there redirects of some kind demand forking to work right.
|
* Non-forking builtins within pipes have to fork anyway,
|
||||||
* pseudo_exec would then need to handle the non-forking command
|
* and are handled in pseudo_exec. "echo foo | read bar"
|
||||||
* in a forked context.
|
* is doomed to failure, and doesn't work on bash, either.
|
||||||
*/
|
*/
|
||||||
for (x = bltins; x->cmd; x++) {
|
if (newjob->num_progs == 1) {
|
||||||
if (strcmp(child->argv[0], x->cmd) == 0 ) {
|
for (x = bltins; x->cmd; x++) {
|
||||||
return(x->function(child));
|
if (strcmp(child->argv[0], x->cmd) == 0 ) {
|
||||||
|
int squirrel[] = {-1, -1, -1};
|
||||||
|
int rcode;
|
||||||
|
setup_redirects(child, squirrel);
|
||||||
|
rcode = x->function(child);
|
||||||
|
restore_redirects(squirrel);
|
||||||
|
return rcode;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1466,7 +1500,7 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2])
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* explicit redirects override pipes */
|
/* explicit redirects override pipes */
|
||||||
setup_redirects(child);
|
setup_redirects(child,NULL);
|
||||||
|
|
||||||
pseudo_exec(child);
|
pseudo_exec(child);
|
||||||
}
|
}
|
||||||
|
108
shell/lash.c
108
shell/lash.c
@ -26,7 +26,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
//
|
//
|
||||||
//This works pretty well now, and is not on by default.
|
//This works pretty well now, and is now on by default.
|
||||||
#define BB_FEATURE_SH_ENVIRONMENT
|
#define BB_FEATURE_SH_ENVIRONMENT
|
||||||
//
|
//
|
||||||
//Backtick support has some problems, use at your own risk!
|
//Backtick support has some problems, use at your own risk!
|
||||||
@ -34,7 +34,7 @@
|
|||||||
//
|
//
|
||||||
//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...
|
||||||
//#define BB_FEATURE_SH_IF_EXPRESSIONS
|
#define BB_FEATURE_SH_IF_EXPRESSIONS
|
||||||
//
|
//
|
||||||
/* This is currently a little broken... */
|
/* This is currently a little broken... */
|
||||||
//#define HANDLE_CONTINUATION_CHARS
|
//#define HANDLE_CONTINUATION_CHARS
|
||||||
@ -316,22 +316,23 @@ static int builtin_fg_bg(struct child_prog *child)
|
|||||||
int i, jobNum;
|
int i, jobNum;
|
||||||
struct job *job=NULL;
|
struct job *job=NULL;
|
||||||
|
|
||||||
|
if (!child->argv[1] || child->argv[2]) {
|
||||||
|
error_msg("%s: exactly one argument is expected\n",
|
||||||
|
child->argv[0]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
if (!child->argv[1] || child->argv[2]) {
|
if (sscanf(child->argv[1], "%%%d", &jobNum) != 1) {
|
||||||
error_msg("%s: exactly one argument is expected\n",
|
error_msg("%s: bad argument '%s'\n",
|
||||||
child->argv[0]);
|
child->argv[0], child->argv[1]);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
if (sscanf(child->argv[1], "%%%d", &jobNum) != 1) {
|
|
||||||
error_msg("%s: bad argument '%s'\n",
|
for (job = child->family->job_list->head; job; job = job->next) {
|
||||||
child->argv[0], child->argv[1]);
|
if (job->jobid == jobNum) {
|
||||||
return EXIT_FAILURE;
|
break;
|
||||||
}
|
|
||||||
for (job = child->family->job_list->head; job; job = job->next) {
|
|
||||||
if (job->jobid == jobNum) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!job) {
|
if (!job) {
|
||||||
error_msg("%s: unknown job %d\n",
|
error_msg("%s: unknown job %d\n",
|
||||||
@ -524,7 +525,7 @@ static int builtin_else(struct child_prog *child)
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd->job_context |= ELSE_EXP_CONTEXT;
|
cmd->job_context |= ELSE_EXP_CONTEXT;
|
||||||
debug_printf("job=%p builtin_else set job context to %x\n", child->family, cmd->job_context);
|
debug_printf("job=%p builtin_else set job context to %x\n", cmd, cmd->job_context);
|
||||||
|
|
||||||
/* Now run the 'else' command */
|
/* Now run the 'else' command */
|
||||||
debug_printf( "'else' now running '%s'\n", charptr1);
|
debug_printf( "'else' now running '%s'\n", charptr1);
|
||||||
@ -583,12 +584,13 @@ static int builtin_unset(struct child_prog *child)
|
|||||||
|
|
||||||
#ifdef BB_FEATURE_SH_IF_EXPRESSIONS
|
#ifdef BB_FEATURE_SH_IF_EXPRESSIONS
|
||||||
/* currently used by if/then/else.
|
/* currently used by if/then/else.
|
||||||
* Needlessly (?) forks and reparses the command line.
|
*
|
||||||
* But pseudo_exec on the pre-parsed args doesn't have the
|
* Reparsing the command line for this purpose is gross,
|
||||||
* "fork, stick around until the child exits, and find it's return code"
|
* incorrect, and fundamentally unfixable; in particular,
|
||||||
* functionality. The fork is not needed if the predicate is
|
* think about what happens with command substitution.
|
||||||
* non-forking builtin, and maybe not even if it's a forking builtin.
|
* We really need to pull out the run, wait, return status
|
||||||
* applets pretty clearly need the fork.
|
* functionality out of busy_loop so we can child->argv++
|
||||||
|
* and use that, without going back through parse_command.
|
||||||
*/
|
*/
|
||||||
static int run_command_predicate(char *cmd)
|
static int run_command_predicate(char *cmd)
|
||||||
{
|
{
|
||||||
@ -684,7 +686,9 @@ static void checkjobs(struct jobset *job_list)
|
|||||||
perror("waitpid");
|
perror("waitpid");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int setup_redirects(struct child_prog *prog)
|
/* squirrel != NULL means we squirrel away copies of stdin, stdout,
|
||||||
|
* and stderr if they are redirected. */
|
||||||
|
static int setup_redirects(struct child_prog *prog, int squirrel[])
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int openfd;
|
int openfd;
|
||||||
@ -714,6 +718,9 @@ static int setup_redirects(struct child_prog *prog)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (openfd != redir->fd) {
|
if (openfd != redir->fd) {
|
||||||
|
if (squirrel && redir->fd < 3) {
|
||||||
|
squirrel[redir->fd] = dup(redir->fd);
|
||||||
|
}
|
||||||
dup2(openfd, redir->fd);
|
dup2(openfd, redir->fd);
|
||||||
close(openfd);
|
close(openfd);
|
||||||
}
|
}
|
||||||
@ -722,6 +729,19 @@ static int setup_redirects(struct child_prog *prog)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void restore_redirects(int squirrel[])
|
||||||
|
{
|
||||||
|
int i, fd;
|
||||||
|
for (i=0; i<3; i++) {
|
||||||
|
fd = squirrel[i];
|
||||||
|
if (fd != -1) {
|
||||||
|
/* No error checking. I sure wouldn't know what
|
||||||
|
* to do with an error if I found one! */
|
||||||
|
dup2(fd, i);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int get_command(FILE * source, char *command)
|
static int get_command(FILE * source, char *command)
|
||||||
{
|
{
|
||||||
@ -1304,11 +1324,18 @@ static int pseudo_exec(struct child_prog *child)
|
|||||||
struct BB_applet search_applet, *applet;
|
struct BB_applet search_applet, *applet;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Check if the command matches any of the forking builtins.
|
/* Check if the command matches any of the non-forking builtins.
|
||||||
* XXX It would probably be wise to check for non-forking builtins
|
* Depending on context, this might be redundant. But it's
|
||||||
* here as well, since in some context the non-forking path
|
* easier to waste a few CPU cycles than it is to figure out
|
||||||
* is disabled or bypassed. See comment in run_command.
|
* if this is one of those cases.
|
||||||
*/
|
*/
|
||||||
|
for (x = bltins; x->cmd; x++) {
|
||||||
|
if (strcmp(child->argv[0], x->cmd) == 0 ) {
|
||||||
|
exit(x->function(child));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if the command matches any of the forking builtins. */
|
||||||
for (x = bltins_forking; x->cmd; x++) {
|
for (x = bltins_forking; x->cmd; x++) {
|
||||||
if (strcmp(child->argv[0], x->cmd) == 0) {
|
if (strcmp(child->argv[0], x->cmd) == 0) {
|
||||||
applet_name=x->cmd;
|
applet_name=x->cmd;
|
||||||
@ -1435,15 +1462,22 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2])
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Check if the command matches any non-forking builtins.
|
/* Check if the command matches any non-forking builtins,
|
||||||
* XXX should probably skip this test, and fork anyway, if
|
* but only if this is a simple command.
|
||||||
* there redirects of some kind demand forking to work right.
|
* Non-forking builtins within pipes have to fork anyway,
|
||||||
* pseudo_exec would then need to handle the non-forking command
|
* and are handled in pseudo_exec. "echo foo | read bar"
|
||||||
* in a forked context.
|
* is doomed to failure, and doesn't work on bash, either.
|
||||||
*/
|
*/
|
||||||
for (x = bltins; x->cmd; x++) {
|
if (newjob->num_progs == 1) {
|
||||||
if (strcmp(child->argv[0], x->cmd) == 0 ) {
|
for (x = bltins; x->cmd; x++) {
|
||||||
return(x->function(child));
|
if (strcmp(child->argv[0], x->cmd) == 0 ) {
|
||||||
|
int squirrel[] = {-1, -1, -1};
|
||||||
|
int rcode;
|
||||||
|
setup_redirects(child, squirrel);
|
||||||
|
rcode = x->function(child);
|
||||||
|
restore_redirects(squirrel);
|
||||||
|
return rcode;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1466,7 +1500,7 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2])
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* explicit redirects override pipes */
|
/* explicit redirects override pipes */
|
||||||
setup_redirects(child);
|
setup_redirects(child,NULL);
|
||||||
|
|
||||||
pseudo_exec(child);
|
pseudo_exec(child);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user