-
Notifications
You must be signed in to change notification settings - Fork 30
/
xec.c
3832 lines (3740 loc) · 91.7 KB
/
xec.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
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2012 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* UNIX shell parse tree executer
*
* David Korn
* AT&T Labs
*
*/
#include "defs.h"
#include <fcin.h>
#include "variables.h"
#include "path.h"
#include "name.h"
#include "io.h"
#include "shnodes.h"
#include "jobs.h"
#include "test.h"
#include "builtins.h"
#include "FEATURE/time"
#include "FEATURE/externs"
#include "FEATURE/locale"
#include "streval.h"
#if !_std_malloc
# include <vmalloc.h>
#endif
#if _lib_vfork
# include <ast_vfork.h>
#else
# define vfork() fork()
#endif
#define SH_NTFORK SH_TIMING
#define NV_BLTPFSH NV_ARRAY
#if _lib_nice
extern int nice(int);
#endif /* _lib_nice */
#if !_lib_spawnveg
# define spawnveg(a,b,c,d) spawnve(a,b,c)
#endif /* !_lib_spawnveg */
#if SHOPT_SPAWN
static pid_t sh_ntfork(Shell_t*,const Shnode_t*,char*[],int*,int);
#endif /* SHOPT_SPAWN */
static void sh_funct(Shell_t *,Namval_t*, int, char*[], struct argnod*,int);
static void coproc_init(Shell_t*, int pipes[]);
static void *timeout;
static char nlock;
static char pipejob;
static char nopost;
static int restorefd;
struct funenv
{
Namval_t *node;
struct argnod *env;
Namval_t **nref;
};
/* ======== command execution ========*/
#if !SHOPT_DEVFD
static void fifo_check(void *handle)
{
Shell_t *shp = (Shell_t*)handle;
pid_t pid = getppid();
if(pid==1)
{
unlink(shp->fifo);
sh_done(shp,0);
}
}
#endif /* !SHOPT_DEVFD */
#if _lib_getrusage
/* getrusage tends to have higher precision */
static void get_cpu_times(Shell_t *shp, struct timeval *tv_usr, struct timeval *tv_sys)
{
NOT_USED(shp);
struct rusage usage_self, usage_child;
getrusage(RUSAGE_SELF, &usage_self);
getrusage(RUSAGE_CHILDREN, &usage_child);
timeradd(&usage_self.ru_utime, &usage_child.ru_utime, tv_usr);
timeradd(&usage_self.ru_stime, &usage_child.ru_stime, tv_sys);
}
#else
#ifdef timeofday
static void get_cpu_times(Shell_t *shp, struct timeval *tv_usr, struct timeval *tv_sys)
{
struct tms cpu_times;
struct timeval tv1, tv2;
double dtime;
if(times(&cpu_times) == (clock_t)-1)
errormsg(SH_DICT, ERROR_exit(1), "times(3) failed: %s", strerror(errno));
dtime = (double)cpu_times.tms_utime / shp->gd->lim.clk_tck;
tv1.tv_sec = dtime / 60;
tv1.tv_usec = 1000000 * (dtime - tv1.tv_sec);
dtime = (double)cpu_times.tms_cutime / shp->gd->lim.clk_tck;
tv2.tv_sec = dtime / 60;
tv2.tv_usec = 1000000 * (dtime - tv2.tv_sec);
timeradd(&tv1, &tv2, tv_usr);
dtime = (double)cpu_times.tms_stime / shp->gd->lim.clk_tck;
tv1.tv_sec = dtime / 60;
tv1.tv_usec = 1000000 * (dtime - tv1.tv_sec);
dtime = (double)cpu_times.tms_cstime / shp->gd->lim.clk_tck;
tv2.tv_sec = dtime / 60;
tv2.tv_usec = 1000000 * (dtime - tv2.tv_sec);
timeradd(&tv1, &tv2, tv_sys);
}
#endif /* timeofday */
#endif /* _lib_getrusage */
#ifdef timeofday
/* 'inline' is commented out because C89 doesn't have it */
static /*inline*/ double timeval_to_double(struct timeval tv)
{
return (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0);
}
#endif
/*
* The following two functions allow command substitution for non-builtins
* to use a pipe and to wait for the pipe to close before restoring to a
* temp file.
*/
static int subpipe[3],subdup,tsetio,usepipe;
static int iousepipe(Shell_t *shp)
{
int fd=sffileno(sfstdout),i,err=errno;
if(usepipe)
{
usepipe++;
sh_iounpipe(shp);
}
if(sh_rpipe(subpipe) < 0)
return(0);
usepipe++;
if(shp->comsub!=1)
{
subpipe[2] = sh_fcntl(subpipe[1],F_DUPFD,10);
sh_close(subpipe[1]);
return(1);
}
subpipe[2] = sh_fcntl(fd,F_dupfd_cloexec,10);
sh_iovalidfd(shp,subpipe[2]);
shp->fdstatus[subpipe[2]] = shp->fdstatus[1];
while(close(fd)<0 && errno==EINTR)
errno = err;
fcntl(subpipe[1],F_DUPFD,fd);
shp->fdstatus[1] = shp->fdstatus[subpipe[1]]&~IOCLEX;
sh_close(subpipe[1]);
if(subdup=shp->subdup) for(i=0; i < 10; i++)
{
if(subdup&(1<<i))
{
sh_close(i);
fcntl(1,F_DUPFD,i);
shp->fdstatus[i] = shp->fdstatus[1];
}
}
return(1);
}
void sh_iounpipe(Shell_t *shp)
{
int fd=sffileno(sfstdout),n,err=errno;
char buff[SF_BUFSIZE];
if(!usepipe)
return;
--usepipe;
if(shp->comsub>1)
{
sh_close(subpipe[2]);
while(read(subpipe[0],buff,sizeof(buff))>0);
goto done;
}
while(close(fd)<0 && errno==EINTR)
errno = err;
fcntl(subpipe[2], F_DUPFD, fd);
shp->fdstatus[1] = shp->fdstatus[subpipe[2]];
if(subdup) for(n=0; n < 10; n++)
{
if(subdup&(1<<n))
{
sh_close(n);
fcntl(1, F_DUPFD, n);
shp->fdstatus[n] = shp->fdstatus[1];
}
}
shp->subdup = 0;
sh_close(subpipe[2]);
if(usepipe==0) while(1)
{
while(job.waitsafe && job.savesig==SIGCHLD)
{
if(!vmbusy())
{
job.in_critical++;
job_reap(SIGCHLD);
job.in_critical--;
break;
}
sh_delay(1,0);
}
if((n = read(subpipe[0],buff,sizeof(buff)))==0)
break;
if(n>0)
sfwrite(sfstdout,buff,n);
else if(errno!=EINTR)
break;
}
done:
sh_close(subpipe[0]);
subpipe[0] = -1;
tsetio = 0;
usepipe = 0;
}
/*
* print time <t> in h:m:s format with precision <p>
*/
#ifdef timeofday
static void l_time(Sfio_t *outfile, struct timeval *tv, int precision)
{
int hr = tv->tv_sec / (60 * 60);
int min = (tv->tv_sec / 60) % 60;
int sec = tv->tv_sec % 60;
int frac = tv->tv_usec;
/* scale fraction from micro to milli, centi, or deci second according to precision */
int n;
for(n = 3 + (3 - precision); n > 0; --n)
frac /= 10;
#else
/* fallback */
static void l_time(Sfio_t *outfile,register clock_t t,int precision)
{
register int min, sec, frac;
register int hr;
if(precision)
{
frac = t%shgd->lim.clk_tck;
frac = (frac*100)/shgd->lim.clk_tck;
}
t /= shgd->lim.clk_tck;
sec = t%60;
t /= 60;
min = t%60;
hr = t/60;
#endif
if(hr)
sfprintf(outfile,"%dh",hr);
if(precision)
sfprintf(outfile,"%dm%02d%c%0*ds",min,sec,GETDECIMAL(0),precision,frac);
else
sfprintf(outfile,"%dm%02ds",min,sec);
}
#define TM_REAL_IDX 0
#define TM_USR_IDX 1
#define TM_SYS_IDX 2
#ifdef timeofday
static void p_time(Shell_t *shp, Sfio_t *out, const char *format, struct timeval tm[3])
#else
static void p_time(Shell_t *shp, Sfio_t *out, const char *format, clock_t *tm)
#endif
{
int c,n,offset = staktell();
const char *first;
Stk_t *stkp = shp->stk;
#ifdef timeofday
struct timeval tv_cpu_sum;
struct timeval *tvp;
#else
double d;
#endif
for(first=format; *format; format++)
{
c = *format;
if(c!='%')
continue;
unsigned char l_modifier = 0;
int precision = 3;
sfwrite(stkp, first, format-first);
c = *++format;
if(c=='\0')
{
/* If a lone percent is the last character of the format pretend
the user had written `%%` for a literal percent */
sfwrite(stkp, "%", 1);
first = format + 1;
break;
}
else if(c=='%')
{
first = format;
continue;
}
if(c>='0' && c <='9')
{
precision = (c>'3')?3:(c-'0');
c = *++format;
}
if(c=='P')
{
#ifdef timeofday
struct timeval tv_real = tm[TM_REAL_IDX];
struct timeval tv_cpu;
timeradd(&tm[TM_USR_IDX], &tm[TM_SYS_IDX], &tv_cpu);
double d = timeval_to_double(tv_real);
if(d)
d = 100.0 * timeval_to_double(tv_cpu) / d;
sfprintf(stkp, "%.*f", precision, d);
first = format + 1;
continue;
#else
if(d=tm[0])
d = 100.*(((double)(tm[1]+tm[2]))/d);
precision = 2;
goto skip;
#endif
}
if(c=='l')
{
l_modifier = 1;
c = *++format;
}
#ifdef timeofday
if(c=='R')
tvp = &tm[TM_REAL_IDX];
else if(c=='U')
tvp = &tm[TM_USR_IDX];
else if(c=='S')
tvp = &tm[TM_SYS_IDX];
else
{
stkseek(stkp,offset);
errormsg(SH_DICT,ERROR_exit(0),e_badtformat,c);
return;
}
if(l_modifier)
l_time(stkp, tvp, precision);
else
{
/* scale fraction from micro to milli, centi, or deci second according to precision */
int n, frac = tvp->tv_usec;
for(n = 3 + (3 - precision); n > 0; --n)
frac /= 10;
if(precision)
sfprintf(stkp, "%d%c%0*d", tvp->tv_sec, GETDECIMAL(0), precision, frac);
else
sfprintf(stkp, "%d", tvp->tv_sec);
}
#else
if(c=='R')
n = 0;
else if(c=='U')
n = 1;
else if(c=='S')
n = 2;
else
{
stkseek(stkp,offset);
errormsg(SH_DICT,ERROR_exit(0),e_badtformat,c);
return;
}
d = (double)tm[n]/shp->gd->lim.clk_tck;
skip:
if(l_modifier)
l_time(stkp, tm[n], precision);
else
sfprintf(stkp,"%.*f",precision, d);
#endif
first = format+1;
}
if(format>first)
sfwrite(stkp,first, format-first);
sfputc(stkp,'\n');
n = stktell(stkp)-offset;
sfwrite(out,stkptr(stkp,offset),n);
stkseek(stkp,offset);
}
#if SHOPT_OPTIMIZE
/*
* clear argument pointers that point into the stack
*/
static int p_arg(struct argnod*,int);
static int p_switch(struct regnod*);
static int p_comarg(register struct comnod *com)
{
Namval_t *np=com->comnamp;
int n = p_arg(com->comset,ARG_ASSIGN);
if(com->comarg && (com->comtyp&COMSCAN))
n+= p_arg(com->comarg,0);
if(com->comstate && np)
{
/* call builtin to cleanup state */
Shbltin_t *bp = &sh.bltindata;
void *save_ptr = bp->ptr;
void *save_data = bp->data;
bp->bnode = np;
bp->vnode = com->comnamq;
bp->ptr = nv_context(np);
bp->data = com->comstate;
bp->flags = SH_END_OPTIM;
((Shbltin_f)funptr(np))(0,(char**)0, bp);
bp->ptr = save_ptr;
bp->data = save_data;
}
com->comstate = 0;
if(com->comarg && !np)
n++;
return(n);
}
extern void sh_optclear(Shell_t*, void*);
static int sh_tclear(register Shnode_t *t)
{
int n=0;
if(!t)
return(0);
switch(t->tre.tretyp&COMMSK)
{
case TTIME:
case TPAR:
return(sh_tclear(t->par.partre));
case TCOM:
return(p_comarg((struct comnod*)t));
case TSETIO:
case TFORK:
return(sh_tclear(t->fork.forktre));
case TIF:
n=sh_tclear(t->if_.iftre);
n+=sh_tclear(t->if_.thtre);
n+=sh_tclear(t->if_.eltre);
return(n);
case TWH:
if(t->wh.whinc)
n=sh_tclear((Shnode_t*)(t->wh.whinc));
n+=sh_tclear(t->wh.whtre);
n+=sh_tclear(t->wh.dotre);
return(n);
case TLST:
case TAND:
case TORF:
case TFIL:
n=sh_tclear(t->lst.lstlef);
return(n+sh_tclear(t->lst.lstrit));
case TARITH:
return(p_arg(t->ar.arexpr,ARG_ARITH));
case TFOR:
n=sh_tclear(t->for_.fortre);
return(n+sh_tclear((Shnode_t*)t->for_.forlst));
case TSW:
n=p_arg(t->sw.swarg,0);
return(n+p_switch(t->sw.swlst));
case TFUN:
n=sh_tclear(t->funct.functtre);
return(n+sh_tclear((Shnode_t*)t->funct.functargs));
case TTST:
if((t->tre.tretyp&TPAREN)==TPAREN)
return(sh_tclear(t->lst.lstlef));
else
{
n=p_arg(&(t->lst.lstlef->arg),0);
if(t->tre.tretyp&TBINARY)
n+=p_arg(&(t->lst.lstrit->arg),0);
}
}
return(n);
}
static int p_arg(register struct argnod *arg,int flag)
{
while(arg)
{
if(strlen(arg->argval) || (arg->argflag==ARG_RAW))
arg->argchn.ap = 0;
else if(flag==0)
sh_tclear((Shnode_t*)arg->argchn.ap);
else
sh_tclear(((struct fornod*)arg->argchn.ap)->fortre);
arg = arg->argnxt.ap;
}
return(0);
}
static int p_switch(register struct regnod *reg)
{
int n=0;
while(reg)
{
n+=p_arg(reg->regptr,0);
n+=sh_tclear(reg->regcom);
reg = reg->regnxt;
}
return(n);
}
# define OPTIMIZE_FLAG (ARG_OPTIMIZE)
# define OPTIMIZE (flags&OPTIMIZE_FLAG)
#else
# define OPTIMIZE_FLAG (0)
# define OPTIMIZE (0)
# define sh_tclear(x)
#endif /* SHOPT_OPTIMIZE */
static void out_pattern(Sfio_t *iop, register const char *cp, int n)
{
register int c;
do
{
switch(c= *cp)
{
case 0:
if(n<0)
return;
c = n;
break;
case '\n':
sfputr(iop,"$'\\n",'\'');
continue;
case '\\':
if (!(c = *++cp))
c = '\\';
/*FALLTHROUGH*/
case ' ':
case '<': case '>': case ';':
case '$': case '`': case '\t':
sfputc(iop,'\\');
break;
}
sfputc(iop,c);
}
while(*cp++);
}
static void out_string(Sfio_t *iop, register const char *cp, int c, int quoted)
{
if(quoted)
{
int n = stktell(stkstd);
cp = sh_fmtq(cp);
if(iop==stkstd && cp==stkptr(stkstd,n))
{
*stkptr(stkstd,stktell(stkstd)-1) = c;
return;
}
}
sfputr(iop,cp,c);
}
struct Level
{
Namfun_t hdr;
short maxlevel;
};
/*
* this is for a debugger but it hasn't been tested yet
* if a debug script sets .sh.level it should set up the scope
* as if you were executing in that level
*/
static void put_level(Namval_t* np,const char *val,int flags,Namfun_t *fp)
{
Shscope_t *sp;
struct Level *lp = (struct Level*)fp;
int16_t level, oldlevel = (int16_t)nv_getnum(np);
nv_putv(np,val,flags,fp);
if(!val)
{
fp = nv_stack(np, NIL(Namfun_t*));
if(fp && !fp->nofree)
free((void*)fp);
return;
}
level = nv_getnum(np);
if(level<0 || level > lp->maxlevel)
{
nv_putv(np, (char*)&oldlevel, NV_INT16, fp);
/* perhaps this should be an error */
return;
}
if(level==oldlevel)
return;
if(sp = sh_getscope(level,SEEK_SET))
{
sh_setscope(sp);
error_info.id = sp->cmdname;
}
}
static const Namdisc_t level_disc = { sizeof(struct Level), put_level };
static struct Level *init_level(Shell_t *shp,int level)
{
struct Level *lp = newof(NiL,struct Level,1,0);
lp->maxlevel = level;
_nv_unset(SH_LEVELNOD,0);
nv_onattr(SH_LEVELNOD,NV_INT16|NV_NOFREE);
shp->last_root = nv_dict(DOTSHNOD);
nv_putval(SH_LEVELNOD,(char*)&lp->maxlevel,NV_INT16);
lp->hdr.disc = &level_disc;
nv_disc(SH_LEVELNOD,&lp->hdr,NV_FIRST);
return(lp);
}
/*
* write the current command on the stack and make it available as .sh.command
*/
int sh_debug(Shell_t *shp, const char *trap, const char *name, const char *subscript, char *const argv[], int flags)
{
Stk_t *stkp=shp->stk;
struct sh_scoped savst;
Namval_t *np = SH_COMMANDNOD;
int n=4, offset=stktell(stkp);
char *sav = stkfreeze(stkp,0);
const char *cp = "+=( ";
Sfio_t *iop = stkstd;
short level;
if(shp->indebug)
return(0);
shp->indebug = 1;
if(name)
{
sfputr(iop,name,-1);
if(subscript)
{
sfputc(iop,'[');
out_string(iop,subscript,']',1);
}
if(!(flags&ARG_APPEND))
cp+=1, n-=1;
if(!(flags&ARG_ASSIGN))
n -= 2;
sfwrite(iop,cp,n);
}
if(*argv && !(flags&ARG_RAW))
out_string(iop, *argv++,' ', 0);
n = (flags&ARG_ARITH);
while(cp = *argv++)
{
if((flags&ARG_EXP) && argv[1]==0)
out_pattern(iop, cp,' ');
else
out_string(iop, cp,' ',n?0: (flags&(ARG_RAW|ARG_NOGLOB))||*argv);
}
if(flags&ARG_ASSIGN)
sfputc(iop,')');
else if(iop==stkstd)
*stkptr(stkp,stktell(stkp)-1) = 0;
np->nvalue.cp = stkfreeze(stkp,1);
/* now setup .sh.level variable */
shp->st.lineno = error_info.line;
level = shp->fn_depth+shp->dot_depth;
shp->last_root = nv_dict(DOTSHNOD);
if(!SH_LEVELNOD->nvfun || !SH_LEVELNOD->nvfun->disc || nv_isattr(SH_LEVELNOD,NV_INT16|NV_NOFREE)!=(NV_INT16|NV_NOFREE))
init_level(shp,level);
else
nv_putval(SH_LEVELNOD,(char*)&level,NV_INT16);
savst = shp->st;
shp->st.trap[SH_DEBUGTRAP] = 0;
n = sh_trap(trap,0);
np->nvalue.cp = 0;
shp->indebug = 0;
if(shp->st.cmdname)
error_info.id = shp->st.cmdname;
nv_putval(SH_PATHNAMENOD,shp->st.filename,NV_NOFREE);
nv_putval(SH_FUNNAMENOD,shp->st.funname,NV_NOFREE);
shp->st = savst;
if(sav != stkptr(stkp,0))
stkset(stkp,sav,offset);
else
stkseek(stkp,offset);
return(n);
}
/*
* Given stream <iop> compile and execute
*/
int sh_eval(register Sfio_t *iop, int mode)
{
register Shnode_t *t;
Shell_t *shp = sh_getinterp();
struct slnod *saveslp = shp->st.staklist;
int jmpval;
struct checkpt *pp = (struct checkpt*)shp->jmplist;
struct checkpt *buffp = (struct checkpt*)stkalloc(shp->stk,sizeof(struct checkpt));
static Sfio_t *io_save;
volatile int traceon=0, lineno=0;
int binscript=shp->binscript;
char comsub = shp->comsub;
io_save = iop; /* preserve correct value across longjmp */
shp->binscript = 0;
shp->comsub = 0;
#define SH_TOPFUN 0x8000 /* this is a temporary tksh hack */
if (mode & SH_TOPFUN)
{
mode ^= SH_TOPFUN;
shp->fn_reset = 1;
}
sh_pushcontext(shp,buffp,SH_JMPEVAL);
buffp->olist = pp->olist;
jmpval = sigsetjmp(buffp->buff,0);
while(jmpval==0)
{
if(mode&SH_READEVAL)
{
lineno = shp->inlineno;
if(traceon=sh_isoption(SH_XTRACE))
sh_offoption(SH_XTRACE);
}
t = (Shnode_t*)sh_parse(shp,iop,(mode&(SH_READEVAL|SH_FUNEVAL))?mode&SH_FUNEVAL:SH_NL);
if(!(mode&SH_FUNEVAL) || !sfreserve(iop,0,0))
{
if(!(mode&SH_READEVAL))
sfclose(iop);
io_save = 0;
mode &= ~SH_FUNEVAL;
}
mode &= ~SH_READEVAL;
if(!sh_isoption(SH_VERBOSE))
sh_offstate(SH_VERBOSE);
if((mode&~SH_FUNEVAL) && shp->gd->hist_ptr)
{
hist_flush(shp->gd->hist_ptr);
mode = sh_state(SH_INTERACTIVE);
}
sh_exec(t,sh_isstate(SH_ERREXIT)|sh_isstate(SH_NOFORK)|(mode&~SH_FUNEVAL));
if(!(mode&SH_FUNEVAL))
break;
}
sh_popcontext(shp,buffp);
shp->binscript = binscript;
shp->comsub = comsub;
if(traceon)
sh_onoption(SH_XTRACE);
if(lineno)
shp->inlineno = lineno;
if(io_save)
{
sfclose(io_save);
io_save = 0;
}
sh_freeup(shp);
shp->st.staklist = saveslp;
shp->fn_reset = 0;
if(jmpval>SH_JMPEVAL)
siglongjmp(*shp->jmplist,jmpval);
return(shp->exitval);
}
/*
* returns 1 when option -<c> is specified
*/
static int checkopt(char *argv[], int c)
{
char *cp;
while(cp = *++argv)
{
if(*cp=='+')
continue;
if(*cp!='-' || cp[1]=='-')
break;
if(strchr(++cp,c))
return(1);
if(*cp=='h' && cp[1]==0 && *++argv==0)
break;
}
return(0);
}
static void free_list(struct openlist *olist)
{
struct openlist *item,*next;
for(item=olist;item;item=next)
{
next = item->next;
free((void*)item);
}
}
/*
* set ${.sh.name} and ${.sh.subscript}
* set _ to reference for ${.sh.name}[$.sh.subscript]
*/
static int set_instance(Shell_t *shp,Namval_t *nq, Namval_t *node, struct Namref *nr)
{
char *sp=0,*cp;
Namarr_t *ap;
Namval_t *np;
if(!nv_isattr(nq,NV_MINIMAL|NV_EXPORT|NV_ARRAY) && (np=(Namval_t*)nq->nvenv) && nv_isarray(np))
nq = np;
cp = nv_name(nq);
memset(nr,0,sizeof(*nr));
nr->np = nq;
nr->root = shp->var_tree;
nr->table = shp->last_table;
#if SHOPT_NAMESPACE
if(!nr->table && shp->namespace)
nr->table = shp->namespace;
#endif /* SHOPT_NAMESPACE */
shp->instance = 1;
if((ap=nv_arrayptr(nq)) && (sp = nv_getsub(nq)))
sp = strdup(sp);
shp->instance = 0;
if(shp->var_tree!=shp->var_base && !nv_search((char*)nq,nr->root,HASH_BUCKET|HASH_NOSCOPE))
{
#if SHOPT_NAMESPACE
nr->root = shp->namespace?nv_dict(shp->namespace):shp->var_base;
#else
nr->root = shp->var_base;
#endif /* SHOPT_NAMESPACE */
}
nv_putval(SH_NAMENOD, cp, NV_NOFREE);
memcpy(node,L_ARGNOD,sizeof(*node));
L_ARGNOD->nvalue.nrp = nr;
L_ARGNOD->nvflag = NV_REF|NV_NOFREE;
L_ARGNOD->nvfun = 0;
L_ARGNOD->nvenv = 0;
if(sp)
{
nv_putval(SH_SUBSCRNOD,nr->sub=sp,NV_NOFREE);
return(ap->nelem&ARRAY_SCAN);
}
return(0);
}
static void unset_instance(Namval_t *nq, Namval_t *node, struct Namref *nr,long mode)
{
L_ARGNOD->nvalue.nrp = node->nvalue.nrp;
L_ARGNOD->nvflag = node->nvflag;
L_ARGNOD->nvfun = node->nvfun;
if(nr->sub)
{
nv_putsub(nr->np, nr->sub, mode);
free((void*)nr->sub);
}
_nv_unset(SH_NAMENOD,0);
_nv_unset(SH_SUBSCRNOD,0);
}
#if SHOPT_FILESCAN
static Sfio_t *openstream(Shell_t *shp, struct ionod *iop, int *save)
{
int savein, fd = sh_redirect(shp,iop,3);
Sfio_t *sp;
savein = dup(0);
if(fd==0)
fd = savein;
sp = sfnew(NULL,NULL,SF_UNBOUND,fd,SF_READ);
close(0);
open(e_devnull,O_RDONLY);
shp->offsets[0] = -1;
shp->offsets[1] = 0;
*save = savein;
return(sp);
}
#endif /* SHOPT_FILESCAN */
#if SHOPT_NAMESPACE
static Namval_t *enter_namespace(Shell_t *shp, Namval_t *nsp)
{
Namval_t *path=nsp, *fpath=nsp, *onsp=shp->namespace;
Dt_t *root=0,*oroot=0;
char *val;
if(nsp)
{
if(!nv_istable(nsp))
nsp = 0;
else if(nv_dict(nsp)->view!=shp->var_base)
return(onsp);
}
if(!nsp && !onsp)
return(0);
if(onsp == nsp)
return(nsp);
if(onsp)
{
oroot = nv_dict(onsp);
if(!nsp)
{
path = nv_search(PATHNOD->nvname,oroot,HASH_NOSCOPE);
fpath = nv_search(FPATHNOD->nvname,oroot,HASH_NOSCOPE);
}
if(shp->var_tree==oroot)
{
shp->var_tree = shp->var_tree->view;
oroot = shp->var_base;
}
}
if(nsp)
{
if(shp->var_tree==shp->var_base)
shp->var_tree = nv_dict(nsp);
else
{
for(root=shp->var_tree; root->view!=oroot;root=root->view);
dtview(root,nv_dict(nsp));
}
}
shp->namespace = nsp;
if(path && (path = nv_search(PATHNOD->nvname,shp->var_tree,HASH_NOSCOPE)) && (val=nv_getval(path)))
nv_putval(path,val,NV_RDONLY);
if(fpath && (fpath = nv_search(FPATHNOD->nvname,shp->var_tree,HASH_NOSCOPE)) && (val=nv_getval(fpath)))
nv_putval(fpath,val,NV_RDONLY);
return(onsp);
}
#endif /* SHOPT_NAMESPACE */
int sh_exec(register const Shnode_t *t, int flags)
{
register Shell_t *shp = sh_getinterp();
Stk_t *stkp = shp->stk;
int unpipe=0;
sh_sigcheck(shp);
if(t && !shp->st.execbrk && !sh_isoption(SH_NOEXEC))
{
register int type = flags;
register char *com0 = 0;
int errorflg = (type&sh_state(SH_ERREXIT))|OPTIMIZE;
int execflg = (type&sh_state(SH_NOFORK));
int execflg2 = (type&sh_state(SH_FORKED));
int mainloop = (type&sh_state(SH_INTERACTIVE));
#if SHOPT_SPAWN
int ntflag = (type&sh_state(SH_NTFORK));
#else
int ntflag = 0;
#endif
int topfd = shp->topfd;
char *sav=stkfreeze(stkp,0);
char *cp=0, **com=0, *comn;
int argn;
int skipexitset = 0;
volatile int was_interactive = 0;
volatile int was_errexit = sh_isstate(SH_ERREXIT);
volatile int was_monitor = sh_isstate(SH_MONITOR);
volatile int echeck = 0;
if(flags&sh_state(SH_INTERACTIVE))
{
if(pipejob==2)
job_unlock();
nlock = 0;
pipejob = 0;
job.curpgid = 0;
job.curjobid = 0;
flags &= ~sh_state(SH_INTERACTIVE);
}
sh_offstate(SH_DEFPATH);
if(!(flags & sh_state(SH_ERREXIT)))
sh_offstate(SH_ERREXIT);
type = t->tre.tretyp;
shp->exitval=0;
shp->lastsig = 0;
shp->lastpath = 0;
if(shp->exittrap || shp->errtrap)
execflg = 0;
switch(type&COMMSK)
{
case TCOM:
{
register struct argnod *argp;
char *trap;
Namval_t *np, *nq, *last_table;
struct ionod *io;
int command=0, flgs=NV_ASSIGN;