-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.cpp
1042 lines (847 loc) · 32.4 KB
/
executor.cpp
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
#include "executor.h"
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <cassert>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <deque>
#include "Tokenizer.h"
#include "Parser.h"
#include "Global.h"
#include "WordExpander.h"
#include "CommandExpander.h"
#include <functional>
#include "builtins.h"
#include <variant>
#include "utils.h"
#include "job_control.h"
namespace executor {
static void run_command_list(const CommandList &cl);
static void set_unexpanded_variables(const std::vector<Command::Simple::VariableAssignment> &variable_assignments) {
for(const Command::Simple::VariableAssignment &va : variable_assignments) {
std::string value;
WordExpander::Options opt;
opt.commonExpansions = true;
opt.fieldSplitting = false;
opt.pathnameExpansion = WordExpander::Options::NEVER;
opt.variableAtAsMultipleFields = false;
if (!WordExpander(opt, va.value).expand_into(value)) {
std::cerr << "Failed expansion: was trying to expand variable '" << va.name << "' "
<< "with value '" << va.value << "'\n";
g.last_return_value = 1;
return;
}
g.variables[va.name] = value;
}
}
static bool touch_file(const std::string &path, const char *mode) {
FILE *f = fopen(path.c_str(), mode);
if(!f) {
perror("shell");
return false;
}
fclose(f);
return true;
}
static bool touch_files(const std::deque<Redirection> &redirections) {
// TODO: maybe this shouldn't be a special case? Could `>a` be equivallent in
// results to just `{}>a`?
for(const Redirection &redir : redirections) {
if(redir.type != Redirection::FileWrite
&& redir.type != Redirection::FileWriteAppend
&& redir.type != Redirection::FileRead) {
continue;
}
std::string path;
WordExpander::Options opt;
opt.commonExpansions = true;
opt.fieldSplitting = false;
opt.pathnameExpansion = WordExpander::Options::ONLY_IF_SINGLE_RESULT;
opt.variableAtAsMultipleFields = false;
if(! WordExpander(opt, redir.path).expand_into(path)) {
std::cerr << "shell: Ambiguous redirect\n";
return false;
}
if(redir.type == Redirection::FileWrite) {
if(!touch_file(path, "w"))
return false;
} else if(redir.type == Redirection::FileWriteAppend) {
if(!touch_file(path, "wa"))
return false;
} else if(redir.type == Redirection::FileRead) {
if(!touch_file(path, "r"))
return false;
}
}
return true;
}
template<typename T>
static bool fd_open(int target_fd, const char *file, int flags, T mode) {
int fd = open(file, flags, mode);
if(fd == -1) {
perror("open");
return false;
}
if(fd != target_fd) {
if(dup2(fd, target_fd) == -1) {
perror("dup2");
if(close(fd) == -1)
perror("close");
return false;
}
if(close(fd) == -1) {
perror("close");
close(target_fd);
return false;
}
}
return true;
}
static int get_unused_fd() {
// TODO: should saved_fd.original_fd also be counted?
int fd = 10;
auto saved_fds = g.saved_fds;
std::sort(saved_fds.begin(), saved_fds.end(), [](const auto &s1, const auto &s2) {
return s1.saved_location < s2.saved_location;
});
for(Global::SavedFd saved : saved_fds) {
if(saved.saved_location == fd)
fd++;
}
return fd;
}
static std::optional<int> fd_save(int original_fd) {
int new_fd = get_unused_fd();
if(dup2(original_fd, new_fd) == -1) {
perror("dup2");
return {};
}
// No need to close(original_fd) because dup2 closes the dest fd
g.saved_fds.push_back(Global::SavedFd{original_fd, new_fd});
return { new_fd };
}
static void fd_restore(int saved_fd) {
int original_location = -1;
// Find the *last* saved file descriptor
for(auto saved : g.saved_fds) {
if(saved.saved_location == saved_fd)
original_location = saved.original_fd;
}
g.saved_fds.erase(std::remove_if(
g.saved_fds.begin(),
g.saved_fds.end(),
[&] (Global::SavedFd s) { return s.saved_location == saved_fd && s.original_fd == original_location; }),
g.saved_fds.end());
if(dup2(saved_fd, original_location) == -1) {
perror("dup2");
}
close(saved_fd);
}
static int file_redirection_type_to_open_option(const Redirection &redir) {
if(redir.type == Redirection::FileRead)
return O_RDONLY;
else if(redir.type == Redirection::FileWrite)
return O_WRONLY | O_CREAT | O_TRUNC;
else if(redir.type == Redirection::FileWriteAppend)
return O_WRONLY | O_CREAT | O_APPEND;
else {
fprintf(stderr, "kish: file_redirection_type_to_open_option called with a non-file redirection\n");
exit(1);
}
}
// TODO: Redirection should be a variant<FileRedirection, Rewiring> so those types make sense
static bool setup_file_redirection(const Redirection &redir) {
int options = file_redirection_type_to_open_option(redir);
return fd_open(redir.fd, redir.path.c_str(), options, 0666);
}
static bool setup_rewiring(const Redirection &redir) {
if(redir.fd == redir.rewire_fd)
return true;
if(dup2(redir.rewire_fd, redir.fd) == -1) {
perror("rewiring");
return false;
}
return true;
}
static bool setup_redirection(const Redirection &redir) {
if(redir.type == Redirection::FileRead
|| redir.type == Redirection::FileWrite
|| redir.type == Redirection::FileWriteAppend)
return setup_file_redirection(redir);
if(redir.type == Redirection::Rewiring) {
return setup_rewiring(redir);
}
// this should never get reached
return false;
}
/*
* Used to save (and later restore, via restore_old_fds()) fds when executing, among others:
* - redirections applied to builtins: `echo $var > file`
* - redirections to non-pipelined command lists: `{ a; b; } > file`, and other compound commands
*/
static std::deque<int> setup_redirections_save_old_fds(const std::deque<Redirection> &redirs) {
// The naive approach to handle this would be
// - save the old fd
// - open() and move the resulting fd to the old fd
//
// But redirection cannot be done that way. Consider the following:
// { ...; } > /dev/stdout
// == { ...; } > /proc/self/fd/1
// In this case the naive method would fail, because stdout (fd=1) would already be moved to a different fd to be replaced,
// so when open() tries to access it, there's nothing in fd 1.
//
// To prevent this,
// - open() needs to be called first,
// - then the original fd needs to be saved,
// - and then the resulting fd from open() needs to be moved to where the original fd was
std::deque<int> saved_fds;
for(const Redirection &redir : redirs) {
if(redir.type == Redirection::Type::Rewiring) { // (for example, 2>&3)
if(redir.fd == redir.rewire_fd)
continue;
auto saved_fd = fd_save(redir.fd);
if(!saved_fd)
continue;
saved_fds.push_front(saved_fd.value());
if(dup2(redir.rewire_fd, redir.fd) == -1) {
perror("dup2");
continue;
}
} else { // file redirection (>file, >>file, ..)
int new_fd = open(redir.path.c_str(), file_redirection_type_to_open_option(redir), 0666);
if(new_fd == -1) {
perror("open");
continue;
}
auto saved_fd = fd_save(redir.fd);
if(!saved_fd)
continue;
saved_fds.push_front(saved_fd.value());
if(dup2(new_fd, redir.fd) == -1) {
perror("dup2");
continue;
}
if(close(new_fd) == -1) {
perror("close");
continue;
}
}
}
return saved_fds;
}
static void restore_old_fds(const std::deque<int> &old_fds) {
for(int saved_fd : old_fds) {
fd_restore(saved_fd);
}
}
[[noreturn]]
static void exec_expanded_simple_command(const Command &expanded_command, const bool search_for_builitin_or_function) {
const Command::Simple &expanded_simple = std::get<Command::Simple>(expanded_command.value);
for(const Redirection &redir : expanded_command.redirections) {
if(!setup_redirection(redir)) {
fprintf(stderr, "kish: could not redirect\n");
exit(1);
}
}
// environment variables from `a=b c`
for(const Command::Simple::VariableAssignment &va : expanded_simple.variable_assignments) {
setenv(va.name.c_str(), va.value.c_str(), 1);
}
if(search_for_builitin_or_function) {
auto builtin = find_builtin(expanded_simple.argv.at(0));
if(builtin) {
int error_code = builtin.value()(expanded_simple);
exit(error_code);
}
if(g.functions.contains(expanded_simple.argv.at(0))) {
// Redirections and inline environment variables for the function are already set up above.
// Replace "$@" to function's argv without remembering it, as we will be exiting shortly anyway
g.argv = expanded_simple.argv;
// Copy the command list as a function can redefine itself (f() { f() { :; }; })
CommandList function_command_list = g.functions.at(expanded_simple.argv.at(0));
run_command_list(function_command_list);
exit(g.last_return_value);
}
}
char **argv = new char*[expanded_simple.argv.size() + 1];
for(size_t i = 0; i < expanded_simple.argv.size(); ++i) {
argv[i] = strdup(expanded_simple.argv.at(i).c_str());
}
argv[expanded_simple.argv.size()] = nullptr;
job_control::before_exec_no_pipeline(true);
execvp(expanded_simple.argv.at(0).c_str(), argv);
perror(expanded_simple.argv.at(0).c_str());
exit(127);
}
[[noreturn]]
static void expand_and_exec_empty_command(const Command &cmd) {
if(touch_files(cmd.redirections))
exit(0);
else
exit(1);
}
[[noreturn]]
static void expand_and_exec_simple_command(Command cmd) {
const Command::Simple &simple_command = std::get<Command::Simple>(cmd.value);
// `a=b` without any commands
if(simple_command.argv.size() == 0 && simple_command.variable_assignments.size() != 0) {
// Variable assignment sets $? to 0, command substitution can modify it further
// Examples:
// `true; a=$(false)` -> $? == 1
// `false; a=""` -> $? == 0
// That's why g.last_return_value is reset first, then any substitution is done
g.last_return_value = 0;
set_unexpanded_variables(simple_command.variable_assignments);
}
// redirections get expanded before evaluating variable assignments in zsh,
// but after in bash. Assuming bash-like behaviour
// `>file` without any commands
if(simple_command.argv.size() == 0 && cmd.redirections.size() != 0) {
if(!touch_files(cmd.redirections))
exit(1);
}
if(simple_command.argv.size() == 0) {
exit(0);
}
// when we are a part of a multi-command pipeline, every substitution happens in a subshell
if(!CommandExpander(&cmd).expand()) {
fprintf(stderr, "Command expansion failed\n");
exit(1);
}
exec_expanded_simple_command(cmd, true);
}
[[noreturn]]
static void expand_and_exec_brace_group(Command expanded) {
// expand redirections:
if(!CommandExpander(&expanded).expand()) {
fprintf(stderr, "Command expansion failed\n");
exit(1);
}
for(const Redirection &redir : expanded.redirections) {
if(!setup_redirection(redir)) {
fprintf(stderr, "kish: could not redirect\n");
exit(1);
}
}
const Command::BraceGroup &brace_group = std::get<Command::BraceGroup>(expanded.value);
run_command_list(brace_group.command_list);
exit(g.last_return_value);
}
[[noreturn]]
static void expand_and_exec_if_command(Command cmd) {
if(!CommandExpander(&cmd).expand()) {
fprintf(stderr, "Command expansion failed\n");
exit(1);
}
for(const Redirection &redir : cmd.redirections) {
if(!setup_redirection(redir)) {
fprintf(stderr, "kish: could not redirect\n");
exit(1);
}
}
g.last_return_value = 0; // if ; ; then ... <- should not depend on $?
const Command::If &if_command = std::get<Command::If>(cmd.value);
run_command_list(if_command.condition);
int condition_return_value = g.last_return_value;
g.last_return_value = 0; // if false; then :; fi <- should reset $? to 0
if(condition_return_value == 0) {
run_command_list(if_command.then);
} else {
for(const auto &elif : if_command.elif) {
g.last_return_value = 0;
run_command_list(elif.condition);
condition_return_value = g.last_return_value;
g.last_return_value = 0;
if(condition_return_value == 0) {
run_command_list(elif.then);
exit(g.last_return_value);
}
}
if(if_command.opt_else.has_value())
run_command_list(if_command.opt_else.value());
}
exit(g.last_return_value);
}
[[noreturn]]
static void expand_and_exec_while_command(Command cmd) {
if(!CommandExpander(&cmd).expand()) {
fprintf(stderr, "Command expansion failed\n");
exit(1);
}
for(const Redirection &redir : cmd.redirections) {
if(!setup_redirection(redir)) {
fprintf(stderr, "kish: could not redirect\n");
exit(1);
}
}
const Command::While &while_command = std::get<Command::While>(cmd.value);
while(true) {
g.last_return_value = 0;
run_command_list(while_command.condition);
if(g.last_return_value != 0)
exit(0);
run_command_list(while_command.body);
}
}
[[noreturn]]
static void expand_and_exec_until_command(Command cmd) {
if(!CommandExpander(&cmd).expand()) {
fprintf(stderr, "Command expansion failed\n");
exit(1);
}
for(const Redirection &redir : cmd.redirections) {
if(!setup_redirection(redir)) {
fprintf(stderr, "kish: could not redirect\n");
exit(1);
}
}
const Command::Until &until_command = std::get<Command::Until>(cmd.value);
while(true) {
g.last_return_value = 0;
run_command_list(until_command.condition);
if(g.last_return_value == 0)
exit(0);
g.last_return_value = 0;
run_command_list(until_command.body);
}
}
[[noreturn]]
static void expand_and_exec_for_command(Command cmd) {
if(!CommandExpander(&cmd).expand()) {
fprintf(stderr, "Command expansion failed\n");
exit(1);
}
for(const Redirection &redir : cmd.redirections) {
if(!setup_redirection(redir)) {
fprintf(stderr, "kish: could not redirect\n");
exit(1);
}
}
const Command::For &for_command = std::get<Command::For>(cmd.value);
std::vector<std::string> expanded_items;
for(const std::string &item : for_command.items) {
WordExpander::Options opt;
opt.commonExpansions = true;
opt.fieldSplitting = true;
opt.pathnameExpansion = WordExpander::Options::ALWAYS;
opt.variableAtAsMultipleFields = true;
WordExpander(opt, item).expand_into(expanded_items);
}
// Note: for loops should not create a new scope for the looped-over variable
// `for x in 1 2 3; do :; done; echo $x` -> 3
// Note: for loops do not reset $?
for(const std::string &item : expanded_items) {
g.variables[for_command.varname] = item;
run_command_list(for_command.body);
}
exit(g.last_return_value);
}
// f() { :; } | g() { :; }
[[noreturn]]
static void expand_and_exec_function_definition_command(Command cmd) {
if(!CommandExpander(&cmd).expand()) {
fprintf(stderr, "Command expansion failed\n");
exit(1);
}
// defining a function in a pipeline doesn't really make sense
// so don't do anything, probably just like other shells
exit(0);
}
// Runs a pipelined command
static std::optional<pid_t> run_command_expand_in_subprocess(const Command &cmd) {
// TODO: better job control here
int pid = job_control::fork_own_process_group();
if(pid == -1) {
perror("fork");
return {};
}
if(pid == 0) {
// TODO: make CommandExpander run here, instead of in expand_and_exec_*_command(cmd)
// Would that work?
std::visit(utils::overloaded {
[&] (const Command::Empty) { expand_and_exec_empty_command(cmd); },
[&] (const Command::Simple) { expand_and_exec_simple_command(cmd); },
[&] (const Command::BraceGroup) { expand_and_exec_brace_group(cmd); },
[&] (const Command::If) { expand_and_exec_if_command(cmd); },
[&] (const Command::While) { expand_and_exec_while_command(cmd); },
[&] (const Command::Until) { expand_and_exec_until_command(cmd); },
[&] (const Command::For) { expand_and_exec_for_command(cmd); },
[&] (const Command::FunctionDefinition) { expand_and_exec_function_definition_command(cmd); },
}, cmd.value);
}
return { pid };
}
// Runs a non-pipelined builtin with possible redirections
static void run_builitin_in_main_process(BuiltinHandler &builtin, const Command &expanded_command) {
const auto &simple_command = std::get<Command::Simple>(expanded_command.value);
std::vector<TemporaryVariableChange> localVariableChanges;
for(const auto &variable_assignment : simple_command.variable_assignments) {
localVariableChanges.emplace_back(variable_assignment.name, variable_assignment.value);
}
auto old_fds = setup_redirections_save_old_fds(expanded_command.redirections);
g.last_return_value = builtin(simple_command);
restore_old_fds(old_fds);
}
// Runs a non-pipelined shell function with possible redirections
static void run_function_in_main_process(const Command &expanded_command) {
const auto &simple_command = std::get<Command::Simple>(expanded_command.value);
/* TODO: handle inline environment variables */
if(! simple_command.variable_assignments.empty()) {
std::cerr << "kish: Inline environment variables passed to functions are not yet implemented\n";
g.last_return_value = 1;
return;
}
auto old_fds = setup_redirections_save_old_fds(expanded_command.redirections);
// Temporarily replace "$@"
std::vector<std::string> old_argv{std::move(g.argv)};
g.argv = simple_command.argv;
// Make a copy, as a function can modify itself while running (f() { f() { :; }; })
CommandList command_list = g.functions.at(simple_command.argv.at(0));
run_command_list(command_list);
// Restore "$@"
g.argv = std::move(old_argv);
restore_old_fds(old_fds);
}
// Runs non-pipelined commands composed of only redirections
static void run_empty_command_expand_in_main_process(const Command &cmd) {
// `>file` with no commands
if(!touch_files(cmd.redirections))
g.last_return_value = 1;
}
// Runs non-pipelined commands composed of only variable assignments with optional redirecions
static void run_empty_simple_command_expand_in_main_process(const Command &cmd) {
const Command::Simple &simple_command = std::get<Command::Simple>(cmd.value);
assert(simple_command.argv.size() == 0);
if(simple_command.variable_assignments.size() != 0) {
// Variable assignment sets $? to 0, command substitution can modify it further
// Examples:
// `true; a=$(false)` -> $? == 1
// `false; a=""` -> $? == 0
// That's why g.last_return_value is reset first, then any substitution is done
g.last_return_value = 0;
set_unexpanded_variables(simple_command.variable_assignments);
}
// redirections get expanded before evaluating variable assignments in zsh,
// but after in bash. Assuming bash-like behaviour
// `>file` with no commands
if(cmd.redirections.size() != 0) {
if(!touch_files(cmd.redirections))
g.last_return_value = 1;
}
}
// Runs non-pipelined simple commands (e.g. `a=b c d >e`) that have argv in them
static void run_nonempty_simple_command_expand_in_main_process(Command expanded) {
if(!CommandExpander(&expanded).expand()) {
fprintf(stderr, "Command expansion failed\n");
g.last_return_value = 1;
return;
}
const Command::Simple &expanded_simple_command = std::get<Command::Simple>(expanded.value);
auto builtin = find_builtin(expanded_simple_command.argv.at(0));
if(builtin) {
run_builitin_in_main_process(builtin.value(), expanded);
} else if(g.functions.contains(expanded_simple_command.argv.at(0))) {
/* TODO: inline environment variables should expand to environment variables here */
/* and return back to their state */
run_function_in_main_process(expanded);
} else {
int pid = job_control::fork_own_process_group();
if(pid == -1) {
perror("fork");
return;
}
if(pid == 0) {
// child
exec_expanded_simple_command(expanded, false); // noreturn
}
job_control::wait_for_one(pid);
}
}
// Runs non-pipelined simple commands (e.g. `a=b c d >e`)
static void run_simple_command_expand_in_main_process(const Command &cmd) {
const Command::Simple &simple_command = std::get<Command::Simple>(cmd.value);
// TODO:
// this should discriminate on how many EXPANDED argv words there are
// all shells seem to behave this way:
// a=1 $empty; echo $a --> "1"
// a=1 $nonempty; echo $a --> ""
if(simple_command.argv.size() == 0)
run_empty_simple_command_expand_in_main_process(cmd);
else
run_nonempty_simple_command_expand_in_main_process(cmd);
}
// Runs non-pipelined brace groups (e.g `{ a; b; } > c`)
static void run_brace_group_expand_in_main_process(Command cmd) {
if(!CommandExpander(&cmd).expand()) {
fprintf(stderr, "Command expansion failed\n");
g.last_return_value = 1;
return;
}
const Command::BraceGroup &brace_group = std::get<Command::BraceGroup>(cmd.value);
auto saved_fds = setup_redirections_save_old_fds(cmd.redirections);
run_command_list(brace_group.command_list);
restore_old_fds(saved_fds);
}
static void run_if_command_expand_in_main_process(Command cmd) {
if(!CommandExpander(&cmd).expand()) {
fprintf(stderr, "Command expansion failed\n");
g.last_return_value = 1;
return;
}
const Command::If &if_command = std::get<Command::If>(cmd.value);
auto saved_fds = setup_redirections_save_old_fds(cmd.redirections);
g.last_return_value = 0; // if ; ; then ... <- should not depend on $?
run_command_list(if_command.condition);
int condition_return_value = g.last_return_value;
g.last_return_value = 0; // if false; then :; fi <- should reset $? to 0
if(condition_return_value == 0) {
run_command_list(if_command.then);
} else {
for(const auto &elif : if_command.elif) {
g.last_return_value = 0;
run_command_list(elif.condition);
condition_return_value = g.last_return_value;
g.last_return_value = 0;
if(condition_return_value == 0) {
run_command_list(elif.then);
restore_old_fds(saved_fds);
return;
}
}
if(if_command.opt_else.has_value())
run_command_list(if_command.opt_else.value());
}
restore_old_fds(saved_fds);
}
static void run_while_command_expand_in_main_process(Command cmd) {
if(!CommandExpander(&cmd).expand()) {
fprintf(stderr, "Command expansion failed\n");
g.last_return_value = 1;
return;
}
const Command::While &while_command = std::get<Command::While>(cmd.value);
auto saved_fds = setup_redirections_save_old_fds(cmd.redirections);
while(true) {
g.last_return_value = 0;
run_command_list(while_command.condition);
int exit_code = g.last_return_value;
g.last_return_value = 0;
if(exit_code != 0) {
restore_old_fds(saved_fds);
return;
}
run_command_list(while_command.body);
}
}
static void run_until_command_expand_in_main_process(Command cmd) {
if(!CommandExpander(&cmd).expand()) {
fprintf(stderr, "Command expansion failed\n");
g.last_return_value = 1;
return;
}
const Command::Until &until_command = std::get<Command::Until>(cmd.value);
auto saved_fds = setup_redirections_save_old_fds(cmd.redirections);
while(true) {
g.last_return_value = 0;
run_command_list(until_command.condition);
int exit_code = g.last_return_value;
g.last_return_value = 0;
if(exit_code == 0) {
restore_old_fds(saved_fds);
return;
}
run_command_list(until_command.body);
}
}
static void run_for_command_expand_in_main_process(Command cmd) {
if(!CommandExpander(&cmd).expand()) {
fprintf(stderr, "Command expansion failed\n");
g.last_return_value = 1;
return;
}
const Command::For &for_command = std::get<Command::For>(cmd.value);
std::vector<std::string> expanded_items;
for(const std::string &item : for_command.items) {
WordExpander::Options opt;
opt.commonExpansions = true;
opt.fieldSplitting = true;
opt.pathnameExpansion = WordExpander::Options::ALWAYS;
opt.variableAtAsMultipleFields = true;
WordExpander(opt, item).expand_into(expanded_items);
}
// Note: for loops should not create a new scope for the looped-over variable
// `for x in 1 2 3; do :; done; echo $x` -> 3
// Note: for loops do not reset $?
for(const std::string &item : expanded_items) {
g.variables[for_command.varname] = item;
run_command_list(for_command.body);
}
}
static void run_function_definition_command_expand_in_main_process(Command cmd) {
if(!CommandExpander(&cmd).expand()) {
fprintf(stderr, "Command expansion failed\n");
g.last_return_value = 1;
return;
}
const Command::FunctionDefinition &function_definition_command = std::get<Command::FunctionDefinition>(cmd.value);
g.functions[function_definition_command.name] = function_definition_command.body;
/* TODO */
g.last_return_value = 0;
}
// Runs any non-pipelined command
static void run_command_expand_in_main_process(const Command &cmd) {
std::visit(utils::overloaded {
[&] (const Command::Empty) { run_empty_command_expand_in_main_process(cmd); },
[&] (const Command::Simple) { run_simple_command_expand_in_main_process(cmd); },
[&] (const Command::BraceGroup) { run_brace_group_expand_in_main_process(cmd); },
[&] (const Command::If) { run_if_command_expand_in_main_process(cmd); },
[&] (const Command::While) { run_while_command_expand_in_main_process(cmd); },
[&] (const Command::Until) { run_until_command_expand_in_main_process(cmd); },
[&] (const Command::For) { run_for_command_expand_in_main_process(cmd); },
[&] (const Command::FunctionDefinition) { run_function_definition_command_expand_in_main_process(cmd); },
}, cmd.value);
}
// Setup pipe between two commands: `left | right`
static void setup_pipe_between(Command &left, Command &right) {
int pipefd[2];
if(pipe(pipefd) == -1) {
perror("pipe");
return;
}
left.redirections.push_front(Redirection{Redirection::Rewiring, STDOUT_FILENO, pipefd[1]});
right.redirections.push_front(Redirection{Redirection::Rewiring, STDIN_FILENO, pipefd[0]});
// TODO: Should this be a `push_front` or a `push_back`? Does the order here even matter?
left.pipe_file_descriptors.push_front(pipefd[1]);
right.pipe_file_descriptors.push_front(pipefd[0]);
}
// Runs a multi-command pipeline, for example: `a | b | c`
static void run_multi_command_pipeline(const Pipeline &pipeline) {
std::vector<int> fds;
Pipeline modified = pipeline;
std::vector<pid_t> pids;
for(size_t i = 0; i < modified.commands.size(); ++i) {
Command &cmd = modified.commands[i];
if(i != modified.commands.size() - 1) {
Command &next = modified.commands[i + 1];
setup_pipe_between(cmd, next);
}
auto maybe_pid = run_command_expand_in_subprocess(cmd);
for(int fd : cmd.pipe_file_descriptors)
close(fd); // ignore errors
if(maybe_pid)
pids.push_back(maybe_pid.value());
}
job_control::wait_for_all(pids);
}
static void run_single_command_pipeline(const Command &command) {
run_command_expand_in_main_process(command);
}
static void run_pipeline(const Pipeline &pipeline) {
if(pipeline.commands.size() == 0) {
g.last_return_value = 0;
} else if(pipeline.commands.size() == 1) {
run_single_command_pipeline(pipeline.commands.at(0));
} else if(pipeline.commands.size() > 1) {
run_multi_command_pipeline(pipeline);
}
if(pipeline.negation_prefix)
g.last_return_value = !g.last_return_value;
}
static void run_and_or_list(const AndOrList &and_or_list) {
for(const WithFollowingOperator<Pipeline> &pipe_op : and_or_list) {
run_pipeline(pipe_op.val);
if(pipe_op.following_operator == "&&" && g.last_return_value != 0)
return;
if(pipe_op.following_operator == "||" && g.last_return_value == 0)
return;
}
}
static void run_command_list(const CommandList &cl) {
for(const WithFollowingOperator<AndOrList> &aol_op : cl) {
run_and_or_list(aol_op.val);
// aol_and_op.following_operator is ";" or "" or "&" (TODO)
}
}
void run_from_string(const std::string &str) {
std::vector<Token> tokens;
try {
tokens = Tokenizer(str).tokenize();
} catch(const Tokenizer::SyntaxError &se) {
std::cerr << "Syntax error: " << se.explanation << "\n";
g.last_return_value = 1;
return;
}
// for(const Token &token : tokens) {
// std::cerr << "[" << token.value << "]" << (token.type == Token::Type::OPERATOR ? " (op)\n" : "\n");
// }
CommandList parsed;
try {
parsed = Parser(tokens).parse();
} catch(const Parser::SyntaxError &se) {
std::cerr << "Syntax error: " << se.explanation << "\n";
g.last_return_value = 1;
return;
}
run_command_list(parsed);
}
template <typename T>
static void subshell_capture_output(T func, std::string &out) {
int pipefd[2];
if(pipe(pipefd) == -1) {
perror("pipe");
g.last_return_value = 1;
return;
}
int pid = job_control::fork_own_process_group();
if(pid == -1) {
perror("fork");
g.last_return_value = 1;
return;
}
if(pid == 0) {
close(pipefd[0]);
if(!setup_rewiring({ Redirection::Rewiring, STDOUT_FILENO, pipefd[1] }))
exit(1);
func();
close(pipefd[1]); // TODO: ?
exit(g.last_return_value);
}
close(pipefd[1]);
bool has_read_something = false;