-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.c
1927 lines (1760 loc) · 45.2 KB
/
exec.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
/* $OpenBSD: exec.c,v 1.52 2015/09/10 22:48:58 nicm Exp $ */
/*-
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
* 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
* 2019, 2020, 2021, 2022, 2023
* mirabilos <m@mirbsd.org>
*
* Provided that these terms and disclaimer and all copyright notices
* are retained or reproduced in an accompanying document, permission
* is granted to deal in this work without restriction, including un-
* limited rights to use, publicly perform, distribute, sell, modify,
* merge, give away, or sublicence.
*
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
* the utmost extent permitted by applicable law, neither express nor
* implied; without malicious intent or gross negligence. In no event
* may a licensor, author or contributor be held liable for indirect,
* direct, other damage, loss, or other issues arising in any way out
* of dealing in the work, even if advised of the possibility of such
* damage or existence of a defect, except proven that it results out
* of said person's immediate fault when using the work as intended.
*/
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.247 2023/09/16 23:07:45 tg Exp $");
#ifndef MKSH_DEFAULT_EXECSHELL
#define MKSH_DEFAULT_EXECSHELL MKSH_UNIXROOT "/bin/sh"
#endif
static int comexec(struct op *, struct tbl * volatile, const char **,
int volatile, volatile int *);
static void scriptexec(struct op *, const char **) MKSH_A_NORETURN;
static int call_builtin(struct tbl *, const char **, const char *, Wahr);
static int iosetup(struct ioword *, struct tbl *);
static const char *do_selectargs(const char **, Wahr);
static Test_op dbteste_isa(Test_env *, Test_meta);
static const char *dbteste_getopnd(Test_env *, Test_op, Wahr);
static void dbteste_error(Test_env *, int, const char *);
/* XXX: horrible kludge to fit within the framework */
static void plain_fmt_entry(char *, size_t, unsigned int, const void *);
static void select_fmt_entry(char *, size_t, unsigned int, const void *);
/*
* execute command tree
*/
int
execute(struct op * volatile t,
/* if XEXEC don't fork */
volatile int flags,
volatile int * volatile xerrok)
{
int i;
volatile int rv = 0, dummy = 0;
int pv[2];
const char ** volatile ap = NULL;
char ** volatile up;
const char *s, *ccp;
struct ioword **iowp;
struct tbl *tp = NULL;
if (t == NULL)
return (0);
/* Caller doesn't care if XERROK should propagate. */
if (xerrok == NULL)
xerrok = &dummy;
if (flags & XFORK)
switch (t->type) {
case TPIPE:
break;
default:
if (flags & XEXEC)
break;
/* FALLTHROUGH */
case TTIME:
/* run in sub-process */
return (exchild(t, flags & ~XTIME, xerrok, -1));
}
newenv(E_EXEC);
if (trap)
runtraps(0);
/* we want to run an executable, do some variance checks */
if (t->type == TCOM) {
/*
* Clear subst_exstat before argument expansion. Used by
* null commands (see comexec() and c_eval()) and by c_set().
*/
subst_exstat = 0;
/* for $LINENO */
current_lineno = t->lineno;
/* check if this is 'var=<<EOF' */
if (
/* we have zero arguments, i.e. no program to run */
t->args[0] == NULL &&
/* we have exactly one variable assignment */
t->vars[0] != NULL && t->vars[1] == NULL &&
/* we have exactly one I/O redirection */
t->ioact != NULL && t->ioact[0] != NULL &&
t->ioact[1] == NULL &&
/* of type "here document" (or "here string") */
(t->ioact[0]->ioflag & IOTYPE) == IOHERE &&
/* the variable assignment begins with a valid varname */
(ccp = skip_wdvarname(t->vars[0], Ja)) != t->vars[0] &&
/* and has no right-hand side (i.e. "varname=") */
ccp[0] == CHAR && ((ccp[1] == '=' && ccp[2] == EOS) ||
/* or "varname+=" */ (ccp[1] == '+' && ccp[2] == CHAR &&
ccp[3] == '=' && ccp[4] == EOS))) {
char *cp, *dp;
if ((rv = herein(t->ioact[0], &cp) /*? 1 : 0*/))
cp = NULL;
strdup2x(dp, evalstr(t->vars[0], DOASNTILDE | DOSCALAR),
rv ? null : cp);
typeset(dp, Flag(FEXPORT) ? EXPORT : 0, 0, 0, 0);
/* free the expanded value */
afree(cp, APERM);
afree(dp, ATEMP);
goto Break;
}
/*
* POSIX says expand command words first, then redirections,
* and assignments last..
*/
up = eval(t->args, t->u.evalflags | DOBLANK | DOGLOB | DOTILDE);
if (flags & XTIME)
/* Allow option parsing (bizarre, but POSIX) */
timex_hook(t, &up);
ap = (const char **)up;
if (ap[0])
tp = findcom(ap[0], FC_BI | FC_FUNC);
}
flags &= ~XTIME;
if (t->ioact != NULL || t->type == TPIPE || t->type == TCOPROC) {
e->savedfd = alloc2(NUFILE, sizeof(ksh_fdsave), ATEMP);
/* initialise to not redirected */
memset(e->savedfd, 0, NUFILE * sizeof(ksh_fdsave));
}
/* mark for replacement later (unless TPIPE) */
vp_pipest->flag |= INT_L;
/* do redirection, to be restored in quitenv() */
if (t->ioact != NULL)
for (iowp = t->ioact; *iowp != NULL; iowp++) {
if (iosetup(*iowp, tp) < 0) {
exstat = rv = 1;
/*
* Redirection failures for special commands
* cause (non-interactive) shell to exit.
*/
if (tp && tp->type == CSHELL &&
(tp->flag & SPEC_BI))
kerrf(KWF_ERR(1) | KWF_PREFIX |
KWF_FILELINE | KWF_ONEMSG |
KWF_NOERRNO, "redirection failure");
/* Deal with FERREXIT, quitenv(), etc. */
goto Break;
}
}
switch (t->type) {
case TCOM:
rv = comexec(t, tp, (const char **)ap, flags, xerrok);
break;
case TPAREN:
rv = execute(t->left, flags | XFORK, xerrok);
break;
case TPIPE:
flags |= XFORK;
flags &= ~XEXEC;
FDSAVE(0, savefd(0));
FDSAVE(1, savefd(1));
while (t->type == TPIPE) {
openpipe(pv);
/* stdout of curr */
ksh_dup2(pv[1], 1, Nee);
/**
* Let exchild() close pv[0] in child
* (if this isn't done, commands like
* (: ; cat /etc/termcap) | sleep 1
* will hang forever).
*/
exchild(t->left, flags | XPIPEO | XCCLOSE,
NULL, pv[0]);
/* stdin of next */
ksh_dup2(pv[0], 0, Nee);
closepipe(pv);
flags |= XPIPEI;
t = t->right;
}
/* stdout of last */
restfd(1, SAVEDFD(e, 1));
/* no need to re-restore this */
e->savedfd[1] = 0;
/* Let exchild() close 0 in parent, after fork, before wait */
i = exchild(t, flags | XPCLOSE | XPIPEST, xerrok, 0);
if (!(flags&XBGND) && !(flags&XXCOM))
rv = i;
break;
case TLIST:
while (t->type == TLIST) {
execute(t->left, flags & XERROK, NULL);
t = t->right;
}
rv = execute(t, flags & XERROK, xerrok);
break;
case TCOPROC: {
#ifndef MKSH_NOPROSPECTOFWORK
sigset_t omask;
/*
* Block sigchild as we are using things changed in the
* signal handler
*/
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
e->type = E_ERRH;
if ((i = kshsetjmp(e->jbuf))) {
sigprocmask(SIG_SETMASK, &omask, NULL);
quitenv(NULL);
unwind(i);
/* NOTREACHED */
}
#endif
/* Already have a (live) co-process? */
if (coproc.job && coproc.write >= 0)
kerrf(KWF_ERR(1) | KWF_PREFIX |
KWF_FILELINE | KWF_ONEMSG |
KWF_NOERRNO, "coprocess already exists");
/* Can we re-use the existing co-process pipe? */
coproc_cleanup(Ja);
/* do this before opening pipes, in case these fail */
FDSAVE(0, savefd(0));
FDSAVE(1, savefd(1));
openpipe(pv);
if (pv[0] != 0) {
ksh_dup2(pv[0], 0, Nee);
close(pv[0]);
}
coproc.write = pv[1];
coproc.job = NULL;
if (coproc.readw >= 0)
ksh_dup2(coproc.readw, 1, Nee);
else {
openpipe(pv);
coproc.read = pv[0];
ksh_dup2(pv[1], 1, Nee);
/* closed before first read */
coproc.readw = pv[1];
coproc.njobs = 0;
/* create new coprocess id */
++coproc.id;
}
#ifndef MKSH_NOPROSPECTOFWORK
sigprocmask(SIG_SETMASK, &omask, NULL);
/* no more need for error handler */
e->type = E_EXEC;
#endif
/*
* exchild() closes coproc.* in child after fork,
* will also increment coproc.njobs when the
* job is actually created.
*/
flags &= ~XEXEC;
exchild(t->left, flags | XBGND | XFORK | XCOPROC | XCCLOSE,
NULL, coproc.readw);
break;
}
case TASYNC:
/*
* XXX non-optimal, I think - "(foo &)", forks for (),
* forks again for async... parent should optimise
* this to "foo &"...
*/
rv = execute(t->left, (flags&~XEXEC)|XBGND|XFORK, xerrok);
break;
case TOR:
case TAND:
rv = execute(t->left, XERROK, NULL);
if ((rv == 0) == (t->type == TAND))
rv = execute(t->right, flags & XERROK, xerrok);
else {
flags |= XERROK;
if (xerrok)
*xerrok = 1;
}
break;
case TBANG:
rv = !execute(t->right, XERROK, xerrok);
flags |= XERROK;
if (xerrok)
*xerrok = 1;
break;
case TDBRACKET: {
Test_env te;
te.flags = TEF_DBRACKET;
te.pos.wp = t->args;
te.isa = dbteste_isa;
te.getopnd = dbteste_getopnd;
te.eval = test_eval;
te.error = dbteste_error;
rv = test_parse(&te);
break;
}
case TFOR:
case TSELECT: {
volatile Wahr is_first = Ja;
if (t->vars == NULL)
/* “for i; do” */
ap = cpyargv(NULL, e->loc->argv, ATEMP) + 1;
else
ap = (const char **)eval((const char **)t->vars,
DOBLANK | DOGLOB | DOTILDE);
e->type = E_LOOP;
while ((i = kshsetjmp(e->jbuf))) {
if ((e->flags&EF_BRKCONT_PASS) ||
(i != LBREAK && i != LCONTIN)) {
quitenv(NULL);
unwind(i);
} else if (i == LBREAK) {
rv = 0;
goto Break;
}
}
/* in case of a continue */
rv = 0;
if (t->type == TFOR) {
while (*ap != NULL) {
setstr(global(t->str), *ap++, KSH_UNWIND_ERROR);
rv = execute(t->left, flags & XERROK, xerrok);
}
} else {
do_TSELECT:
if ((ccp = do_selectargs(ap, is_first))) {
is_first = Nee;
setstr(global(t->str), ccp, KSH_UNWIND_ERROR);
execute(t->left, flags & XERROK, xerrok);
goto do_TSELECT;
}
rv = 1;
}
break;
}
case TWHILE:
case TUNTIL:
e->type = E_LOOP;
while ((i = kshsetjmp(e->jbuf))) {
if ((e->flags&EF_BRKCONT_PASS) ||
(i != LBREAK && i != LCONTIN)) {
quitenv(NULL);
unwind(i);
} else if (i == LBREAK) {
rv = 0;
goto Break;
}
}
/* in case of a continue */
rv = 0;
while ((execute(t->left, XERROK, NULL) == 0) ==
(t->type == TWHILE))
rv = execute(t->right, flags & XERROK, xerrok);
break;
case TIF:
case TELIF:
if (t->right == NULL)
/* should be error */
break;
rv = execute(execute(t->left, XERROK, NULL) == 0 ?
t->right->left : t->right->right, flags & XERROK, xerrok);
break;
case TCASE:
i = 0;
ccp = evalstr(t->str, DOTILDE | DOSCALAR);
for (t = t->left; t != NULL && t->type == TPAT; t = t->right) {
for (ap = (const char **)t->vars; *ap; ap++) {
if (i || ((s = evalstr(*ap, DOTILDE|DOPAT)) &&
gmatchx(ccp, s, Nee))) {
record_match(ccp);
rv = execute(t->left, flags & XERROK,
xerrok);
i = 0;
switch (t->u.charflag) {
case '&':
i = 1;
/* FALLTHROUGH */
case '|':
goto TCASE_next;
}
goto TCASE_out;
}
}
i = 0;
TCASE_next:
/* empty */;
}
TCASE_out:
break;
case TBRACE:
rv = execute(t->left, flags & XERROK, xerrok);
break;
case TFUNCT:
rv = define(t->str, t);
break;
case TTIME:
/*
* Clear XEXEC so nested execute() call doesn't exit
* (allows "ls -l | time grep foo").
*/
rv = timex(t, flags & ~XEXEC, xerrok);
break;
case TEXEC:
/* an eval'd TCOM */
up = makenv();
restoresigs();
cleanup_proc_env();
/* I/O redirection cleanup to be done in child process */
if (!Flag(FPOSIX) && !Flag(FSH) && t->left->ioact != NULL)
for (iowp = t->left->ioact; *iowp != NULL; iowp++)
if (((*iowp)->ioflag & IODUPSELF) &&
fcntl((*iowp)->unit, F_SETFD, 0) == -1)
kwarnf0(KWF_INTERNAL | KWF_WARNING,
Tcloexec_failed, "clear",
(*iowp)->unit);
/* try to execute */
{
union mksh_ccphack cargs;
cargs.ro = t->args;
execve(t->str, cargs.rw, up);
rv = errno;
}
if (rv == ENOEXEC)
scriptexec(t, (const char **)up);
else
kerrf(KWF_VERRNO | KWF_ERR(126) | KWF_PREFIX |
KWF_FILELINE | KWF_ONEMSG, rv, t->str);
}
Break:
exstat = rv & 0xFF;
if (vp_pipest->flag & INT_L) {
unset(vp_pipest, 1);
vp_pipest->flag = DEFINED | ISSET | INTEGER | RDONLY |
ARRAY | INT_U | INT_L;
vp_pipest->val.i = rv;
}
/* restores IO */
quitenv(NULL);
if ((flags&XEXEC))
/* exit child */
unwind(LEXIT);
if (rv != 0 && !(flags & XERROK) &&
(xerrok == NULL || !*xerrok)) {
trapsig(ksh_SIGERR);
if (Flag(FERREXIT))
unwind(LERREXT);
}
return (rv);
}
/*
* execute simple command
*/
static int
comexec(struct op *t, struct tbl * volatile tp, const char **ap,
volatile int flags, volatile int *xerrok)
{
int i;
volatile int rv = 0;
const char *cp;
const char **lastp;
int type_flags;
Wahr resetspec;
int fcflags = FC_BI | FC_FUNC | FC_PATH;
struct block *l_expand, *l_assign;
int optc;
const char *exec_argv0 = NULL;
Wahr exec_clrenv = Nee;
volatile kui old_inuse;
const char * volatile old_kshname;
volatile kby old_flags[FNFLAGS];
static struct op texec; /* static for use by child process */
/* snag the last argument for $_ */
if (Flag(FTALKING) && *(lastp = ap)) {
/*
* XXX not the same as AT&T ksh, which only seems to set $_
* after a newline (but not in functions/dot scripts, but in
* interactive and script) - perhaps save last arg here and
* set it in shell()?.
*/
while (*++lastp)
;
/* setstr() can't fail here */
setstr(typeset("_", LOCAL, 0, INTEGER, 0), *--lastp,
KSH_RETURN_ERROR);
}
/**
* Deal with the shell builtins builtin, exec and command since
* they can be followed by other commands. This must be done before
* we know if we should create a local block which must be done
* before we can do a path search (in case the assignments change
* PATH).
* Odd cases:
* FOO=bar exec >/dev/null FOO is kept but not exported
* FOO=bar exec foobar FOO is exported
* FOO=bar command exec >/dev/null FOO is neither kept nor exported
* FOO=bar command FOO is neither kept nor exported
* PATH=... foobar use new PATH in foobar search
*/
resetspec = Nee;
while (tp && tp->type == CSHELL) {
/* undo effects of command */
fcflags = FC_BI | FC_FUNC | FC_PATH;
if (tp->val.f == c_builtin) {
if ((cp = *++ap) == NULL ||
(!strcmp(cp, "--") && (cp = *++ap) == NULL)) {
tp = NULL;
break;
}
if ((tp = findcom(cp, FC_BI)) == NULL)
kerrf(KWF_ERR(1) | KWF_PREFIX | KWF_FILELINE |
KWF_THREEMSG | KWF_NOERRNO, Tbuiltin, cp,
Tnot_found);
if (tp->type == CSHELL && (tp->flag & LOW_BI))
break;
continue;
} else if (tp->val.f == c_exec) {
if (ap[1] == NULL)
break;
ksh_getopt_reset(&builtin_opt, GF_ERROR);
while ((optc = ksh_getopt(ap, &builtin_opt, "a:c")) != -1)
switch (optc) {
case 'a':
exec_argv0 = builtin_opt.optarg;
break;
case 'c':
exec_clrenv = Ja;
/* ensure we can actually do this */
resetspec = Ja;
break;
default:
rv = 2;
goto Leave;
}
ap += builtin_opt.optind;
flags |= XEXEC;
/* POSuX demands ksh88-like behaviour here */
if (Flag(FPOSIX))
fcflags = FC_PATH;
} else if (tp->val.f == c_command) {
Wahr saw_p = Nee;
/*
* Ugly dealing with options in two places (here
* and in c_command(), but such is life)
*/
ksh_getopt_reset(&builtin_opt, 0);
while ((optc = ksh_getopt(ap, &builtin_opt, ":p")) == 'p')
saw_p = Ja;
if (optc != -1)
/* command -vV or something */
break;
/* don't look for functions */
fcflags = FC_BI | FC_PATH;
if (saw_p) {
if (Flag(FRESTRICTED)) {
kwarnf(KWF_PREFIX | KWF_FILELINE |
KWF_TWOMSG | KWF_NOERRNO,
"command -p", "restricted");
rv = 1;
goto Leave;
}
fcflags |= FC_DEFPATH;
}
ap += builtin_opt.optind;
/*
* POSIX says special builtins lose their status
* if accessed using command.
*/
resetspec = Ja;
if (!ap[0]) {
/* ensure command with no args exits with 0 */
subst_exstat = 0;
break;
}
} else if (tp->flag & LOW_BI) {
/* if we have any flags, do not use the builtin */
if ((ap[1] && ap[1][0] == '-' && ap[1][1] != '\0' &&
/* argument, begins with -, is not - or -- */
(ap[1][1] != '-' || ap[1][2] != '\0')) ||
/* always prefer the external utility */
(tp->flag & LOWER_BI)) {
struct tbl *ext_cmd;
ext_cmd = findcom(tp->name, FC_FUNC | FC_PATH);
if (ext_cmd && (ext_cmd->type == CFUNC ||
(ext_cmd->flag & ISSET)))
tp = ext_cmd;
}
break;
} else if (tp->val.f == c_trap) {
t->u.evalflags &= ~DOTCOMEXEC;
break;
} else
break;
tp = findcom(ap[0], fcflags & (FC_BI | FC_FUNC));
}
if (t->u.evalflags & DOTCOMEXEC)
flags |= XEXEC;
l_expand = e->loc;
if (!resetspec && (!ap[0] || (tp && (tp->flag & KEEPASN))))
type_flags = 0;
else {
/* create new variable/function block */
newblock();
/* all functions keep assignments */
type_flags = LOCAL | LOCAL_COPY | EXPORT;
}
l_assign = e->loc;
if (exec_clrenv)
l_assign->flags |= BF_STOPENV;
if (Flag(FEXPORT))
type_flags |= EXPORT;
if (Flag(FXTRACE))
change_xtrace(2, Nee);
for (i = 0; t->vars[i]; i++) {
/* do NOT lookup in the new var/fn block just created */
e->loc = l_expand;
cp = evalstr(t->vars[i], DOASNTILDE | DOSCALAR);
e->loc = l_assign;
if (Flag(FXTRACE)) {
const char *ccp;
ccp = skip_varname(cp, Ja);
if (*ccp == '+')
++ccp;
if (*ccp == '=')
++ccp;
shf_write(cp, ccp - cp, shl_xtrace);
print_value_quoted(shl_xtrace, ccp);
shf_putc(' ', shl_xtrace);
}
/* but assign in there as usual */
typeset(cp, type_flags, 0, 0, 0);
}
if (Flag(FXTRACE)) {
change_xtrace(2, Nee);
if (ap[rv = 0]) {
xtrace_ap_loop:
print_value_quoted(shl_xtrace, ap[rv]);
if (ap[++rv]) {
shf_putc(' ', shl_xtrace);
goto xtrace_ap_loop;
}
}
change_xtrace(1, Nee);
}
if ((cp = *ap) == NULL) {
rv = subst_exstat;
goto Leave;
} else if (!tp) {
if (Flag(FRESTRICTED) && mksh_vdirsep(cp)) {
kwarnf(KWF_PREFIX | KWF_FILELINE | KWF_TWOMSG |
KWF_NOERRNO, cp, "restricted");
rv = 1;
goto Leave;
}
tp = findcom(cp, fcflags);
}
switch (tp->type) {
/* shell built-in */
case CSHELL:
do_call_builtin:
if (l_expand != l_assign)
l_assign->flags |= (tp->flag & NEXTLOC_BI);
rv = call_builtin(tp, (const char **)ap, null, resetspec);
break;
/* function call */
case CFUNC:
if (!(tp->flag & ISSET)) {
struct tbl *ftp;
if (!tp->u.fpath) {
fpath_error:
rv = (tp->u2.errnov == ENOENT) ? 127 : 126;
kwarnf(KWF_ERR(rv) | KWF_VERRNO |
KWF_PREFIX | KWF_FILELINE | KWF_TWOMSG,
tp->u2.errnov, cp,
"function definition file");
break;
}
errno = 0;
if (include(tp->u.fpath, NULL, Nee) < 0 ||
!(ftp = findfunc(cp, hash(cp), Nee)) ||
!(ftp->flag & ISSET)) {
rv = errno;
if ((ftp = findcom(cp, FC_BI)) &&
(ftp->type == CSHELL) &&
(ftp->flag & LOW_BI)) {
tp = ftp;
goto do_call_builtin;
}
if (rv) {
tp->u2.errnov = rv;
cp = tp->u.fpath;
goto fpath_error;
}
kwarnf0(KWF_ERR(127) |
KWF_PREFIX | KWF_FILELINE | KWF_NOERRNO,
Tf_sD_s_s, cp,
"function not defined by", tp->u.fpath);
rv = 127;
break;
}
tp = ftp;
}
/*
* ksh functions set $0 to function name, POSIX
* functions leave $0 unchanged.
*/
old_kshname = kshname;
if (tp->flag & FKSH)
kshname = ap[0];
else
ap[0] = kshname;
e->loc->argv = ap;
for (i = 0; *ap++ != NULL; i++)
;
e->loc->argc = i - 1;
/*
* ksh-style functions handle getopts sanely,
* Bourne/POSIX functions are insane...
*/
if (tp->flag & FKSH) {
e->loc->flags |= BF_DOGETOPTS;
e->loc->getopts_state = user_opt;
getopts_reset(1);
}
for (type_flags = 0; type_flags < FNFLAGS; ++type_flags)
old_flags[type_flags] = shell_flags[type_flags];
change_xtrace((Flag(FXTRACEREC) ? Flag(FXTRACE) : 0) |
((tp->flag & TRACE) ? 1 : 0), Nee);
old_inuse = tp->flag & FINUSE;
tp->flag |= FINUSE;
e->type = E_FUNC;
if (!(i = kshsetjmp(e->jbuf))) {
execute(tp->val.t, flags & XERROK, NULL);
i = LRETURN;
}
kshname = old_kshname;
change_xtrace(old_flags[(int)FXTRACE], Nee);
#ifndef MKSH_LEGACY_MODE
if (tp->flag & FKSH) {
/* Korn style functions restore Flags on return */
old_flags[(int)FXTRACE] = Flag(FXTRACE);
/* some must not be restored / need special handling */
for (type_flags = 0; type_flags < FNFLAGS; ++type_flags)
#ifndef MKSH_UNEMPLOYED
if (type_flags == FMONITOR)
change_flag(type_flags, OF_INTERNAL,
old_flags[type_flags]);
else
#endif
if (type_flags != FPRIVILEGED)
shell_flags[type_flags] =
old_flags[type_flags];
}
#endif
tp->flag = (tp->flag & ~FINUSE) | old_inuse;
/*
* Were we deleted while executing? If so, free the
* execution tree.
*/
if ((tp->flag & (FDELETE|FINUSE)) == FDELETE) {
if (tp->flag & ALLOC) {
tp->flag &= ~ALLOC;
tfree(tp->val.t, tp->areap);
}
tp->flag = 0;
}
switch (i) {
case LRETURN:
case LERROR:
case LERREXT:
rv = exstat & 0xFF;
break;
case LINTR:
case LEXIT:
case LLEAVE:
case LSHELL:
quitenv(NULL);
unwind(i);
/* NOTREACHED */
default:
quitenv(NULL);
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tunexpected_type, Tunwind, Tfunction, i);
}
break;
/* executable command */
case CEXEC:
/* tracked alias */
case CTALIAS:
if (!(tp->flag&ISSET)) {
if (tp->u2.errnov == ENOENT) {
rv = 127;
kwarnf(KWF_ERR(127) |
KWF_PREFIX | KWF_FILELINE | KWF_TWOMSG |
KWF_NOERRNO, cp, Tinacc_not_found);
} else {
rv = 126;
kwarnf(KWF_ERR(126) | KWF_VERRNO |
KWF_PREFIX | KWF_FILELINE | KWF_TWOMSG,
tp->u2.errnov, cp, "can't execute");
}
break;
}
/* set $_ to program's full path */
/* setstr() can't fail here */
setstr(typeset("_", LOCAL | EXPORT, 0, INTEGER, 0),
tp->val.s, KSH_RETURN_ERROR);
/* to fork, we set up a TEXEC node and call execute */
texec.type = TEXEC;
/* for vistree/dumptree */
texec.left = t;
texec.str = tp->val.s;
texec.args = ap;
/* in this case we do not fork, of course */
if (flags & XEXEC) {
if (exec_argv0)
texec.args[0] = exec_argv0;
j_exit();
}
rv = exchild(&texec, flags, xerrok, -1);
break;
}
Leave:
if (flags & XEXEC) {
exstat = rv & 0xFF;
unwind(LEXIT);
}
return (rv);
}
static void
scriptexec(struct op *tp, const char **ap)
{
const char *sh;
#ifndef MKSH_SMALL
int fd;
unsigned char buf[68];
#endif
union mksh_ccphack args, cap;
sh = str_val(global(TEXECSHELL));
if (sh && *sh)
sh = search_path(sh, path, X_OK, NULL);
if (!sh || !*sh)
sh = MKSH_DEFAULT_EXECSHELL;
*tp->args-- = tp->str;
#ifndef MKSH_SMALL
if ((fd = binopen2(tp->str, O_RDONLY | O_MAYEXEC)) >= 0) {
unsigned char *cp;
#ifndef MKSH_EBCDIC
unsigned short m;
#endif
ssize_t n;
/* read first couple of octets from file */
n = read(fd, buf, sizeof(buf) - 1);
close(fd);
/* read error or short read? */
if (n < 5)
goto nomagic;
/* terminate buffer */
buf[n] = '\0';
/* scan for newline or NUL (end of buffer) */
cp = buf;
while (!ctype(*cp, C_NL | C_NUL))
++cp;
/* if the shebang line is longer than MAXINTERP, bail out */
if (!*cp)
goto noshebang;
/* replace newline by NUL */
*cp = '\0';
/* restore start of shebang position */
cp = buf;
/* bail out if no shebang magic found */
if (cp[0] == '#' && cp[1] == '!')
cp += 2;
#ifdef __OS2__
else if (!strncmp(cp, Textproc, 7) &&
ctype(cp[7], C_BLANK))
cp += 8;
#endif
else
goto noshebang;
/* skip whitespace before shell name */
while (ctype(*cp, C_BLANK))
++cp;
/* just whitespace on the line? */
if (*cp == '\0')
goto noshebang;
/* no, we actually found an interpreter name */
sh = (char *)cp;
/* look for end of shell/interpreter name */
while (!ctype(*cp, C_BLANK | C_NUL))
++cp;
/* any arguments? */
if (*cp) {
*cp++ = '\0';
/* skip spaces before arguments */
while (ctype(*cp, C_BLANK))
++cp;
/* pass it all in ONE argument (historic reasons) */
if (*cp)
*tp->args-- = (char *)cp;
}
#ifdef __OS2__
/*
* On OS/2, the directory structure differs from normal
* Unix, which can make many scripts whose shebang
* hardcodes the path to an interpreter fail (and there
* might be no /usr/bin/env); for user convenience, if
* the specified interpreter is not usable, do a PATH
* search to find it.
*/
if (mksh_vdirsep(sh) && !search_path(sh, path, X_OK, NULL)) {
cp = search_path(_getname(sh), path, X_OK, NULL);
if (cp)
sh = cp;
}
#endif
goto nomagic;
noshebang:
#ifndef MKSH_EBCDIC
m = buf[0] << 8 | buf[1];
if (m == 0x7F45 && buf[2] == 'L' && buf[3] == 'F')
kerrf0(KWF_ERR(1) | KWF_PREFIX | KWF_FILELINE |
KWF_NOERRNO, "%s: not executable: %d-bit ELF file",
tp->str, 32 * buf[4]);
if ((m == /* OMAGIC */ 0407) ||
(m == /* NMAGIC */ 0410) ||
(m == /* ZMAGIC */ 0413) ||
(m == /* QMAGIC */ 0314) ||