forked from bminor/bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_cmd.cc
5558 lines (4697 loc) · 165 KB
/
execute_cmd.cc
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.cc -- Execute a COMMAND structure. */
/* Copyright (C) 1987-2022 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"
#include "shell.hh"
#if defined(HAVE_SYS_FILE_H)
#include <sys/file.h>
#endif
#include <fcntl.h>
#include <fnmatch.h>
namespace bash
{
/* Static functions defined and used in this file. */
#if defined(COMMAND_TIMING)
static void mkfmt (char *, int, bool, time_t, int);
#endif
static inline int builtin_status (int);
static inline char *getinterp (const char *, ssize_t, int *);
static inline void coproc_close (Coproc *);
/* A convenience macro to avoid resetting line_number_for_err_trap while
running the ERR trap. */
#define SET_LINE_NUMBER(v) \
do \
{ \
line_number = v; \
if (!signal_in_progress (ERROR_TRAP) \
&& running_trap != (ERROR_TRAP + 1)) \
line_number_for_err_trap = line_number; \
} \
while (0)
/* This can't be in executing_line_number() because that's used for LINENO
and we want LINENO to reflect the line number of commands run during
the ERR trap. Right now this is only used to push to BASH_LINENO. */
#define GET_LINE_NUMBER() \
(signal_in_progress (ERROR_TRAP) && running_trap == ERROR_TRAP + 1) \
? line_number_for_err_trap \
: executing_line_number ()
/* Return the line number of the currently executing command. */
int
Shell::executing_line_number ()
{
if (executing && !showing_function_line
&& (variable_context == 0 || !interactive_shell)
&& currently_executing_command)
{
// XXX - should we only return this for some types of commands?
return currently_executing_command->line;
}
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
Shell::execute_command (COMMAND *command)
{
current_fds_to_close = nullptr;
fd_bitmap bitmap;
/* Just do the command, but not asynchronously. */
int result
= execute_command_internal (command, false, NO_PIPE, NO_PIPE, &bitmap);
#if defined(PROCESS_SUBSTITUTION)
/* don't unlink fifos if we're in a shell function; wait until the function
returns. */
if (variable_context == 0 && executing_list == 0)
unlink_fifo_list ();
#endif /* PROCESS_SUBSTITUTION */
QUIT;
return result;
}
/* Return true if TYPE is a shell control structure type. */
// Most command types are control structures, so the default is true.
bool
COMMAND::control_structure ()
{
return true;
}
// Simple command isn't a control structure.
bool
SIMPLE_COM::control_structure ()
{
return false;
}
// Subshell command isn't a control structure.
bool
SUBSHELL_COM::control_structure ()
{
return false;
}
// Coprocess command isn't a control structure.
bool
COPROC_COM::control_structure ()
{
return false;
}
#if 0
void
undo_partial_redirects ()
{
if (redirection_undo_list)
{
cleanup_redirects (redirection_undo_list);
redirection_undo_list = nullptr;
}
}
/* Function to unwind_protect the redirections for functions and builtins. */
static void
cleanup_func_redirects (REDIRECT *list)
{
do_redirections (list, RX_ACTIVE);
}
void
dispose_exec_redirects ()
{
if (exec_redirection_undo_list)
{
dispose_redirects (exec_redirection_undo_list);
exec_redirection_undo_list = nullptr;
}
}
void
dispose_partial_redirects ()
{
if (redirection_undo_list)
{
dispose_redirects (redirection_undo_list);
redirection_undo_list = nullptr;
}
}
#endif
#if defined(JOB_CONTROL)
#if 0
/* A function to restore the signal mask to its proper value when the shell
is interrupted or errors occur while creating a pipeline. */
static void
restore_signal_mask (void *set)
{
sigprocmask (SIG_SETMASK, (sigset_t *)set, nullptr);
}
#endif
#endif /* JOB_CONTROL */
#if defined(DEBUG) && 0
/* A debugging function that can be called from gdb, for instance. */
void
open_files ()
{
int fd_table_size = getdtablesize ();
fprintf (stderr, "pid %ld open files:", static_cast<long> (getpid ()));
for (int i = 3; i < fd_table_size; i++)
{
int f;
if ((f = fcntl (i, F_GETFD, 0)) != -1)
fprintf (stderr, " %d (%s)", i, f ? "close" : "open");
}
fprintf (stderr, "\n");
}
#endif
void
Shell::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 asynchronously.
COMMAND is exactly what read_command () places into GLOBAL_COMMAND.
ASYNCHRONOUS, 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
Shell::execute_command_internal (COMMAND *command, bool asynchronous,
int pipe_in, int pipe_out,
fd_bitmap *fds_to_close)
{
#if defined(PROCESS_SUBSTITUTION)
fifo_vector ofifo_list;
bool saved_fifo;
#endif
if (breaking || continuing || read_but_dont_execute)
return last_command_exit_value;
if (command == nullptr)
return EXECUTION_SUCCESS;
QUIT;
run_pending_traps ();
currently_executing_command = command;
bool 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 */
bool is_subshell = (command->type == cm_subshell);
/* 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 (is_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 last_command_exit_value
= execute_coproc (static_cast<COPROC_COM *> (command), pipe_in,
pipe_out, fds_to_close);
#endif
bool user_subshell
= is_subshell || ((command->flags & CMD_WANT_SUBSHELL) != 0);
#if defined(TIME_BEFORE_SUBSHELL)
if ((command->flags & CMD_TIME_PIPELINE) && user_subshell && !asynchronous)
{
command->flags |= CMD_FORCE_SUBSHELL;
int exec_result = time_command (command, asynchronous, pipe_in, pipe_out,
fds_to_close);
currently_executing_command = nullptr;
return exec_result;
}
#endif
if (is_subshell
|| (command->flags & (CMD_WANT_SUBSHELL | CMD_FORCE_SUBSHELL))
|| (command->control_structure ()
&& (pipe_out != NO_PIPE || pipe_in != NO_PIPE || asynchronous)))
{
/* Fork a subshell, turn off the subshell bit, turn off job
control and call execute_command () on the command again. */
int save_line_number = line_number;
if (is_subshell)
SET_LINE_NUMBER (command->line); /* XXX - save value? */
/* Otherwise we defer setting line_number */
string tcmd (make_command_string (command));
make_child_flags fork_flags = asynchronous ? FORK_ASYNC : FORK_SYNC;
pid_t paren_pid = make_child (tcmd, fork_flags);
if (user_subshell && signal_is_trapped (ERROR_TRAP)
&& !signal_in_progress (DEBUG_TRAP) && running_trap == 0)
{
the_printed_command_except_trap = the_printed_command;
}
if (paren_pid == 0)
{
/* We want to run the exit trap for forced {} subshells, and we
want to note this before execute_in_subshell modifies the
COMMAND struct. Need to keep in mind that execute_in_subshell
runs the exit trap for () subshells itself. */
/* This handles { command; } & */
bool s = !user_subshell && command->type == cm_group
&& pipe_in == NO_PIPE && pipe_out == NO_PIPE
&& asynchronous;
/* run exit trap for : | { ...; } and { ...; } | : */
/* run exit trap for : | ( ...; ) and ( ...; ) | : */
s |= !user_subshell && command->type == cm_group
&& (pipe_in != NO_PIPE || pipe_out != NO_PIPE) && !asynchronous;
last_command_exit_value = execute_in_subshell (
command, asynchronous, pipe_in, pipe_out, fds_to_close);
if (s)
subshell_exit (last_command_exit_value);
else
sh_exit (last_command_exit_value);
/* NOTREACHED */
}
else
{
close_pipes (pipe_in, pipe_out);
#if defined(PROCESS_SUBSTITUTION) && defined(HAVE_DEV_FD)
if (variable_context == 0) /* wait until shell function completes */
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, nullptr);
line_number = save_line_number;
if (!asynchronous)
{
bool was_error_trap = signal_is_trapped (ERROR_TRAP)
&& signal_is_ignored (ERROR_TRAP) == 0;
invert = (command->flags & CMD_INVERT_RETURN)
!= 0; /* XXX - reload flag? */
bool ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0;
int exec_result = wait_for (paren_pid, 0);
/* If we have to, invert the return value. */
if (invert)
exec_result
= ((exec_result == EXECUTION_SUCCESS) ? EXECUTION_FAILURE
: EXECUTION_SUCCESS);
last_command_exit_value = exec_result;
if (user_subshell && was_error_trap && !ignore_return && !invert
&& exec_result != EXECUTION_SUCCESS)
{
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 && !invert
&& exit_immediately_on_error
&& exec_result != EXECUTION_SUCCESS)
{
run_pending_traps ();
throw bash_exception (ERREXIT);
}
return last_command_exit_value;
}
else
{
DESCRIBE_PID (paren_pid);
run_pending_traps ();
/* Posix 2013 2.9.3.1: "the exit status of an asynchronous list
shall be zero." */
last_command_exit_value = 0;
return EXECUTION_SUCCESS;
}
}
}
#if defined(COMMAND_TIMING)
if (command->flags & CMD_TIME_PIPELINE)
{
int exec_result;
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 = nullptr;
}
return exec_result;
}
#endif /* COMMAND_TIMING */
if (command->control_structure () && command->redirects)
stdin_redir = stdin_redirects (command->redirects);
#if defined(PROCESS_SUBSTITUTION)
#if !defined(HAVE_DEV_FD)
reap_procsubs ();
#endif
/* XXX - also if sourcelevel != 0? */
if (variable_context != 0 || executing_list)
{
ofifo_list = copy_fifo_list ();
saved_fifo = true;
}
else
saved_fifo = false;
#endif
/* Handle WHILE FOR CASE etc. with redirections. (Also '&' input
redirection.) */
bool was_error_trap
= signal_is_trapped (ERROR_TRAP) && !signal_is_ignored (ERROR_TRAP);
bool ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0;
if (do_redirections (command->redirects, RX_ACTIVE | RX_UNDOABLE) != 0)
{
undo_partial_redirects ();
dispose_exec_redirects ();
/* Handle redirection error as command failure if errexit set. */
last_command_exit_value = EXECUTION_FAILURE;
if (!ignore_return && !invert && pipe_in == NO_PIPE
&& pipe_out == NO_PIPE)
{
if (was_error_trap)
{
int save_line_number = line_number;
line_number = line_number_for_err_trap;
run_error_trap ();
line_number = save_line_number;
}
if (exit_immediately_on_error)
{
run_pending_traps ();
throw bash_exception (ERREXIT);
}
}
return last_command_exit_value;
}
QUIT;
REDIRECT *my_undo_list = redirection_undo_list;
redirection_undo_list = nullptr;
REDIRECT *exec_undo_list = exec_redirection_undo_list;
exec_redirection_undo_list = nullptr;
int exec_result = EXECUTION_SUCCESS;
int save_line_number;
try
{
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 throw occurs as the
result of a `return' builtin.
*/
#if defined(RECYCLES_PIDS)
last_made_pid = NO_PID;
#endif
was_error_trap = signal_is_trapped (ERROR_TRAP)
&& !signal_is_ignored (ERROR_TRAP);
if (ignore_return)
command->flags |= CMD_IGNORE_RETURN;
SIMPLE_COM *simple_com = static_cast<SIMPLE_COM *> (command);
SET_LINE_NUMBER (command->line);
exec_result = execute_simple_command (
simple_com, 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 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. Don't worry
about whether or not last_made_pid == last_pid;
already_making_children tells us whether or not there are
unwaited-for children to wait for and reap. */
if (already_making_children && pipe_out == NO_PIPE)
{
stop_pipeline (asynchronous, nullptr);
if (asynchronous)
{
DESCRIBE_PID (last_made_pid);
exec_result = EXECUTION_SUCCESS;
invert = false; /* async commands always succeed */
}
else
#if !defined(JOB_CONTROL)
/* Do not wait for asynchronous processes started from
startup files. */
if (last_made_pid != NO_PID
&& last_made_pid != last_asynchronous_pid)
#else
if (last_made_pid != NO_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, 0);
}
}
/* 2009/02/13 -- pipeline failure is processed elsewhere. This
handles only the failure of a simple command. We don't want to run
the error trap if the command run by the `command' builtin fails;
we want to defer that until the command builtin itself returns
failure. */
/* 2020/07/14 -- this changes with how the command builtin is handled
*/
if (was_error_trap && !ignore_return && !invert && pipe_in == NO_PIPE
&& pipe_out == NO_PIPE
&& (command->flags & CMD_COMMAND_BUILTIN) == 0
&& 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 && !invert
&& ((posixly_correct && !interactive && 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 ();
/* The catch block will undo the redirections and rethrow. */
throw bash_exception (ERREXIT);
}
break;
case cm_for:
if (ignore_return)
command->flags |= CMD_IGNORE_RETURN;
exec_result
= execute_for_command (static_cast<FOR_SELECT_COM *> (command));
break;
#if defined(ARITH_FOR_COMMAND)
case cm_arith_for:
if (ignore_return)
command->flags |= CMD_IGNORE_RETURN;
exec_result = execute_arith_for_command (
static_cast<ARITH_FOR_COM *> (command));
break;
#endif
#if defined(SELECT_COMMAND)
case cm_select:
if (ignore_return)
command->flags |= CMD_IGNORE_RETURN;
exec_result = execute_select_command (
static_cast<FOR_SELECT_COM *> (command));
break;
#endif
case cm_case:
if (ignore_return)
command->flags |= CMD_IGNORE_RETURN;
exec_result
= execute_case_command (static_cast<CASE_COM *> (command));
break;
case cm_while:
case cm_until:
if (ignore_return)
command->flags |= CMD_IGNORE_RETURN;
exec_result = execute_while_or_until (
static_cast<UNTIL_WHILE_COM *> (command));
break;
case cm_if:
if (ignore_return)
command->flags |= CMD_IGNORE_RETURN;
exec_result = execute_if_command (static_cast<IF_COM *> (command));
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, true, pipe_in,
pipe_out, fds_to_close);
}
else
{
GROUP_COM *group_com = static_cast<GROUP_COM *> (command);
if (ignore_return && group_com->command)
group_com->command->flags |= CMD_IGNORE_RETURN;
exec_result
= execute_command_internal (group_com->command, asynchronous,
pipe_in, pipe_out, fds_to_close);
}
break;
case cm_connection:
exec_result = execute_connection (
static_cast<CONNECTION *> (command), asynchronous, pipe_in,
pipe_out, fds_to_close);
if (asynchronous)
invert = false; /* XXX */
break;
#if defined(DPAREN_ARITHMETIC)
case cm_arith:
#endif
#if defined(COND_COMMAND)
case cm_cond:
#endif
case cm_function_def:
was_error_trap = signal_is_trapped (ERROR_TRAP)
&& !signal_is_ignored (ERROR_TRAP);
#if defined(DPAREN_ARITHMETIC)
if (ignore_return && command->type == cm_arith)
command->flags |= CMD_IGNORE_RETURN;
#endif
#if defined(COND_COMMAND)
if (ignore_return && command->type == cm_cond)
command->flags |= CMD_IGNORE_RETURN;
#endif
line_number_for_err_trap = save_line_number = line_number; // XXX
#if defined(DPAREN_ARITHMETIC)
if (command->type == cm_arith)
exec_result
= execute_arith_command (static_cast<ARITH_COM *> (command));
else
#endif
#if defined(COND_COMMAND)
if (command->type == cm_cond)
exec_result
= execute_cond_command (static_cast<COND_COM *> (command));
else
#endif
if (command->type == cm_function_def)
{
FUNCTION_DEF *fndef = static_cast<FUNCTION_DEF *> (command);
exec_result = execute_intern_function (fndef->name, fndef);
}
line_number = save_line_number;
if (was_error_trap && !ignore_return && !invert
&& 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 && !invert && exit_immediately_on_error
&& exec_result != EXECUTION_SUCCESS)
{
last_command_exit_value = exec_result;
run_pending_traps ();
throw bash_exception (ERREXIT);
}
break;
case cm_subshell:
case cm_coproc:
default:
command_error ("execute_command", CMDERR_BADTYPE, command->type);
}
}
catch (const std::exception &)
{
if (my_undo_list)
cleanup_redirects (my_undo_list);
if (exec_undo_list)
delete exec_undo_list;
throw;
}
if (my_undo_list)
cleanup_redirects (my_undo_list);
if (exec_undo_list)
delete exec_undo_list;
#if defined(PROCESS_SUBSTITUTION)
if (saved_fifo)
close_new_fifos (ofifo_list);
#endif
/* 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)
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wswitch-enum"
#endif
/* 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;
default:
break;
}
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#endif
last_command_exit_value = exec_result;
run_pending_traps ();
currently_executing_command = nullptr;
return last_command_exit_value;
}
#if defined(COMMAND_TIMING)
#if defined(HAVE_GETRUSAGE) && defined(HAVE_GETTIMEOFDAY)
extern struct timeval *difftimeval (struct timeval *, struct timeval *,
struct timeval *);
extern struct timeval *addtimeval (struct timeval *, struct timeval *,
struct timeval *);
extern int timeval_to_cpu (struct timeval *, struct timeval *,
struct timeval *);
#endif
#define POSIX_TIMEFORMAT "real %2R\nuser %2U\nsys %2S"
#define BASH_TIMEFORMAT "\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS"
static const int precs[] = { 0, 100, 10, 1 };
/* Expand one `%'-prefixed escape sequence from a time format string. */
static void
mkfmt (char *buf, int prec, bool lng, time_t sec, int sec_fraction)
{
time_t min;
char abuf[INT_STRLEN_BOUND (time_t) + 1];
int ind, aind;
ind = 0;
abuf[sizeof (abuf) - 1] = '\0';
/* If LNG is non-zero, we want to decompose SEC into minutes and seconds. */
if (lng)
{
min = sec / 60;
sec %= 60;
aind = sizeof (abuf) - 2;
do
abuf[aind--] = (min % 10) + '0';
while (min /= 10);
aind++;
while (abuf[aind])
buf[ind++] = abuf[aind++];
buf[ind++] = 'm';
}
/* Now add the seconds. */
aind = sizeof (abuf) - 2;
do
abuf[aind--] = (sec % 10) + '0';
while (sec /= 10);
aind++;
while (abuf[aind])
buf[ind++] = abuf[aind++];
/* We want to add a decimal point and PREC places after it if PREC is
nonzero. PREC is not greater than 3. SEC_FRACTION is between 0
and 999. */
if (prec != 0)
{
buf[ind++] = locale_decpoint ();
for (aind = 1; aind <= prec; aind++)
{
buf[ind++] = static_cast<char> (sec_fraction / precs[aind]) + '0';
sec_fraction %= precs[aind];
}
}
if (lng)
buf[ind++] = 's';
buf[ind] = '\0';
}
/* Interpret the format string FORMAT, interpolating the following escape
sequences:
%[prec][l][RUS]
where the optional `prec' is a precision, meaning the number of
characters after the decimal point, the optional `l' means to format
using minutes and seconds (MMmNN[.FF]s), like the `times' builtin',
and the last character is one of
R number of seconds of `real' time
U number of seconds of `user' time
S number of seconds of `system' time
An occurrence of `%%' in the format string is translated to a `%'. The
result is printed to FP, a pointer to a FILE. The other variables are
the seconds and thousandths of a second of real, user, and system time,
resectively. */
void
Shell::print_formatted_time (FILE *fp, const char *format, time_t rs, int rsf,
time_t us, int usf, time_t ss, int ssf, int cpu)
{
char ts[INT_STRLEN_BOUND (time_t) + sizeof ("mSS.FFFF")];
time_t sum;
int sum_frac;
size_t len = strlen (format);
size_t ssize = (len + 64) - (len % 64);
string str;
str.reserve (ssize);
for (const char *s = format; *s; s++)
{
if (*s != '%' || s[1] == '\0')
str.push_back (*s);
else if (s[1] == '%')
{
s++;
str.push_back (*s);
}
else if (s[1] == 'P')
{
s++;
#if 0
/* clamp CPU usage at 100% */
if (cpu > 10000)
cpu = 10000;
#endif
sum = cpu / 100;
sum_frac = (cpu % 100) * 10;
mkfmt (ts, 2, 0, sum, sum_frac);
str += ts;
}
else
{
int prec = 3; // default is three places past the decimal point.
bool lng = false; // default is to not use minutes or append `s'
s++;
if (c_isdigit (*s)) /* `precision' */
{
prec = *s++ - '0';
if (prec > 3)
prec = 3;
}
if (*s == 'l') /* `length extender' */
{
lng = true;
s++;
}
if (*s == 'R' || *s == 'E')
mkfmt (ts, prec, lng, rs, rsf);
else if (*s == 'U')
mkfmt (ts, prec, lng, us, usf);
else if (*s == 'S')
mkfmt (ts, prec, lng, ss, ssf);
else
{
internal_error (_ ("TIMEFORMAT: `%c': invalid format character"),
*s);
return;
}
str += ts;
}
}
fprintf (fp, "%s\n", str.c_str ());
fflush (fp);
}
int
Shell::time_command (COMMAND *command, bool asynchronous, int pipe_in,
int pipe_out, fd_bitmap *fds_to_close)
{
#if defined(HAVE_GETRUSAGE) && defined(HAVE_GETTIMEOFDAY)
struct timeval real, user, sys;
struct timeval before, after;
#if defined(HAVE_STRUCT_TIMEZONE)
struct timezone dtz; /* posix doesn't define this */
#endif