-
Notifications
You must be signed in to change notification settings - Fork 80
/
execute_cmd.c
5009 lines (4265 loc) · 134 KB
/
execute_cmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* execute_cmd.c -- Execute a COMMAND structure. */
/* Copyright (C) 1987-2009 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX)
#pragma alloca
#endif /* _AIX && RISC6000 && !__GNUC__ */
#include <stdio.h>
#include "chartypes.h"
#include "bashtypes.h"
#if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
# include <sys/file.h>
#endif
#include "filecntl.h"
#include "posixstat.h"
#include <signal.h>
#ifndef _MINIX
# include <sys/param.h>
#endif
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include "posixtime.h"
#if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE)
# include <sys/resource.h>
#endif
#if defined (HAVE_SYS_TIMES_H) && defined (HAVE_TIMES)
# include <sys/times.h>
#endif
#include <errno.h>
#if !defined (errno)
extern int errno;
#endif
#define NEED_FPURGE_DECL
#include "bashansi.h"
#include "bashintl.h"
#include "memalloc.h"
#include "shell.h"
#include <y.tab.h> /* use <...> so we pick it up from the build directory */
#include "flags.h"
#include "builtins.h"
#include "hashlib.h"
#include "jobs.h"
#include "execute_cmd.h"
#include "findcmd.h"
#include "redir.h"
#include "trap.h"
#include "pathexp.h"
#include "hashcmd.h"
#if defined (COND_COMMAND)
# include "test.h"
#endif
#include "builtins/common.h"
#include "builtins/builtext.h" /* list of builtins */
#include <glob/strmatch.h>
#include <tilde/tilde.h>
#if defined (BUFFERED_INPUT)
# include "input.h"
#endif
#if defined (ALIAS)
# include "alias.h"
#endif
#if defined (HISTORY)
# include "bashhist.h"
#endif
extern int posixly_correct;
extern int expand_aliases;
extern int autocd;
extern int breaking, continuing, loop_level;
extern int parse_and_execute_level, running_trap, sourcelevel;
extern int command_string_index, line_number;
extern int dot_found_in_search;
extern int already_making_children;
extern int tempenv_assign_error;
extern char *the_printed_command, *shell_name;
extern pid_t last_command_subst_pid;
extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
extern char **subshell_argv, **subshell_envp;
extern int subshell_argc;
#if 0
extern char *glob_argv_flags;
#endif
extern int close __P((int));
/* Static functions defined and used in this file. */
static void close_pipes __P((int, int));
static void do_piping __P((int, int));
static void bind_lastarg __P((char *));
static int shell_control_structure __P((enum command_type));
static void cleanup_redirects __P((REDIRECT *));
#if defined (JOB_CONTROL)
static int restore_signal_mask __P((sigset_t *));
#endif
static void async_redirect_stdin __P((void));
static int builtin_status __P((int));
static int execute_for_command __P((FOR_COM *));
#if defined (SELECT_COMMAND)
static int print_index_and_element __P((int, int, WORD_LIST *));
static void indent __P((int, int));
static void print_select_list __P((WORD_LIST *, int, int, int));
static char *select_query __P((WORD_LIST *, int, char *, int));
static int execute_select_command __P((SELECT_COM *));
#endif
#if defined (DPAREN_ARITHMETIC)
static int execute_arith_command __P((ARITH_COM *));
#endif
#if defined (COND_COMMAND)
static int execute_cond_node __P((COND_COM *));
static int execute_cond_command __P((COND_COM *));
#endif
#if defined (COMMAND_TIMING)
static int mkfmt __P((char *, int, int, time_t, int));
static void print_formatted_time __P((FILE *, char *,
time_t, int, time_t, int,
time_t, int, int));
static int time_command __P((COMMAND *, int, int, int, struct fd_bitmap *));
#endif
#if defined (ARITH_FOR_COMMAND)
static intmax_t eval_arith_for_expr __P((WORD_LIST *, int *));
static int execute_arith_for_command __P((ARITH_FOR_COM *));
#endif
static int execute_case_command __P((CASE_COM *));
static int execute_while_command __P((WHILE_COM *));
static int execute_until_command __P((WHILE_COM *));
static int execute_while_or_until __P((WHILE_COM *, int));
static int execute_if_command __P((IF_COM *));
static int execute_null_command __P((REDIRECT *, int, int, int));
static void fix_assignment_words __P((WORD_LIST *));
static int execute_simple_command __P((SIMPLE_COM *, int, int, int, struct fd_bitmap *));
static int execute_builtin __P((sh_builtin_func_t *, WORD_LIST *, int, int));
static int execute_function __P((SHELL_VAR *, WORD_LIST *, int, struct fd_bitmap *, int, int));
static int execute_builtin_or_function __P((WORD_LIST *, sh_builtin_func_t *,
SHELL_VAR *,
REDIRECT *, struct fd_bitmap *, int));
static void execute_subshell_builtin_or_function __P((WORD_LIST *, REDIRECT *,
sh_builtin_func_t *,
SHELL_VAR *,
int, int, int,
struct fd_bitmap *,
int));
static void execute_disk_command __P((WORD_LIST *, REDIRECT *, char *,
int, int, int, struct fd_bitmap *, int));
static char *getinterp __P((char *, int, int *));
static void initialize_subshell __P((void));
static int execute_in_subshell __P((COMMAND *, int, int, int, struct fd_bitmap *));
#if defined (COPROCESS_SUPPORT)
static int execute_coproc __P((COMMAND *, int, int, struct fd_bitmap *));
#endif
static int execute_pipeline __P((COMMAND *, int, int, int, struct fd_bitmap *));
static int execute_connection __P((COMMAND *, int, int, int, struct fd_bitmap *));
static int execute_intern_function __P((WORD_DESC *, COMMAND *));
/* Set to 1 if fd 0 was the subject of redirection to a subshell. Global
so that reader_loop can set it to zero before executing a command. */
int stdin_redir;
/* The name of the command that is currently being executed.
`test' needs this, for example. */
char *this_command_name;
/* The printed representation of the currently-executing command (same as
the_printed_command), except when a trap is being executed. Useful for
a debugger to know where exactly the program is currently executing. */
char *the_printed_command_except_trap;
/* For catching RETURN in a function. */
int return_catch_flag;
int return_catch_value;
procenv_t return_catch;
/* The value returned by the last synchronous command. */
int last_command_exit_value;
/* Whether or not the last command (corresponding to last_command_exit_value)
was terminated by a signal, and, if so, which one. */
int last_command_exit_signal;
/* The list of redirections to perform which will undo the redirections
that I made in the shell. */
REDIRECT *redirection_undo_list = (REDIRECT *)NULL;
/* The list of redirections to perform which will undo the internal
redirections performed by the `exec' builtin. These are redirections
that must be undone even when exec discards redirection_undo_list. */
REDIRECT *exec_redirection_undo_list = (REDIRECT *)NULL;
/* When greater than zero, value is the `level' of builtins we are
currently executing (e.g. `eval echo a' would have it set to 2). */
int executing_builtin = 0;
/* Non-zero if we are executing a command list (a;b;c, etc.) */
int executing_list = 0;
/* Non-zero if failing commands in a command substitution should not exit the
shell even if -e is set. Used to pass the CMD_IGNORE_RETURN flag down to
commands run in command substitutions by parse_and_execute. */
int comsub_ignore_return = 0;
/* Non-zero if we have just forked and are currently running in a subshell
environment. */
int subshell_environment;
/* Count of nested subshells, like SHLVL. Available via $BASH_SUBSHELL */
int subshell_level = 0;
/* Currently-executing shell function. */
SHELL_VAR *this_shell_function;
/* If non-zero, matches in case and [[ ... ]] are case-insensitive */
int match_ignore_case = 0;
struct stat SB; /* used for debugging */
static int special_builtin_failed;
static COMMAND *currently_executing_command;
/* The line number that the currently executing function starts on. */
static int function_line_number;
/* XXX - set to 1 if we're running the DEBUG trap and we want to show the line
number containing the function name. Used by executing_line_number to
report the correct line number. Kind of a hack. */
static int showing_function_line;
static int line_number_for_err_trap;
/* A sort of function nesting level counter */
static int funcnest = 0;
int funcnest_max = 0; /* XXX - for bash-4.2 */
struct fd_bitmap *current_fds_to_close = (struct fd_bitmap *)NULL;
#define FD_BITMAP_DEFAULT_SIZE 32
/* Functions to allocate and deallocate the structures used to pass
information from the shell to its children about file descriptors
to close. */
struct fd_bitmap *
new_fd_bitmap (size)
int size;
{
struct fd_bitmap *ret;
ret = (struct fd_bitmap *)xmalloc (sizeof (struct fd_bitmap));
ret->size = size;
if (size)
{
ret->bitmap = (char *)xmalloc (size);
memset (ret->bitmap, '\0', size);
}
else
ret->bitmap = (char *)NULL;
return (ret);
}
void
dispose_fd_bitmap (fdbp)
struct fd_bitmap *fdbp;
{
FREE (fdbp->bitmap);
free (fdbp);
}
void
close_fd_bitmap (fdbp)
struct fd_bitmap *fdbp;
{
register int i;
if (fdbp)
{
for (i = 0; i < fdbp->size; i++)
if (fdbp->bitmap[i])
{
close (i);
fdbp->bitmap[i] = 0;
}
}
}
/* Return the line number of the currently executing command. */
int
executing_line_number ()
{
if (executing && showing_function_line == 0 &&
(variable_context == 0 || interactive_shell == 0) &&
currently_executing_command)
{
#if defined (COND_COMMAND)
if (currently_executing_command->type == cm_cond)
return currently_executing_command->value.Cond->line;
#endif
#if defined (DPAREN_ARITHMETIC)
else if (currently_executing_command->type == cm_arith)
return currently_executing_command->value.Arith->line;
#endif
#if defined (ARITH_FOR_COMMAND)
else if (currently_executing_command->type == cm_arith_for)
return currently_executing_command->value.ArithFor->line;
#endif
return line_number;
}
else
return line_number;
}
/* Execute the command passed in COMMAND. COMMAND is exactly what
read_command () places into GLOBAL_COMMAND. See "command.h" for the
details of the command structure.
EXECUTION_SUCCESS or EXECUTION_FAILURE are the only possible
return values. Executing a command with nothing in it returns
EXECUTION_SUCCESS. */
int
execute_command (command)
COMMAND *command;
{
struct fd_bitmap *bitmap;
int result;
current_fds_to_close = (struct fd_bitmap *)NULL;
bitmap = new_fd_bitmap (FD_BITMAP_DEFAULT_SIZE);
begin_unwind_frame ("execute-command");
add_unwind_protect (dispose_fd_bitmap, (char *)bitmap);
/* Just do the command, but not asynchronously. */
result = execute_command_internal (command, 0, NO_PIPE, NO_PIPE, bitmap);
dispose_fd_bitmap (bitmap);
discard_unwind_frame ("execute-command");
#if defined (PROCESS_SUBSTITUTION)
/* don't unlink fifos if we're in a shell function; wait until the function
returns. */
if (variable_context == 0)
unlink_fifo_list ();
#endif /* PROCESS_SUBSTITUTION */
QUIT;
return (result);
}
/* Return 1 if TYPE is a shell control structure type. */
static int
shell_control_structure (type)
enum command_type type;
{
switch (type)
{
#if defined (ARITH_FOR_COMMAND)
case cm_arith_for:
#endif
#if defined (SELECT_COMMAND)
case cm_select:
#endif
#if defined (DPAREN_ARITHMETIC)
case cm_arith:
#endif
#if defined (COND_COMMAND)
case cm_cond:
#endif
case cm_case:
case cm_while:
case cm_until:
case cm_if:
case cm_for:
case cm_group:
case cm_function_def:
return (1);
default:
return (0);
}
}
/* A function to use to unwind_protect the redirection undo list
for loops. */
static void
cleanup_redirects (list)
REDIRECT *list;
{
do_redirections (list, RX_ACTIVE);
dispose_redirects (list);
}
#if 0
/* Function to unwind_protect the redirections for functions and builtins. */
static void
cleanup_func_redirects (list)
REDIRECT *list;
{
do_redirections (list, RX_ACTIVE);
}
#endif
void
dispose_exec_redirects ()
{
if (exec_redirection_undo_list)
{
dispose_redirects (exec_redirection_undo_list);
exec_redirection_undo_list = (REDIRECT *)NULL;
}
}
#if defined (JOB_CONTROL)
/* A function to restore the signal mask to its proper value when the shell
is interrupted or errors occur while creating a pipeline. */
static int
restore_signal_mask (set)
sigset_t *set;
{
return (sigprocmask (SIG_SETMASK, set, (sigset_t *)NULL));
}
#endif /* JOB_CONTROL */
#ifdef DEBUG
/* A debugging function that can be called from gdb, for instance. */
void
open_files ()
{
register int i;
int f, fd_table_size;
fd_table_size = getdtablesize ();
fprintf (stderr, "pid %ld open files:", (long)getpid ());
for (i = 3; i < fd_table_size; i++)
{
if ((f = fcntl (i, F_GETFD, 0)) != -1)
fprintf (stderr, " %d (%s)", i, f ? "close" : "open");
}
fprintf (stderr, "\n");
}
#endif
static void
async_redirect_stdin ()
{
int fd;
fd = open ("/dev/null", O_RDONLY);
if (fd > 0)
{
dup2 (fd, 0);
close (fd);
}
else if (fd < 0)
internal_error (_("cannot redirect standard input from /dev/null: %s"), strerror (errno));
}
#define DESCRIBE_PID(pid) do { if (interactive) describe_pid (pid); } while (0)
/* Execute the command passed in COMMAND, perhaps doing it asynchrounously.
COMMAND is exactly what read_command () places into GLOBAL_COMMAND.
ASYNCHROUNOUS, if non-zero, says to do this command in the background.
PIPE_IN and PIPE_OUT are file descriptors saying where input comes
from and where it goes. They can have the value of NO_PIPE, which means
I/O is stdin/stdout.
FDS_TO_CLOSE is a list of file descriptors to close once the child has
been forked. This list often contains the unusable sides of pipes, etc.
EXECUTION_SUCCESS or EXECUTION_FAILURE are the only possible
return values. Executing a command with nothing in it returns
EXECUTION_SUCCESS. */
int
execute_command_internal (command, asynchronous, pipe_in, pipe_out,
fds_to_close)
COMMAND *command;
int asynchronous;
int pipe_in, pipe_out;
struct fd_bitmap *fds_to_close;
{
int exec_result, user_subshell, invert, ignore_return, was_error_trap;
REDIRECT *my_undo_list, *exec_undo_list;
volatile int last_pid;
volatile int save_line_number;
#if 0
if (command == 0 || breaking || continuing || read_but_dont_execute)
return (EXECUTION_SUCCESS);
#else
if (breaking || continuing)
return (last_command_exit_value);
if (command == 0 || read_but_dont_execute)
return (EXECUTION_SUCCESS);
#endif
QUIT;
run_pending_traps ();
#if 0
if (running_trap == 0)
#endif
currently_executing_command = command;
invert = (command->flags & CMD_INVERT_RETURN) != 0;
/* If we're inverting the return value and `set -e' has been executed,
we don't want a failing command to inadvertently cause the shell
to exit. */
if (exit_immediately_on_error && invert) /* XXX */
command->flags |= CMD_IGNORE_RETURN; /* XXX */
exec_result = EXECUTION_SUCCESS;
/* If a command was being explicitly run in a subshell, or if it is
a shell control-structure, and it has a pipe, then we do the command
in a subshell. */
if (command->type == cm_subshell && (command->flags & CMD_NO_FORK))
return (execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close));
#if defined (COPROCESS_SUPPORT)
if (command->type == cm_coproc)
return (execute_coproc (command, pipe_in, pipe_out, fds_to_close));
#endif
user_subshell = command->type == cm_subshell || ((command->flags & CMD_WANT_SUBSHELL) != 0);
if (command->type == cm_subshell ||
(command->flags & (CMD_WANT_SUBSHELL|CMD_FORCE_SUBSHELL)) ||
(shell_control_structure (command->type) &&
(pipe_out != NO_PIPE || pipe_in != NO_PIPE || asynchronous)))
{
pid_t paren_pid;
/* Fork a subshell, turn off the subshell bit, turn off job
control and call execute_command () on the command again. */
line_number_for_err_trap = line_number;
paren_pid = make_child (savestring (make_command_string (command)),
asynchronous);
if (paren_pid == 0)
exit (execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close));
/* NOTREACHED */
else
{
close_pipes (pipe_in, pipe_out);
#if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD)
unlink_fifo_list ();
#endif
/* If we are part of a pipeline, and not the end of the pipeline,
then we should simply return and let the last command in the
pipe be waited for. If we are not in a pipeline, or are the
last command in the pipeline, then we wait for the subshell
and return its exit status as usual. */
if (pipe_out != NO_PIPE)
return (EXECUTION_SUCCESS);
stop_pipeline (asynchronous, (COMMAND *)NULL);
if (asynchronous == 0)
{
was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0;
invert = (command->flags & CMD_INVERT_RETURN) != 0;
ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0;
last_command_exit_value = wait_for (paren_pid);
/* If we have to, invert the return value. */
if (invert)
exec_result = ((last_command_exit_value == EXECUTION_SUCCESS)
? EXECUTION_FAILURE
: EXECUTION_SUCCESS);
else
exec_result = last_command_exit_value;
if (user_subshell && was_error_trap && ignore_return == 0 && invert == 0 && exec_result != EXECUTION_SUCCESS)
{
last_command_exit_value = exec_result;
save_line_number = line_number;
line_number = line_number_for_err_trap;
run_error_trap ();
line_number = save_line_number;
}
if (user_subshell && ignore_return == 0 && invert == 0 && exit_immediately_on_error && exec_result != EXECUTION_SUCCESS)
{
last_command_exit_value = exec_result;
run_pending_traps ();
jump_to_top_level (ERREXIT);
}
return (last_command_exit_value = exec_result);
}
else
{
DESCRIBE_PID (paren_pid);
run_pending_traps ();
return (EXECUTION_SUCCESS);
}
}
}
#if defined (COMMAND_TIMING)
if (command->flags & CMD_TIME_PIPELINE)
{
if (asynchronous)
{
command->flags |= CMD_FORCE_SUBSHELL;
exec_result = execute_command_internal (command, 1, pipe_in, pipe_out, fds_to_close);
}
else
{
exec_result = time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close);
#if 0
if (running_trap == 0)
#endif
currently_executing_command = (COMMAND *)NULL;
}
return (exec_result);
}
#endif /* COMMAND_TIMING */
if (shell_control_structure (command->type) && command->redirects)
stdin_redir = stdin_redirects (command->redirects);
/* Handle WHILE FOR CASE etc. with redirections. (Also '&' input
redirection.) */
if (do_redirections (command->redirects, RX_ACTIVE|RX_UNDOABLE) != 0)
{
cleanup_redirects (redirection_undo_list);
redirection_undo_list = (REDIRECT *)NULL;
dispose_exec_redirects ();
return (last_command_exit_value = EXECUTION_FAILURE);
}
if (redirection_undo_list)
{
my_undo_list = (REDIRECT *)copy_redirects (redirection_undo_list);
dispose_redirects (redirection_undo_list);
redirection_undo_list = (REDIRECT *)NULL;
}
else
my_undo_list = (REDIRECT *)NULL;
if (exec_redirection_undo_list)
{
exec_undo_list = (REDIRECT *)copy_redirects (exec_redirection_undo_list);
dispose_redirects (exec_redirection_undo_list);
exec_redirection_undo_list = (REDIRECT *)NULL;
}
else
exec_undo_list = (REDIRECT *)NULL;
if (my_undo_list || exec_undo_list)
begin_unwind_frame ("loop_redirections");
if (my_undo_list)
add_unwind_protect ((Function *)cleanup_redirects, my_undo_list);
if (exec_undo_list)
add_unwind_protect ((Function *)dispose_redirects, exec_undo_list);
ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0;
QUIT;
switch (command->type)
{
case cm_simple:
{
save_line_number = line_number;
/* We can't rely on variables retaining their values across a
call to execute_simple_command if a longjmp occurs as the
result of a `return' builtin. This is true for sure with gcc. */
#if defined (RECYCLES_PIDS)
last_made_pid = NO_PID;
#endif
last_pid = last_made_pid;
was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0;
if (ignore_return && command->value.Simple)
command->value.Simple->flags |= CMD_IGNORE_RETURN;
if (command->flags & CMD_STDIN_REDIR)
command->value.Simple->flags |= CMD_STDIN_REDIR;
line_number_for_err_trap = line_number = command->value.Simple->line;
exec_result =
execute_simple_command (command->value.Simple, pipe_in, pipe_out,
asynchronous, fds_to_close);
line_number = save_line_number;
/* The temporary environment should be used for only the simple
command immediately following its definition. */
dispose_used_env_vars ();
#if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
/* Reclaim memory allocated with alloca () on machines which
may be using the alloca emulation code. */
(void) alloca (0);
#endif /* (ultrix && mips) || C_ALLOCA */
/* If we forked to do the command, then we must wait_for ()
the child. */
/* XXX - this is something to watch out for if there are problems
when the shell is compiled without job control. */
if (already_making_children && pipe_out == NO_PIPE &&
last_made_pid != last_pid)
{
stop_pipeline (asynchronous, (COMMAND *)NULL);
if (asynchronous)
{
DESCRIBE_PID (last_made_pid);
}
else
#if !defined (JOB_CONTROL)
/* Do not wait for asynchronous processes started from
startup files. */
if (last_made_pid != last_asynchronous_pid)
#endif
/* When executing a shell function that executes other
commands, this causes the last simple command in
the function to be waited for twice. This also causes
subshells forked to execute builtin commands (e.g., in
pipelines) to be waited for twice. */
exec_result = wait_for (last_made_pid);
}
}
/* 2009/02/13 -- pipeline failure is processed elsewhere. This handles
only the failure of a simple command. */
if (was_error_trap && ignore_return == 0 && invert == 0 && pipe_in == NO_PIPE && pipe_out == NO_PIPE && exec_result != EXECUTION_SUCCESS)
{
last_command_exit_value = exec_result;
line_number = line_number_for_err_trap;
run_error_trap ();
line_number = save_line_number;
}
if (ignore_return == 0 && invert == 0 &&
((posixly_correct && interactive == 0 && special_builtin_failed) ||
(exit_immediately_on_error && pipe_in == NO_PIPE && pipe_out == NO_PIPE && exec_result != EXECUTION_SUCCESS)))
{
last_command_exit_value = exec_result;
run_pending_traps ();
jump_to_top_level (ERREXIT);
}
break;
case cm_for:
if (ignore_return)
command->value.For->flags |= CMD_IGNORE_RETURN;
exec_result = execute_for_command (command->value.For);
break;
#if defined (ARITH_FOR_COMMAND)
case cm_arith_for:
if (ignore_return)
command->value.ArithFor->flags |= CMD_IGNORE_RETURN;
exec_result = execute_arith_for_command (command->value.ArithFor);
break;
#endif
#if defined (SELECT_COMMAND)
case cm_select:
if (ignore_return)
command->value.Select->flags |= CMD_IGNORE_RETURN;
exec_result = execute_select_command (command->value.Select);
break;
#endif
case cm_case:
if (ignore_return)
command->value.Case->flags |= CMD_IGNORE_RETURN;
exec_result = execute_case_command (command->value.Case);
break;
case cm_while:
if (ignore_return)
command->value.While->flags |= CMD_IGNORE_RETURN;
exec_result = execute_while_command (command->value.While);
break;
case cm_until:
if (ignore_return)
command->value.While->flags |= CMD_IGNORE_RETURN;
exec_result = execute_until_command (command->value.While);
break;
case cm_if:
if (ignore_return)
command->value.If->flags |= CMD_IGNORE_RETURN;
exec_result = execute_if_command (command->value.If);
break;
case cm_group:
/* This code can be executed from either of two paths: an explicit
'{}' command, or via a function call. If we are executed via a
function call, we have already taken care of the function being
executed in the background (down there in execute_simple_command ()),
and this command should *not* be marked as asynchronous. If we
are executing a regular '{}' group command, and asynchronous == 1,
we must want to execute the whole command in the background, so we
need a subshell, and we want the stuff executed in that subshell
(this group command) to be executed in the foreground of that
subshell (i.e. there will not be *another* subshell forked).
What we do is to force a subshell if asynchronous, and then call
execute_command_internal again with asynchronous still set to 1,
but with the original group command, so the printed command will
look right.
The code above that handles forking off subshells will note that
both subshell and async are on, and turn off async in the child
after forking the subshell (but leave async set in the parent, so
the normal call to describe_pid is made). This turning off
async is *crucial*; if it is not done, this will fall into an
infinite loop of executions through this spot in subshell after
subshell until the process limit is exhausted. */
if (asynchronous)
{
command->flags |= CMD_FORCE_SUBSHELL;
exec_result =
execute_command_internal (command, 1, pipe_in, pipe_out,
fds_to_close);
}
else
{
if (ignore_return && command->value.Group->command)
command->value.Group->command->flags |= CMD_IGNORE_RETURN;
exec_result =
execute_command_internal (command->value.Group->command,
asynchronous, pipe_in, pipe_out,
fds_to_close);
}
break;
case cm_connection:
exec_result = execute_connection (command, asynchronous,
pipe_in, pipe_out, fds_to_close);
break;
#if defined (DPAREN_ARITHMETIC)
case cm_arith:
was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0;
if (ignore_return)
command->value.Arith->flags |= CMD_IGNORE_RETURN;
line_number_for_err_trap = save_line_number = line_number;
exec_result = execute_arith_command (command->value.Arith);
line_number = save_line_number;
if (was_error_trap && ignore_return == 0 && invert == 0 && exec_result != EXECUTION_SUCCESS)
{
last_command_exit_value = exec_result;
save_line_number = line_number;
line_number = line_number_for_err_trap;
run_error_trap ();
line_number = save_line_number;
}
if (ignore_return == 0 && invert == 0 && exit_immediately_on_error && exec_result != EXECUTION_SUCCESS)
{
last_command_exit_value = exec_result;
run_pending_traps ();
jump_to_top_level (ERREXIT);
}
break;
#endif
#if defined (COND_COMMAND)
case cm_cond:
was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0;
if (ignore_return)
command->value.Cond->flags |= CMD_IGNORE_RETURN;
line_number_for_err_trap = save_line_number = line_number;
exec_result = execute_cond_command (command->value.Cond);
line_number = save_line_number;
if (was_error_trap && ignore_return == 0 && invert == 0 && exec_result != EXECUTION_SUCCESS)
{
last_command_exit_value = exec_result;
save_line_number = line_number;
line_number = line_number_for_err_trap;
run_error_trap ();
line_number = save_line_number;
}
if (ignore_return == 0 && invert == 0 && exit_immediately_on_error && exec_result != EXECUTION_SUCCESS)
{
last_command_exit_value = exec_result;
run_pending_traps ();
jump_to_top_level (ERREXIT);
}
break;
#endif
case cm_function_def:
exec_result = execute_intern_function (command->value.Function_def->name,
command->value.Function_def->command);
break;
default:
command_error ("execute_command", CMDERR_BADTYPE, command->type, 0);
}
if (my_undo_list)
{
do_redirections (my_undo_list, RX_ACTIVE);
dispose_redirects (my_undo_list);
}
if (exec_undo_list)
dispose_redirects (exec_undo_list);
if (my_undo_list || exec_undo_list)
discard_unwind_frame ("loop_redirections");
/* Invert the return value if we have to */
if (invert)
exec_result = (exec_result == EXECUTION_SUCCESS)
? EXECUTION_FAILURE
: EXECUTION_SUCCESS;
#if defined (DPAREN_ARITHMETIC) || defined (COND_COMMAND)
/* This is where we set PIPESTATUS from the exit status of the appropriate
compound commands (the ones that look enough like simple commands to
cause confusion). We might be able to optimize by not doing this if
subshell_environment != 0. */
switch (command->type)
{
# if defined (DPAREN_ARITHMETIC)
case cm_arith:
# endif
# if defined (COND_COMMAND)
case cm_cond:
# endif
set_pipestatus_from_exit (exec_result);
break;
}
#endif
last_command_exit_value = exec_result;
run_pending_traps ();
#if 0
if (running_trap == 0)
#endif
currently_executing_command = (COMMAND *)NULL;
return (last_command_exit_value);
}
#if defined (COMMAND_TIMING)