aboutsummaryrefslogtreecommitdiff
path: root/shell/hush.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-07-14 19:01:25 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-07-14 19:01:25 +0200
commit889550b36b057fb8ad5c825c7a17e46fde4ebbf6 (patch)
tree6b3f4d9a311035acaa970ac2dfce62c609d4ce55 /shell/hush.c
parent6ac37da4256a248b8b8046d3b356a3b9c4831c97 (diff)
downloadbusybox-w32-889550b36b057fb8ad5c825c7a17e46fde4ebbf6.tar.gz
busybox-w32-889550b36b057fb8ad5c825c7a17e46fde4ebbf6.tar.bz2
busybox-w32-889550b36b057fb8ad5c825c7a17e46fde4ebbf6.zip
hush: make pun_pipe loop clearer; fix "cmd | var=`cmd` | cmd" handling
function old new delta free_strings - 38 +38 pseudo_exec_argv 161 171 +10 free_pipe 227 205 -22 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 1/1 up/down: 48/-22) Total: 26 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell/hush.c')
-rw-r--r--shell/hush.c42
1 files changed, 26 insertions, 16 deletions
diff --git a/shell/hush.c b/shell/hush.c
index 31ca22a2e..8a467be80 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -3713,11 +3713,17 @@ static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save,
3713{ 3713{
3714 char **new_env; 3714 char **new_env;
3715 3715
3716 /* Case when we are here: ... | var=val | ... */ 3716 new_env = expand_assignments(argv, assignment_cnt);
3717 if (!argv[assignment_cnt]) 3717
3718 if (!argv[assignment_cnt]) {
3719 /* Case when we are here: ... | var=val | ...
3720 * (note that we do not exit early, i.e., do not optimize out
3721 * expand_assignments(): think about ... | var=`sleep 1` | ...
3722 */
3723 free_strings(new_env);
3718 _exit(EXIT_SUCCESS); 3724 _exit(EXIT_SUCCESS);
3725 }
3719 3726
3720 new_env = expand_assignments(argv, assignment_cnt);
3721#if BB_MMU 3727#if BB_MMU
3722 set_vars_and_save_old(new_env); 3728 set_vars_and_save_old(new_env);
3723 free(new_env); /* optional */ 3729 free(new_env); /* optional */
@@ -3727,6 +3733,7 @@ static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save,
3727 nommu_save->new_env = new_env; 3733 nommu_save->new_env = new_env;
3728 nommu_save->old_vars = set_vars_and_save_old(new_env); 3734 nommu_save->old_vars = set_vars_and_save_old(new_env);
3729#endif 3735#endif
3736
3730 if (argv_expanded) { 3737 if (argv_expanded) {
3731 argv = argv_expanded; 3738 argv = argv_expanded;
3732 } else { 3739 } else {
@@ -4145,8 +4152,9 @@ static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
4145static NOINLINE int run_pipe(struct pipe *pi) 4152static NOINLINE int run_pipe(struct pipe *pi)
4146{ 4153{
4147 static const char *const null_ptr = NULL; 4154 static const char *const null_ptr = NULL;
4148 int i; 4155
4149 int nextin; 4156 int cmd_no;
4157 int next_infd;
4150 struct command *command; 4158 struct command *command;
4151 char **argv_expanded; 4159 char **argv_expanded;
4152 char **argv; 4160 char **argv;
@@ -4160,7 +4168,7 @@ static NOINLINE int run_pipe(struct pipe *pi)
4160 4168
4161 IF_HUSH_JOB(pi->pgrp = -1;) 4169 IF_HUSH_JOB(pi->pgrp = -1;)
4162 pi->stopped_cmds = 0; 4170 pi->stopped_cmds = 0;
4163 command = &(pi->cmds[0]); 4171 command = &pi->cmds[0];
4164 argv_expanded = NULL; 4172 argv_expanded = NULL;
4165 4173
4166 if (pi->num_cmds != 1 4174 if (pi->num_cmds != 1
@@ -4358,9 +4366,10 @@ static NOINLINE int run_pipe(struct pipe *pi)
4358 4366
4359 /* Going to fork a child per each pipe member */ 4367 /* Going to fork a child per each pipe member */
4360 pi->alive_cmds = 0; 4368 pi->alive_cmds = 0;
4361 nextin = 0; 4369 next_infd = 0;
4362 4370
4363 for (i = 0; i < pi->num_cmds; i++) { 4371 cmd_no = 0;
4372 while (cmd_no < pi->num_cmds) {
4364 struct fd_pair pipefds; 4373 struct fd_pair pipefds;
4365#if !BB_MMU 4374#if !BB_MMU
4366 volatile nommu_save_t nommu_save; 4375 volatile nommu_save_t nommu_save;
@@ -4369,7 +4378,8 @@ static NOINLINE int run_pipe(struct pipe *pi)
4369 nommu_save.argv = NULL; 4378 nommu_save.argv = NULL;
4370 nommu_save.argv_from_re_execing = NULL; 4379 nommu_save.argv_from_re_execing = NULL;
4371#endif 4380#endif
4372 command = &(pi->cmds[i]); 4381 command = &pi->cmds[cmd_no];
4382 cmd_no++;
4373 if (command->argv) { 4383 if (command->argv) {
4374 debug_printf_exec(": pipe member '%s' '%s'...\n", 4384 debug_printf_exec(": pipe member '%s' '%s'...\n",
4375 command->argv[0], command->argv[1]); 4385 command->argv[0], command->argv[1]);
@@ -4380,7 +4390,7 @@ static NOINLINE int run_pipe(struct pipe *pi)
4380 /* pipes are inserted between pairs of commands */ 4390 /* pipes are inserted between pairs of commands */
4381 pipefds.rd = 0; 4391 pipefds.rd = 0;
4382 pipefds.wr = 1; 4392 pipefds.wr = 1;
4383 if ((i + 1) < pi->num_cmds) 4393 if (cmd_no < pi->num_cmds)
4384 xpiped_pair(pipefds); 4394 xpiped_pair(pipefds);
4385 4395
4386 command->pid = BB_MMU ? fork() : vfork(); 4396 command->pid = BB_MMU ? fork() : vfork();
@@ -4413,7 +4423,7 @@ static NOINLINE int run_pipe(struct pipe *pi)
4413 if (open(bb_dev_null, O_RDONLY)) 4423 if (open(bb_dev_null, O_RDONLY))
4414 xopen("/", O_RDONLY); 4424 xopen("/", O_RDONLY);
4415 } else { 4425 } else {
4416 xmove_fd(nextin, 0); 4426 xmove_fd(next_infd, 0);
4417 } 4427 }
4418 xmove_fd(pipefds.wr, 1); 4428 xmove_fd(pipefds.wr, 1);
4419 if (pipefds.rd > 1) 4429 if (pipefds.rd > 1)
@@ -4460,12 +4470,12 @@ static NOINLINE int run_pipe(struct pipe *pi)
4460#endif 4470#endif
4461 } 4471 }
4462 4472
4463 if (i) 4473 if (cmd_no > 1)
4464 close(nextin); 4474 close(next_infd);
4465 if ((i + 1) < pi->num_cmds) 4475 if (cmd_no < pi->num_cmds)
4466 close(pipefds.wr); 4476 close(pipefds.wr);
4467 /* Pass read (output) pipe end to next iteration */ 4477 /* Pass read (output) pipe end to next iteration */
4468 nextin = pipefds.rd; 4478 next_infd = pipefds.rd;
4469 } 4479 }
4470 4480
4471 if (!pi->alive_cmds) { 4481 if (!pi->alive_cmds) {
@@ -7153,7 +7163,7 @@ int hush_main(int argc, char **argv)
7153#endif 7163#endif
7154 case 'n': 7164 case 'n':
7155 case 'x': 7165 case 'x':
7156 if (!set_mode('-', opt)) 7166 if (set_mode('-', opt) == 0) /* no error */
7157 break; 7167 break;
7158 default: 7168 default:
7159#ifndef BB_VER 7169#ifndef BB_VER