fix breakage found by randomconfig
[platform/upstream/busybox.git] / shell / ash.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * ash shell port for busybox
4  *
5  * Copyright (c) 1989, 1991, 1993, 1994
6  *      The Regents of the University of California.  All rights reserved.
7  *
8  * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
9  * was re-ported from NetBSD and debianized.
10  *
11  *
12  * This code is derived from software contributed to Berkeley by
13  * Kenneth Almquist.
14  *
15  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
16  *
17  * Original BSD copyright notice is retained at the end of this file.
18  */
19
20 /*
21  * rewrite arith.y to micro stack based cryptic algorithm by
22  * Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
23  *
24  * Modified by Paul Mundt <lethal@linux-sh.org> (c) 2004 to support
25  * dynamic variables.
26  *
27  * Modified by Vladimir Oleynik <dzo@simtreas.ru> (c) 2001-2005 to be
28  * used in busybox and size optimizations,
29  * rewrote arith (see notes to this), added locale support,
30  * rewrote dynamic variables.
31  *
32  */
33
34 /*
35  * The follow should be set to reflect the type of system you have:
36  *      JOBS -> 1 if you have Berkeley job control, 0 otherwise.
37  *      define SYSV if you are running under System V.
38  *      define DEBUG=1 to compile in debugging ('set -o debug' to turn on)
39  *      define DEBUG=2 to compile in and turn on debugging.
40  *
41  * When debugging is on, debugging info will be written to ./trace and
42  * a quit signal will generate a core dump.
43  */
44 #define DEBUG 0
45 #define PROFILE 0
46
47 #define IFS_BROKEN
48
49 #define JOBS ENABLE_ASH_JOB_CONTROL
50
51 #if DEBUG
52 #ifndef _GNU_SOURCE
53 #define _GNU_SOURCE
54 #endif
55 #endif
56
57 #include "busybox.h" /* for applet_names */
58 #include <paths.h>
59 #include <setjmp.h>
60 #include <fnmatch.h>
61 #if JOBS || ENABLE_ASH_READ_NCHARS
62 #include <termios.h>
63 #endif
64
65 #if defined(__uClinux__)
66 #error "Do not even bother, ash will not run on uClinux"
67 #endif
68
69
70 /* ============ Hash table sizes. Configurable. */
71
72 #define VTABSIZE 39
73 #define ATABSIZE 39
74 #define CMDTABLESIZE 31         /* should be prime */
75
76
77 /* ============ Misc helpers */
78
79 #define xbarrier() do { __asm__ __volatile__ ("": : :"memory"); } while (0)
80
81 /* C99 say: "char" declaration may be signed or unsigned default */
82 #define signed_char2int(sc) ((int)((signed char)sc))
83
84
85 /* ============ Shell options */
86
87 static const char *const optletters_optnames[] = {
88         "e"   "errexit",
89         "f"   "noglob",
90         "I"   "ignoreeof",
91         "i"   "interactive",
92         "m"   "monitor",
93         "n"   "noexec",
94         "s"   "stdin",
95         "x"   "xtrace",
96         "v"   "verbose",
97         "C"   "noclobber",
98         "a"   "allexport",
99         "b"   "notify",
100         "u"   "nounset",
101         "\0"  "vi"
102 #if DEBUG
103         ,"\0"  "nolog"
104         ,"\0"  "debug"
105 #endif
106 };
107
108 #define optletters(n) optletters_optnames[(n)][0]
109 #define optnames(n) (&optletters_optnames[(n)][1])
110
111 enum { NOPTS = ARRAY_SIZE(optletters_optnames) };
112
113 static char optlist[NOPTS] ALIGN1;
114
115 #define eflag optlist[0]
116 #define fflag optlist[1]
117 #define Iflag optlist[2]
118 #define iflag optlist[3]
119 #define mflag optlist[4]
120 #define nflag optlist[5]
121 #define sflag optlist[6]
122 #define xflag optlist[7]
123 #define vflag optlist[8]
124 #define Cflag optlist[9]
125 #define aflag optlist[10]
126 #define bflag optlist[11]
127 #define uflag optlist[12]
128 #define viflag optlist[13]
129 #if DEBUG
130 #define nolog optlist[14]
131 #define debug optlist[15]
132 #endif
133
134
135 /* ============ Misc data */
136
137 static const char homestr[] ALIGN1 = "HOME";
138 static const char snlfmt[] ALIGN1 = "%s\n";
139 static const char illnum[] ALIGN1 = "Illegal number: %s";
140
141 /*
142  * We enclose jmp_buf in a structure so that we can declare pointers to
143  * jump locations.  The global variable handler contains the location to
144  * jump to when an exception occurs, and the global variable exception
145  * contains a code identifying the exception.  To implement nested
146  * exception handlers, the user should save the value of handler on entry
147  * to an inner scope, set handler to point to a jmploc structure for the
148  * inner scope, and restore handler on exit from the scope.
149  */
150 struct jmploc {
151         jmp_buf loc;
152 };
153
154 struct globals_misc {
155         /* pid of main shell */
156         int rootpid;
157         /* shell level: 0 for the main shell, 1 for its children, and so on */
158         int shlvl;
159 #define rootshell (!shlvl)
160         char *minusc;  /* argument to -c option */
161
162         char *curdir; // = nullstr;     /* current working directory */
163         char *physdir; // = nullstr;    /* physical working directory */
164
165         char *arg0; /* value of $0 */
166
167         struct jmploc *exception_handler;
168
169 // disabled by vda: cannot understand how it was supposed to work -
170 // cannot fix bugs. That's why you have to explain your non-trivial designs!
171 //      /* do we generate EXSIG events */
172 //      int exsig; /* counter */
173         volatile int suppressint; /* counter */
174         volatile /*sig_atomic_t*/ smallint intpending; /* 1 = got SIGINT */
175         /* last pending signal */
176         volatile /*sig_atomic_t*/ smallint pendingsig;
177         smallint exception; /* kind of exception (0..5) */
178         /* exceptions */
179 #define EXINT 0         /* SIGINT received */
180 #define EXERROR 1       /* a generic error */
181 #define EXSHELLPROC 2   /* execute a shell procedure */
182 #define EXEXEC 3        /* command execution failed */
183 #define EXEXIT 4        /* exit the shell */
184 #define EXSIG 5         /* trapped signal in wait(1) */
185
186         /* trap handler commands */
187         smallint isloginsh;
188         char *trap[NSIG];
189         char nullstr[1];        /* zero length string */
190         /*
191          * Sigmode records the current value of the signal handlers for the various
192          * modes.  A value of zero means that the current handler is not known.
193          * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
194          */
195         char sigmode[NSIG - 1];
196 #define S_DFL 1                 /* default signal handling (SIG_DFL) */
197 #define S_CATCH 2               /* signal is caught */
198 #define S_IGN 3                 /* signal is ignored (SIG_IGN) */
199 #define S_HARD_IGN 4            /* signal is ignored permenantly */
200 #define S_RESET 5               /* temporary - to reset a hard ignored sig */
201
202         /* indicates specified signal received */
203         char gotsig[NSIG - 1];
204 };
205 extern struct globals_misc *const ash_ptr_to_globals_misc;
206 #define G_misc (*ash_ptr_to_globals_misc)
207 #define rootpid   (G_misc.rootpid  )
208 #define shlvl     (G_misc.shlvl    )
209 #define minusc    (G_misc.minusc   )
210 #define curdir    (G_misc.curdir   )
211 #define physdir   (G_misc.physdir  )
212 #define arg0      (G_misc.arg0     )
213 #define exception_handler (G_misc.exception_handler)
214 #define exception         (G_misc.exception        )
215 #define suppressint       (G_misc.suppressint      )
216 #define intpending        (G_misc.intpending       )
217 //#define exsig             (G_misc.exsig            )
218 #define pendingsig        (G_misc.pendingsig       )
219 #define isloginsh (G_misc.isloginsh)
220 #define trap      (G_misc.trap     )
221 #define nullstr   (G_misc.nullstr  )
222 #define sigmode   (G_misc.sigmode  )
223 #define gotsig    (G_misc.gotsig   )
224 #define INIT_G_misc() do { \
225         (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \
226         barrier(); \
227         curdir = nullstr; \
228         physdir = nullstr; \
229 } while (0)
230
231
232 /* ============ Interrupts / exceptions */
233
234 /*
235  * These macros allow the user to suspend the handling of interrupt signals
236  * over a period of time.  This is similar to SIGHOLD or to sigblock, but
237  * much more efficient and portable.  (But hacking the kernel is so much
238  * more fun than worrying about efficiency and portability. :-))
239  */
240 #define INT_OFF \
241         do { \
242                 suppressint++; \
243                 xbarrier(); \
244         } while (0)
245
246 /*
247  * Called to raise an exception.  Since C doesn't include exceptions, we
248  * just do a longjmp to the exception handler.  The type of exception is
249  * stored in the global variable "exception".
250  */
251 static void raise_exception(int) ATTRIBUTE_NORETURN;
252 static void
253 raise_exception(int e)
254 {
255 #if DEBUG
256         if (exception_handler == NULL)
257                 abort();
258 #endif
259         INT_OFF;
260         exception = e;
261         longjmp(exception_handler->loc, 1);
262 }
263
264 /*
265  * Called from trap.c when a SIGINT is received.  (If the user specifies
266  * that SIGINT is to be trapped or ignored using the trap builtin, then
267  * this routine is not called.)  Suppressint is nonzero when interrupts
268  * are held using the INT_OFF macro.  (The test for iflag is just
269  * defensive programming.)
270  */
271 static void raise_interrupt(void) ATTRIBUTE_NORETURN;
272 static void
273 raise_interrupt(void)
274 {
275         int i;
276
277         intpending = 0;
278         /* Signal is not automatically unmasked after it is raised,
279          * do it ourself - unmask all signals */
280         sigprocmask_allsigs(SIG_UNBLOCK);
281         /* pendingsig = 0; - now done in onsig() */
282
283         i = EXSIG;
284         if (gotsig[SIGINT - 1] && !trap[SIGINT]) {
285                 if (!(rootshell && iflag)) {
286                         /* Kill ourself with SIGINT */
287                         signal(SIGINT, SIG_DFL);
288                         raise(SIGINT);
289                 }
290                 i = EXINT;
291         }
292         raise_exception(i);
293         /* NOTREACHED */
294 }
295
296 #if ENABLE_ASH_OPTIMIZE_FOR_SIZE
297 static void
298 int_on(void)
299 {
300         if (--suppressint == 0 && intpending) {
301                 raise_interrupt();
302         }
303 }
304 #define INT_ON int_on()
305 static void
306 force_int_on(void)
307 {
308         suppressint = 0;
309         if (intpending)
310                 raise_interrupt();
311 }
312 #define FORCE_INT_ON force_int_on()
313 #else
314 #define INT_ON \
315         do { \
316                 xbarrier(); \
317                 if (--suppressint == 0 && intpending) \
318                         raise_interrupt(); \
319         } while (0)
320 #define FORCE_INT_ON \
321         do { \
322                 xbarrier(); \
323                 suppressint = 0; \
324                 if (intpending) \
325                         raise_interrupt(); \
326         } while (0)
327 #endif /* ASH_OPTIMIZE_FOR_SIZE */
328
329 #define SAVE_INT(v) ((v) = suppressint)
330
331 #define RESTORE_INT(v) \
332         do { \
333                 xbarrier(); \
334                 suppressint = (v); \
335                 if (suppressint == 0 && intpending) \
336                         raise_interrupt(); \
337         } while (0)
338
339 /*
340  * Ignore a signal. Only one usage site - in forkchild()
341  */
342 static void
343 ignoresig(int signo)
344 {
345         if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
346                 signal(signo, SIG_IGN);
347         }
348         sigmode[signo - 1] = S_HARD_IGN;
349 }
350
351 /*
352  * Signal handler. Only one usage site - in setsignal()
353  */
354 static void
355 onsig(int signo)
356 {
357         gotsig[signo - 1] = 1;
358         pendingsig = signo;
359
360         if (/* exsig || */ (signo == SIGINT && !trap[SIGINT])) {
361                 if (!suppressint) {
362                         pendingsig = 0;
363                         raise_interrupt(); /* does not return */
364                 }
365                 intpending = 1;
366         }
367 }
368
369
370 /* ============ Stdout/stderr output */
371
372 static void
373 outstr(const char *p, FILE *file)
374 {
375         INT_OFF;
376         fputs(p, file);
377         INT_ON;
378 }
379
380 static void
381 flush_stdout_stderr(void)
382 {
383         INT_OFF;
384         fflush(stdout);
385         fflush(stderr);
386         INT_ON;
387 }
388
389 static void
390 flush_stderr(void)
391 {
392         INT_OFF;
393         fflush(stderr);
394         INT_ON;
395 }
396
397 static void
398 outcslow(int c, FILE *dest)
399 {
400         INT_OFF;
401         putc(c, dest);
402         fflush(dest);
403         INT_ON;
404 }
405
406 static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2)));
407 static int
408 out1fmt(const char *fmt, ...)
409 {
410         va_list ap;
411         int r;
412
413         INT_OFF;
414         va_start(ap, fmt);
415         r = vprintf(fmt, ap);
416         va_end(ap);
417         INT_ON;
418         return r;
419 }
420
421 static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4)));
422 static int
423 fmtstr(char *outbuf, size_t length, const char *fmt, ...)
424 {
425         va_list ap;
426         int ret;
427
428         va_start(ap, fmt);
429         INT_OFF;
430         ret = vsnprintf(outbuf, length, fmt, ap);
431         va_end(ap);
432         INT_ON;
433         return ret;
434 }
435
436 static void
437 out1str(const char *p)
438 {
439         outstr(p, stdout);
440 }
441
442 static void
443 out2str(const char *p)
444 {
445         outstr(p, stderr);
446         flush_stderr();
447 }
448
449
450 /* ============ Parser structures */
451
452 /* control characters in argument strings */
453 #define CTLESC '\201'           /* escape next character */
454 #define CTLVAR '\202'           /* variable defn */
455 #define CTLENDVAR '\203'
456 #define CTLBACKQ '\204'
457 #define CTLQUOTE 01             /* ored with CTLBACKQ code if in quotes */
458 /*      CTLBACKQ | CTLQUOTE == '\205' */
459 #define CTLARI  '\206'          /* arithmetic expression */
460 #define CTLENDARI '\207'
461 #define CTLQUOTEMARK '\210'
462
463 /* variable substitution byte (follows CTLVAR) */
464 #define VSTYPE  0x0f            /* type of variable substitution */
465 #define VSNUL   0x10            /* colon--treat the empty string as unset */
466 #define VSQUOTE 0x80            /* inside double quotes--suppress splitting */
467
468 /* values of VSTYPE field */
469 #define VSNORMAL        0x1     /* normal variable:  $var or ${var} */
470 #define VSMINUS         0x2     /* ${var-text} */
471 #define VSPLUS          0x3     /* ${var+text} */
472 #define VSQUESTION      0x4     /* ${var?message} */
473 #define VSASSIGN        0x5     /* ${var=text} */
474 #define VSTRIMRIGHT     0x6     /* ${var%pattern} */
475 #define VSTRIMRIGHTMAX  0x7     /* ${var%%pattern} */
476 #define VSTRIMLEFT      0x8     /* ${var#pattern} */
477 #define VSTRIMLEFTMAX   0x9     /* ${var##pattern} */
478 #define VSLENGTH        0xa     /* ${#var} */
479 #if ENABLE_ASH_BASH_COMPAT
480 #define VSSUBSTR        0xc     /* ${var:position:length} */
481 #define VSREPLACE       0xd     /* ${var/pattern/replacement} */
482 #define VSREPLACEALL    0xe     /* ${var//pattern/replacement} */
483 #endif
484
485 static const char dolatstr[] ALIGN1 = {
486         CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
487 };
488
489 #define NCMD 0
490 #define NPIPE 1
491 #define NREDIR 2
492 #define NBACKGND 3
493 #define NSUBSHELL 4
494 #define NAND 5
495 #define NOR 6
496 #define NSEMI 7
497 #define NIF 8
498 #define NWHILE 9
499 #define NUNTIL 10
500 #define NFOR 11
501 #define NCASE 12
502 #define NCLIST 13
503 #define NDEFUN 14
504 #define NARG 15
505 #define NTO 16
506 #define NCLOBBER 17
507 #define NFROM 18
508 #define NFROMTO 19
509 #define NAPPEND 20
510 #define NTOFD 21
511 #define NFROMFD 22
512 #define NHERE 23
513 #define NXHERE 24
514 #define NNOT 25
515
516 union node;
517
518 struct ncmd {
519         int type;
520         union node *assign;
521         union node *args;
522         union node *redirect;
523 };
524
525 struct npipe {
526         int type;
527         int backgnd;
528         struct nodelist *cmdlist;
529 };
530
531 struct nredir {
532         int type;
533         union node *n;
534         union node *redirect;
535 };
536
537 struct nbinary {
538         int type;
539         union node *ch1;
540         union node *ch2;
541 };
542
543 struct nif {
544         int type;
545         union node *test;
546         union node *ifpart;
547         union node *elsepart;
548 };
549
550 struct nfor {
551         int type;
552         union node *args;
553         union node *body;
554         char *var;
555 };
556
557 struct ncase {
558         int type;
559         union node *expr;
560         union node *cases;
561 };
562
563 struct nclist {
564         int type;
565         union node *next;
566         union node *pattern;
567         union node *body;
568 };
569
570 struct narg {
571         int type;
572         union node *next;
573         char *text;
574         struct nodelist *backquote;
575 };
576
577 struct nfile {
578         int type;
579         union node *next;
580         int fd;
581         union node *fname;
582         char *expfname;
583 };
584
585 struct ndup {
586         int type;
587         union node *next;
588         int fd;
589         int dupfd;
590         union node *vname;
591 };
592
593 struct nhere {
594         int type;
595         union node *next;
596         int fd;
597         union node *doc;
598 };
599
600 struct nnot {
601         int type;
602         union node *com;
603 };
604
605 union node {
606         int type;
607         struct ncmd ncmd;
608         struct npipe npipe;
609         struct nredir nredir;
610         struct nbinary nbinary;
611         struct nif nif;
612         struct nfor nfor;
613         struct ncase ncase;
614         struct nclist nclist;
615         struct narg narg;
616         struct nfile nfile;
617         struct ndup ndup;
618         struct nhere nhere;
619         struct nnot nnot;
620 };
621
622 struct nodelist {
623         struct nodelist *next;
624         union node *n;
625 };
626
627 struct funcnode {
628         int count;
629         union node n;
630 };
631
632 /*
633  * Free a parse tree.
634  */
635 static void
636 freefunc(struct funcnode *f)
637 {
638         if (f && --f->count < 0)
639                 free(f);
640 }
641
642
643 /* ============ Debugging output */
644
645 #if DEBUG
646
647 static FILE *tracefile;
648
649 static void
650 trace_printf(const char *fmt, ...)
651 {
652         va_list va;
653
654         if (debug != 1)
655                 return;
656         va_start(va, fmt);
657         vfprintf(tracefile, fmt, va);
658         va_end(va);
659 }
660
661 static void
662 trace_vprintf(const char *fmt, va_list va)
663 {
664         if (debug != 1)
665                 return;
666         vfprintf(tracefile, fmt, va);
667 }
668
669 static void
670 trace_puts(const char *s)
671 {
672         if (debug != 1)
673                 return;
674         fputs(s, tracefile);
675 }
676
677 static void
678 trace_puts_quoted(char *s)
679 {
680         char *p;
681         char c;
682
683         if (debug != 1)
684                 return;
685         putc('"', tracefile);
686         for (p = s; *p; p++) {
687                 switch (*p) {
688                 case '\n':  c = 'n';  goto backslash;
689                 case '\t':  c = 't';  goto backslash;
690                 case '\r':  c = 'r';  goto backslash;
691                 case '"':  c = '"';  goto backslash;
692                 case '\\':  c = '\\';  goto backslash;
693                 case CTLESC:  c = 'e';  goto backslash;
694                 case CTLVAR:  c = 'v';  goto backslash;
695                 case CTLVAR+CTLQUOTE:  c = 'V'; goto backslash;
696                 case CTLBACKQ:  c = 'q';  goto backslash;
697                 case CTLBACKQ+CTLQUOTE:  c = 'Q'; goto backslash;
698  backslash:
699                         putc('\\', tracefile);
700                         putc(c, tracefile);
701                         break;
702                 default:
703                         if (*p >= ' ' && *p <= '~')
704                                 putc(*p, tracefile);
705                         else {
706                                 putc('\\', tracefile);
707                                 putc(*p >> 6 & 03, tracefile);
708                                 putc(*p >> 3 & 07, tracefile);
709                                 putc(*p & 07, tracefile);
710                         }
711                         break;
712                 }
713         }
714         putc('"', tracefile);
715 }
716
717 static void
718 trace_puts_args(char **ap)
719 {
720         if (debug != 1)
721                 return;
722         if (!*ap)
723                 return;
724         while (1) {
725                 trace_puts_quoted(*ap);
726                 if (!*++ap) {
727                         putc('\n', tracefile);
728                         break;
729                 }
730                 putc(' ', tracefile);
731         }
732 }
733
734 static void
735 opentrace(void)
736 {
737         char s[100];
738 #ifdef O_APPEND
739         int flags;
740 #endif
741
742         if (debug != 1) {
743                 if (tracefile)
744                         fflush(tracefile);
745                 /* leave open because libedit might be using it */
746                 return;
747         }
748         strcpy(s, "./trace");
749         if (tracefile) {
750                 if (!freopen(s, "a", tracefile)) {
751                         fprintf(stderr, "Can't re-open %s\n", s);
752                         debug = 0;
753                         return;
754                 }
755         } else {
756                 tracefile = fopen(s, "a");
757                 if (tracefile == NULL) {
758                         fprintf(stderr, "Can't open %s\n", s);
759                         debug = 0;
760                         return;
761                 }
762         }
763 #ifdef O_APPEND
764         flags = fcntl(fileno(tracefile), F_GETFL);
765         if (flags >= 0)
766                 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
767 #endif
768         setlinebuf(tracefile);
769         fputs("\nTracing started.\n", tracefile);
770 }
771
772 static void
773 indent(int amount, char *pfx, FILE *fp)
774 {
775         int i;
776
777         for (i = 0; i < amount; i++) {
778                 if (pfx && i == amount - 1)
779                         fputs(pfx, fp);
780                 putc('\t', fp);
781         }
782 }
783
784 /* little circular references here... */
785 static void shtree(union node *n, int ind, char *pfx, FILE *fp);
786
787 static void
788 sharg(union node *arg, FILE *fp)
789 {
790         char *p;
791         struct nodelist *bqlist;
792         int subtype;
793
794         if (arg->type != NARG) {
795                 out1fmt("<node type %d>\n", arg->type);
796                 abort();
797         }
798         bqlist = arg->narg.backquote;
799         for (p = arg->narg.text; *p; p++) {
800                 switch (*p) {
801                 case CTLESC:
802                         putc(*++p, fp);
803                         break;
804                 case CTLVAR:
805                         putc('$', fp);
806                         putc('{', fp);
807                         subtype = *++p;
808                         if (subtype == VSLENGTH)
809                                 putc('#', fp);
810
811                         while (*p != '=')
812                                 putc(*p++, fp);
813
814                         if (subtype & VSNUL)
815                                 putc(':', fp);
816
817                         switch (subtype & VSTYPE) {
818                         case VSNORMAL:
819                                 putc('}', fp);
820                                 break;
821                         case VSMINUS:
822                                 putc('-', fp);
823                                 break;
824                         case VSPLUS:
825                                 putc('+', fp);
826                                 break;
827                         case VSQUESTION:
828                                 putc('?', fp);
829                                 break;
830                         case VSASSIGN:
831                                 putc('=', fp);
832                                 break;
833                         case VSTRIMLEFT:
834                                 putc('#', fp);
835                                 break;
836                         case VSTRIMLEFTMAX:
837                                 putc('#', fp);
838                                 putc('#', fp);
839                                 break;
840                         case VSTRIMRIGHT:
841                                 putc('%', fp);
842                                 break;
843                         case VSTRIMRIGHTMAX:
844                                 putc('%', fp);
845                                 putc('%', fp);
846                                 break;
847                         case VSLENGTH:
848                                 break;
849                         default:
850                                 out1fmt("<subtype %d>", subtype);
851                         }
852                         break;
853                 case CTLENDVAR:
854                         putc('}', fp);
855                         break;
856                 case CTLBACKQ:
857                 case CTLBACKQ|CTLQUOTE:
858                         putc('$', fp);
859                         putc('(', fp);
860                         shtree(bqlist->n, -1, NULL, fp);
861                         putc(')', fp);
862                         break;
863                 default:
864                         putc(*p, fp);
865                         break;
866                 }
867         }
868 }
869
870 static void
871 shcmd(union node *cmd, FILE *fp)
872 {
873         union node *np;
874         int first;
875         const char *s;
876         int dftfd;
877
878         first = 1;
879         for (np = cmd->ncmd.args; np; np = np->narg.next) {
880                 if (!first)
881                         putc(' ', fp);
882                 sharg(np, fp);
883                 first = 0;
884         }
885         for (np = cmd->ncmd.redirect; np; np = np->nfile.next) {
886                 if (!first)
887                         putc(' ', fp);
888                 dftfd = 0;
889                 switch (np->nfile.type) {
890                 case NTO:      s = ">>"+1; dftfd = 1; break;
891                 case NCLOBBER: s = ">|"; dftfd = 1; break;
892                 case NAPPEND:  s = ">>"; dftfd = 1; break;
893                 case NTOFD:    s = ">&"; dftfd = 1; break;
894                 case NFROM:    s = "<";  break;
895                 case NFROMFD:  s = "<&"; break;
896                 case NFROMTO:  s = "<>"; break;
897                 default:       s = "*error*"; break;
898                 }
899                 if (np->nfile.fd != dftfd)
900                         fprintf(fp, "%d", np->nfile.fd);
901                 fputs(s, fp);
902                 if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
903                         fprintf(fp, "%d", np->ndup.dupfd);
904                 } else {
905                         sharg(np->nfile.fname, fp);
906                 }
907                 first = 0;
908         }
909 }
910
911 static void
912 shtree(union node *n, int ind, char *pfx, FILE *fp)
913 {
914         struct nodelist *lp;
915         const char *s;
916
917         if (n == NULL)
918                 return;
919
920         indent(ind, pfx, fp);
921         switch (n->type) {
922         case NSEMI:
923                 s = "; ";
924                 goto binop;
925         case NAND:
926                 s = " && ";
927                 goto binop;
928         case NOR:
929                 s = " || ";
930  binop:
931                 shtree(n->nbinary.ch1, ind, NULL, fp);
932                 /* if (ind < 0) */
933                         fputs(s, fp);
934                 shtree(n->nbinary.ch2, ind, NULL, fp);
935                 break;
936         case NCMD:
937                 shcmd(n, fp);
938                 if (ind >= 0)
939                         putc('\n', fp);
940                 break;
941         case NPIPE:
942                 for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
943                         shcmd(lp->n, fp);
944                         if (lp->next)
945                                 fputs(" | ", fp);
946                 }
947                 if (n->npipe.backgnd)
948                         fputs(" &", fp);
949                 if (ind >= 0)
950                         putc('\n', fp);
951                 break;
952         default:
953                 fprintf(fp, "<node type %d>", n->type);
954                 if (ind >= 0)
955                         putc('\n', fp);
956                 break;
957         }
958 }
959
960 static void
961 showtree(union node *n)
962 {
963         trace_puts("showtree called\n");
964         shtree(n, 1, NULL, stdout);
965 }
966
967 #define TRACE(param)    trace_printf param
968 #define TRACEV(param)   trace_vprintf param
969
970 #else
971
972 #define TRACE(param)
973 #define TRACEV(param)
974
975 #endif /* DEBUG */
976
977
978 /* ============ Parser data */
979
980 /*
981  * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up.
982  */
983 struct strlist {
984         struct strlist *next;
985         char *text;
986 };
987
988 #if ENABLE_ASH_ALIAS
989 struct alias;
990 #endif
991
992 struct strpush {
993         struct strpush *prev;   /* preceding string on stack */
994         char *prevstring;
995         int prevnleft;
996 #if ENABLE_ASH_ALIAS
997         struct alias *ap;       /* if push was associated with an alias */
998 #endif
999         char *string;           /* remember the string since it may change */
1000 };
1001
1002 struct parsefile {
1003         struct parsefile *prev; /* preceding file on stack */
1004         int linno;              /* current line */
1005         int fd;                 /* file descriptor (or -1 if string) */
1006         int nleft;              /* number of chars left in this line */
1007         int lleft;              /* number of chars left in this buffer */
1008         char *nextc;            /* next char in buffer */
1009         char *buf;              /* input buffer */
1010         struct strpush *strpush; /* for pushing strings at this level */
1011         struct strpush basestrpush; /* so pushing one is fast */
1012 };
1013
1014 static struct parsefile basepf;         /* top level input file */
1015 static struct parsefile *g_parsefile = &basepf;  /* current input file */
1016 static int startlinno;                 /* line # where last token started */
1017 static char *commandname;              /* currently executing command */
1018 static struct strlist *cmdenviron;     /* environment for builtin command */
1019 static int exitstatus;                 /* exit status of last command */
1020
1021
1022 /* ============ Message printing */
1023
1024 static void
1025 ash_vmsg(const char *msg, va_list ap)
1026 {
1027         fprintf(stderr, "%s: ", arg0);
1028         if (commandname) {
1029                 if (strcmp(arg0, commandname))
1030                         fprintf(stderr, "%s: ", commandname);
1031                 if (!iflag || g_parsefile->fd)
1032                         fprintf(stderr, "line %d: ", startlinno);
1033         }
1034         vfprintf(stderr, msg, ap);
1035         outcslow('\n', stderr);
1036 }
1037
1038 /*
1039  * Exverror is called to raise the error exception.  If the second argument
1040  * is not NULL then error prints an error message using printf style
1041  * formatting.  It then raises the error exception.
1042  */
1043 static void ash_vmsg_and_raise(int, const char *, va_list) ATTRIBUTE_NORETURN;
1044 static void
1045 ash_vmsg_and_raise(int cond, const char *msg, va_list ap)
1046 {
1047 #if DEBUG
1048         if (msg) {
1049                 TRACE(("ash_vmsg_and_raise(%d, \"", cond));
1050                 TRACEV((msg, ap));
1051                 TRACE(("\") pid=%d\n", getpid()));
1052         } else
1053                 TRACE(("ash_vmsg_and_raise(%d, NULL) pid=%d\n", cond, getpid()));
1054         if (msg)
1055 #endif
1056                 ash_vmsg(msg, ap);
1057
1058         flush_stdout_stderr();
1059         raise_exception(cond);
1060         /* NOTREACHED */
1061 }
1062
1063 static void ash_msg_and_raise_error(const char *, ...) ATTRIBUTE_NORETURN;
1064 static void
1065 ash_msg_and_raise_error(const char *msg, ...)
1066 {
1067         va_list ap;
1068
1069         va_start(ap, msg);
1070         ash_vmsg_and_raise(EXERROR, msg, ap);
1071         /* NOTREACHED */
1072         va_end(ap);
1073 }
1074
1075 static void ash_msg_and_raise(int, const char *, ...) ATTRIBUTE_NORETURN;
1076 static void
1077 ash_msg_and_raise(int cond, const char *msg, ...)
1078 {
1079         va_list ap;
1080
1081         va_start(ap, msg);
1082         ash_vmsg_and_raise(cond, msg, ap);
1083         /* NOTREACHED */
1084         va_end(ap);
1085 }
1086
1087 /*
1088  * error/warning routines for external builtins
1089  */
1090 static void
1091 ash_msg(const char *fmt, ...)
1092 {
1093         va_list ap;
1094
1095         va_start(ap, fmt);
1096         ash_vmsg(fmt, ap);
1097         va_end(ap);
1098 }
1099
1100 /*
1101  * Return a string describing an error.  The returned string may be a
1102  * pointer to a static buffer that will be overwritten on the next call.
1103  * Action describes the operation that got the error.
1104  */
1105 static const char *
1106 errmsg(int e, const char *em)
1107 {
1108         if (e == ENOENT || e == ENOTDIR) {
1109                 return em;
1110         }
1111         return strerror(e);
1112 }
1113
1114
1115 /* ============ Memory allocation */
1116
1117 /*
1118  * It appears that grabstackstr() will barf with such alignments
1119  * because stalloc() will return a string allocated in a new stackblock.
1120  */
1121 #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE)
1122 enum {
1123         /* Most machines require the value returned from malloc to be aligned
1124          * in some way.  The following macro will get this right
1125          * on many machines.  */
1126         SHELL_SIZE = sizeof(union {int i; char *cp; double d; }) - 1,
1127         /* Minimum size of a block */
1128         MINSIZE = SHELL_ALIGN(504),
1129 };
1130
1131 struct stack_block {
1132         struct stack_block *prev;
1133         char space[MINSIZE];
1134 };
1135
1136 struct stackmark {
1137         struct stack_block *stackp;
1138         char *stacknxt;
1139         size_t stacknleft;
1140         struct stackmark *marknext;
1141 };
1142
1143
1144 struct globals_memstack {
1145         struct stack_block *g_stackp; // = &stackbase;
1146         struct stackmark *markp;
1147         char *g_stacknxt; // = stackbase.space;
1148         char *sstrend; // = stackbase.space + MINSIZE;
1149         size_t g_stacknleft; // = MINSIZE;
1150         int    herefd; // = -1;
1151         struct stack_block stackbase;
1152 };
1153 extern struct globals_memstack *const ash_ptr_to_globals_memstack;
1154 #define G_memstack (*ash_ptr_to_globals_memstack)
1155 #define g_stackp     (G_memstack.g_stackp    )
1156 #define markp        (G_memstack.markp       )
1157 #define g_stacknxt   (G_memstack.g_stacknxt  )
1158 #define sstrend      (G_memstack.sstrend     )
1159 #define g_stacknleft (G_memstack.g_stacknleft)
1160 #define herefd       (G_memstack.herefd      )
1161 #define stackbase    (G_memstack.stackbase   )
1162 #define INIT_G_memstack() do { \
1163         (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \
1164         barrier(); \
1165         g_stackp = &stackbase; \
1166         g_stacknxt = stackbase.space; \
1167         g_stacknleft = MINSIZE; \
1168         sstrend = stackbase.space + MINSIZE; \
1169         herefd = -1; \
1170 } while (0)
1171
1172 #define stackblock()     ((void *)g_stacknxt)
1173 #define stackblocksize() g_stacknleft
1174
1175
1176 static void *
1177 ckrealloc(void * p, size_t nbytes)
1178 {
1179         p = realloc(p, nbytes);
1180         if (!p)
1181                 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1182         return p;
1183 }
1184
1185 static void *
1186 ckmalloc(size_t nbytes)
1187 {
1188         return ckrealloc(NULL, nbytes);
1189 }
1190
1191 static void *
1192 ckzalloc(size_t nbytes)
1193 {
1194         return memset(ckmalloc(nbytes), 0, nbytes);
1195 }
1196
1197 /*
1198  * Make a copy of a string in safe storage.
1199  */
1200 static char *
1201 ckstrdup(const char *s)
1202 {
1203         char *p = strdup(s);
1204         if (!p)
1205                 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1206         return p;
1207 }
1208
1209 /*
1210  * Parse trees for commands are allocated in lifo order, so we use a stack
1211  * to make this more efficient, and also to avoid all sorts of exception
1212  * handling code to handle interrupts in the middle of a parse.
1213  *
1214  * The size 504 was chosen because the Ultrix malloc handles that size
1215  * well.
1216  */
1217 static void *
1218 stalloc(size_t nbytes)
1219 {
1220         char *p;
1221         size_t aligned;
1222
1223         aligned = SHELL_ALIGN(nbytes);
1224         if (aligned > g_stacknleft) {
1225                 size_t len;
1226                 size_t blocksize;
1227                 struct stack_block *sp;
1228
1229                 blocksize = aligned;
1230                 if (blocksize < MINSIZE)
1231                         blocksize = MINSIZE;
1232                 len = sizeof(struct stack_block) - MINSIZE + blocksize;
1233                 if (len < blocksize)
1234                         ash_msg_and_raise_error(bb_msg_memory_exhausted);
1235                 INT_OFF;
1236                 sp = ckmalloc(len);
1237                 sp->prev = g_stackp;
1238                 g_stacknxt = sp->space;
1239                 g_stacknleft = blocksize;
1240                 sstrend = g_stacknxt + blocksize;
1241                 g_stackp = sp;
1242                 INT_ON;
1243         }
1244         p = g_stacknxt;
1245         g_stacknxt += aligned;
1246         g_stacknleft -= aligned;
1247         return p;
1248 }
1249
1250 static void *
1251 stzalloc(size_t nbytes)
1252 {
1253         return memset(stalloc(nbytes), 0, nbytes);
1254 }
1255
1256 static void
1257 stunalloc(void *p)
1258 {
1259 #if DEBUG
1260         if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) {
1261                 write(STDERR_FILENO, "stunalloc\n", 10);
1262                 abort();
1263         }
1264 #endif
1265         g_stacknleft += g_stacknxt - (char *)p;
1266         g_stacknxt = p;
1267 }
1268
1269 /*
1270  * Like strdup but works with the ash stack.
1271  */
1272 static char *
1273 ststrdup(const char *p)
1274 {
1275         size_t len = strlen(p) + 1;
1276         return memcpy(stalloc(len), p, len);
1277 }
1278
1279 static void
1280 setstackmark(struct stackmark *mark)
1281 {
1282         mark->stackp = g_stackp;
1283         mark->stacknxt = g_stacknxt;
1284         mark->stacknleft = g_stacknleft;
1285         mark->marknext = markp;
1286         markp = mark;
1287 }
1288
1289 static void
1290 popstackmark(struct stackmark *mark)
1291 {
1292         struct stack_block *sp;
1293
1294         if (!mark->stackp)
1295                 return;
1296
1297         INT_OFF;
1298         markp = mark->marknext;
1299         while (g_stackp != mark->stackp) {
1300                 sp = g_stackp;
1301                 g_stackp = sp->prev;
1302                 free(sp);
1303         }
1304         g_stacknxt = mark->stacknxt;
1305         g_stacknleft = mark->stacknleft;
1306         sstrend = mark->stacknxt + mark->stacknleft;
1307         INT_ON;
1308 }
1309
1310 /*
1311  * When the parser reads in a string, it wants to stick the string on the
1312  * stack and only adjust the stack pointer when it knows how big the
1313  * string is.  Stackblock (defined in stack.h) returns a pointer to a block
1314  * of space on top of the stack and stackblocklen returns the length of
1315  * this block.  Growstackblock will grow this space by at least one byte,
1316  * possibly moving it (like realloc).  Grabstackblock actually allocates the
1317  * part of the block that has been used.
1318  */
1319 static void
1320 growstackblock(void)
1321 {
1322         size_t newlen;
1323
1324         newlen = g_stacknleft * 2;
1325         if (newlen < g_stacknleft)
1326                 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1327         if (newlen < 128)
1328                 newlen += 128;
1329
1330         if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) {
1331                 struct stack_block *oldstackp;
1332                 struct stackmark *xmark;
1333                 struct stack_block *sp;
1334                 struct stack_block *prevstackp;
1335                 size_t grosslen;
1336
1337                 INT_OFF;
1338                 oldstackp = g_stackp;
1339                 sp = g_stackp;
1340                 prevstackp = sp->prev;
1341                 grosslen = newlen + sizeof(struct stack_block) - MINSIZE;
1342                 sp = ckrealloc(sp, grosslen);
1343                 sp->prev = prevstackp;
1344                 g_stackp = sp;
1345                 g_stacknxt = sp->space;
1346                 g_stacknleft = newlen;
1347                 sstrend = sp->space + newlen;
1348
1349                 /*
1350                  * Stack marks pointing to the start of the old block
1351                  * must be relocated to point to the new block
1352                  */
1353                 xmark = markp;
1354                 while (xmark != NULL && xmark->stackp == oldstackp) {
1355                         xmark->stackp = g_stackp;
1356                         xmark->stacknxt = g_stacknxt;
1357                         xmark->stacknleft = g_stacknleft;
1358                         xmark = xmark->marknext;
1359                 }
1360                 INT_ON;
1361         } else {
1362                 char *oldspace = g_stacknxt;
1363                 size_t oldlen = g_stacknleft;
1364                 char *p = stalloc(newlen);
1365
1366                 /* free the space we just allocated */
1367                 g_stacknxt = memcpy(p, oldspace, oldlen);
1368                 g_stacknleft += newlen;
1369         }
1370 }
1371
1372 static void
1373 grabstackblock(size_t len)
1374 {
1375         len = SHELL_ALIGN(len);
1376         g_stacknxt += len;
1377         g_stacknleft -= len;
1378 }
1379
1380 /*
1381  * The following routines are somewhat easier to use than the above.
1382  * The user declares a variable of type STACKSTR, which may be declared
1383  * to be a register.  The macro STARTSTACKSTR initializes things.  Then
1384  * the user uses the macro STPUTC to add characters to the string.  In
1385  * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
1386  * grown as necessary.  When the user is done, she can just leave the
1387  * string there and refer to it using stackblock().  Or she can allocate
1388  * the space for it using grabstackstr().  If it is necessary to allow
1389  * someone else to use the stack temporarily and then continue to grow
1390  * the string, the user should use grabstack to allocate the space, and
1391  * then call ungrabstr(p) to return to the previous mode of operation.
1392  *
1393  * USTPUTC is like STPUTC except that it doesn't check for overflow.
1394  * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
1395  * is space for at least one character.
1396  */
1397 static void *
1398 growstackstr(void)
1399 {
1400         size_t len = stackblocksize();
1401         if (herefd >= 0 && len >= 1024) {
1402                 full_write(herefd, stackblock(), len);
1403                 return stackblock();
1404         }
1405         growstackblock();
1406         return (char *)stackblock() + len;
1407 }
1408
1409 /*
1410  * Called from CHECKSTRSPACE.
1411  */
1412 static char *
1413 makestrspace(size_t newlen, char *p)
1414 {
1415         size_t len = p - g_stacknxt;
1416         size_t size = stackblocksize();
1417
1418         for (;;) {
1419                 size_t nleft;
1420
1421                 size = stackblocksize();
1422                 nleft = size - len;
1423                 if (nleft >= newlen)
1424                         break;
1425                 growstackblock();
1426         }
1427         return (char *)stackblock() + len;
1428 }
1429
1430 static char *
1431 stack_nputstr(const char *s, size_t n, char *p)
1432 {
1433         p = makestrspace(n, p);
1434         p = (char *)memcpy(p, s, n) + n;
1435         return p;
1436 }
1437
1438 static char *
1439 stack_putstr(const char *s, char *p)
1440 {
1441         return stack_nputstr(s, strlen(s), p);
1442 }
1443
1444 static char *
1445 _STPUTC(int c, char *p)
1446 {
1447         if (p == sstrend)
1448                 p = growstackstr();
1449         *p++ = c;
1450         return p;
1451 }
1452
1453 #define STARTSTACKSTR(p)        ((p) = stackblock())
1454 #define STPUTC(c, p)            ((p) = _STPUTC((c), (p)))
1455 #define CHECKSTRSPACE(n, p) \
1456         do { \
1457                 char *q = (p); \
1458                 size_t l = (n); \
1459                 size_t m = sstrend - q; \
1460                 if (l > m) \
1461                         (p) = makestrspace(l, q); \
1462         } while (0)
1463 #define USTPUTC(c, p)           (*(p)++ = (c))
1464 #define STACKSTRNUL(p) \
1465         do { \
1466                 if ((p) == sstrend) \
1467                         (p) = growstackstr(); \
1468                 *(p) = '\0'; \
1469         } while (0)
1470 #define STUNPUTC(p)             (--(p))
1471 #define STTOPC(p)               ((p)[-1])
1472 #define STADJUST(amount, p)     ((p) += (amount))
1473
1474 #define grabstackstr(p)         stalloc((char *)(p) - (char *)stackblock())
1475 #define ungrabstackstr(s, p)    stunalloc(s)
1476 #define stackstrend()           ((void *)sstrend)
1477
1478
1479 /* ============ String helpers */
1480
1481 /*
1482  * prefix -- see if pfx is a prefix of string.
1483  */
1484 static char *
1485 prefix(const char *string, const char *pfx)
1486 {
1487         while (*pfx) {
1488                 if (*pfx++ != *string++)
1489                         return NULL;
1490         }
1491         return (char *) string;
1492 }
1493
1494 /*
1495  * Check for a valid number.  This should be elsewhere.
1496  */
1497 static int
1498 is_number(const char *p)
1499 {
1500         do {
1501                 if (!isdigit(*p))
1502                         return 0;
1503         } while (*++p != '\0');
1504         return 1;
1505 }
1506
1507 /*
1508  * Convert a string of digits to an integer, printing an error message on
1509  * failure.
1510  */
1511 static int
1512 number(const char *s)
1513 {
1514         if (!is_number(s))
1515                 ash_msg_and_raise_error(illnum, s);
1516         return atoi(s);
1517 }
1518
1519 /*
1520  * Produce a possibly single quoted string suitable as input to the shell.
1521  * The return string is allocated on the stack.
1522  */
1523 static char *
1524 single_quote(const char *s)
1525 {
1526         char *p;
1527
1528         STARTSTACKSTR(p);
1529
1530         do {
1531                 char *q;
1532                 size_t len;
1533
1534                 len = strchrnul(s, '\'') - s;
1535
1536                 q = p = makestrspace(len + 3, p);
1537
1538                 *q++ = '\'';
1539                 q = (char *)memcpy(q, s, len) + len;
1540                 *q++ = '\'';
1541                 s += len;
1542
1543                 STADJUST(q - p, p);
1544
1545                 len = strspn(s, "'");
1546                 if (!len)
1547                         break;
1548
1549                 q = p = makestrspace(len + 3, p);
1550
1551                 *q++ = '"';
1552                 q = (char *)memcpy(q, s, len) + len;
1553                 *q++ = '"';
1554                 s += len;
1555
1556                 STADJUST(q - p, p);
1557         } while (*s);
1558
1559         USTPUTC(0, p);
1560
1561         return stackblock();
1562 }
1563
1564
1565 /* ============ nextopt */
1566
1567 static char **argptr;                  /* argument list for builtin commands */
1568 static char *optionarg;                /* set by nextopt (like getopt) */
1569 static char *optptr;                   /* used by nextopt */
1570
1571 /*
1572  * XXX - should get rid of.  have all builtins use getopt(3).  the
1573  * library getopt must have the BSD extension static variable "optreset"
1574  * otherwise it can't be used within the shell safely.
1575  *
1576  * Standard option processing (a la getopt) for builtin routines.  The
1577  * only argument that is passed to nextopt is the option string; the
1578  * other arguments are unnecessary.  It return the character, or '\0' on
1579  * end of input.
1580  */
1581 static int
1582 nextopt(const char *optstring)
1583 {
1584         char *p;
1585         const char *q;
1586         char c;
1587
1588         p = optptr;
1589         if (p == NULL || *p == '\0') {
1590                 p = *argptr;
1591                 if (p == NULL || *p != '-' || *++p == '\0')
1592                         return '\0';
1593                 argptr++;
1594                 if (LONE_DASH(p))        /* check for "--" */
1595                         return '\0';
1596         }
1597         c = *p++;
1598         for (q = optstring; *q != c;) {
1599                 if (*q == '\0')
1600                         ash_msg_and_raise_error("illegal option -%c", c);
1601                 if (*++q == ':')
1602                         q++;
1603         }
1604         if (*++q == ':') {
1605                 if (*p == '\0' && (p = *argptr++) == NULL)
1606                         ash_msg_and_raise_error("no arg for -%c option", c);
1607                 optionarg = p;
1608                 p = NULL;
1609         }
1610         optptr = p;
1611         return c;
1612 }
1613
1614
1615 /* ============ Math support definitions */
1616
1617 #if ENABLE_ASH_MATH_SUPPORT_64
1618 typedef int64_t arith_t;
1619 #define arith_t_type long long
1620 #else
1621 typedef long arith_t;
1622 #define arith_t_type long
1623 #endif
1624
1625 #if ENABLE_ASH_MATH_SUPPORT
1626 static arith_t dash_arith(const char *);
1627 static arith_t arith(const char *expr, int *perrcode);
1628 #endif
1629
1630 #if ENABLE_ASH_RANDOM_SUPPORT
1631 static unsigned long rseed;
1632 #ifndef DYNAMIC_VAR
1633 #define DYNAMIC_VAR
1634 #endif
1635 #endif
1636
1637
1638 /* ============ Shell variables */
1639
1640 /*
1641  * The parsefile structure pointed to by the global variable parsefile
1642  * contains information about the current file being read.
1643  */
1644 struct redirtab {
1645         struct redirtab *next;
1646         int renamed[10];
1647         int nullredirs;
1648 };
1649
1650 struct shparam {
1651         int nparam;             /* # of positional parameters (without $0) */
1652 #if ENABLE_ASH_GETOPTS
1653         int optind;             /* next parameter to be processed by getopts */
1654         int optoff;             /* used by getopts */
1655 #endif
1656         unsigned char malloced; /* if parameter list dynamically allocated */
1657         char **p;               /* parameter list */
1658 };
1659
1660 /*
1661  * Free the list of positional parameters.
1662  */
1663 static void
1664 freeparam(volatile struct shparam *param)
1665 {
1666         char **ap;
1667
1668         if (param->malloced) {
1669                 for (ap = param->p; *ap; ap++)
1670                         free(*ap);
1671                 free(param->p);
1672         }
1673 }
1674
1675 #if ENABLE_ASH_GETOPTS
1676 static void getoptsreset(const char *value);
1677 #endif
1678
1679 struct var {
1680         struct var *next;               /* next entry in hash list */
1681         int flags;                      /* flags are defined above */
1682         const char *text;               /* name=value */
1683         void (*func)(const char *);     /* function to be called when  */
1684                                         /* the variable gets set/unset */
1685 };
1686
1687 struct localvar {
1688         struct localvar *next;          /* next local variable in list */
1689         struct var *vp;                 /* the variable that was made local */
1690         int flags;                      /* saved flags */
1691         const char *text;               /* saved text */
1692 };
1693
1694 /* flags */
1695 #define VEXPORT         0x01    /* variable is exported */
1696 #define VREADONLY       0x02    /* variable cannot be modified */
1697 #define VSTRFIXED       0x04    /* variable struct is statically allocated */
1698 #define VTEXTFIXED      0x08    /* text is statically allocated */
1699 #define VSTACK          0x10    /* text is allocated on the stack */
1700 #define VUNSET          0x20    /* the variable is not set */
1701 #define VNOFUNC         0x40    /* don't call the callback function */
1702 #define VNOSET          0x80    /* do not set variable - just readonly test */
1703 #define VNOSAVE         0x100   /* when text is on the heap before setvareq */
1704 #ifdef DYNAMIC_VAR
1705 # define VDYNAMIC       0x200   /* dynamic variable */
1706 #else
1707 # define VDYNAMIC       0
1708 #endif
1709
1710 #ifdef IFS_BROKEN
1711 static const char defifsvar[] ALIGN1 = "IFS= \t\n";
1712 #define defifs (defifsvar + 4)
1713 #else
1714 static const char defifs[] ALIGN1 = " \t\n";
1715 #endif
1716
1717
1718 /* Need to be before varinit_data[] */
1719 #if ENABLE_LOCALE_SUPPORT
1720 static void
1721 change_lc_all(const char *value)
1722 {
1723         if (value && *value != '\0')
1724                 setlocale(LC_ALL, value);
1725 }
1726 static void
1727 change_lc_ctype(const char *value)
1728 {
1729         if (value && *value != '\0')
1730                 setlocale(LC_CTYPE, value);
1731 }
1732 #endif
1733 #if ENABLE_ASH_MAIL
1734 static void chkmail(void);
1735 static void changemail(const char *);
1736 #endif
1737 static void changepath(const char *);
1738 #if ENABLE_ASH_RANDOM_SUPPORT
1739 static void change_random(const char *);
1740 #endif
1741
1742 static const struct {
1743         int flags;
1744         const char *text;
1745         void (*func)(const char *);
1746 } varinit_data[] = {
1747 #ifdef IFS_BROKEN
1748         { VSTRFIXED|VTEXTFIXED       , defifsvar   , NULL            },
1749 #else
1750         { VSTRFIXED|VTEXTFIXED|VUNSET, "IFS\0"     , NULL            },
1751 #endif
1752 #if ENABLE_ASH_MAIL
1753         { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL\0"    , changemail      },
1754         { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH\0", changemail      },
1755 #endif
1756         { VSTRFIXED|VTEXTFIXED       , bb_PATH_root_path, changepath },
1757         { VSTRFIXED|VTEXTFIXED       , "PS1=$ "    , NULL            },
1758         { VSTRFIXED|VTEXTFIXED       , "PS2=> "    , NULL            },
1759         { VSTRFIXED|VTEXTFIXED       , "PS4=+ "    , NULL            },
1760 #if ENABLE_ASH_GETOPTS
1761         { VSTRFIXED|VTEXTFIXED       , "OPTIND=1"  , getoptsreset    },
1762 #endif
1763 #if ENABLE_ASH_RANDOM_SUPPORT
1764         { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM\0", change_random },
1765 #endif
1766 #if ENABLE_LOCALE_SUPPORT
1767         { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL\0"  , change_lc_all   },
1768         { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE\0", change_lc_ctype },
1769 #endif
1770 #if ENABLE_FEATURE_EDITING_SAVEHISTORY
1771         { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE\0", NULL            },
1772 #endif
1773 };
1774
1775
1776 struct globals_var {
1777         struct shparam shellparam;      /* $@ current positional parameters */
1778         struct redirtab *redirlist;
1779         int g_nullredirs;
1780         int preverrout_fd;   /* save fd2 before print debug if xflag is set. */
1781         struct var *vartab[VTABSIZE];
1782         struct var varinit[ARRAY_SIZE(varinit_data)];
1783 };
1784 extern struct globals_var *const ash_ptr_to_globals_var;
1785 #define G_var (*ash_ptr_to_globals_var)
1786 #define shellparam    (G_var.shellparam   )
1787 #define redirlist     (G_var.redirlist    )
1788 #define g_nullredirs  (G_var.g_nullredirs )
1789 #define preverrout_fd (G_var.preverrout_fd)
1790 #define vartab        (G_var.vartab       )
1791 #define varinit       (G_var.varinit      )
1792 #define INIT_G_var() do { \
1793         unsigned i; \
1794         (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \
1795         barrier(); \
1796         for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \
1797                 varinit[i].flags = varinit_data[i].flags; \
1798                 varinit[i].text  = varinit_data[i].text; \
1799                 varinit[i].func  = varinit_data[i].func; \
1800         } \
1801 } while (0)
1802
1803 #define vifs      varinit[0]
1804 #if ENABLE_ASH_MAIL
1805 # define vmail    (&vifs)[1]
1806 # define vmpath   (&vmail)[1]
1807 # define vpath    (&vmpath)[1]
1808 #else
1809 # define vpath    (&vifs)[1]
1810 #endif
1811 #define vps1      (&vpath)[1]
1812 #define vps2      (&vps1)[1]
1813 #define vps4      (&vps2)[1]
1814 #if ENABLE_ASH_GETOPTS
1815 # define voptind  (&vps4)[1]
1816 # if ENABLE_ASH_RANDOM_SUPPORT
1817 #  define vrandom (&voptind)[1]
1818 # endif
1819 #else
1820 # if ENABLE_ASH_RANDOM_SUPPORT
1821 #  define vrandom (&vps4)[1]
1822 # endif
1823 #endif
1824
1825 /*
1826  * The following macros access the values of the above variables.
1827  * They have to skip over the name.  They return the null string
1828  * for unset variables.
1829  */
1830 #define ifsval()        (vifs.text + 4)
1831 #define ifsset()        ((vifs.flags & VUNSET) == 0)
1832 #if ENABLE_ASH_MAIL
1833 # define mailval()      (vmail.text + 5)
1834 # define mpathval()     (vmpath.text + 9)
1835 # define mpathset()     ((vmpath.flags & VUNSET) == 0)
1836 #endif
1837 #define pathval()       (vpath.text + 5)
1838 #define ps1val()        (vps1.text + 4)
1839 #define ps2val()        (vps2.text + 4)
1840 #define ps4val()        (vps4.text + 4)
1841 #if ENABLE_ASH_GETOPTS
1842 # define optindval()    (voptind.text + 7)
1843 #endif
1844
1845
1846 #define is_name(c)      ((c) == '_' || isalpha((unsigned char)(c)))
1847 #define is_in_name(c)   ((c) == '_' || isalnum((unsigned char)(c)))
1848
1849 #if ENABLE_ASH_GETOPTS
1850 static void
1851 getoptsreset(const char *value)
1852 {
1853         shellparam.optind = number(value);
1854         shellparam.optoff = -1;
1855 }
1856 #endif
1857
1858 /*
1859  * Return of a legal variable name (a letter or underscore followed by zero or
1860  * more letters, underscores, and digits).
1861  */
1862 static char *
1863 endofname(const char *name)
1864 {
1865         char *p;
1866
1867         p = (char *) name;
1868         if (!is_name(*p))
1869                 return p;
1870         while (*++p) {
1871                 if (!is_in_name(*p))
1872                         break;
1873         }
1874         return p;
1875 }
1876
1877 /*
1878  * Compares two strings up to the first = or '\0'.  The first
1879  * string must be terminated by '='; the second may be terminated by
1880  * either '=' or '\0'.
1881  */
1882 static int
1883 varcmp(const char *p, const char *q)
1884 {
1885         int c, d;
1886
1887         while ((c = *p) == (d = *q)) {
1888                 if (!c || c == '=')
1889                         goto out;
1890                 p++;
1891                 q++;
1892         }
1893         if (c == '=')
1894                 c = '\0';
1895         if (d == '=')
1896                 d = '\0';
1897  out:
1898         return c - d;
1899 }
1900
1901 static int
1902 varequal(const char *a, const char *b)
1903 {
1904         return !varcmp(a, b);
1905 }
1906
1907 /*
1908  * Find the appropriate entry in the hash table from the name.
1909  */
1910 static struct var **
1911 hashvar(const char *p)
1912 {
1913         unsigned hashval;
1914
1915         hashval = ((unsigned char) *p) << 4;
1916         while (*p && *p != '=')
1917                 hashval += (unsigned char) *p++;
1918         return &vartab[hashval % VTABSIZE];
1919 }
1920
1921 static int
1922 vpcmp(const void *a, const void *b)
1923 {
1924         return varcmp(*(const char **)a, *(const char **)b);
1925 }
1926
1927 /*
1928  * This routine initializes the builtin variables.
1929  */
1930 static void
1931 initvar(void)
1932 {
1933         struct var *vp;
1934         struct var *end;
1935         struct var **vpp;
1936
1937         /*
1938          * PS1 depends on uid
1939          */
1940 #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT
1941         vps1.text = "PS1=\\w \\$ ";
1942 #else
1943         if (!geteuid())
1944                 vps1.text = "PS1=# ";
1945 #endif
1946         vp = varinit;
1947         end = vp + ARRAY_SIZE(varinit);
1948         do {
1949                 vpp = hashvar(vp->text);
1950                 vp->next = *vpp;
1951                 *vpp = vp;
1952         } while (++vp < end);
1953 }
1954
1955 static struct var **
1956 findvar(struct var **vpp, const char *name)
1957 {
1958         for (; *vpp; vpp = &(*vpp)->next) {
1959                 if (varequal((*vpp)->text, name)) {
1960                         break;
1961                 }
1962         }
1963         return vpp;
1964 }
1965
1966 /*
1967  * Find the value of a variable.  Returns NULL if not set.
1968  */
1969 static char *
1970 lookupvar(const char *name)
1971 {
1972         struct var *v;
1973
1974         v = *findvar(hashvar(name), name);
1975         if (v) {
1976 #ifdef DYNAMIC_VAR
1977         /*
1978          * Dynamic variables are implemented roughly the same way they are
1979          * in bash. Namely, they're "special" so long as they aren't unset.
1980          * As soon as they're unset, they're no longer dynamic, and dynamic
1981          * lookup will no longer happen at that point. -- PFM.
1982          */
1983                 if ((v->flags & VDYNAMIC))
1984                         (*v->func)(NULL);
1985 #endif
1986                 if (!(v->flags & VUNSET))
1987                         return strchrnul(v->text, '=') + 1;
1988         }
1989         return NULL;
1990 }
1991
1992 /*
1993  * Search the environment of a builtin command.
1994  */
1995 static char *
1996 bltinlookup(const char *name)
1997 {
1998         struct strlist *sp;
1999
2000         for (sp = cmdenviron; sp; sp = sp->next) {
2001                 if (varequal(sp->text, name))
2002                         return strchrnul(sp->text, '=') + 1;
2003         }
2004         return lookupvar(name);
2005 }
2006
2007 /*
2008  * Same as setvar except that the variable and value are passed in
2009  * the first argument as name=value.  Since the first argument will
2010  * be actually stored in the table, it should not be a string that
2011  * will go away.
2012  * Called with interrupts off.
2013  */
2014 static void
2015 setvareq(char *s, int flags)
2016 {
2017         struct var *vp, **vpp;
2018
2019         vpp = hashvar(s);
2020         flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1));
2021         vp = *findvar(vpp, s);
2022         if (vp) {
2023                 if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) {
2024                         const char *n;
2025
2026                         if (flags & VNOSAVE)
2027                                 free(s);
2028                         n = vp->text;
2029                         ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n);
2030                 }
2031
2032                 if (flags & VNOSET)
2033                         return;
2034
2035                 if (vp->func && (flags & VNOFUNC) == 0)
2036                         (*vp->func)(strchrnul(s, '=') + 1);
2037
2038                 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
2039                         free((char*)vp->text);
2040
2041                 flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET);
2042         } else {
2043                 if (flags & VNOSET)
2044                         return;
2045                 /* not found */
2046                 vp = ckzalloc(sizeof(*vp));
2047                 vp->next = *vpp;
2048                 /*vp->func = NULL; - ckzalloc did it */
2049                 *vpp = vp;
2050         }
2051         if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE)))
2052                 s = ckstrdup(s);
2053         vp->text = s;
2054         vp->flags = flags;
2055 }
2056
2057 /*
2058  * Set the value of a variable.  The flags argument is ored with the
2059  * flags of the variable.  If val is NULL, the variable is unset.
2060  */
2061 static void
2062 setvar(const char *name, const char *val, int flags)
2063 {
2064         char *p, *q;
2065         size_t namelen;
2066         char *nameeq;
2067         size_t vallen;
2068
2069         q = endofname(name);
2070         p = strchrnul(q, '=');
2071         namelen = p - name;
2072         if (!namelen || p != q)
2073                 ash_msg_and_raise_error("%.*s: bad variable name", namelen, name);
2074         vallen = 0;
2075         if (val == NULL) {
2076                 flags |= VUNSET;
2077         } else {
2078                 vallen = strlen(val);
2079         }
2080         INT_OFF;
2081         nameeq = ckmalloc(namelen + vallen + 2);
2082         p = (char *)memcpy(nameeq, name, namelen) + namelen;
2083         if (val) {
2084                 *p++ = '=';
2085                 p = (char *)memcpy(p, val, vallen) + vallen;
2086         }
2087         *p = '\0';
2088         setvareq(nameeq, flags | VNOSAVE);
2089         INT_ON;
2090 }
2091
2092 #if ENABLE_ASH_GETOPTS
2093 /*
2094  * Safe version of setvar, returns 1 on success 0 on failure.
2095  */
2096 static int
2097 setvarsafe(const char *name, const char *val, int flags)
2098 {
2099         int err;
2100         volatile int saveint;
2101         struct jmploc *volatile savehandler = exception_handler;
2102         struct jmploc jmploc;
2103
2104         SAVE_INT(saveint);
2105         if (setjmp(jmploc.loc))
2106                 err = 1;
2107         else {
2108                 exception_handler = &jmploc;
2109                 setvar(name, val, flags);
2110                 err = 0;
2111         }
2112         exception_handler = savehandler;
2113         RESTORE_INT(saveint);
2114         return err;
2115 }
2116 #endif
2117
2118 /*
2119  * Unset the specified variable.
2120  */
2121 static int
2122 unsetvar(const char *s)
2123 {
2124         struct var **vpp;
2125         struct var *vp;
2126         int retval;
2127
2128         vpp = findvar(hashvar(s), s);
2129         vp = *vpp;
2130         retval = 2;
2131         if (vp) {
2132                 int flags = vp->flags;
2133
2134                 retval = 1;
2135                 if (flags & VREADONLY)
2136                         goto out;
2137 #ifdef DYNAMIC_VAR
2138                 vp->flags &= ~VDYNAMIC;
2139 #endif
2140                 if (flags & VUNSET)
2141                         goto ok;
2142                 if ((flags & VSTRFIXED) == 0) {
2143                         INT_OFF;
2144                         if ((flags & (VTEXTFIXED|VSTACK)) == 0)
2145                                 free((char*)vp->text);
2146                         *vpp = vp->next;
2147                         free(vp);
2148                         INT_ON;
2149                 } else {
2150                         setvar(s, 0, 0);
2151                         vp->flags &= ~VEXPORT;
2152                 }
2153  ok:
2154                 retval = 0;
2155         }
2156  out:
2157         return retval;
2158 }
2159
2160 /*
2161  * Process a linked list of variable assignments.
2162  */
2163 static void
2164 listsetvar(struct strlist *list_set_var, int flags)
2165 {
2166         struct strlist *lp = list_set_var;
2167
2168         if (!lp)
2169                 return;
2170         INT_OFF;
2171         do {
2172                 setvareq(lp->text, flags);
2173                 lp = lp->next;
2174         } while (lp);
2175         INT_ON;
2176 }
2177
2178 /*
2179  * Generate a list of variables satisfying the given conditions.
2180  */
2181 static char **
2182 listvars(int on, int off, char ***end)
2183 {
2184         struct var **vpp;
2185         struct var *vp;
2186         char **ep;
2187         int mask;
2188
2189         STARTSTACKSTR(ep);
2190         vpp = vartab;
2191         mask = on | off;
2192         do {
2193                 for (vp = *vpp; vp; vp = vp->next) {
2194                         if ((vp->flags & mask) == on) {
2195                                 if (ep == stackstrend())
2196                                         ep = growstackstr();
2197                                 *ep++ = (char *) vp->text;
2198                         }
2199                 }
2200         } while (++vpp < vartab + VTABSIZE);
2201         if (ep == stackstrend())
2202                 ep = growstackstr();
2203         if (end)
2204                 *end = ep;
2205         *ep++ = NULL;
2206         return grabstackstr(ep);
2207 }
2208
2209
2210 /* ============ Path search helper
2211  *
2212  * The variable path (passed by reference) should be set to the start
2213  * of the path before the first call; padvance will update
2214  * this value as it proceeds.  Successive calls to padvance will return
2215  * the possible path expansions in sequence.  If an option (indicated by
2216  * a percent sign) appears in the path entry then the global variable
2217  * pathopt will be set to point to it; otherwise pathopt will be set to
2218  * NULL.
2219  */
2220 static const char *pathopt;     /* set by padvance */
2221
2222 static char *
2223 padvance(const char **path, const char *name)
2224 {
2225         const char *p;
2226         char *q;
2227         const char *start;
2228         size_t len;
2229
2230         if (*path == NULL)
2231                 return NULL;
2232         start = *path;
2233         for (p = start; *p && *p != ':' && *p != '%'; p++)
2234                 continue;
2235         len = p - start + strlen(name) + 2;     /* "2" is for '/' and '\0' */
2236         while (stackblocksize() < len)
2237                 growstackblock();
2238         q = stackblock();
2239         if (p != start) {
2240                 memcpy(q, start, p - start);
2241                 q += p - start;
2242                 *q++ = '/';
2243         }
2244         strcpy(q, name);
2245         pathopt = NULL;
2246         if (*p == '%') {
2247                 pathopt = ++p;
2248                 while (*p && *p != ':')
2249                         p++;
2250         }
2251         if (*p == ':')
2252                 *path = p + 1;
2253         else
2254                 *path = NULL;
2255         return stalloc(len);
2256 }
2257
2258
2259 /* ============ Prompt */
2260
2261 static smallint doprompt;                   /* if set, prompt the user */
2262 static smallint needprompt;                 /* true if interactive and at start of line */
2263
2264 #if ENABLE_FEATURE_EDITING
2265 static line_input_t *line_input_state;
2266 static const char *cmdedit_prompt;
2267 static void
2268 putprompt(const char *s)
2269 {
2270         if (ENABLE_ASH_EXPAND_PRMT) {
2271                 free((char*)cmdedit_prompt);
2272                 cmdedit_prompt = ckstrdup(s);
2273                 return;
2274         }
2275         cmdedit_prompt = s;
2276 }
2277 #else
2278 static void
2279 putprompt(const char *s)
2280 {
2281         out2str(s);
2282 }
2283 #endif
2284
2285 #if ENABLE_ASH_EXPAND_PRMT
2286 /* expandstr() needs parsing machinery, so it is far away ahead... */
2287 static const char *expandstr(const char *ps);
2288 #else
2289 #define expandstr(s) s
2290 #endif
2291
2292 static void
2293 setprompt(int whichprompt)
2294 {
2295         const char *prompt;
2296 #if ENABLE_ASH_EXPAND_PRMT
2297         struct stackmark smark;
2298 #endif
2299
2300         needprompt = 0;
2301
2302         switch (whichprompt) {
2303         case 1:
2304                 prompt = ps1val();
2305                 break;
2306         case 2:
2307                 prompt = ps2val();
2308                 break;
2309         default:                        /* 0 */
2310                 prompt = nullstr;
2311         }
2312 #if ENABLE_ASH_EXPAND_PRMT
2313         setstackmark(&smark);
2314         stalloc(stackblocksize());
2315 #endif
2316         putprompt(expandstr(prompt));
2317 #if ENABLE_ASH_EXPAND_PRMT
2318         popstackmark(&smark);
2319 #endif
2320 }
2321
2322
2323 /* ============ The cd and pwd commands */
2324
2325 #define CD_PHYSICAL 1
2326 #define CD_PRINT 2
2327
2328 static int docd(const char *, int);
2329
2330 static int
2331 cdopt(void)
2332 {
2333         int flags = 0;
2334         int i, j;
2335
2336         j = 'L';
2337         while ((i = nextopt("LP"))) {
2338                 if (i != j) {
2339                         flags ^= CD_PHYSICAL;
2340                         j = i;
2341                 }
2342         }
2343
2344         return flags;
2345 }
2346
2347 /*
2348  * Update curdir (the name of the current directory) in response to a
2349  * cd command.
2350  */
2351 static const char *
2352 updatepwd(const char *dir)
2353 {
2354         char *new;
2355         char *p;
2356         char *cdcomppath;
2357         const char *lim;
2358
2359         cdcomppath = ststrdup(dir);
2360         STARTSTACKSTR(new);
2361         if (*dir != '/') {
2362                 if (curdir == nullstr)
2363                         return 0;
2364                 new = stack_putstr(curdir, new);
2365         }
2366         new = makestrspace(strlen(dir) + 2, new);
2367         lim = (char *)stackblock() + 1;
2368         if (*dir != '/') {
2369                 if (new[-1] != '/')
2370                         USTPUTC('/', new);
2371                 if (new > lim && *lim == '/')
2372                         lim++;
2373         } else {
2374                 USTPUTC('/', new);
2375                 cdcomppath++;
2376                 if (dir[1] == '/' && dir[2] != '/') {
2377                         USTPUTC('/', new);
2378                         cdcomppath++;
2379                         lim++;
2380                 }
2381         }
2382         p = strtok(cdcomppath, "/");
2383         while (p) {
2384                 switch (*p) {
2385                 case '.':
2386                         if (p[1] == '.' && p[2] == '\0') {
2387                                 while (new > lim) {
2388                                         STUNPUTC(new);
2389                                         if (new[-1] == '/')
2390                                                 break;
2391                                 }
2392                                 break;
2393                         }
2394                         if (p[1] == '\0')
2395                                 break;
2396                         /* fall through */
2397                 default:
2398                         new = stack_putstr(p, new);
2399                         USTPUTC('/', new);
2400                 }
2401                 p = strtok(0, "/");
2402         }
2403         if (new > lim)
2404                 STUNPUTC(new);
2405         *new = 0;
2406         return stackblock();
2407 }
2408
2409 /*
2410  * Find out what the current directory is. If we already know the current
2411  * directory, this routine returns immediately.
2412  */
2413 static char *
2414 getpwd(void)
2415 {
2416         char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */
2417         return dir ? dir : nullstr;
2418 }
2419
2420 static void
2421 setpwd(const char *val, int setold)
2422 {
2423         char *oldcur, *dir;
2424
2425         oldcur = dir = curdir;
2426
2427         if (setold) {
2428                 setvar("OLDPWD", oldcur, VEXPORT);
2429         }
2430         INT_OFF;
2431         if (physdir != nullstr) {
2432                 if (physdir != oldcur)
2433                         free(physdir);
2434                 physdir = nullstr;
2435         }
2436         if (oldcur == val || !val) {
2437                 char *s = getpwd();
2438                 physdir = s;
2439                 if (!val)
2440                         dir = s;
2441         } else
2442                 dir = ckstrdup(val);
2443         if (oldcur != dir && oldcur != nullstr) {
2444                 free(oldcur);
2445         }
2446         curdir = dir;
2447         INT_ON;
2448         setvar("PWD", dir, VEXPORT);
2449 }
2450
2451 static void hashcd(void);
2452
2453 /*
2454  * Actually do the chdir.  We also call hashcd to let the routines in exec.c
2455  * know that the current directory has changed.
2456  */
2457 static int
2458 docd(const char *dest, int flags)
2459 {
2460         const char *dir = 0;
2461         int err;
2462
2463         TRACE(("docd(\"%s\", %d) called\n", dest, flags));
2464
2465         INT_OFF;
2466         if (!(flags & CD_PHYSICAL)) {
2467                 dir = updatepwd(dest);
2468                 if (dir)
2469                         dest = dir;
2470         }
2471         err = chdir(dest);
2472         if (err)
2473                 goto out;
2474         setpwd(dir, 1);
2475         hashcd();
2476  out:
2477         INT_ON;
2478         return err;
2479 }
2480
2481 static int
2482 cdcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
2483 {
2484         const char *dest;
2485         const char *path;
2486         const char *p;
2487         char c;
2488         struct stat statb;
2489         int flags;
2490
2491         flags = cdopt();
2492         dest = *argptr;
2493         if (!dest)
2494                 dest = bltinlookup(homestr);
2495         else if (LONE_DASH(dest)) {
2496                 dest = bltinlookup("OLDPWD");
2497                 flags |= CD_PRINT;
2498         }
2499         if (!dest)
2500                 dest = nullstr;
2501         if (*dest == '/')
2502                 goto step7;
2503         if (*dest == '.') {
2504                 c = dest[1];
2505  dotdot:
2506                 switch (c) {
2507                 case '\0':
2508                 case '/':
2509                         goto step6;
2510                 case '.':
2511                         c = dest[2];
2512                         if (c != '.')
2513                                 goto dotdot;
2514                 }
2515         }
2516         if (!*dest)
2517                 dest = ".";
2518         path = bltinlookup("CDPATH");
2519         if (!path) {
2520  step6:
2521  step7:
2522                 p = dest;
2523                 goto docd;
2524         }
2525         do {
2526                 c = *path;
2527                 p = padvance(&path, dest);
2528                 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
2529                         if (c && c != ':')
2530                                 flags |= CD_PRINT;
2531  docd:
2532                         if (!docd(p, flags))
2533                                 goto out;
2534                         break;
2535                 }
2536         } while (path);
2537         ash_msg_and_raise_error("can't cd to %s", dest);
2538         /* NOTREACHED */
2539  out:
2540         if (flags & CD_PRINT)
2541                 out1fmt(snlfmt, curdir);
2542         return 0;
2543 }
2544
2545 static int
2546 pwdcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
2547 {
2548         int flags;
2549         const char *dir = curdir;
2550
2551         flags = cdopt();
2552         if (flags) {
2553                 if (physdir == nullstr)
2554                         setpwd(dir, 0);
2555                 dir = physdir;
2556         }
2557         out1fmt(snlfmt, dir);
2558         return 0;
2559 }
2560
2561
2562 /* ============ ... */
2563
2564 #define IBUFSIZ COMMON_BUFSIZE
2565 #define basebuf bb_common_bufsiz1       /* buffer for top level input file */
2566
2567 /* Syntax classes */
2568 #define CWORD 0                 /* character is nothing special */
2569 #define CNL 1                   /* newline character */
2570 #define CBACK 2                 /* a backslash character */
2571 #define CSQUOTE 3               /* single quote */
2572 #define CDQUOTE 4               /* double quote */
2573 #define CENDQUOTE 5             /* a terminating quote */
2574 #define CBQUOTE 6               /* backwards single quote */
2575 #define CVAR 7                  /* a dollar sign */
2576 #define CENDVAR 8               /* a '}' character */
2577 #define CLP 9                   /* a left paren in arithmetic */
2578 #define CRP 10                  /* a right paren in arithmetic */
2579 #define CENDFILE 11             /* end of file */
2580 #define CCTL 12                 /* like CWORD, except it must be escaped */
2581 #define CSPCL 13                /* these terminate a word */
2582 #define CIGN 14                 /* character should be ignored */
2583
2584 #if ENABLE_ASH_ALIAS
2585 #define SYNBASE 130
2586 #define PEOF -130
2587 #define PEOA -129
2588 #define PEOA_OR_PEOF PEOA
2589 #else
2590 #define SYNBASE 129
2591 #define PEOF -129
2592 #define PEOA_OR_PEOF PEOF
2593 #endif
2594
2595 /* number syntax index */
2596 #define BASESYNTAX 0    /* not in quotes */
2597 #define DQSYNTAX   1    /* in double quotes */
2598 #define SQSYNTAX   2    /* in single quotes */
2599 #define ARISYNTAX  3    /* in arithmetic */
2600 #define PSSYNTAX   4    /* prompt */
2601
2602 #if ENABLE_ASH_OPTIMIZE_FOR_SIZE
2603 #define USE_SIT_FUNCTION
2604 #endif
2605
2606 #if ENABLE_ASH_MATH_SUPPORT
2607 static const char S_I_T[][4] = {
2608 #if ENABLE_ASH_ALIAS
2609         { CSPCL, CIGN, CIGN, CIGN },            /* 0, PEOA */
2610 #endif
2611         { CSPCL, CWORD, CWORD, CWORD },         /* 1, ' ' */
2612         { CNL, CNL, CNL, CNL },                 /* 2, \n */
2613         { CWORD, CCTL, CCTL, CWORD },           /* 3, !*-/:=?[]~ */
2614         { CDQUOTE, CENDQUOTE, CWORD, CWORD },   /* 4, '"' */
2615         { CVAR, CVAR, CWORD, CVAR },            /* 5, $ */
2616         { CSQUOTE, CWORD, CENDQUOTE, CWORD },   /* 6, "'" */
2617         { CSPCL, CWORD, CWORD, CLP },           /* 7, ( */
2618         { CSPCL, CWORD, CWORD, CRP },           /* 8, ) */
2619         { CBACK, CBACK, CCTL, CBACK },          /* 9, \ */
2620         { CBQUOTE, CBQUOTE, CWORD, CBQUOTE },   /* 10, ` */
2621         { CENDVAR, CENDVAR, CWORD, CENDVAR },   /* 11, } */
2622 #ifndef USE_SIT_FUNCTION
2623         { CENDFILE, CENDFILE, CENDFILE, CENDFILE }, /* 12, PEOF */
2624         { CWORD, CWORD, CWORD, CWORD },         /* 13, 0-9A-Za-z */
2625         { CCTL, CCTL, CCTL, CCTL }              /* 14, CTLESC ... */
2626 #endif
2627 };
2628 #else
2629 static const char S_I_T[][3] = {
2630 #if ENABLE_ASH_ALIAS
2631         { CSPCL, CIGN, CIGN },                  /* 0, PEOA */
2632 #endif
2633         { CSPCL, CWORD, CWORD },                /* 1, ' ' */
2634         { CNL, CNL, CNL },                      /* 2, \n */
2635         { CWORD, CCTL, CCTL },                  /* 3, !*-/:=?[]~ */
2636         { CDQUOTE, CENDQUOTE, CWORD },          /* 4, '"' */
2637         { CVAR, CVAR, CWORD },                  /* 5, $ */
2638         { CSQUOTE, CWORD, CENDQUOTE },          /* 6, "'" */
2639         { CSPCL, CWORD, CWORD },                /* 7, ( */
2640         { CSPCL, CWORD, CWORD },                /* 8, ) */
2641         { CBACK, CBACK, CCTL },                 /* 9, \ */
2642         { CBQUOTE, CBQUOTE, CWORD },            /* 10, ` */
2643         { CENDVAR, CENDVAR, CWORD },            /* 11, } */
2644 #ifndef USE_SIT_FUNCTION
2645         { CENDFILE, CENDFILE, CENDFILE },       /* 12, PEOF */
2646         { CWORD, CWORD, CWORD },                /* 13, 0-9A-Za-z */
2647         { CCTL, CCTL, CCTL }                    /* 14, CTLESC ... */
2648 #endif
2649 };
2650 #endif /* ASH_MATH_SUPPORT */
2651
2652 #ifdef USE_SIT_FUNCTION
2653
2654 static int
2655 SIT(int c, int syntax)
2656 {
2657         static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-/:;<=>?[\\]`|}~";
2658 #if ENABLE_ASH_ALIAS
2659         static const char syntax_index_table[] ALIGN1 = {
2660                 1, 2, 1, 3, 4, 5, 1, 6,         /* "\t\n !\"$&'" */
2661                 7, 8, 3, 3, 3, 3, 1, 1,         /* "()*-/:;<" */
2662                 3, 1, 3, 3, 9, 3, 10, 1,        /* "=>?[\\]`|" */
2663                 11, 3                           /* "}~" */
2664         };
2665 #else
2666         static const char syntax_index_table[] ALIGN1 = {
2667                 0, 1, 0, 2, 3, 4, 0, 5,         /* "\t\n !\"$&'" */
2668                 6, 7, 2, 2, 2, 2, 0, 0,         /* "()*-/:;<" */
2669                 2, 0, 2, 2, 8, 2, 9, 0,         /* "=>?[\\]`|" */
2670                 10, 2                           /* "}~" */
2671         };
2672 #endif
2673         const char *s;
2674         int indx;
2675
2676         if (c == PEOF)          /* 2^8+2 */
2677                 return CENDFILE;
2678 #if ENABLE_ASH_ALIAS
2679         if (c == PEOA)          /* 2^8+1 */
2680                 indx = 0;
2681         else
2682 #endif
2683 #define U_C(c) ((unsigned char)(c))
2684
2685         if ((unsigned char)c >= (unsigned char)(CTLESC)
2686          && (unsigned char)c <= (unsigned char)(CTLQUOTEMARK)
2687         ) {
2688                 return CCTL;
2689         } else {
2690                 s = strchrnul(spec_symbls, c);
2691                 if (*s == '\0')
2692                         return CWORD;
2693                 indx = syntax_index_table[s - spec_symbls];
2694         }
2695         return S_I_T[indx][syntax];
2696 }
2697
2698 #else   /* !USE_SIT_FUNCTION */
2699
2700 #if ENABLE_ASH_ALIAS
2701 #define CSPCL_CIGN_CIGN_CIGN                     0
2702 #define CSPCL_CWORD_CWORD_CWORD                  1
2703 #define CNL_CNL_CNL_CNL                          2
2704 #define CWORD_CCTL_CCTL_CWORD                    3
2705 #define CDQUOTE_CENDQUOTE_CWORD_CWORD            4
2706 #define CVAR_CVAR_CWORD_CVAR                     5
2707 #define CSQUOTE_CWORD_CENDQUOTE_CWORD            6
2708 #define CSPCL_CWORD_CWORD_CLP                    7
2709 #define CSPCL_CWORD_CWORD_CRP                    8
2710 #define CBACK_CBACK_CCTL_CBACK                   9
2711 #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE           10
2712 #define CENDVAR_CENDVAR_CWORD_CENDVAR           11
2713 #define CENDFILE_CENDFILE_CENDFILE_CENDFILE     12
2714 #define CWORD_CWORD_CWORD_CWORD                 13
2715 #define CCTL_CCTL_CCTL_CCTL                     14
2716 #else
2717 #define CSPCL_CWORD_CWORD_CWORD                  0
2718 #define CNL_CNL_CNL_CNL                          1
2719 #define CWORD_CCTL_CCTL_CWORD                    2
2720 #define CDQUOTE_CENDQUOTE_CWORD_CWORD            3
2721 #define CVAR_CVAR_CWORD_CVAR                     4
2722 #define CSQUOTE_CWORD_CENDQUOTE_CWORD            5
2723 #define CSPCL_CWORD_CWORD_CLP                    6
2724 #define CSPCL_CWORD_CWORD_CRP                    7
2725 #define CBACK_CBACK_CCTL_CBACK                   8
2726 #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE            9
2727 #define CENDVAR_CENDVAR_CWORD_CENDVAR           10
2728 #define CENDFILE_CENDFILE_CENDFILE_CENDFILE     11
2729 #define CWORD_CWORD_CWORD_CWORD                 12
2730 #define CCTL_CCTL_CCTL_CCTL                     13
2731 #endif
2732
2733 static const char syntax_index_table[258] = {
2734         /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */
2735         /*   0  PEOF */      CENDFILE_CENDFILE_CENDFILE_CENDFILE,
2736 #if ENABLE_ASH_ALIAS
2737         /*   1  PEOA */      CSPCL_CIGN_CIGN_CIGN,
2738 #endif
2739         /*   2  -128 0x80 */ CWORD_CWORD_CWORD_CWORD,
2740         /*   3  -127 CTLESC       */ CCTL_CCTL_CCTL_CCTL,
2741         /*   4  -126 CTLVAR       */ CCTL_CCTL_CCTL_CCTL,
2742         /*   5  -125 CTLENDVAR    */ CCTL_CCTL_CCTL_CCTL,
2743         /*   6  -124 CTLBACKQ     */ CCTL_CCTL_CCTL_CCTL,
2744         /*   7  -123 CTLQUOTE     */ CCTL_CCTL_CCTL_CCTL,
2745         /*   8  -122 CTLARI       */ CCTL_CCTL_CCTL_CCTL,
2746         /*   9  -121 CTLENDARI    */ CCTL_CCTL_CCTL_CCTL,
2747         /*  10  -120 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL,
2748         /*  11  -119      */ CWORD_CWORD_CWORD_CWORD,
2749         /*  12  -118      */ CWORD_CWORD_CWORD_CWORD,
2750         /*  13  -117      */ CWORD_CWORD_CWORD_CWORD,
2751         /*  14  -116      */ CWORD_CWORD_CWORD_CWORD,
2752         /*  15  -115      */ CWORD_CWORD_CWORD_CWORD,
2753         /*  16  -114      */ CWORD_CWORD_CWORD_CWORD,
2754         /*  17  -113      */ CWORD_CWORD_CWORD_CWORD,
2755         /*  18  -112      */ CWORD_CWORD_CWORD_CWORD,
2756         /*  19  -111      */ CWORD_CWORD_CWORD_CWORD,
2757         /*  20  -110      */ CWORD_CWORD_CWORD_CWORD,
2758         /*  21  -109      */ CWORD_CWORD_CWORD_CWORD,
2759         /*  22  -108      */ CWORD_CWORD_CWORD_CWORD,
2760         /*  23  -107      */ CWORD_CWORD_CWORD_CWORD,
2761         /*  24  -106      */ CWORD_CWORD_CWORD_CWORD,
2762         /*  25  -105      */ CWORD_CWORD_CWORD_CWORD,
2763         /*  26  -104      */ CWORD_CWORD_CWORD_CWORD,
2764         /*  27  -103      */ CWORD_CWORD_CWORD_CWORD,
2765         /*  28  -102      */ CWORD_CWORD_CWORD_CWORD,
2766         /*  29  -101      */ CWORD_CWORD_CWORD_CWORD,
2767         /*  30  -100      */ CWORD_CWORD_CWORD_CWORD,
2768         /*  31   -99      */ CWORD_CWORD_CWORD_CWORD,
2769         /*  32   -98      */ CWORD_CWORD_CWORD_CWORD,
2770         /*  33   -97      */ CWORD_CWORD_CWORD_CWORD,
2771         /*  34   -96      */ CWORD_CWORD_CWORD_CWORD,
2772         /*  35   -95      */ CWORD_CWORD_CWORD_CWORD,
2773         /*  36   -94      */ CWORD_CWORD_CWORD_CWORD,
2774         /*  37   -93      */ CWORD_CWORD_CWORD_CWORD,
2775         /*  38   -92      */ CWORD_CWORD_CWORD_CWORD,
2776         /*  39   -91      */ CWORD_CWORD_CWORD_CWORD,
2777         /*  40   -90      */ CWORD_CWORD_CWORD_CWORD,
2778         /*  41   -89      */ CWORD_CWORD_CWORD_CWORD,
2779         /*  42   -88      */ CWORD_CWORD_CWORD_CWORD,
2780         /*  43   -87      */ CWORD_CWORD_CWORD_CWORD,
2781         /*  44   -86      */ CWORD_CWORD_CWORD_CWORD,
2782         /*  45   -85      */ CWORD_CWORD_CWORD_CWORD,
2783         /*  46   -84      */ CWORD_CWORD_CWORD_CWORD,
2784         /*  47   -83      */ CWORD_CWORD_CWORD_CWORD,
2785         /*  48   -82      */ CWORD_CWORD_CWORD_CWORD,
2786         /*  49   -81      */ CWORD_CWORD_CWORD_CWORD,
2787         /*  50   -80      */ CWORD_CWORD_CWORD_CWORD,
2788         /*  51   -79      */ CWORD_CWORD_CWORD_CWORD,
2789         /*  52   -78      */ CWORD_CWORD_CWORD_CWORD,
2790         /*  53   -77      */ CWORD_CWORD_CWORD_CWORD,
2791         /*  54   -76      */ CWORD_CWORD_CWORD_CWORD,
2792         /*  55   -75      */ CWORD_CWORD_CWORD_CWORD,
2793         /*  56   -74      */ CWORD_CWORD_CWORD_CWORD,
2794         /*  57   -73      */ CWORD_CWORD_CWORD_CWORD,
2795         /*  58   -72      */ CWORD_CWORD_CWORD_CWORD,
2796         /*  59   -71      */ CWORD_CWORD_CWORD_CWORD,
2797         /*  60   -70      */ CWORD_CWORD_CWORD_CWORD,
2798         /*  61   -69      */ CWORD_CWORD_CWORD_CWORD,
2799         /*  62   -68      */ CWORD_CWORD_CWORD_CWORD,
2800         /*  63   -67      */ CWORD_CWORD_CWORD_CWORD,
2801         /*  64   -66      */ CWORD_CWORD_CWORD_CWORD,
2802         /*  65   -65      */ CWORD_CWORD_CWORD_CWORD,
2803         /*  66   -64      */ CWORD_CWORD_CWORD_CWORD,
2804         /*  67   -63      */ CWORD_CWORD_CWORD_CWORD,
2805         /*  68   -62      */ CWORD_CWORD_CWORD_CWORD,
2806         /*  69   -61      */ CWORD_CWORD_CWORD_CWORD,
2807         /*  70   -60      */ CWORD_CWORD_CWORD_CWORD,
2808         /*  71   -59      */ CWORD_CWORD_CWORD_CWORD,
2809         /*  72   -58      */ CWORD_CWORD_CWORD_CWORD,
2810         /*  73   -57      */ CWORD_CWORD_CWORD_CWORD,
2811         /*  74   -56      */ CWORD_CWORD_CWORD_CWORD,
2812         /*  75   -55      */ CWORD_CWORD_CWORD_CWORD,
2813         /*  76   -54      */ CWORD_CWORD_CWORD_CWORD,
2814         /*  77   -53      */ CWORD_CWORD_CWORD_CWORD,
2815         /*  78   -52      */ CWORD_CWORD_CWORD_CWORD,
2816         /*  79   -51      */ CWORD_CWORD_CWORD_CWORD,
2817         /*  80   -50      */ CWORD_CWORD_CWORD_CWORD,
2818         /*  81   -49      */ CWORD_CWORD_CWORD_CWORD,
2819         /*  82   -48      */ CWORD_CWORD_CWORD_CWORD,
2820         /*  83   -47      */ CWORD_CWORD_CWORD_CWORD,
2821         /*  84   -46      */ CWORD_CWORD_CWORD_CWORD,
2822         /*  85   -45      */ CWORD_CWORD_CWORD_CWORD,
2823         /*  86   -44      */ CWORD_CWORD_CWORD_CWORD,
2824         /*  87   -43      */ CWORD_CWORD_CWORD_CWORD,
2825         /*  88   -42      */ CWORD_CWORD_CWORD_CWORD,
2826         /*  89   -41      */ CWORD_CWORD_CWORD_CWORD,
2827         /*  90   -40      */ CWORD_CWORD_CWORD_CWORD,
2828         /*  91   -39      */ CWORD_CWORD_CWORD_CWORD,
2829         /*  92   -38      */ CWORD_CWORD_CWORD_CWORD,
2830         /*  93   -37      */ CWORD_CWORD_CWORD_CWORD,
2831         /*  94   -36      */ CWORD_CWORD_CWORD_CWORD,
2832         /*  95   -35      */ CWORD_CWORD_CWORD_CWORD,
2833         /*  96   -34      */ CWORD_CWORD_CWORD_CWORD,
2834         /*  97   -33      */ CWORD_CWORD_CWORD_CWORD,
2835         /*  98   -32      */ CWORD_CWORD_CWORD_CWORD,
2836         /*  99   -31      */ CWORD_CWORD_CWORD_CWORD,
2837         /* 100   -30      */ CWORD_CWORD_CWORD_CWORD,
2838         /* 101   -29      */ CWORD_CWORD_CWORD_CWORD,
2839         /* 102   -28      */ CWORD_CWORD_CWORD_CWORD,
2840         /* 103   -27      */ CWORD_CWORD_CWORD_CWORD,
2841         /* 104   -26      */ CWORD_CWORD_CWORD_CWORD,
2842         /* 105   -25      */ CWORD_CWORD_CWORD_CWORD,
2843         /* 106   -24      */ CWORD_CWORD_CWORD_CWORD,
2844         /* 107   -23      */ CWORD_CWORD_CWORD_CWORD,
2845         /* 108   -22      */ CWORD_CWORD_CWORD_CWORD,
2846         /* 109   -21      */ CWORD_CWORD_CWORD_CWORD,
2847         /* 110   -20      */ CWORD_CWORD_CWORD_CWORD,
2848         /* 111   -19      */ CWORD_CWORD_CWORD_CWORD,
2849         /* 112   -18      */ CWORD_CWORD_CWORD_CWORD,
2850         /* 113   -17      */ CWORD_CWORD_CWORD_CWORD,
2851         /* 114   -16      */ CWORD_CWORD_CWORD_CWORD,
2852         /* 115   -15      */ CWORD_CWORD_CWORD_CWORD,
2853         /* 116   -14      */ CWORD_CWORD_CWORD_CWORD,
2854         /* 117   -13      */ CWORD_CWORD_CWORD_CWORD,
2855         /* 118   -12      */ CWORD_CWORD_CWORD_CWORD,
2856         /* 119   -11      */ CWORD_CWORD_CWORD_CWORD,
2857         /* 120   -10      */ CWORD_CWORD_CWORD_CWORD,
2858         /* 121    -9      */ CWORD_CWORD_CWORD_CWORD,
2859         /* 122    -8      */ CWORD_CWORD_CWORD_CWORD,
2860         /* 123    -7      */ CWORD_CWORD_CWORD_CWORD,
2861         /* 124    -6      */ CWORD_CWORD_CWORD_CWORD,
2862         /* 125    -5      */ CWORD_CWORD_CWORD_CWORD,
2863         /* 126    -4      */ CWORD_CWORD_CWORD_CWORD,
2864         /* 127    -3      */ CWORD_CWORD_CWORD_CWORD,
2865         /* 128    -2      */ CWORD_CWORD_CWORD_CWORD,
2866         /* 129    -1      */ CWORD_CWORD_CWORD_CWORD,
2867         /* 130     0      */ CWORD_CWORD_CWORD_CWORD,
2868         /* 131     1      */ CWORD_CWORD_CWORD_CWORD,
2869         /* 132     2      */ CWORD_CWORD_CWORD_CWORD,
2870         /* 133     3      */ CWORD_CWORD_CWORD_CWORD,
2871         /* 134     4      */ CWORD_CWORD_CWORD_CWORD,
2872         /* 135     5      */ CWORD_CWORD_CWORD_CWORD,
2873         /* 136     6      */ CWORD_CWORD_CWORD_CWORD,
2874         /* 137     7      */ CWORD_CWORD_CWORD_CWORD,
2875         /* 138     8      */ CWORD_CWORD_CWORD_CWORD,
2876         /* 139     9 "\t" */ CSPCL_CWORD_CWORD_CWORD,
2877         /* 140    10 "\n" */ CNL_CNL_CNL_CNL,
2878         /* 141    11      */ CWORD_CWORD_CWORD_CWORD,
2879         /* 142    12      */ CWORD_CWORD_CWORD_CWORD,
2880         /* 143    13      */ CWORD_CWORD_CWORD_CWORD,
2881         /* 144    14      */ CWORD_CWORD_CWORD_CWORD,
2882         /* 145    15      */ CWORD_CWORD_CWORD_CWORD,
2883         /* 146    16      */ CWORD_CWORD_CWORD_CWORD,
2884         /* 147    17      */ CWORD_CWORD_CWORD_CWORD,
2885         /* 148    18      */ CWORD_CWORD_CWORD_CWORD,
2886         /* 149    19      */ CWORD_CWORD_CWORD_CWORD,
2887         /* 150    20      */ CWORD_CWORD_CWORD_CWORD,
2888         /* 151    21      */ CWORD_CWORD_CWORD_CWORD,
2889         /* 152    22      */ CWORD_CWORD_CWORD_CWORD,
2890         /* 153    23      */ CWORD_CWORD_CWORD_CWORD,
2891         /* 154    24      */ CWORD_CWORD_CWORD_CWORD,
2892         /* 155    25      */ CWORD_CWORD_CWORD_CWORD,
2893         /* 156    26      */ CWORD_CWORD_CWORD_CWORD,
2894         /* 157    27      */ CWORD_CWORD_CWORD_CWORD,
2895         /* 158    28      */ CWORD_CWORD_CWORD_CWORD,
2896         /* 159    29      */ CWORD_CWORD_CWORD_CWORD,
2897         /* 160    30      */ CWORD_CWORD_CWORD_CWORD,
2898         /* 161    31      */ CWORD_CWORD_CWORD_CWORD,
2899         /* 162    32  " " */ CSPCL_CWORD_CWORD_CWORD,
2900         /* 163    33  "!" */ CWORD_CCTL_CCTL_CWORD,
2901         /* 164    34  """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD,
2902         /* 165    35  "#" */ CWORD_CWORD_CWORD_CWORD,
2903         /* 166    36  "$" */ CVAR_CVAR_CWORD_CVAR,
2904         /* 167    37  "%" */ CWORD_CWORD_CWORD_CWORD,
2905         /* 168    38  "&" */ CSPCL_CWORD_CWORD_CWORD,
2906         /* 169    39  "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD,
2907         /* 170    40  "(" */ CSPCL_CWORD_CWORD_CLP,
2908         /* 171    41  ")" */ CSPCL_CWORD_CWORD_CRP,
2909         /* 172    42  "*" */ CWORD_CCTL_CCTL_CWORD,
2910         /* 173    43  "+" */ CWORD_CWORD_CWORD_CWORD,
2911         /* 174    44  "," */ CWORD_CWORD_CWORD_CWORD,
2912         /* 175    45  "-" */ CWORD_CCTL_CCTL_CWORD,
2913         /* 176    46  "." */ CWORD_CWORD_CWORD_CWORD,
2914         /* 177    47  "/" */ CWORD_CCTL_CCTL_CWORD,
2915         /* 178    48  "0" */ CWORD_CWORD_CWORD_CWORD,
2916         /* 179    49  "1" */ CWORD_CWORD_CWORD_CWORD,
2917         /* 180    50  "2" */ CWORD_CWORD_CWORD_CWORD,
2918         /* 181    51  "3" */ CWORD_CWORD_CWORD_CWORD,
2919         /* 182    52  "4" */ CWORD_CWORD_CWORD_CWORD,
2920         /* 183    53  "5" */ CWORD_CWORD_CWORD_CWORD,
2921         /* 184    54  "6" */ CWORD_CWORD_CWORD_CWORD,
2922         /* 185    55  "7" */ CWORD_CWORD_CWORD_CWORD,
2923         /* 186    56  "8" */ CWORD_CWORD_CWORD_CWORD,
2924         /* 187    57  "9" */ CWORD_CWORD_CWORD_CWORD,
2925         /* 188    58  ":" */ CWORD_CCTL_CCTL_CWORD,
2926         /* 189    59  ";" */ CSPCL_CWORD_CWORD_CWORD,
2927         /* 190    60  "<" */ CSPCL_CWORD_CWORD_CWORD,
2928         /* 191    61  "=" */ CWORD_CCTL_CCTL_CWORD,
2929         /* 192    62  ">" */ CSPCL_CWORD_CWORD_CWORD,
2930         /* 193    63  "?" */ CWORD_CCTL_CCTL_CWORD,
2931         /* 194    64  "@" */ CWORD_CWORD_CWORD_CWORD,
2932         /* 195    65  "A" */ CWORD_CWORD_CWORD_CWORD,
2933         /* 196    66  "B" */ CWORD_CWORD_CWORD_CWORD,
2934         /* 197    67  "C" */ CWORD_CWORD_CWORD_CWORD,
2935         /* 198    68  "D" */ CWORD_CWORD_CWORD_CWORD,
2936         /* 199    69  "E" */ CWORD_CWORD_CWORD_CWORD,
2937         /* 200    70  "F" */ CWORD_CWORD_CWORD_CWORD,
2938         /* 201    71  "G" */ CWORD_CWORD_CWORD_CWORD,
2939         /* 202    72  "H" */ CWORD_CWORD_CWORD_CWORD,
2940         /* 203    73  "I" */ CWORD_CWORD_CWORD_CWORD,
2941         /* 204    74  "J" */ CWORD_CWORD_CWORD_CWORD,
2942         /* 205    75  "K" */ CWORD_CWORD_CWORD_CWORD,
2943         /* 206    76  "L" */ CWORD_CWORD_CWORD_CWORD,
2944         /* 207    77  "M" */ CWORD_CWORD_CWORD_CWORD,
2945         /* 208    78  "N" */ CWORD_CWORD_CWORD_CWORD,
2946         /* 209    79  "O" */ CWORD_CWORD_CWORD_CWORD,
2947         /* 210    80  "P" */ CWORD_CWORD_CWORD_CWORD,
2948         /* 211    81  "Q" */ CWORD_CWORD_CWORD_CWORD,
2949         /* 212    82  "R" */ CWORD_CWORD_CWORD_CWORD,
2950         /* 213    83  "S" */ CWORD_CWORD_CWORD_CWORD,
2951         /* 214    84  "T" */ CWORD_CWORD_CWORD_CWORD,
2952         /* 215    85  "U" */ CWORD_CWORD_CWORD_CWORD,
2953         /* 216    86  "V" */ CWORD_CWORD_CWORD_CWORD,
2954         /* 217    87  "W" */ CWORD_CWORD_CWORD_CWORD,
2955         /* 218    88  "X" */ CWORD_CWORD_CWORD_CWORD,
2956         /* 219    89  "Y" */ CWORD_CWORD_CWORD_CWORD,
2957         /* 220    90  "Z" */ CWORD_CWORD_CWORD_CWORD,
2958         /* 221    91  "[" */ CWORD_CCTL_CCTL_CWORD,
2959         /* 222    92  "\" */ CBACK_CBACK_CCTL_CBACK,
2960         /* 223    93  "]" */ CWORD_CCTL_CCTL_CWORD,
2961         /* 224    94  "^" */ CWORD_CWORD_CWORD_CWORD,
2962         /* 225    95  "_" */ CWORD_CWORD_CWORD_CWORD,
2963         /* 226    96  "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE,
2964         /* 227    97  "a" */ CWORD_CWORD_CWORD_CWORD,
2965         /* 228    98  "b" */ CWORD_CWORD_CWORD_CWORD,
2966         /* 229    99  "c" */ CWORD_CWORD_CWORD_CWORD,
2967         /* 230   100  "d" */ CWORD_CWORD_CWORD_CWORD,
2968         /* 231   101  "e" */ CWORD_CWORD_CWORD_CWORD,
2969         /* 232   102  "f" */ CWORD_CWORD_CWORD_CWORD,
2970         /* 233   103  "g" */ CWORD_CWORD_CWORD_CWORD,
2971         /* 234   104  "h" */ CWORD_CWORD_CWORD_CWORD,
2972         /* 235   105  "i" */ CWORD_CWORD_CWORD_CWORD,
2973         /* 236   106  "j" */ CWORD_CWORD_CWORD_CWORD,
2974         /* 237   107  "k" */ CWORD_CWORD_CWORD_CWORD,
2975         /* 238   108  "l" */ CWORD_CWORD_CWORD_CWORD,
2976         /* 239   109  "m" */ CWORD_CWORD_CWORD_CWORD,
2977         /* 240   110  "n" */ CWORD_CWORD_CWORD_CWORD,
2978         /* 241   111  "o" */ CWORD_CWORD_CWORD_CWORD,
2979         /* 242   112  "p" */ CWORD_CWORD_CWORD_CWORD,
2980         /* 243   113  "q" */ CWORD_CWORD_CWORD_CWORD,
2981         /* 244   114  "r" */ CWORD_CWORD_CWORD_CWORD,
2982         /* 245   115  "s" */ CWORD_CWORD_CWORD_CWORD,
2983         /* 246   116  "t" */ CWORD_CWORD_CWORD_CWORD,
2984         /* 247   117  "u" */ CWORD_CWORD_CWORD_CWORD,
2985         /* 248   118  "v" */ CWORD_CWORD_CWORD_CWORD,
2986         /* 249   119  "w" */ CWORD_CWORD_CWORD_CWORD,
2987         /* 250   120  "x" */ CWORD_CWORD_CWORD_CWORD,
2988         /* 251   121  "y" */ CWORD_CWORD_CWORD_CWORD,
2989         /* 252   122  "z" */ CWORD_CWORD_CWORD_CWORD,
2990         /* 253   123  "{" */ CWORD_CWORD_CWORD_CWORD,
2991         /* 254   124  "|" */ CSPCL_CWORD_CWORD_CWORD,
2992         /* 255   125  "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR,
2993         /* 256   126  "~" */ CWORD_CCTL_CCTL_CWORD,
2994         /* 257   127      */ CWORD_CWORD_CWORD_CWORD,
2995 };
2996
2997 #define SIT(c, syntax) (S_I_T[(int)syntax_index_table[((int)c)+SYNBASE]][syntax])
2998
2999 #endif  /* USE_SIT_FUNCTION */
3000
3001
3002 /* ============ Alias handling */
3003
3004 #if ENABLE_ASH_ALIAS
3005
3006 #define ALIASINUSE 1
3007 #define ALIASDEAD  2
3008
3009 struct alias {
3010         struct alias *next;
3011         char *name;
3012         char *val;
3013         int flag;
3014 };
3015
3016
3017 static struct alias **atab; // [ATABSIZE];
3018 #define INIT_G_alias() do { \
3019         atab = xzalloc(ATABSIZE * sizeof(atab[0])); \
3020 } while (0)
3021
3022
3023 static struct alias **
3024 __lookupalias(const char *name) {
3025         unsigned int hashval;
3026         struct alias **app;
3027         const char *p;
3028         unsigned int ch;
3029
3030         p = name;
3031
3032         ch = (unsigned char)*p;
3033         hashval = ch << 4;
3034         while (ch) {
3035                 hashval += ch;
3036                 ch = (unsigned char)*++p;
3037         }
3038         app = &atab[hashval % ATABSIZE];
3039
3040         for (; *app; app = &(*app)->next) {
3041                 if (strcmp(name, (*app)->name) == 0) {
3042                         break;
3043                 }
3044         }
3045
3046         return app;
3047 }
3048
3049 static struct alias *
3050 lookupalias(const char *name, int check)
3051 {
3052         struct alias *ap = *__lookupalias(name);
3053
3054         if (check && ap && (ap->flag & ALIASINUSE))
3055                 return NULL;
3056         return ap;
3057 }
3058
3059 static struct alias *
3060 freealias(struct alias *ap)
3061 {
3062         struct alias *next;
3063
3064         if (ap->flag & ALIASINUSE) {
3065                 ap->flag |= ALIASDEAD;
3066                 return ap;
3067         }
3068
3069         next = ap->next;
3070         free(ap->name);
3071         free(ap->val);
3072         free(ap);
3073         return next;
3074 }
3075
3076 static void
3077 setalias(const char *name, const char *val)
3078 {
3079         struct alias *ap, **app;
3080
3081         app = __lookupalias(name);
3082         ap = *app;
3083         INT_OFF;
3084         if (ap) {
3085                 if (!(ap->flag & ALIASINUSE)) {
3086                         free(ap->val);
3087                 }
3088                 ap->val = ckstrdup(val);
3089                 ap->flag &= ~ALIASDEAD;
3090         } else {
3091                 /* not found */
3092                 ap = ckzalloc(sizeof(struct alias));
3093                 ap->name = ckstrdup(name);
3094                 ap->val = ckstrdup(val);
3095                 /*ap->flag = 0; - ckzalloc did it */
3096                 /*ap->next = NULL;*/
3097                 *app = ap;
3098         }
3099         INT_ON;
3100 }
3101
3102 static int
3103 unalias(const char *name)
3104 {
3105         struct alias **app;
3106
3107         app = __lookupalias(name);
3108
3109         if (*app) {
3110                 INT_OFF;
3111                 *app = freealias(*app);
3112                 INT_ON;
3113                 return 0;
3114         }
3115
3116         return 1;
3117 }
3118
3119 static void
3120 rmaliases(void)
3121 {
3122         struct alias *ap, **app;
3123         int i;
3124
3125         INT_OFF;
3126         for (i = 0; i < ATABSIZE; i++) {
3127                 app = &atab[i];
3128                 for (ap = *app; ap; ap = *app) {
3129                         *app = freealias(*app);
3130                         if (ap == *app) {
3131                                 app = &ap->next;
3132                         }
3133                 }
3134         }
3135         INT_ON;
3136 }
3137
3138 static void
3139 printalias(const struct alias *ap)
3140 {
3141         out1fmt("%s=%s\n", ap->name, single_quote(ap->val));
3142 }
3143
3144 /*
3145  * TODO - sort output
3146  */
3147 static int
3148 aliascmd(int argc ATTRIBUTE_UNUSED, char **argv)
3149 {
3150         char *n, *v;
3151         int ret = 0;
3152         struct alias *ap;
3153
3154         if (!argv[1]) {
3155                 int i;
3156
3157                 for (i = 0; i < ATABSIZE; i++) {
3158                         for (ap = atab[i]; ap; ap = ap->next) {
3159                                 printalias(ap);
3160                         }
3161                 }
3162                 return 0;
3163         }
3164         while ((n = *++argv) != NULL) {
3165                 v = strchr(n+1, '=');
3166                 if (v == NULL) { /* n+1: funny ksh stuff */
3167                         ap = *__lookupalias(n);
3168                         if (ap == NULL) {
3169                                 fprintf(stderr, "%s: %s not found\n", "alias", n);
3170                                 ret = 1;
3171                         } else
3172                                 printalias(ap);
3173                 } else {
3174                         *v++ = '\0';
3175                         setalias(n, v);
3176                 }
3177         }
3178
3179         return ret;
3180 }
3181
3182 static int
3183 unaliascmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
3184 {
3185         int i;
3186
3187         while ((i = nextopt("a")) != '\0') {
3188                 if (i == 'a') {
3189                         rmaliases();
3190                         return 0;
3191                 }
3192         }
3193         for (i = 0; *argptr; argptr++) {
3194                 if (unalias(*argptr)) {
3195                         fprintf(stderr, "%s: %s not found\n", "unalias", *argptr);
3196                         i = 1;
3197                 }
3198         }
3199
3200         return i;
3201 }
3202
3203 #endif /* ASH_ALIAS */
3204
3205
3206 /* ============ jobs.c */
3207
3208 /* Mode argument to forkshell.  Don't change FORK_FG or FORK_BG. */
3209 #define FORK_FG 0
3210 #define FORK_BG 1
3211 #define FORK_NOJOB 2
3212
3213 /* mode flags for showjob(s) */
3214 #define SHOW_PGID       0x01    /* only show pgid - for jobs -p */
3215 #define SHOW_PID        0x04    /* include process pid */
3216 #define SHOW_CHANGED    0x08    /* only jobs whose state has changed */
3217
3218 /*
3219  * A job structure contains information about a job.  A job is either a
3220  * single process or a set of processes contained in a pipeline.  In the
3221  * latter case, pidlist will be non-NULL, and will point to a -1 terminated
3222  * array of pids.
3223  */
3224
3225 struct procstat {
3226         pid_t   pid;            /* process id */
3227         int     status;         /* last process status from wait() */
3228         char    *cmd;           /* text of command being run */
3229 };
3230
3231 struct job {
3232         struct procstat ps0;    /* status of process */
3233         struct procstat *ps;    /* status or processes when more than one */
3234 #if JOBS
3235         int stopstatus;         /* status of a stopped job */
3236 #endif
3237         uint32_t
3238                 nprocs: 16,     /* number of processes */
3239                 state: 8,
3240 #define JOBRUNNING      0       /* at least one proc running */
3241 #define JOBSTOPPED      1       /* all procs are stopped */
3242 #define JOBDONE         2       /* all procs are completed */
3243 #if JOBS
3244                 sigint: 1,      /* job was killed by SIGINT */
3245                 jobctl: 1,      /* job running under job control */
3246 #endif
3247                 waited: 1,      /* true if this entry has been waited for */
3248                 used: 1,        /* true if this entry is in used */
3249                 changed: 1;     /* true if status has changed */
3250         struct job *prev_job;   /* previous job */
3251 };
3252
3253 static pid_t backgndpid;        /* pid of last background process */
3254 static smallint job_warning;    /* user was warned about stopped jobs (can be 2, 1 or 0). */
3255
3256 static struct job *makejob(/*union node *,*/ int);
3257 #if !JOBS
3258 #define forkshell(job, node, mode) forkshell(job, mode)
3259 #endif
3260 static int forkshell(struct job *, union node *, int);
3261 static int waitforjob(struct job *);
3262
3263 #if !JOBS
3264 enum { doing_jobctl = 0 };
3265 #define setjobctl(on) do {} while (0)
3266 #else
3267 static smallint doing_jobctl;
3268 static void setjobctl(int);
3269 #endif
3270
3271 /*
3272  * Set the signal handler for the specified signal.  The routine figures
3273  * out what it should be set to.
3274  */
3275 static void
3276 setsignal(int signo)
3277 {
3278         int action;
3279         char *t, tsig;
3280         struct sigaction act;
3281
3282         t = trap[signo];
3283         action = S_IGN;
3284         if (t == NULL)
3285                 action = S_DFL;
3286         else if (*t != '\0')
3287                 action = S_CATCH;
3288         if (rootshell && action == S_DFL) {
3289                 switch (signo) {
3290                 case SIGINT:
3291                         if (iflag || minusc || sflag == 0)
3292                                 action = S_CATCH;
3293                         break;
3294                 case SIGQUIT:
3295 #if DEBUG
3296                         if (debug)
3297                                 break;
3298 #endif
3299                         /* FALLTHROUGH */
3300                 case SIGTERM:
3301                         if (iflag)
3302                                 action = S_IGN;
3303                         break;
3304 #if JOBS
3305                 case SIGTSTP:
3306                 case SIGTTOU:
3307                         if (mflag)
3308                                 action = S_IGN;
3309                         break;
3310 #endif
3311                 }
3312         }
3313
3314         t = &sigmode[signo - 1];
3315         tsig = *t;
3316         if (tsig == 0) {
3317                 /*
3318                  * current setting unknown
3319                  */
3320                 if (sigaction(signo, NULL, &act) == -1) {
3321                         /*
3322                          * Pretend it worked; maybe we should give a warning
3323                          * here, but other shells don't. We don't alter
3324                          * sigmode, so that we retry every time.
3325                          */
3326                         return;
3327                 }
3328                 tsig = S_RESET; /* force to be set */
3329                 if (act.sa_handler == SIG_IGN) {
3330                         tsig = S_HARD_IGN;
3331                         if (mflag
3332                          && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU)
3333                         ) {
3334                                 tsig = S_IGN;   /* don't hard ignore these */
3335                         }
3336                 }
3337         }
3338         if (tsig == S_HARD_IGN || tsig == action)
3339                 return;
3340         act.sa_handler = SIG_DFL;
3341         switch (action) {
3342         case S_CATCH:
3343                 act.sa_handler = onsig;
3344                 break;
3345         case S_IGN:
3346                 act.sa_handler = SIG_IGN;
3347                 break;
3348         }
3349         *t = action;
3350         act.sa_flags = 0;
3351         sigfillset(&act.sa_mask);
3352         sigaction_set(signo, &act);
3353 }
3354
3355 /* mode flags for set_curjob */
3356 #define CUR_DELETE 2
3357 #define CUR_RUNNING 1
3358 #define CUR_STOPPED 0
3359
3360 /* mode flags for dowait */
3361 #define DOWAIT_NONBLOCK WNOHANG
3362 #define DOWAIT_BLOCK    0
3363
3364 #if JOBS
3365 /* pgrp of shell on invocation */
3366 static int initialpgrp;
3367 static int ttyfd = -1;
3368 #endif
3369 /* array of jobs */
3370 static struct job *jobtab;
3371 /* size of array */
3372 static unsigned njobs;
3373 /* current job */
3374 static struct job *curjob;
3375 /* number of presumed living untracked jobs */
3376 static int jobless;
3377
3378 static void
3379 set_curjob(struct job *jp, unsigned mode)
3380 {
3381         struct job *jp1;
3382         struct job **jpp, **curp;
3383
3384         /* first remove from list */
3385         jpp = curp = &curjob;
3386         do {
3387                 jp1 = *jpp;
3388                 if (jp1 == jp)
3389                         break;
3390                 jpp = &jp1->prev_job;
3391         } while (1);
3392         *jpp = jp1->prev_job;
3393
3394         /* Then re-insert in correct position */
3395         jpp = curp;
3396         switch (mode) {
3397         default:
3398 #if DEBUG
3399                 abort();
3400 #endif
3401         case CUR_DELETE:
3402                 /* job being deleted */
3403                 break;
3404         case CUR_RUNNING:
3405                 /* newly created job or backgrounded job,
3406                    put after all stopped jobs. */
3407                 do {
3408                         jp1 = *jpp;
3409 #if JOBS
3410                         if (!jp1 || jp1->state != JOBSTOPPED)
3411 #endif
3412                                 break;
3413                         jpp = &jp1->prev_job;
3414                 } while (1);
3415                 /* FALLTHROUGH */
3416 #if JOBS
3417         case CUR_STOPPED:
3418 #endif
3419                 /* newly stopped job - becomes curjob */
3420                 jp->prev_job = *jpp;
3421                 *jpp = jp;
3422                 break;
3423         }
3424 }
3425
3426 #if JOBS || DEBUG
3427 static int
3428 jobno(const struct job *jp)
3429 {
3430         return jp - jobtab + 1;
3431 }
3432 #endif
3433
3434 /*
3435  * Convert a job name to a job structure.
3436  */
3437 #if !JOBS
3438 #define getjob(name, getctl) getjob(name)
3439 #endif
3440 static struct job *
3441 getjob(const char *name, int getctl)
3442 {
3443         struct job *jp;
3444         struct job *found;
3445         const char *err_msg = "No such job: %s";
3446         unsigned num;
3447         int c;
3448         const char *p;
3449         char *(*match)(const char *, const char *);
3450
3451         jp = curjob;
3452         p = name;
3453         if (!p)
3454                 goto currentjob;
3455
3456         if (*p != '%')
3457                 goto err;
3458
3459         c = *++p;
3460         if (!c)
3461                 goto currentjob;
3462
3463         if (!p[1]) {
3464                 if (c == '+' || c == '%') {
3465  currentjob:
3466                         err_msg = "No current job";
3467                         goto check;
3468                 }
3469                 if (c == '-') {
3470                         if (jp)
3471                                 jp = jp->prev_job;
3472                         err_msg = "No previous job";
3473  check:
3474                         if (!jp)
3475                                 goto err;
3476                         goto gotit;
3477                 }
3478         }
3479
3480         if (is_number(p)) {
3481 // TODO: number() instead? It does error checking...
3482                 num = atoi(p);
3483                 if (num < njobs) {
3484                         jp = jobtab + num - 1;
3485                         if (jp->used)
3486                                 goto gotit;
3487                         goto err;
3488                 }
3489         }
3490
3491         match = prefix;
3492         if (*p == '?') {
3493                 match = strstr;
3494                 p++;
3495         }
3496
3497         found = 0;
3498         while (1) {
3499                 if (!jp)
3500                         goto err;
3501                 if (match(jp->ps[0].cmd, p)) {
3502                         if (found)
3503                                 goto err;
3504                         found = jp;
3505                         err_msg = "%s: ambiguous";
3506                 }
3507                 jp = jp->prev_job;
3508         }
3509
3510  gotit:
3511 #if JOBS
3512         err_msg = "job %s not created under job control";
3513         if (getctl && jp->jobctl == 0)
3514                 goto err;
3515 #endif
3516         return jp;
3517  err:
3518         ash_msg_and_raise_error(err_msg, name);
3519 }
3520
3521 /*
3522  * Mark a job structure as unused.
3523  */
3524 static void
3525 freejob(struct job *jp)
3526 {
3527         struct procstat *ps;
3528         int i;
3529
3530         INT_OFF;
3531         for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) {
3532                 if (ps->cmd != nullstr)
3533                         free(ps->cmd);
3534         }
3535         if (jp->ps != &jp->ps0)
3536                 free(jp->ps);
3537         jp->used = 0;
3538         set_curjob(jp, CUR_DELETE);
3539         INT_ON;
3540 }
3541
3542 #if JOBS
3543 static void
3544 xtcsetpgrp(int fd, pid_t pgrp)
3545 {
3546         if (tcsetpgrp(fd, pgrp))
3547                 ash_msg_and_raise_error("cannot set tty process group (%m)");
3548 }
3549
3550 /*
3551  * Turn job control on and off.
3552  *
3553  * Note:  This code assumes that the third arg to ioctl is a character
3554  * pointer, which is true on Berkeley systems but not System V.  Since
3555  * System V doesn't have job control yet, this isn't a problem now.
3556  *
3557  * Called with interrupts off.
3558  */
3559 static void
3560 setjobctl(int on)
3561 {
3562         int fd;
3563         int pgrp;
3564
3565         if (on == doing_jobctl || rootshell == 0)
3566                 return;
3567         if (on) {
3568                 int ofd;
3569                 ofd = fd = open(_PATH_TTY, O_RDWR);
3570                 if (fd < 0) {
3571         /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails.
3572          * That sometimes helps to acquire controlling tty.
3573          * Obviously, a workaround for bugs when someone
3574          * failed to provide a controlling tty to bash! :) */
3575                         fd = 2;
3576                         while (!isatty(fd))
3577                                 if (--fd < 0)
3578                                         goto out;
3579                 }
3580                 fd = fcntl(fd, F_DUPFD, 10);
3581                 if (ofd >= 0)
3582                         close(ofd);
3583                 if (fd < 0)
3584                         goto out;
3585                 /* fd is a tty at this point */
3586                 close_on_exec_on(fd);
3587                 do { /* while we are in the background */
3588                         pgrp = tcgetpgrp(fd);
3589                         if (pgrp < 0) {
3590  out:
3591                                 ash_msg("can't access tty; job control turned off");
3592                                 mflag = on = 0;
3593                                 goto close;
3594                         }
3595                         if (pgrp == getpgrp())
3596                                 break;
3597                         killpg(0, SIGTTIN);
3598                 } while (1);
3599                 initialpgrp = pgrp;
3600
3601                 setsignal(SIGTSTP);
3602                 setsignal(SIGTTOU);
3603                 setsignal(SIGTTIN);
3604                 pgrp = rootpid;
3605                 setpgid(0, pgrp);
3606                 xtcsetpgrp(fd, pgrp);
3607         } else {
3608                 /* turning job control off */
3609                 fd = ttyfd;
3610                 pgrp = initialpgrp;
3611                 /* was xtcsetpgrp, but this can make exiting ash
3612                  * loop forever if pty is already deleted */
3613                 tcsetpgrp(fd, pgrp);
3614                 setpgid(0, pgrp);
3615                 setsignal(SIGTSTP);
3616                 setsignal(SIGTTOU);
3617                 setsignal(SIGTTIN);
3618  close:
3619                 if (fd >= 0)
3620                         close(fd);
3621                 fd = -1;
3622         }
3623         ttyfd = fd;
3624         doing_jobctl = on;
3625 }
3626
3627 static int
3628 killcmd(int argc, char **argv)
3629 {
3630         int i = 1;
3631         if (argv[1] && strcmp(argv[1], "-l") != 0) {
3632                 do {
3633                         if (argv[i][0] == '%') {
3634                                 struct job *jp = getjob(argv[i], 0);
3635                                 unsigned pid = jp->ps[0].pid;
3636                                 /* Enough space for ' -NNN<nul>' */
3637                                 argv[i] = alloca(sizeof(int)*3 + 3);
3638                                 /* kill_main has matching code to expect
3639                                  * leading space. Needed to not confuse
3640                                  * negative pids with "kill -SIGNAL_NO" syntax */
3641                                 sprintf(argv[i], " -%u", pid);
3642                         }
3643                 } while (argv[++i]);
3644         }
3645         return kill_main(argc, argv);
3646 }
3647
3648 static void
3649 showpipe(struct job *jp, FILE *out)
3650 {
3651         struct procstat *sp;
3652         struct procstat *spend;
3653
3654         spend = jp->ps + jp->nprocs;
3655         for (sp = jp->ps + 1; sp < spend; sp++)
3656                 fprintf(out, " | %s", sp->cmd);
3657         outcslow('\n', out);
3658         flush_stdout_stderr();
3659 }
3660
3661
3662 static int
3663 restartjob(struct job *jp, int mode)
3664 {
3665         struct procstat *ps;
3666         int i;
3667         int status;
3668         pid_t pgid;
3669
3670         INT_OFF;
3671         if (jp->state == JOBDONE)
3672                 goto out;
3673         jp->state = JOBRUNNING;
3674         pgid = jp->ps->pid;
3675         if (mode == FORK_FG)
3676                 xtcsetpgrp(ttyfd, pgid);
3677         killpg(pgid, SIGCONT);
3678         ps = jp->ps;
3679         i = jp->nprocs;
3680         do {
3681                 if (WIFSTOPPED(ps->status)) {
3682                         ps->status = -1;
3683                 }
3684                 ps++;
3685         } while (--i);
3686  out:
3687         status = (mode == FORK_FG) ? waitforjob(jp) : 0;
3688         INT_ON;
3689         return status;
3690 }
3691
3692 static int
3693 fg_bgcmd(int argc ATTRIBUTE_UNUSED, char **argv)
3694 {
3695         struct job *jp;
3696         FILE *out;
3697         int mode;
3698         int retval;
3699
3700         mode = (**argv == 'f') ? FORK_FG : FORK_BG;
3701         nextopt(nullstr);
3702         argv = argptr;
3703         out = stdout;
3704         do {
3705                 jp = getjob(*argv, 1);
3706                 if (mode == FORK_BG) {
3707                         set_curjob(jp, CUR_RUNNING);
3708                         fprintf(out, "[%d] ", jobno(jp));
3709                 }
3710                 outstr(jp->ps->cmd, out);
3711                 showpipe(jp, out);
3712                 retval = restartjob(jp, mode);
3713         } while (*argv && *++argv);
3714         return retval;
3715 }
3716 #endif
3717
3718 static int
3719 sprint_status(char *s, int status, int sigonly)
3720 {
3721         int col;
3722         int st;
3723
3724         col = 0;
3725         if (!WIFEXITED(status)) {
3726 #if JOBS
3727                 if (WIFSTOPPED(status))
3728                         st = WSTOPSIG(status);
3729                 else
3730 #endif
3731                         st = WTERMSIG(status);
3732                 if (sigonly) {
3733                         if (st == SIGINT || st == SIGPIPE)
3734                                 goto out;
3735 #if JOBS
3736                         if (WIFSTOPPED(status))
3737                                 goto out;
3738 #endif
3739                 }
3740                 st &= 0x7f;
3741                 col = fmtstr(s, 32, strsignal(st));
3742                 if (WCOREDUMP(status)) {
3743                         col += fmtstr(s + col, 16, " (core dumped)");
3744                 }
3745         } else if (!sigonly) {
3746                 st = WEXITSTATUS(status);
3747                 if (st)
3748                         col = fmtstr(s, 16, "Done(%d)", st);
3749                 else
3750                         col = fmtstr(s, 16, "Done");
3751         }
3752  out:
3753         return col;
3754 }
3755
3756 /*
3757  * Do a wait system call.  If job control is compiled in, we accept
3758  * stopped processes.  If block is zero, we return a value of zero
3759  * rather than blocking.
3760  *
3761  * System V doesn't have a non-blocking wait system call.  It does
3762  * have a SIGCLD signal that is sent to a process when one of it's
3763  * children dies.  The obvious way to use SIGCLD would be to install
3764  * a handler for SIGCLD which simply bumped a counter when a SIGCLD
3765  * was received, and have waitproc bump another counter when it got
3766  * the status of a process.  Waitproc would then know that a wait
3767  * system call would not block if the two counters were different.
3768  * This approach doesn't work because if a process has children that
3769  * have not been waited for, System V will send it a SIGCLD when it
3770  * installs a signal handler for SIGCLD.  What this means is that when
3771  * a child exits, the shell will be sent SIGCLD signals continuously
3772  * until is runs out of stack space, unless it does a wait call before
3773  * restoring the signal handler.  The code below takes advantage of
3774  * this (mis)feature by installing a signal handler for SIGCLD and
3775  * then checking to see whether it was called.  If there are any
3776  * children to be waited for, it will be.
3777  *
3778  * If neither SYSV nor BSD is defined, we don't implement nonblocking
3779  * waits at all.  In this case, the user will not be informed when
3780  * a background process until the next time she runs a real program
3781  * (as opposed to running a builtin command or just typing return),
3782  * and the jobs command may give out of date information.
3783  */
3784 static int
3785 waitproc(int wait_flags, int *status)
3786 {
3787 #if JOBS
3788         if (doing_jobctl)
3789                 wait_flags |= WUNTRACED;
3790 #endif
3791         /* NB: _not_ safe_waitpid, we need to detect EINTR */
3792         return waitpid(-1, status, wait_flags);
3793 }
3794
3795 /*
3796  * Wait for a process to terminate.
3797  */
3798 static int
3799 dowait(int wait_flags, struct job *job)
3800 {
3801         int pid;
3802         int status;
3803         struct job *jp;
3804         struct job *thisjob;
3805         int state;
3806
3807         TRACE(("dowait(%d) called\n", wait_flags));
3808         pid = waitproc(wait_flags, &status);
3809         TRACE(("wait returns pid=%d, status=%d\n", pid, status));
3810         if (pid <= 0) {
3811                 /* If we were doing blocking wait and (probably) got EINTR,
3812                  * check for pending sigs received while waiting.
3813                  * (NB: can be moved into callers if needed) */
3814                 if (wait_flags == DOWAIT_BLOCK && pendingsig)
3815                         raise_exception(EXSIG);
3816                 return pid;
3817         }
3818         INT_OFF;
3819         thisjob = NULL;
3820         for (jp = curjob; jp; jp = jp->prev_job) {
3821                 struct procstat *sp;
3822                 struct procstat *spend;
3823                 if (jp->state == JOBDONE)
3824                         continue;
3825                 state = JOBDONE;
3826                 spend = jp->ps + jp->nprocs;
3827                 sp = jp->ps;
3828                 do {
3829                         if (sp->pid == pid) {
3830                                 TRACE(("Job %d: changing status of proc %d "
3831                                         "from 0x%x to 0x%x\n",
3832                                         jobno(jp), pid, sp->status, status));
3833                                 sp->status = status;
3834                                 thisjob = jp;
3835                         }
3836                         if (sp->status == -1)
3837                                 state = JOBRUNNING;
3838 #if JOBS
3839                         if (state == JOBRUNNING)
3840                                 continue;
3841                         if (WIFSTOPPED(sp->status)) {
3842                                 jp->stopstatus = sp->status;
3843                                 state = JOBSTOPPED;
3844                         }
3845 #endif
3846                 } while (++sp < spend);
3847                 if (thisjob)
3848                         goto gotjob;
3849         }
3850 #if JOBS
3851         if (!WIFSTOPPED(status))
3852 #endif
3853                 jobless--;
3854         goto out;
3855
3856  gotjob:
3857         if (state != JOBRUNNING) {
3858                 thisjob->changed = 1;
3859
3860                 if (thisjob->state != state) {
3861                         TRACE(("Job %d: changing state from %d to %d\n",
3862                                 jobno(thisjob), thisjob->state, state));
3863                         thisjob->state = state;
3864 #if JOBS
3865                         if (state == JOBSTOPPED) {
3866                                 set_curjob(thisjob, CUR_STOPPED);
3867                         }
3868 #endif
3869                 }
3870         }
3871
3872  out:
3873         INT_ON;
3874
3875         if (thisjob && thisjob == job) {
3876                 char s[48 + 1];
3877                 int len;
3878
3879                 len = sprint_status(s, status, 1);
3880                 if (len) {
3881                         s[len] = '\n';
3882                         s[len + 1] = '\0';
3883                         out2str(s);
3884                 }
3885         }
3886         return pid;
3887 }
3888
3889 #if JOBS
3890 static void
3891 showjob(FILE *out, struct job *jp, int mode)
3892 {
3893         struct procstat *ps;
3894         struct procstat *psend;
3895         int col;
3896         int indent_col;
3897         char s[80];
3898
3899         ps = jp->ps;
3900
3901         if (mode & SHOW_PGID) {
3902                 /* just output process (group) id of pipeline */
3903                 fprintf(out, "%d\n", ps->pid);
3904                 return;
3905         }
3906
3907         col = fmtstr(s, 16, "[%d]   ", jobno(jp));
3908         indent_col = col;
3909
3910         if (jp == curjob)
3911                 s[col - 2] = '+';
3912         else if (curjob && jp == curjob->prev_job)
3913                 s[col - 2] = '-';
3914
3915         if (mode & SHOW_PID)
3916                 col += fmtstr(s + col, 16, "%d ", ps->pid);
3917
3918         psend = ps + jp->nprocs;
3919
3920         if (jp->state == JOBRUNNING) {
3921                 strcpy(s + col, "Running");
3922                 col += sizeof("Running") - 1;
3923         } else {
3924                 int status = psend[-1].status;
3925                 if (jp->state == JOBSTOPPED)
3926                         status = jp->stopstatus;
3927                 col += sprint_status(s + col, status, 0);
3928         }
3929
3930         goto start;
3931
3932         do {
3933                 /* for each process */
3934                 col = fmtstr(s, 48, " |\n%*c%d ", indent_col, ' ', ps->pid) - 3;
3935  start:
3936                 fprintf(out, "%s%*c%s",
3937                         s, 33 - col >= 0 ? 33 - col : 0, ' ', ps->cmd
3938                 );
3939                 if (!(mode & SHOW_PID)) {
3940                         showpipe(jp, out);
3941                         break;
3942                 }
3943                 if (++ps == psend) {
3944                         outcslow('\n', out);
3945                         break;
3946                 }
3947         } while (1);
3948
3949         jp->changed = 0;
3950
3951         if (jp->state == JOBDONE) {
3952                 TRACE(("showjob: freeing job %d\n", jobno(jp)));
3953                 freejob(jp);
3954         }
3955 }
3956
3957 /*
3958  * Print a list of jobs.  If "change" is nonzero, only print jobs whose
3959  * statuses have changed since the last call to showjobs.
3960  */
3961 static void
3962 showjobs(FILE *out, int mode)
3963 {
3964         struct job *jp;
3965
3966         TRACE(("showjobs(%x) called\n", mode));
3967
3968         /* If not even one job changed, there is nothing to do */
3969         while (dowait(DOWAIT_NONBLOCK, NULL) > 0)
3970                 continue;
3971
3972         for (jp = curjob; jp; jp = jp->prev_job) {
3973                 if (!(mode & SHOW_CHANGED) || jp->changed) {
3974                         showjob(out, jp, mode);
3975                 }
3976         }
3977 }
3978
3979 static int
3980 jobscmd(int argc ATTRIBUTE_UNUSED, char **argv)
3981 {
3982         int mode, m;
3983
3984         mode = 0;
3985         while ((m = nextopt("lp"))) {
3986                 if (m == 'l')
3987                         mode = SHOW_PID;
3988                 else
3989                         mode = SHOW_PGID;
3990         }
3991
3992         argv = argptr;
3993         if (*argv) {
3994                 do
3995                         showjob(stdout, getjob(*argv,0), mode);
3996                 while (*++argv);
3997         } else
3998                 showjobs(stdout, mode);
3999
4000         return 0;
4001 }
4002 #endif /* JOBS */
4003
4004 static int
4005 getstatus(struct job *job)
4006 {
4007         int status;
4008         int retval;
4009
4010         status = job->ps[job->nprocs - 1].status;
4011         retval = WEXITSTATUS(status);
4012         if (!WIFEXITED(status)) {
4013 #if JOBS
4014                 retval = WSTOPSIG(status);
4015                 if (!WIFSTOPPED(status))
4016 #endif
4017                 {
4018                         /* XXX: limits number of signals */
4019                         retval = WTERMSIG(status);
4020 #if JOBS
4021                         if (retval == SIGINT)
4022                                 job->sigint = 1;
4023 #endif
4024                 }
4025                 retval += 128;
4026         }
4027         TRACE(("getstatus: job %d, nproc %d, status %x, retval %x\n",
4028                 jobno(job), job->nprocs, status, retval));
4029         return retval;
4030 }
4031
4032 static int
4033 waitcmd(int argc ATTRIBUTE_UNUSED, char **argv)
4034 {
4035         struct job *job;
4036         int retval;
4037         struct job *jp;
4038
4039 //      exsig++;
4040 //      xbarrier();
4041         if (pendingsig)
4042                 raise_exception(EXSIG);
4043
4044         nextopt(nullstr);
4045         retval = 0;
4046
4047         argv = argptr;
4048         if (!*argv) {
4049                 /* wait for all jobs */
4050                 for (;;) {
4051                         jp = curjob;
4052                         while (1) {
4053                                 if (!jp) /* no running procs */
4054                                         goto ret;
4055                                 if (jp->state == JOBRUNNING)
4056                                         break;
4057                                 jp->waited = 1;
4058                                 jp = jp->prev_job;
4059                         }
4060                         dowait(DOWAIT_BLOCK, NULL);
4061                 }
4062         }
4063
4064         retval = 127;
4065         do {
4066                 if (**argv != '%') {
4067                         pid_t pid = number(*argv);
4068                         job = curjob;
4069                         while (1) {
4070                                 if (!job)
4071                                         goto repeat;
4072                                 if (job->ps[job->nprocs - 1].pid == pid)
4073                                         break;
4074                                 job = job->prev_job;
4075                         }
4076                 } else
4077                         job = getjob(*argv, 0);
4078                 /* loop until process terminated or stopped */
4079                 while (job->state == JOBRUNNING)
4080                         dowait(DOWAIT_BLOCK, NULL);
4081                 job->waited = 1;
4082                 retval = getstatus(job);
4083  repeat:
4084                 ;
4085         } while (*++argv);
4086
4087  ret:
4088         return retval;
4089 }
4090
4091 static struct job *
4092 growjobtab(void)
4093 {
4094         size_t len;
4095         ptrdiff_t offset;
4096         struct job *jp, *jq;
4097
4098         len = njobs * sizeof(*jp);
4099         jq = jobtab;
4100         jp = ckrealloc(jq, len + 4 * sizeof(*jp));
4101
4102         offset = (char *)jp - (char *)jq;
4103         if (offset) {
4104                 /* Relocate pointers */
4105                 size_t l = len;
4106
4107                 jq = (struct job *)((char *)jq + l);
4108                 while (l) {
4109                         l -= sizeof(*jp);
4110                         jq--;
4111 #define joff(p) ((struct job *)((char *)(p) + l))
4112 #define jmove(p) (p) = (void *)((char *)(p) + offset)
4113                         if (joff(jp)->ps == &jq->ps0)
4114                                 jmove(joff(jp)->ps);
4115                         if (joff(jp)->prev_job)
4116                                 jmove(joff(jp)->prev_job);
4117                 }
4118                 if (curjob)
4119                         jmove(curjob);
4120 #undef joff
4121 #undef jmove
4122         }
4123
4124         njobs += 4;
4125         jobtab = jp;
4126         jp = (struct job *)((char *)jp + len);
4127         jq = jp + 3;
4128         do {
4129                 jq->used = 0;
4130         } while (--jq >= jp);
4131         return jp;
4132 }
4133
4134 /*
4135  * Return a new job structure.
4136  * Called with interrupts off.
4137  */
4138 static struct job *
4139 makejob(/*union node *node,*/ int nprocs)
4140 {
4141         int i;
4142         struct job *jp;
4143
4144         for (i = njobs, jp = jobtab; ; jp++) {
4145                 if (--i < 0) {
4146                         jp = growjobtab();
4147                         break;
4148                 }
4149                 if (jp->used == 0)
4150                         break;
4151                 if (jp->state != JOBDONE || !jp->waited)
4152                         continue;
4153 #if JOBS
4154                 if (doing_jobctl)
4155                         continue;
4156 #endif
4157                 freejob(jp);
4158                 break;
4159         }
4160         memset(jp, 0, sizeof(*jp));
4161 #if JOBS
4162         /* jp->jobctl is a bitfield.
4163          * "jp->jobctl |= jobctl" likely to give awful code */
4164         if (doing_jobctl)
4165                 jp->jobctl = 1;
4166 #endif
4167         jp->prev_job = curjob;
4168         curjob = jp;
4169         jp->used = 1;
4170         jp->ps = &jp->ps0;
4171         if (nprocs > 1) {
4172                 jp->ps = ckmalloc(nprocs * sizeof(struct procstat));
4173         }
4174         TRACE(("makejob(%d) returns %%%d\n", nprocs,
4175                                 jobno(jp)));
4176         return jp;
4177 }
4178
4179 #if JOBS
4180 /*
4181  * Return a string identifying a command (to be printed by the
4182  * jobs command).
4183  */
4184 static char *cmdnextc;
4185
4186 static void
4187 cmdputs(const char *s)
4188 {
4189         static const char vstype[VSTYPE + 1][3] = {
4190                 "", "}", "-", "+", "?", "=",
4191                 "%", "%%", "#", "##"
4192                 USE_ASH_BASH_COMPAT(, ":", "/", "//")
4193         };
4194
4195         const char *p, *str;
4196         char c, cc[2] = " ";
4197         char *nextc;
4198         int subtype = 0;
4199         int quoted = 0;
4200
4201         nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc);
4202         p = s;
4203         while ((c = *p++) != 0) {
4204                 str = NULL;
4205                 switch (c) {
4206                 case CTLESC:
4207                         c = *p++;
4208                         break;
4209                 case CTLVAR:
4210                         subtype = *p++;
4211                         if ((subtype & VSTYPE) == VSLENGTH)
4212                                 str = "${#";
4213                         else
4214                                 str = "${";
4215                         if (!(subtype & VSQUOTE) == !(quoted & 1))
4216                                 goto dostr;
4217                         quoted ^= 1;
4218                         c = '"';
4219                         break;
4220                 case CTLENDVAR:
4221                         str = "\"}" + !(quoted & 1);
4222                         quoted >>= 1;
4223                         subtype = 0;
4224                         goto dostr;
4225                 case CTLBACKQ:
4226                         str = "$(...)";
4227                         goto dostr;
4228                 case CTLBACKQ+CTLQUOTE:
4229                         str = "\"$(...)\"";
4230                         goto dostr;
4231 #if ENABLE_ASH_MATH_SUPPORT
4232                 case CTLARI:
4233                         str = "$((";
4234                         goto dostr;
4235                 case CTLENDARI:
4236                         str = "))";
4237                         goto dostr;
4238 #endif
4239                 case CTLQUOTEMARK:
4240                         quoted ^= 1;
4241                         c = '"';
4242                         break;
4243                 case '=':
4244                         if (subtype == 0)
4245                                 break;
4246                         if ((subtype & VSTYPE) != VSNORMAL)
4247                                 quoted <<= 1;
4248                         str = vstype[subtype & VSTYPE];
4249                         if (subtype & VSNUL)
4250                                 c = ':';
4251                         else
4252                                 goto checkstr;
4253                         break;
4254                 case '\'':
4255                 case '\\':
4256                 case '"':
4257                 case '$':
4258                         /* These can only happen inside quotes */
4259                         cc[0] = c;
4260                         str = cc;
4261                         c = '\\';
4262                         break;
4263                 default:
4264                         break;
4265                 }
4266                 USTPUTC(c, nextc);
4267  checkstr:
4268                 if (!str)
4269                         continue;
4270  dostr:
4271                 while ((c = *str++)) {
4272                         USTPUTC(c, nextc);
4273                 }
4274         }
4275         if (quoted & 1) {
4276                 USTPUTC('"', nextc);
4277         }
4278         *nextc = 0;
4279         cmdnextc = nextc;
4280 }
4281
4282 /* cmdtxt() and cmdlist() call each other */
4283 static void cmdtxt(union node *n);
4284
4285 static void
4286 cmdlist(union node *np, int sep)
4287 {
4288         for (; np; np = np->narg.next) {
4289                 if (!sep)
4290                         cmdputs(" ");
4291                 cmdtxt(np);
4292                 if (sep && np->narg.next)
4293                         cmdputs(" ");
4294         }
4295 }
4296
4297 static void
4298 cmdtxt(union node *n)
4299 {
4300         union node *np;
4301         struct nodelist *lp;
4302         const char *p;
4303         char s[2];
4304
4305         if (!n)
4306                 return;
4307         switch (n->type) {
4308         default:
4309 #if DEBUG
4310                 abort();
4311 #endif
4312         case NPIPE:
4313                 lp = n->npipe.cmdlist;
4314                 for (;;) {
4315                         cmdtxt(lp->n);
4316                         lp = lp->next;
4317                         if (!lp)
4318                                 break;
4319                         cmdputs(" | ");
4320                 }
4321                 break;
4322         case NSEMI:
4323                 p = "; ";
4324                 goto binop;
4325         case NAND:
4326                 p = " && ";
4327                 goto binop;
4328         case NOR:
4329                 p = " || ";
4330  binop:
4331                 cmdtxt(n->nbinary.ch1);
4332                 cmdputs(p);
4333                 n = n->nbinary.ch2;
4334                 goto donode;
4335         case NREDIR:
4336         case NBACKGND:
4337                 n = n->nredir.n;
4338                 goto donode;
4339         case NNOT:
4340                 cmdputs("!");
4341                 n = n->nnot.com;
4342  donode:
4343                 cmdtxt(n);
4344                 break;
4345         case NIF:
4346                 cmdputs("if ");
4347                 cmdtxt(n->nif.test);
4348                 cmdputs("; then ");
4349                 n = n->nif.ifpart;
4350                 if (n->nif.elsepart) {
4351                         cmdtxt(n);
4352                         cmdputs("; else ");
4353                         n = n->nif.elsepart;
4354                 }
4355                 p = "; fi";
4356                 goto dotail;
4357         case NSUBSHELL:
4358                 cmdputs("(");
4359                 n = n->nredir.n;
4360                 p = ")";
4361                 goto dotail;
4362         case NWHILE:
4363                 p = "while ";
4364                 goto until;
4365         case NUNTIL:
4366                 p = "until ";
4367  until:
4368                 cmdputs(p);
4369                 cmdtxt(n->nbinary.ch1);
4370                 n = n->nbinary.ch2;
4371                 p = "; done";
4372  dodo:
4373                 cmdputs("; do ");
4374  dotail:
4375                 cmdtxt(n);
4376                 goto dotail2;
4377         case NFOR:
4378                 cmdputs("for ");
4379                 cmdputs(n->nfor.var);
4380                 cmdputs(" in ");
4381                 cmdlist(n->nfor.args, 1);
4382                 n = n->nfor.body;
4383                 p = "; done";
4384                 goto dodo;
4385         case NDEFUN:
4386                 cmdputs(n->narg.text);
4387                 p = "() { ... }";
4388                 goto dotail2;
4389         case NCMD:
4390                 cmdlist(n->ncmd.args, 1);
4391                 cmdlist(n->ncmd.redirect, 0);
4392                 break;
4393         case NARG:
4394                 p = n->narg.text;
4395  dotail2:
4396                 cmdputs(p);
4397                 break;
4398         case NHERE:
4399         case NXHERE:
4400                 p = "<<...";
4401                 goto dotail2;
4402         case NCASE:
4403                 cmdputs("case ");
4404                 cmdputs(n->ncase.expr->narg.text);
4405                 cmdputs(" in ");
4406                 for (np = n->ncase.cases; np; np = np->nclist.next) {
4407                         cmdtxt(np->nclist.pattern);
4408                         cmdputs(") ");
4409                         cmdtxt(np->nclist.body);
4410                         cmdputs(";; ");
4411                 }
4412                 p = "esac";
4413                 goto dotail2;
4414         case NTO:
4415                 p = ">";
4416                 goto redir;
4417         case NCLOBBER:
4418                 p = ">|";
4419                 goto redir;
4420         case NAPPEND:
4421                 p = ">>";
4422                 goto redir;
4423         case NTOFD:
4424                 p = ">&";
4425                 goto redir;
4426         case NFROM:
4427                 p = "<";
4428                 goto redir;
4429         case NFROMFD:
4430                 p = "<&";
4431                 goto redir;
4432         case NFROMTO:
4433                 p = "<>";
4434  redir:
4435                 s[0] = n->nfile.fd + '0';
4436                 s[1] = '\0';
4437                 cmdputs(s);
4438                 cmdputs(p);
4439                 if (n->type == NTOFD || n->type == NFROMFD) {
4440                         s[0] = n->ndup.dupfd + '0';
4441                         p = s;
4442                         goto dotail2;
4443                 }
4444                 n = n->nfile.fname;
4445                 goto donode;
4446         }
4447 }
4448
4449 static char *
4450 commandtext(union node *n)
4451 {
4452         char *name;
4453
4454         STARTSTACKSTR(cmdnextc);
4455         cmdtxt(n);
4456         name = stackblock();
4457         TRACE(("commandtext: name %p, end %p\n\t\"%s\"\n",
4458                         name, cmdnextc, cmdnextc));
4459         return ckstrdup(name);
4460 }
4461 #endif /* JOBS */
4462
4463 /*
4464  * Fork off a subshell.  If we are doing job control, give the subshell its
4465  * own process group.  Jp is a job structure that the job is to be added to.
4466  * N is the command that will be evaluated by the child.  Both jp and n may
4467  * be NULL.  The mode parameter can be one of the following:
4468  *      FORK_FG - Fork off a foreground process.
4469  *      FORK_BG - Fork off a background process.
4470  *      FORK_NOJOB - Like FORK_FG, but don't give the process its own
4471  *                   process group even if job control is on.
4472  *
4473  * When job control is turned off, background processes have their standard
4474  * input redirected to /dev/null (except for the second and later processes
4475  * in a pipeline).
4476  *
4477  * Called with interrupts off.
4478  */
4479 /*
4480  * Clear traps on a fork.
4481  */
4482 static void
4483 clear_traps(void)
4484 {
4485         char **tp;
4486
4487         for (tp = trap; tp < &trap[NSIG]; tp++) {
4488                 if (*tp && **tp) {      /* trap not NULL or "" (SIG_IGN) */
4489                         INT_OFF;
4490                         free(*tp);
4491                         *tp = NULL;
4492                         if (tp != &trap[0])
4493                                 setsignal(tp - trap);
4494                         INT_ON;
4495                 }
4496         }
4497 }
4498
4499 /* Lives far away from here, needed for forkchild */
4500 static void closescript(void);
4501
4502 /* Called after fork(), in child */
4503 static void
4504 forkchild(struct job *jp, /*union node *n,*/ int mode)
4505 {
4506         int oldlvl;
4507
4508         TRACE(("Child shell %d\n", getpid()));
4509         oldlvl = shlvl;
4510         shlvl++;
4511
4512         closescript();
4513         clear_traps();
4514 #if JOBS
4515         /* do job control only in root shell */
4516         doing_jobctl = 0;
4517         if (mode != FORK_NOJOB && jp->jobctl && !oldlvl) {
4518                 pid_t pgrp;
4519
4520                 if (jp->nprocs == 0)
4521                         pgrp = getpid();
4522                 else
4523                         pgrp = jp->ps[0].pid;
4524                 /* This can fail because we are doing it in the parent also */
4525                 (void)setpgid(0, pgrp);
4526                 if (mode == FORK_FG)
4527                         xtcsetpgrp(ttyfd, pgrp);
4528                 setsignal(SIGTSTP);
4529                 setsignal(SIGTTOU);
4530         } else
4531 #endif
4532         if (mode == FORK_BG) {
4533                 ignoresig(SIGINT);
4534                 ignoresig(SIGQUIT);
4535                 if (jp->nprocs == 0) {
4536                         close(0);
4537                         if (open(bb_dev_null, O_RDONLY) != 0)
4538                                 ash_msg_and_raise_error("can't open %s", bb_dev_null);
4539                 }
4540         }
4541         if (!oldlvl && iflag) {
4542                 setsignal(SIGINT);
4543                 setsignal(SIGQUIT);
4544                 setsignal(SIGTERM);
4545         }
4546         for (jp = curjob; jp; jp = jp->prev_job)
4547                 freejob(jp);
4548         jobless = 0;
4549 }
4550
4551 /* Called after fork(), in parent */
4552 #if !JOBS
4553 #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid)
4554 #endif
4555 static void
4556 forkparent(struct job *jp, union node *n, int mode, pid_t pid)
4557 {
4558         TRACE(("In parent shell: child = %d\n", pid));
4559         if (!jp) {
4560                 while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0)
4561                         continue;
4562                 jobless++;
4563                 return;
4564         }
4565 #if JOBS
4566         if (mode != FORK_NOJOB && jp->jobctl) {
4567                 int pgrp;
4568
4569                 if (jp->nprocs == 0)
4570                         pgrp = pid;
4571                 else
4572                         pgrp = jp->ps[0].pid;
4573                 /* This can fail because we are doing it in the child also */
4574                 setpgid(pid, pgrp);
4575         }
4576 #endif
4577         if (mode == FORK_BG) {
4578                 backgndpid = pid;               /* set $! */
4579                 set_curjob(jp, CUR_RUNNING);
4580         }
4581         if (jp) {
4582                 struct procstat *ps = &jp->ps[jp->nprocs++];
4583                 ps->pid = pid;
4584                 ps->status = -1;
4585                 ps->cmd = nullstr;
4586 #if JOBS
4587                 if (doing_jobctl && n)
4588                         ps->cmd = commandtext(n);
4589 #endif
4590         }
4591 }
4592
4593 static int
4594 forkshell(struct job *jp, union node *n, int mode)
4595 {
4596         int pid;
4597
4598         TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode));
4599         pid = fork();
4600         if (pid < 0) {
4601                 TRACE(("Fork failed, errno=%d", errno));
4602                 if (jp)
4603                         freejob(jp);
4604                 ash_msg_and_raise_error("cannot fork");
4605         }
4606         if (pid == 0)
4607                 forkchild(jp, /*n,*/ mode);
4608         else
4609                 forkparent(jp, n, mode, pid);
4610         return pid;
4611 }
4612
4613 /*
4614  * Wait for job to finish.
4615  *
4616  * Under job control we have the problem that while a child process is
4617  * running interrupts generated by the user are sent to the child but not
4618  * to the shell.  This means that an infinite loop started by an inter-
4619  * active user may be hard to kill.  With job control turned off, an
4620  * interactive user may place an interactive program inside a loop.  If
4621  * the interactive program catches interrupts, the user doesn't want
4622  * these interrupts to also abort the loop.  The approach we take here
4623  * is to have the shell ignore interrupt signals while waiting for a
4624  * foreground process to terminate, and then send itself an interrupt
4625  * signal if the child process was terminated by an interrupt signal.
4626  * Unfortunately, some programs want to do a bit of cleanup and then
4627  * exit on interrupt; unless these processes terminate themselves by
4628  * sending a signal to themselves (instead of calling exit) they will
4629  * confuse this approach.
4630  *
4631  * Called with interrupts off.
4632  */
4633 static int
4634 waitforjob(struct job *jp)
4635 {
4636         int st;
4637
4638         TRACE(("waitforjob(%%%d) called\n", jobno(jp)));
4639         while (jp->state == JOBRUNNING) {
4640                 dowait(DOWAIT_BLOCK, jp);
4641         }
4642         st = getstatus(jp);
4643 #if JOBS
4644         if (jp->jobctl) {
4645                 xtcsetpgrp(ttyfd, rootpid);
4646                 /*
4647                  * This is truly gross.
4648                  * If we're doing job control, then we did a TIOCSPGRP which
4649                  * caused us (the shell) to no longer be in the controlling
4650                  * session -- so we wouldn't have seen any ^C/SIGINT.  So, we
4651                  * intuit from the subprocess exit status whether a SIGINT
4652                  * occurred, and if so interrupt ourselves.  Yuck.  - mycroft
4653                  */
4654                 if (jp->sigint) /* TODO: do the same with all signals */
4655                         raise(SIGINT); /* ... by raise(jp->sig) instead? */
4656         }
4657         if (jp->state == JOBDONE)
4658 #endif
4659                 freejob(jp);
4660         return st;
4661 }
4662
4663 /*
4664  * return 1 if there are stopped jobs, otherwise 0
4665  */
4666 static int
4667 stoppedjobs(void)
4668 {
4669         struct job *jp;
4670         int retval;
4671
4672         retval = 0;
4673         if (job_warning)
4674                 goto out;
4675         jp = curjob;
4676         if (jp && jp->state == JOBSTOPPED) {
4677                 out2str("You have stopped jobs.\n");
4678                 job_warning = 2;
4679                 retval++;
4680         }
4681  out:
4682         return retval;
4683 }
4684
4685
4686 /* ============ redir.c
4687  *
4688  * Code for dealing with input/output redirection.
4689  */
4690
4691 #define EMPTY -2                /* marks an unused slot in redirtab */
4692 #define CLOSED -3               /* marks a slot of previously-closed fd */
4693 #ifndef PIPE_BUF
4694 # define PIPESIZE 4096          /* amount of buffering in a pipe */
4695 #else
4696 # define PIPESIZE PIPE_BUF
4697 #endif
4698
4699 /*
4700  * Open a file in noclobber mode.
4701  * The code was copied from bash.
4702  */
4703 static int
4704 noclobberopen(const char *fname)
4705 {
4706         int r, fd;
4707         struct stat finfo, finfo2;
4708
4709         /*
4710          * If the file exists and is a regular file, return an error
4711          * immediately.
4712          */
4713         r = stat(fname, &finfo);
4714         if (r == 0 && S_ISREG(finfo.st_mode)) {
4715                 errno = EEXIST;
4716                 return -1;
4717         }
4718
4719         /*
4720          * If the file was not present (r != 0), make sure we open it
4721          * exclusively so that if it is created before we open it, our open
4722          * will fail.  Make sure that we do not truncate an existing file.
4723          * Note that we don't turn on O_EXCL unless the stat failed -- if the
4724          * file was not a regular file, we leave O_EXCL off.
4725          */
4726         if (r != 0)
4727                 return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666);
4728         fd = open(fname, O_WRONLY|O_CREAT, 0666);
4729
4730         /* If the open failed, return the file descriptor right away. */
4731         if (fd < 0)
4732                 return fd;
4733
4734         /*
4735          * OK, the open succeeded, but the file may have been changed from a
4736          * non-regular file to a regular file between the stat and the open.
4737          * We are assuming that the O_EXCL open handles the case where FILENAME
4738          * did not exist and is symlinked to an existing file between the stat
4739          * and open.
4740          */
4741
4742         /*
4743          * If we can open it and fstat the file descriptor, and neither check
4744          * revealed that it was a regular file, and the file has not been
4745          * replaced, return the file descriptor.
4746          */
4747         if (fstat(fd, &finfo2) == 0 && !S_ISREG(finfo2.st_mode)
4748          && finfo.st_dev == finfo2.st_dev && finfo.st_ino == finfo2.st_ino)
4749                 return fd;
4750
4751         /* The file has been replaced.  badness. */
4752         close(fd);
4753         errno = EEXIST;
4754         return -1;
4755 }
4756
4757 /*
4758  * Handle here documents.  Normally we fork off a process to write the
4759  * data to a pipe.  If the document is short, we can stuff the data in
4760  * the pipe without forking.
4761  */
4762 /* openhere needs this forward reference */
4763 static void expandhere(union node *arg, int fd);
4764 static int
4765 openhere(union node *redir)
4766 {
4767         int pip[2];
4768         size_t len = 0;
4769
4770         if (pipe(pip) < 0)
4771                 ash_msg_and_raise_error("pipe call failed");
4772         if (redir->type == NHERE) {
4773                 len = strlen(redir->nhere.doc->narg.text);
4774                 if (len <= PIPESIZE) {
4775                         full_write(pip[1], redir->nhere.doc->narg.text, len);
4776                         goto out;
4777                 }
4778         }
4779         if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
4780                 close(pip[0]);
4781                 signal(SIGINT, SIG_IGN);
4782                 signal(SIGQUIT, SIG_IGN);
4783                 signal(SIGHUP, SIG_IGN);
4784 #ifdef SIGTSTP
4785                 signal(SIGTSTP, SIG_IGN);
4786 #endif
4787                 signal(SIGPIPE, SIG_DFL);
4788                 if (redir->type == NHERE)
4789                         full_write(pip[1], redir->nhere.doc->narg.text, len);
4790                 else
4791                         expandhere(redir->nhere.doc, pip[1]);
4792                 _exit(EXIT_SUCCESS);
4793         }
4794  out:
4795         close(pip[1]);
4796         return pip[0];
4797 }
4798
4799 static int
4800 openredirect(union node *redir)
4801 {
4802         char *fname;
4803         int f;
4804
4805         switch (redir->nfile.type) {
4806         case NFROM:
4807                 fname = redir->nfile.expfname;
4808                 f = open(fname, O_RDONLY);
4809                 if (f < 0)
4810                         goto eopen;
4811                 break;
4812         case NFROMTO:
4813                 fname = redir->nfile.expfname;
4814                 f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666);
4815                 if (f < 0)
4816                         goto ecreate;
4817                 break;
4818         case NTO:
4819                 /* Take care of noclobber mode. */
4820                 if (Cflag) {
4821                         fname = redir->nfile.expfname;
4822                         f = noclobberopen(fname);
4823                         if (f < 0)
4824                                 goto ecreate;
4825                         break;
4826                 }
4827                 /* FALLTHROUGH */
4828         case NCLOBBER:
4829                 fname = redir->nfile.expfname;
4830                 f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
4831                 if (f < 0)
4832                         goto ecreate;
4833                 break;
4834         case NAPPEND:
4835                 fname = redir->nfile.expfname;
4836                 f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666);
4837                 if (f < 0)
4838                         goto ecreate;
4839                 break;
4840         default:
4841 #if DEBUG
4842                 abort();
4843 #endif
4844                 /* Fall through to eliminate warning. */
4845         case NTOFD:
4846         case NFROMFD:
4847                 f = -1;
4848                 break;
4849         case NHERE:
4850         case NXHERE:
4851                 f = openhere(redir);
4852                 break;
4853         }
4854
4855         return f;
4856  ecreate:
4857         ash_msg_and_raise_error("cannot create %s: %s", fname, errmsg(errno, "nonexistent directory"));
4858  eopen:
4859         ash_msg_and_raise_error("cannot open %s: %s", fname, errmsg(errno, "no such file"));
4860 }
4861
4862 /*
4863  * Copy a file descriptor to be >= to.  Returns -1
4864  * if the source file descriptor is closed, EMPTY if there are no unused
4865  * file descriptors left.
4866  */
4867 static int
4868 copyfd(int from, int to)
4869 {
4870         int newfd;
4871
4872         newfd = fcntl(from, F_DUPFD, to);
4873         if (newfd < 0) {
4874                 if (errno == EMFILE)
4875                         return EMPTY;
4876                 ash_msg_and_raise_error("%d: %m", from);
4877         }
4878         return newfd;
4879 }
4880
4881 static void
4882 dupredirect(union node *redir, int f)
4883 {
4884         int fd = redir->nfile.fd;
4885
4886         if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) {
4887                 if (redir->ndup.dupfd >= 0) {   /* if not ">&-" */
4888                         copyfd(redir->ndup.dupfd, fd);
4889                 }
4890                 return;
4891         }
4892
4893         if (f != fd) {
4894                 copyfd(f, fd);
4895                 close(f);
4896         }
4897 }
4898
4899 /*
4900  * Process a list of redirection commands.  If the REDIR_PUSH flag is set,
4901  * old file descriptors are stashed away so that the redirection can be
4902  * undone by calling popredir.  If the REDIR_BACKQ flag is set, then the
4903  * standard output, and the standard error if it becomes a duplicate of
4904  * stdout, is saved in memory.
4905  */
4906 /* flags passed to redirect */
4907 #define REDIR_PUSH    01        /* save previous values of file descriptors */
4908 #define REDIR_SAVEFD2 03        /* set preverrout */
4909 static void
4910 redirect(union node *redir, int flags)
4911 {
4912         union node *n;
4913         struct redirtab *sv;
4914         int i;
4915         int fd;
4916         int newfd;
4917
4918         g_nullredirs++;
4919         if (!redir) {
4920                 return;
4921         }
4922         sv = NULL;
4923         INT_OFF;
4924         if (flags & REDIR_PUSH) {
4925                 sv = ckmalloc(sizeof(*sv));
4926                 sv->next = redirlist;
4927                 redirlist = sv;
4928                 sv->nullredirs = g_nullredirs - 1;
4929                 for (i = 0; i < 10; i++)
4930                         sv->renamed[i] = EMPTY;
4931                 g_nullredirs = 0;
4932         }
4933         n = redir;
4934         do {
4935                 fd = n->nfile.fd;
4936                 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD)
4937                  && n->ndup.dupfd == fd)
4938                         continue; /* redirect from/to same file descriptor */
4939
4940                 newfd = openredirect(n);
4941                 if (fd == newfd) {
4942                         /* Descriptor wasn't open before redirect.
4943                          * Mark it for close in the future */
4944                         if (sv && sv->renamed[fd] == EMPTY)
4945                                 sv->renamed[fd] = CLOSED;
4946                         continue;
4947                 }
4948                 if (sv && sv->renamed[fd] == EMPTY) {
4949                         i = fcntl(fd, F_DUPFD, 10);
4950
4951                         if (i == -1) {
4952                                 i = errno;
4953                                 if (i != EBADF) {
4954                                         close(newfd);
4955                                         errno = i;
4956                                         ash_msg_and_raise_error("%d: %m", fd);
4957                                         /* NOTREACHED */
4958                                 }
4959                         } else {
4960                                 sv->renamed[fd] = i;
4961                                 close(fd);
4962                         }
4963                 } else {
4964                         close(fd);
4965                 }
4966                 dupredirect(n, newfd);
4967         } while ((n = n->nfile.next));
4968         INT_ON;
4969         if ((flags & REDIR_SAVEFD2) && sv && sv->renamed[2] >= 0)
4970                 preverrout_fd = sv->renamed[2];
4971 }
4972
4973 /*
4974  * Undo the effects of the last redirection.
4975  */
4976 static void
4977 popredir(int drop)
4978 {
4979         struct redirtab *rp;
4980         int i;
4981
4982         if (--g_nullredirs >= 0)
4983                 return;
4984         INT_OFF;
4985         rp = redirlist;
4986         for (i = 0; i < 10; i++) {
4987                 if (rp->renamed[i] == CLOSED) {
4988                         if (!drop)
4989                                 close(i);
4990                         continue;
4991                 }
4992                 if (rp->renamed[i] != EMPTY) {
4993                         if (!drop) {
4994                                 close(i);
4995                                 copyfd(rp->renamed[i], i);
4996                         }
4997                         close(rp->renamed[i]);
4998                 }
4999         }
5000         redirlist = rp->next;
5001         g_nullredirs = rp->nullredirs;
5002         free(rp);
5003         INT_ON;
5004 }
5005
5006 /*
5007  * Undo all redirections.  Called on error or interrupt.
5008  */
5009
5010 /*
5011  * Discard all saved file descriptors.
5012  */
5013 static void
5014 clearredir(int drop)
5015 {
5016         for (;;) {
5017                 g_nullredirs = 0;
5018                 if (!redirlist)
5019                         break;
5020                 popredir(drop);
5021         }
5022 }
5023
5024 static int
5025 redirectsafe(union node *redir, int flags)
5026 {
5027         int err;
5028         volatile int saveint;
5029         struct jmploc *volatile savehandler = exception_handler;
5030         struct jmploc jmploc;
5031
5032         SAVE_INT(saveint);
5033         err = setjmp(jmploc.loc) * 2;
5034         if (!err) {
5035                 exception_handler = &jmploc;
5036                 redirect(redir, flags);
5037         }
5038         exception_handler = savehandler;
5039         if (err && exception != EXERROR)
5040                 longjmp(exception_handler->loc, 1);
5041         RESTORE_INT(saveint);
5042         return err;
5043 }
5044
5045
5046 /* ============ Routines to expand arguments to commands
5047  *
5048  * We have to deal with backquotes, shell variables, and file metacharacters.
5049  */
5050
5051 /*
5052  * expandarg flags
5053  */
5054 #define EXP_FULL        0x1     /* perform word splitting & file globbing */
5055 #define EXP_TILDE       0x2     /* do normal tilde expansion */
5056 #define EXP_VARTILDE    0x4     /* expand tildes in an assignment */
5057 #define EXP_REDIR       0x8     /* file glob for a redirection (1 match only) */
5058 #define EXP_CASE        0x10    /* keeps quotes around for CASE pattern */
5059 #define EXP_RECORD      0x20    /* need to record arguments for ifs breakup */
5060 #define EXP_VARTILDE2   0x40    /* expand tildes after colons only */
5061 #define EXP_WORD        0x80    /* expand word in parameter expansion */
5062 #define EXP_QWORD       0x100   /* expand word in quoted parameter expansion */
5063 /*
5064  * _rmescape() flags
5065  */
5066 #define RMESCAPE_ALLOC  0x1     /* Allocate a new string */
5067 #define RMESCAPE_GLOB   0x2     /* Add backslashes for glob */
5068 #define RMESCAPE_QUOTED 0x4     /* Remove CTLESC unless in quotes */
5069 #define RMESCAPE_GROW   0x8     /* Grow strings instead of stalloc */
5070 #define RMESCAPE_HEAP   0x10    /* Malloc strings instead of stalloc */
5071
5072 /*
5073  * Structure specifying which parts of the string should be searched
5074  * for IFS characters.
5075  */
5076 struct ifsregion {
5077         struct ifsregion *next; /* next region in list */
5078         int begoff;             /* offset of start of region */
5079         int endoff;             /* offset of end of region */
5080         int nulonly;            /* search for nul bytes only */
5081 };
5082
5083 struct arglist {
5084         struct strlist *list;
5085         struct strlist **lastp;
5086 };
5087
5088 /* output of current string */
5089 static char *expdest;
5090 /* list of back quote expressions */
5091 static struct nodelist *argbackq;
5092 /* first struct in list of ifs regions */
5093 static struct ifsregion ifsfirst;
5094 /* last struct in list */
5095 static struct ifsregion *ifslastp;
5096 /* holds expanded arg list */
5097 static struct arglist exparg;
5098
5099 /*
5100  * Our own itoa().
5101  */
5102 static int
5103 cvtnum(arith_t num)
5104 {
5105         int len;
5106
5107         expdest = makestrspace(32, expdest);
5108 #if ENABLE_ASH_MATH_SUPPORT_64
5109         len = fmtstr(expdest, 32, "%lld", (long long) num);
5110 #else
5111         len = fmtstr(expdest, 32, "%ld", num);
5112 #endif
5113         STADJUST(len, expdest);
5114         return len;
5115 }
5116
5117 static size_t
5118 esclen(const char *start, const char *p)
5119 {
5120         size_t esc = 0;
5121
5122         while (p > start && *--p == CTLESC) {
5123                 esc++;
5124         }
5125         return esc;
5126 }
5127
5128 /*
5129  * Remove any CTLESC characters from a string.
5130  */
5131 static char *
5132 _rmescapes(char *str, int flag)
5133 {
5134         static const char qchars[] ALIGN1 = { CTLESC, CTLQUOTEMARK, '\0' };
5135
5136         char *p, *q, *r;
5137         unsigned inquotes;
5138         int notescaped;
5139         int globbing;
5140
5141         p = strpbrk(str, qchars);
5142         if (!p) {
5143                 return str;
5144         }
5145         q = p;
5146         r = str;
5147         if (flag & RMESCAPE_ALLOC) {
5148                 size_t len = p - str;
5149                 size_t fulllen = len + strlen(p) + 1;
5150
5151                 if (flag & RMESCAPE_GROW) {
5152                         r = makestrspace(fulllen, expdest);
5153                 } else if (flag & RMESCAPE_HEAP) {
5154                         r = ckmalloc(fulllen);
5155                 } else {
5156                         r = stalloc(fulllen);
5157                 }
5158                 q = r;
5159                 if (len > 0) {
5160                         q = (char *)memcpy(q, str, len) + len;
5161                 }
5162         }
5163         inquotes = (flag & RMESCAPE_QUOTED) ^ RMESCAPE_QUOTED;
5164         globbing = flag & RMESCAPE_GLOB;
5165         notescaped = globbing;
5166         while (*p) {
5167                 if (*p == CTLQUOTEMARK) {
5168                         inquotes = ~inquotes;
5169                         p++;
5170                         notescaped = globbing;
5171                         continue;
5172                 }
5173                 if (*p == '\\') {
5174                         /* naked back slash */
5175                         notescaped = 0;
5176                         goto copy;
5177                 }
5178                 if (*p == CTLESC) {
5179                         p++;
5180                         if (notescaped && inquotes && *p != '/') {
5181                                 *q++ = '\\';
5182                         }
5183                 }
5184                 notescaped = globbing;
5185  copy:
5186                 *q++ = *p++;
5187         }
5188         *q = '\0';
5189         if (flag & RMESCAPE_GROW) {
5190                 expdest = r;
5191                 STADJUST(q - r + 1, expdest);
5192         }
5193         return r;
5194 }
5195 #define rmescapes(p) _rmescapes((p), 0)
5196
5197 #define pmatch(a, b) !fnmatch((a), (b), 0)
5198
5199 /*
5200  * Prepare a pattern for a expmeta (internal glob(3)) call.
5201  *
5202  * Returns an stalloced string.
5203  */
5204 static char *
5205 preglob(const char *pattern, int quoted, int flag)
5206 {
5207         flag |= RMESCAPE_GLOB;
5208         if (quoted) {
5209                 flag |= RMESCAPE_QUOTED;
5210         }
5211         return _rmescapes((char *)pattern, flag);
5212 }
5213
5214 /*
5215  * Put a string on the stack.
5216  */
5217 static void
5218 memtodest(const char *p, size_t len, int syntax, int quotes)
5219 {
5220         char *q = expdest;
5221
5222         q = makestrspace(len * 2, q);
5223
5224         while (len--) {
5225                 int c = signed_char2int(*p++);
5226                 if (!c)
5227                         continue;
5228                 if (quotes && (SIT(c, syntax) == CCTL || SIT(c, syntax) == CBACK))
5229                         USTPUTC(CTLESC, q);
5230                 USTPUTC(c, q);
5231         }
5232
5233         expdest = q;
5234 }
5235
5236 static void
5237 strtodest(const char *p, int syntax, int quotes)
5238 {
5239         memtodest(p, strlen(p), syntax, quotes);
5240 }
5241
5242 /*
5243  * Record the fact that we have to scan this region of the
5244  * string for IFS characters.
5245  */
5246 static void
5247 recordregion(int start, int end, int nulonly)
5248 {
5249         struct ifsregion *ifsp;
5250
5251         if (ifslastp == NULL) {
5252                 ifsp = &ifsfirst;
5253         } else {
5254                 INT_OFF;
5255                 ifsp = ckzalloc(sizeof(*ifsp));
5256                 /*ifsp->next = NULL; - ckzalloc did it */
5257                 ifslastp->next = ifsp;
5258                 INT_ON;
5259         }
5260         ifslastp = ifsp;
5261         ifslastp->begoff = start;
5262         ifslastp->endoff = end;
5263         ifslastp->nulonly = nulonly;
5264 }
5265
5266 static void
5267 removerecordregions(int endoff)
5268 {
5269         if (ifslastp == NULL)
5270                 return;
5271
5272         if (ifsfirst.endoff > endoff) {
5273                 while (ifsfirst.next != NULL) {
5274                         struct ifsregion *ifsp;
5275                         INT_OFF;
5276                         ifsp = ifsfirst.next->next;
5277                         free(ifsfirst.next);
5278                         ifsfirst.next = ifsp;
5279                         INT_ON;
5280                 }
5281                 if (ifsfirst.begoff > endoff)
5282                         ifslastp = NULL;
5283                 else {
5284                         ifslastp = &ifsfirst;
5285                         ifsfirst.endoff = endoff;
5286                 }
5287                 return;
5288         }
5289
5290         ifslastp = &ifsfirst;
5291         while (ifslastp->next && ifslastp->next->begoff < endoff)
5292                 ifslastp=ifslastp->next;
5293         while (ifslastp->next != NULL) {
5294                 struct ifsregion *ifsp;
5295                 INT_OFF;
5296                 ifsp = ifslastp->next->next;
5297                 free(ifslastp->next);
5298                 ifslastp->next = ifsp;
5299                 INT_ON;
5300         }
5301         if (ifslastp->endoff > endoff)
5302                 ifslastp->endoff = endoff;
5303 }
5304
5305 static char *
5306 exptilde(char *startp, char *p, int flag)
5307 {
5308         char c;
5309         char *name;
5310         struct passwd *pw;
5311         const char *home;
5312         int quotes = flag & (EXP_FULL | EXP_CASE);
5313         int startloc;
5314
5315         name = p + 1;
5316
5317         while ((c = *++p) != '\0') {
5318                 switch (c) {
5319                 case CTLESC:
5320                         return startp;
5321                 case CTLQUOTEMARK:
5322                         return startp;
5323                 case ':':
5324                         if (flag & EXP_VARTILDE)
5325                                 goto done;
5326                         break;
5327                 case '/':
5328                 case CTLENDVAR:
5329                         goto done;
5330                 }
5331         }
5332  done:
5333         *p = '\0';
5334         if (*name == '\0') {
5335                 home = lookupvar(homestr);
5336         } else {
5337                 pw = getpwnam(name);
5338                 if (pw == NULL)
5339                         goto lose;
5340                 home = pw->pw_dir;
5341         }
5342         if (!home || !*home)
5343                 goto lose;
5344         *p = c;
5345         startloc = expdest - (char *)stackblock();
5346         strtodest(home, SQSYNTAX, quotes);
5347         recordregion(startloc, expdest - (char *)stackblock(), 0);
5348         return p;
5349  lose:
5350         *p = c;
5351         return startp;
5352 }
5353
5354 /*
5355  * Execute a command inside back quotes.  If it's a builtin command, we
5356  * want to save its output in a block obtained from malloc.  Otherwise
5357  * we fork off a subprocess and get the output of the command via a pipe.
5358  * Should be called with interrupts off.
5359  */
5360 struct backcmd {                /* result of evalbackcmd */
5361         int fd;                 /* file descriptor to read from */
5362         int nleft;              /* number of chars in buffer */
5363         char *buf;              /* buffer */
5364         struct job *jp;         /* job structure for command */
5365 };
5366
5367 /* These forward decls are needed to use "eval" code for backticks handling: */
5368 static smalluint back_exitstatus; /* exit status of backquoted command */
5369 #define EV_EXIT 01              /* exit after evaluating tree */
5370 static void evaltree(union node *, int);
5371
5372 static void
5373 evalbackcmd(union node *n, struct backcmd *result)
5374 {
5375         int saveherefd;
5376
5377         result->fd = -1;
5378         result->buf = NULL;
5379         result->nleft = 0;
5380         result->jp = NULL;
5381         if (n == NULL) {
5382                 goto out;
5383         }
5384
5385         saveherefd = herefd;
5386         herefd = -1;
5387
5388         {
5389                 int pip[2];
5390                 struct job *jp;
5391
5392                 if (pipe(pip) < 0)
5393                         ash_msg_and_raise_error("pipe call failed");
5394                 jp = makejob(/*n,*/ 1);
5395                 if (forkshell(jp, n, FORK_NOJOB) == 0) {
5396                         FORCE_INT_ON;
5397                         close(pip[0]);
5398                         if (pip[1] != 1) {
5399                                 close(1);
5400                                 copyfd(pip[1], 1);
5401                                 close(pip[1]);
5402                         }
5403                         eflag = 0;
5404                         evaltree(n, EV_EXIT); /* actually evaltreenr... */
5405                         /* NOTREACHED */
5406                 }
5407                 close(pip[1]);
5408                 result->fd = pip[0];
5409                 result->jp = jp;
5410         }
5411         herefd = saveherefd;
5412  out:
5413         TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
5414                 result->fd, result->buf, result->nleft, result->jp));
5415 }
5416
5417 /*
5418  * Expand stuff in backwards quotes.
5419  */
5420 static void
5421 expbackq(union node *cmd, int quoted, int quotes)
5422 {
5423         struct backcmd in;
5424         int i;
5425         char buf[128];
5426         char *p;
5427         char *dest;
5428         int startloc;
5429         int syntax = quoted ? DQSYNTAX : BASESYNTAX;
5430         struct stackmark smark;
5431
5432         INT_OFF;
5433         setstackmark(&smark);
5434         dest = expdest;
5435         startloc = dest - (char *)stackblock();
5436         grabstackstr(dest);
5437         evalbackcmd(cmd, &in);
5438         popstackmark(&smark);
5439
5440         p = in.buf;
5441         i = in.nleft;
5442         if (i == 0)
5443                 goto read;
5444         for (;;) {
5445                 memtodest(p, i, syntax, quotes);
5446  read:
5447                 if (in.fd < 0)
5448                         break;
5449                 i = nonblock_safe_read(in.fd, buf, sizeof(buf));
5450                 TRACE(("expbackq: read returns %d\n", i));
5451                 if (i <= 0)
5452                         break;
5453                 p = buf;
5454         }
5455
5456         free(in.buf);
5457         if (in.fd >= 0) {
5458                 close(in.fd);
5459                 back_exitstatus = waitforjob(in.jp);
5460         }
5461         INT_ON;
5462
5463         /* Eat all trailing newlines */
5464         dest = expdest;
5465         for (; dest > (char *)stackblock() && dest[-1] == '\n';)
5466                 STUNPUTC(dest);
5467         expdest = dest;
5468
5469         if (quoted == 0)
5470                 recordregion(startloc, dest - (char *)stackblock(), 0);
5471         TRACE(("evalbackq: size=%d: \"%.*s\"\n",
5472                 (dest - (char *)stackblock()) - startloc,
5473                 (dest - (char *)stackblock()) - startloc,
5474                 stackblock() + startloc));
5475 }
5476
5477 #if ENABLE_ASH_MATH_SUPPORT
5478 /*
5479  * Expand arithmetic expression.  Backup to start of expression,
5480  * evaluate, place result in (backed up) result, adjust string position.
5481  */
5482 static void
5483 expari(int quotes)
5484 {
5485         char *p, *start;
5486         int begoff;
5487         int flag;
5488         int len;
5489
5490         /*      ifsfree(); */
5491
5492         /*
5493          * This routine is slightly over-complicated for
5494          * efficiency.  Next we scan backwards looking for the
5495          * start of arithmetic.
5496          */
5497         start = stackblock();
5498         p = expdest - 1;
5499         *p = '\0';
5500         p--;
5501         do {
5502                 int esc;
5503
5504                 while (*p != CTLARI) {
5505                         p--;
5506 #if DEBUG
5507                         if (p < start) {
5508                                 ash_msg_and_raise_error("missing CTLARI (shouldn't happen)");
5509                         }
5510 #endif
5511                 }
5512
5513                 esc = esclen(start, p);
5514                 if (!(esc % 2)) {
5515                         break;
5516                 }
5517
5518                 p -= esc + 1;
5519         } while (1);
5520
5521         begoff = p - start;
5522
5523         removerecordregions(begoff);
5524
5525         flag = p[1];
5526
5527         expdest = p;
5528
5529         if (quotes)
5530                 rmescapes(p + 2);
5531
5532         len = cvtnum(dash_arith(p + 2));
5533
5534         if (flag != '"')
5535                 recordregion(begoff, begoff + len, 0);
5536 }
5537 #endif
5538
5539 /* argstr needs it */
5540 static char *evalvar(char *p, int flag, struct strlist *var_str_list);
5541
5542 /*
5543  * Perform variable and command substitution.  If EXP_FULL is set, output CTLESC
5544  * characters to allow for further processing.  Otherwise treat
5545  * $@ like $* since no splitting will be performed.
5546  *
5547  * var_str_list (can be NULL) is a list of "VAR=val" strings which take precedence
5548  * over shell varables. Needed for "A=a B=$A; echo $B" case - we use it
5549  * for correct expansion of "B=$A" word.
5550  */
5551 static void
5552 argstr(char *p, int flag, struct strlist *var_str_list)
5553 {
5554         static const char spclchars[] ALIGN1 = {
5555                 '=',
5556                 ':',
5557                 CTLQUOTEMARK,
5558                 CTLENDVAR,
5559                 CTLESC,
5560                 CTLVAR,
5561                 CTLBACKQ,
5562                 CTLBACKQ | CTLQUOTE,
5563 #if ENABLE_ASH_MATH_SUPPORT
5564                 CTLENDARI,
5565 #endif
5566                 0
5567         };
5568         const char *reject = spclchars;
5569         int c;
5570         int quotes = flag & (EXP_FULL | EXP_CASE);      /* do CTLESC */
5571         int breakall = flag & EXP_WORD;
5572         int inquotes;
5573         size_t length;
5574         int startloc;
5575
5576         if (!(flag & EXP_VARTILDE)) {
5577                 reject += 2;
5578         } else if (flag & EXP_VARTILDE2) {
5579                 reject++;
5580         }
5581         inquotes = 0;
5582         length = 0;
5583         if (flag & EXP_TILDE) {
5584                 char *q;
5585
5586                 flag &= ~EXP_TILDE;
5587  tilde:
5588                 q = p;
5589                 if (*q == CTLESC && (flag & EXP_QWORD))
5590                         q++;
5591                 if (*q == '~')
5592                         p = exptilde(p, q, flag);
5593         }
5594  start:
5595         startloc = expdest - (char *)stackblock();
5596         for (;;) {
5597                 length += strcspn(p + length, reject);
5598                 c = p[length];
5599                 if (c && (!(c & 0x80)
5600 #if ENABLE_ASH_MATH_SUPPORT
5601                                         || c == CTLENDARI
5602 #endif
5603                    )) {
5604                         /* c == '=' || c == ':' || c == CTLENDARI */
5605                         length++;
5606                 }
5607                 if (length > 0) {
5608                         int newloc;
5609                         expdest = stack_nputstr(p, length, expdest);
5610                         newloc = expdest - (char *)stackblock();
5611                         if (breakall && !inquotes && newloc > startloc) {
5612                                 recordregion(startloc, newloc, 0);
5613                         }
5614                         startloc = newloc;
5615                 }
5616                 p += length + 1;
5617                 length = 0;
5618
5619                 switch (c) {
5620                 case '\0':
5621                         goto breakloop;
5622                 case '=':
5623                         if (flag & EXP_VARTILDE2) {
5624                                 p--;
5625                                 continue;
5626                         }
5627                         flag |= EXP_VARTILDE2;
5628                         reject++;
5629                         /* fall through */
5630                 case ':':
5631                         /*
5632                          * sort of a hack - expand tildes in variable
5633                          * assignments (after the first '=' and after ':'s).
5634                          */
5635                         if (*--p == '~') {
5636                                 goto tilde;
5637                         }
5638                         continue;
5639                 }
5640
5641                 switch (c) {
5642                 case CTLENDVAR: /* ??? */
5643                         goto breakloop;
5644                 case CTLQUOTEMARK:
5645                         /* "$@" syntax adherence hack */
5646                         if (
5647                                 !inquotes &&
5648                                 !memcmp(p, dolatstr, 4) &&
5649                                 (p[4] == CTLQUOTEMARK || (
5650                                         p[4] == CTLENDVAR &&
5651                                         p[5] == CTLQUOTEMARK
5652                                 ))
5653                         ) {
5654                                 p = evalvar(p + 1, flag, /* var_str_list: */ NULL) + 1;
5655                                 goto start;
5656                         }
5657                         inquotes = !inquotes;
5658  addquote:
5659                         if (quotes) {
5660                                 p--;
5661                                 length++;
5662                                 startloc++;
5663                         }
5664                         break;
5665                 case CTLESC:
5666                         startloc++;
5667                         length++;
5668                         goto addquote;
5669                 case CTLVAR:
5670                         p = evalvar(p, flag, var_str_list);
5671                         goto start;
5672                 case CTLBACKQ:
5673                         c = 0;
5674                 case CTLBACKQ|CTLQUOTE:
5675                         expbackq(argbackq->n, c, quotes);
5676                         argbackq = argbackq->next;
5677                         goto start;
5678 #if ENABLE_ASH_MATH_SUPPORT
5679                 case CTLENDARI:
5680                         p--;
5681                         expari(quotes);
5682                         goto start;
5683 #endif
5684                 }
5685         }
5686  breakloop:
5687         ;
5688 }
5689
5690 static char *
5691 scanleft(char *startp, char *rmesc, char *rmescend ATTRIBUTE_UNUSED, char *str, int quotes,
5692         int zero)
5693 {
5694 // This commented out code was added by James Simmons <jsimmons@infradead.org>
5695 // as part of a larger change when he added support for ${var/a/b}.
5696 // However, it broke # and % operators:
5697 //
5698 //var=ababcdcd
5699 //                 ok       bad
5700 //echo ${var#ab}   abcdcd   abcdcd
5701 //echo ${var##ab}  abcdcd   abcdcd
5702 //echo ${var#a*b}  abcdcd   ababcdcd  (!)
5703 //echo ${var##a*b} cdcd     cdcd
5704 //echo ${var#?}    babcdcd  ababcdcd  (!)
5705 //echo ${var##?}   babcdcd  babcdcd
5706 //echo ${var#*}    ababcdcd babcdcd   (!)
5707 //echo ${var##*}
5708 //echo ${var%cd}   ababcd   ababcd
5709 //echo ${var%%cd}  ababcd   abab      (!)
5710 //echo ${var%c*d}  ababcd   ababcd
5711 //echo ${var%%c*d} abab     ababcdcd  (!)
5712 //echo ${var%?}    ababcdc  ababcdc
5713 //echo ${var%%?}   ababcdc  ababcdcd  (!)
5714 //echo ${var%*}    ababcdcd ababcdcd
5715 //echo ${var%%*}
5716 //
5717 // Commenting it back out helped. Remove it completely if it really
5718 // is not needed.
5719
5720         char *loc, *loc2; //, *full;
5721         char c;
5722
5723         loc = startp;
5724         loc2 = rmesc;
5725         do {
5726                 int match; // = strlen(str);
5727                 const char *s = loc2;
5728
5729                 c = *loc2;
5730                 if (zero) {
5731                         *loc2 = '\0';
5732                         s = rmesc;
5733                 }
5734                 match = pmatch(str, s); // this line was deleted
5735
5736 //              // chop off end if its '*'
5737 //              full = strrchr(str, '*');
5738 //              if (full && full != str)
5739 //                      match--;
5740 //
5741 //              // If str starts with '*' replace with s.
5742 //              if ((*str == '*') && strlen(s) >= match) {
5743 //                      full = xstrdup(s);
5744 //                      strncpy(full+strlen(s)-match+1, str+1, match-1);
5745 //              } else
5746 //                      full = xstrndup(str, match);
5747 //              match = strncmp(s, full, strlen(full));
5748 //              free(full);
5749 //
5750                 *loc2 = c;
5751                 if (match) // if (!match)
5752                         return loc;
5753                 if (quotes && *loc == CTLESC)
5754                         loc++;
5755                 loc++;
5756                 loc2++;
5757         } while (c);
5758         return 0;
5759 }
5760
5761 static char *
5762 scanright(char *startp, char *rmesc, char *rmescend, char *str, int quotes,
5763         int zero)
5764 {
5765         int esc = 0;
5766         char *loc;
5767         char *loc2;
5768
5769         for (loc = str - 1, loc2 = rmescend; loc >= startp; loc2--) {
5770                 int match;
5771                 char c = *loc2;
5772                 const char *s = loc2;
5773                 if (zero) {
5774                         *loc2 = '\0';
5775                         s = rmesc;
5776                 }
5777                 match = pmatch(str, s);
5778                 *loc2 = c;
5779                 if (match)
5780                         return loc;
5781                 loc--;
5782                 if (quotes) {
5783                         if (--esc < 0) {
5784                                 esc = esclen(startp, loc);
5785                         }
5786                         if (esc % 2) {
5787                                 esc--;
5788                                 loc--;
5789                         }
5790                 }
5791         }
5792         return 0;
5793 }
5794
5795 static void varunset(const char *, const char *, const char *, int) ATTRIBUTE_NORETURN;
5796 static void
5797 varunset(const char *end, const char *var, const char *umsg, int varflags)
5798 {
5799         const char *msg;
5800         const char *tail;
5801
5802         tail = nullstr;
5803         msg = "parameter not set";
5804         if (umsg) {
5805                 if (*end == CTLENDVAR) {
5806                         if (varflags & VSNUL)
5807                                 tail = " or null";
5808                 } else
5809                         msg = umsg;
5810         }
5811         ash_msg_and_raise_error("%.*s: %s%s", end - var - 1, var, msg, tail);
5812 }
5813
5814 #if ENABLE_ASH_BASH_COMPAT
5815 static char *
5816 parse_sub_pattern(char *arg, int inquotes)
5817 {
5818         char *idx, *repl = NULL;
5819         unsigned char c;
5820
5821         idx = arg;
5822         while (1) {
5823                 c = *arg;
5824                 if (!c)
5825                         break;
5826                 if (c == '/') {
5827                         /* Only the first '/' seen is our separator */
5828                         if (!repl) {
5829                                 repl = idx + 1;
5830                                 c = '\0';
5831                         }
5832                 }
5833                 *idx++ = c;
5834                 if (!inquotes && c == '\\' && arg[1] == '\\')
5835                         arg++; /* skip both \\, not just first one */
5836                 arg++;
5837         }
5838         *idx = c; /* NUL */
5839
5840         return repl;
5841 }
5842 #endif /* ENABLE_ASH_BASH_COMPAT */
5843
5844 static const char *
5845 subevalvar(char *p, char *str, int strloc, int subtype,
5846                 int startloc, int varflags, int quotes, struct strlist *var_str_list)
5847 {
5848         struct nodelist *saveargbackq = argbackq;
5849         char *startp;
5850         char *loc;
5851         char *rmesc, *rmescend;
5852         USE_ASH_BASH_COMPAT(char *repl = NULL;)
5853         USE_ASH_BASH_COMPAT(char null = '\0';)
5854         USE_ASH_BASH_COMPAT(int pos, len, orig_len;)
5855         int saveherefd = herefd;
5856         int amount, workloc, resetloc;
5857         int zero;
5858         char *(*scan)(char*, char*, char*, char*, int, int);
5859
5860         herefd = -1;
5861         argstr(p, (subtype != VSASSIGN && subtype != VSQUESTION) ? EXP_CASE : 0,
5862                         var_str_list);
5863         STPUTC('\0', expdest);
5864         herefd = saveherefd;
5865         argbackq = saveargbackq;
5866         startp = (char *)stackblock() + startloc;
5867
5868         switch (subtype) {
5869         case VSASSIGN:
5870                 setvar(str, startp, 0);
5871                 amount = startp - expdest;
5872                 STADJUST(amount, expdest);
5873                 return startp;
5874
5875 #if ENABLE_ASH_BASH_COMPAT
5876         case VSSUBSTR:
5877                 loc = str = stackblock() + strloc;
5878 // TODO: number() instead? It does error checking...
5879                 pos = atoi(loc);
5880                 len = str - startp - 1;
5881
5882                 /* *loc != '\0', guaranteed by parser */
5883                 if (quotes) {
5884                         char *ptr;
5885
5886                         /* We must adjust the length by the number of escapes we find. */
5887                         for (ptr = startp; ptr < (str - 1); ptr++) {
5888                                 if(*ptr == CTLESC) {
5889                                         len--;
5890                                         ptr++;
5891                                 }
5892                         }
5893                 }
5894                 orig_len = len;
5895
5896                 if (*loc++ == ':') {
5897 // TODO: number() instead? It does error checking...
5898                         len = atoi(loc);
5899                 } else {
5900                         len = orig_len;
5901                         while (*loc && *loc != ':')
5902                                 loc++;
5903                         if (*loc++ == ':')
5904 // TODO: number() instead? It does error checking...
5905                                 len = atoi(loc);
5906                 }
5907                 if (pos >= orig_len) {
5908                         pos = 0;
5909                         len = 0;
5910                 }
5911                 if (len > (orig_len - pos))
5912                         len = orig_len - pos;
5913
5914                 for (str = startp; pos; str++, pos--) {
5915                         if (quotes && *str == CTLESC)
5916                                 str++;
5917                 }
5918                 for (loc = startp; len; len--) {
5919                         if (quotes && *str == CTLESC)
5920                                 *loc++ = *str++;
5921                         *loc++ = *str++;
5922                 }
5923                 *loc = '\0';
5924                 amount = loc - expdest;
5925                 STADJUST(amount, expdest);
5926                 return loc;
5927 #endif
5928
5929         case VSQUESTION:
5930                 varunset(p, str, startp, varflags);
5931                 /* NOTREACHED */
5932         }
5933         resetloc = expdest - (char *)stackblock();
5934
5935         /* We'll comeback here if we grow the stack while handling
5936          * a VSREPLACE or VSREPLACEALL, since our pointers into the
5937          * stack will need rebasing, and we'll need to remove our work
5938          * areas each time
5939          */
5940  USE_ASH_BASH_COMPAT(restart:)
5941
5942         amount = expdest - ((char *)stackblock() + resetloc);
5943         STADJUST(-amount, expdest);
5944         startp = (char *)stackblock() + startloc;
5945
5946         rmesc = startp;
5947         rmescend = (char *)stackblock() + strloc;
5948         if (quotes) {
5949                 rmesc = _rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW);
5950                 if (rmesc != startp) {
5951                         rmescend = expdest;
5952                         startp = (char *)stackblock() + startloc;
5953                 }
5954         }
5955         rmescend--;
5956         str = (char *)stackblock() + strloc;
5957         preglob(str, varflags & VSQUOTE, 0);
5958         workloc = expdest - (char *)stackblock();
5959
5960 #if ENABLE_ASH_BASH_COMPAT
5961         if (subtype == VSREPLACE || subtype == VSREPLACEALL) {
5962                 char *idx, *end, *restart_detect;
5963
5964                 if(!repl) {
5965                         repl = parse_sub_pattern(str, varflags & VSQUOTE);
5966                         if (!repl)
5967                                 repl = &null;
5968                 }
5969
5970                 /* If there's no pattern to match, return the expansion unmolested */
5971                 if (*str == '\0')
5972                         return 0;
5973
5974                 len = 0;
5975                 idx = startp;
5976                 end = str - 1;
5977                 while (idx < end) {
5978                         loc = scanright(idx, rmesc, rmescend, str, quotes, 1);
5979                         if (!loc) {
5980                                 /* No match, advance */
5981                                 restart_detect = stackblock();
5982                                 STPUTC(*idx, expdest);
5983                                 if (quotes && *idx == CTLESC) {
5984                                         idx++;
5985                                         len++;
5986                                         STPUTC(*idx, expdest);
5987                                 }
5988                                 if (stackblock() != restart_detect)
5989                                         goto restart;
5990                                 idx++;
5991                                 len++;
5992                                 rmesc++;
5993                                 continue;
5994                         }
5995
5996                         if (subtype == VSREPLACEALL) {
5997                                 while (idx < loc) {
5998                                         if (quotes && *idx == CTLESC)
5999                                                 idx++;
6000                                         idx++;
6001                                         rmesc++;
6002                                 }
6003                         } else
6004                                 idx = loc;
6005
6006                         for (loc = repl; *loc; loc++) {
6007                                 restart_detect = stackblock();
6008                                 STPUTC(*loc, expdest);
6009                                 if (stackblock() != restart_detect)
6010                                         goto restart;
6011                                 len++;
6012                         }
6013
6014                         if (subtype == VSREPLACE) {
6015                                 while (*idx) {
6016                                         restart_detect = stackblock();
6017                                         STPUTC(*idx, expdest);
6018                                         if (stackblock() != restart_detect)
6019                                                 goto restart;
6020                                         len++;
6021                                         idx++;
6022                                 }
6023                                 break;
6024                         }
6025                 }
6026
6027                 /* We've put the replaced text into a buffer at workloc, now
6028                  * move it to the right place and adjust the stack.
6029                  */
6030                 startp = stackblock() + startloc;
6031                 STPUTC('\0', expdest);
6032                 memmove(startp, stackblock() + workloc, len);
6033                 startp[len++] = '\0';
6034                 amount = expdest - ((char *)stackblock() + startloc + len - 1);
6035                 STADJUST(-amount, expdest);
6036                 return startp;
6037         }
6038 #endif /* ENABLE_ASH_BASH_COMPAT */
6039
6040         subtype -= VSTRIMRIGHT;
6041 #if DEBUG
6042         if (subtype < 0 || subtype > 7)
6043                 abort();
6044 #endif
6045         /* zero = subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX */
6046         zero = subtype >> 1;
6047         /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */
6048         scan = (subtype & 1) ^ zero ? scanleft : scanright;
6049
6050         loc = scan(startp, rmesc, rmescend, str, quotes, zero);
6051         if (loc) {
6052                 if (zero) {
6053                         memmove(startp, loc, str - loc);
6054                         loc = startp + (str - loc) - 1;
6055                 }
6056                 *loc = '\0';
6057                 amount = loc - expdest;
6058                 STADJUST(amount, expdest);
6059         }
6060         return loc;
6061 }
6062
6063 /*
6064  * Add the value of a specialized variable to the stack string.
6065  */
6066 static ssize_t
6067 varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
6068 {
6069         int num;
6070         char *p;
6071         int i;
6072         int sep = 0;
6073         int sepq = 0;
6074         ssize_t len = 0;
6075         char **ap;
6076         int syntax;
6077         int quoted = varflags & VSQUOTE;
6078         int subtype = varflags & VSTYPE;
6079         int quotes = flags & (EXP_FULL | EXP_CASE);
6080
6081         if (quoted && (flags & EXP_FULL))
6082                 sep = 1 << CHAR_BIT;
6083
6084         syntax = quoted ? DQSYNTAX : BASESYNTAX;
6085         switch (*name) {
6086         case '$':
6087                 num = rootpid;
6088                 goto numvar;
6089         case '?':
6090                 num = exitstatus;
6091                 goto numvar;
6092         case '#':
6093                 num = shellparam.nparam;
6094                 goto numvar;
6095         case '!':
6096                 num = backgndpid;
6097                 if (num == 0)
6098                         return -1;
6099  numvar:
6100                 len = cvtnum(num);
6101                 break;
6102         case '-':
6103                 p = makestrspace(NOPTS, expdest);
6104                 for (i = NOPTS - 1; i >= 0; i--) {
6105                         if (optlist[i]) {
6106                                 USTPUTC(optletters(i), p);
6107                                 len++;
6108                         }
6109                 }
6110                 expdest = p;
6111                 break;
6112         case '@':
6113                 if (sep)
6114                         goto param;
6115                 /* fall through */
6116         case '*':
6117                 sep = ifsset() ? signed_char2int(ifsval()[0]) : ' ';
6118                 if (quotes && (SIT(sep, syntax) == CCTL || SIT(sep, syntax) == CBACK))
6119                         sepq = 1;
6120  param:
6121                 ap = shellparam.p;
6122                 if (!ap)
6123                         return -1;
6124                 while ((p = *ap++)) {
6125                         size_t partlen;
6126
6127                         partlen = strlen(p);
6128                         len += partlen;
6129
6130                         if (!(subtype == VSPLUS || subtype == VSLENGTH))
6131                                 memtodest(p, partlen, syntax, quotes);
6132
6133                         if (*ap && sep) {
6134                                 char *q;
6135
6136                                 len++;
6137                                 if (subtype == VSPLUS || subtype == VSLENGTH) {
6138                                         continue;
6139                                 }
6140                                 q = expdest;
6141                                 if (sepq)
6142                                         STPUTC(CTLESC, q);
6143                                 STPUTC(sep, q);
6144                                 expdest = q;
6145                         }
6146                 }
6147                 return len;
6148         case '0':
6149         case '1':
6150         case '2':
6151         case '3':
6152         case '4':
6153         case '5':
6154         case '6':
6155         case '7':
6156         case '8':
6157         case '9':
6158 // TODO: number() instead? It does error checking...
6159                 num = atoi(name);
6160                 if (num < 0 || num > shellparam.nparam)
6161                         return -1;
6162                 p = num ? shellparam.p[num - 1] : arg0;
6163                 goto value;
6164         default:
6165                 /* NB: name has form "VAR=..." */
6166
6167                 /* "A=a B=$A" case: var_str_list is a list of "A=a" strings
6168                  * which should be considered before we check variables. */
6169                 if (var_str_list) {
6170                         unsigned name_len = (strchrnul(name, '=') - name) + 1;
6171                         p = NULL;
6172                         do {
6173                                 char *str, *eq;
6174                                 str = var_str_list->text;
6175                                 eq = strchr(str, '=');
6176                                 if (!eq) /* stop at first non-assignment */
6177                                         break;
6178                                 eq++;
6179                                 if (name_len == (unsigned)(eq - str)
6180                                  && strncmp(str, name, name_len) == 0) {
6181                                         p = eq;
6182                                         /* goto value; - WRONG! */
6183                                         /* think "A=1 A=2 B=$A" */
6184                                 }
6185                                 var_str_list = var_str_list->next;
6186                         } while (var_str_list);
6187                         if (p)
6188                                 goto value;
6189                 }
6190                 p = lookupvar(name);
6191  value:
6192                 if (!p)
6193                         return -1;
6194
6195                 len = strlen(p);
6196                 if (!(subtype == VSPLUS || subtype == VSLENGTH))
6197                         memtodest(p, len, syntax, quotes);
6198                 return len;
6199         }
6200
6201         if (subtype == VSPLUS || subtype == VSLENGTH)
6202                 STADJUST(-len, expdest);
6203         return len;
6204 }
6205
6206 /*
6207  * Expand a variable, and return a pointer to the next character in the
6208  * input string.
6209  */
6210 static char *
6211 evalvar(char *p, int flag, struct strlist *var_str_list)
6212 {
6213         char varflags;
6214         char subtype;
6215         char quoted;
6216         char easy;
6217         char *var;
6218         int patloc;
6219         int startloc;
6220         ssize_t varlen;
6221
6222         varflags = *p++;
6223         subtype = varflags & VSTYPE;
6224         quoted = varflags & VSQUOTE;
6225         var = p;
6226         easy = (!quoted || (*var == '@' && shellparam.nparam));
6227         startloc = expdest - (char *)stackblock();
6228         p = strchr(p, '=') + 1;
6229
6230  again:
6231         varlen = varvalue(var, varflags, flag, var_str_list);
6232         if (varflags & VSNUL)
6233                 varlen--;
6234
6235         if (subtype == VSPLUS) {
6236                 varlen = -1 - varlen;
6237                 goto vsplus;
6238         }
6239
6240         if (subtype == VSMINUS) {
6241  vsplus:
6242                 if (varlen < 0) {
6243                         argstr(
6244                                 p, flag | EXP_TILDE |
6245                                         (quoted ?  EXP_QWORD : EXP_WORD),
6246                                 var_str_list
6247                         );
6248                         goto end;
6249                 }
6250                 if (easy)
6251                         goto record;
6252                 goto end;
6253         }
6254
6255         if (subtype == VSASSIGN || subtype == VSQUESTION) {
6256                 if (varlen < 0) {
6257                         if (subevalvar(p, var, /* strloc: */ 0,
6258                                         subtype, startloc, varflags,
6259                                         /* quotes: */ 0,
6260                                         var_str_list)
6261                         ) {
6262                                 varflags &= ~VSNUL;
6263                                 /*
6264                                  * Remove any recorded regions beyond
6265                                  * start of variable
6266                                  */
6267                                 removerecordregions(startloc);
6268                                 goto again;
6269                         }
6270                         goto end;
6271                 }
6272                 if (easy)
6273                         goto record;
6274                 goto end;
6275         }
6276
6277         if (varlen < 0 && uflag)
6278                 varunset(p, var, 0, 0);
6279
6280         if (subtype == VSLENGTH) {
6281                 cvtnum(varlen > 0 ? varlen : 0);
6282                 goto record;
6283         }
6284
6285         if (subtype == VSNORMAL) {
6286                 if (easy)
6287                         goto record;
6288                 goto end;
6289         }
6290
6291 #if DEBUG
6292         switch (subtype) {
6293         case VSTRIMLEFT:
6294         case VSTRIMLEFTMAX:
6295         case VSTRIMRIGHT:
6296         case VSTRIMRIGHTMAX:
6297 #if ENABLE_ASH_BASH_COMPAT
6298         case VSSUBSTR:
6299         case VSREPLACE:
6300         case VSREPLACEALL:
6301 #endif
6302                 break;
6303         default:
6304                 abort();
6305         }
6306 #endif
6307
6308         if (varlen >= 0) {
6309                 /*
6310                  * Terminate the string and start recording the pattern
6311                  * right after it
6312                  */
6313                 STPUTC('\0', expdest);
6314                 patloc = expdest - (char *)stackblock();
6315                 if (0 == subevalvar(p, /* str: */ NULL, patloc, subtype,
6316                                 startloc, varflags,
6317                                 /* quotes: */ flag & (EXP_FULL | EXP_CASE),
6318                                 var_str_list)
6319                 ) {
6320                         int amount = expdest - (
6321                                 (char *)stackblock() + patloc - 1
6322                         );
6323                         STADJUST(-amount, expdest);
6324                 }
6325                 /* Remove any recorded regions beyond start of variable */
6326                 removerecordregions(startloc);
6327  record:
6328                 recordregion(startloc, expdest - (char *)stackblock(), quoted);
6329         }
6330
6331  end:
6332         if (subtype != VSNORMAL) {      /* skip to end of alternative */
6333                 int nesting = 1;
6334                 for (;;) {
6335                         char c = *p++;
6336                         if (c == CTLESC)
6337                                 p++;
6338                         else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
6339                                 if (varlen >= 0)
6340                                         argbackq = argbackq->next;
6341                         } else if (c == CTLVAR) {
6342                                 if ((*p++ & VSTYPE) != VSNORMAL)
6343                                         nesting++;
6344                         } else if (c == CTLENDVAR) {
6345                                 if (--nesting == 0)
6346                                         break;
6347                         }
6348                 }
6349         }
6350         return p;
6351 }
6352
6353 /*
6354  * Break the argument string into pieces based upon IFS and add the
6355  * strings to the argument list.  The regions of the string to be
6356  * searched for IFS characters have been stored by recordregion.
6357  */
6358 static void
6359 ifsbreakup(char *string, struct arglist *arglist)
6360 {
6361         struct ifsregion *ifsp;
6362         struct strlist *sp;
6363         char *start;
6364         char *p;
6365         char *q;
6366         const char *ifs, *realifs;
6367         int ifsspc;
6368         int nulonly;
6369
6370         start = string;
6371         if (ifslastp != NULL) {
6372                 ifsspc = 0;
6373                 nulonly = 0;
6374                 realifs = ifsset() ? ifsval() : defifs;
6375                 ifsp = &ifsfirst;
6376                 do {
6377                         p = string + ifsp->begoff;
6378                         nulonly = ifsp->nulonly;
6379                         ifs = nulonly ? nullstr : realifs;
6380                         ifsspc = 0;
6381                         while (p < string + ifsp->endoff) {
6382                                 q = p;
6383                                 if (*p == CTLESC)
6384                                         p++;
6385                                 if (!strchr(ifs, *p)) {
6386                                         p++;
6387                                         continue;
6388                                 }
6389                                 if (!nulonly)
6390                                         ifsspc = (strchr(defifs, *p) != NULL);
6391                                 /* Ignore IFS whitespace at start */
6392                                 if (q == start && ifsspc) {
6393                                         p++;
6394                                         start = p;
6395                                         continue;
6396                                 }
6397                                 *q = '\0';
6398                                 sp = stzalloc(sizeof(*sp));
6399                                 sp->text = start;
6400                                 *arglist->lastp = sp;
6401                                 arglist->lastp = &sp->next;
6402                                 p++;
6403                                 if (!nulonly) {
6404                                         for (;;) {
6405                                                 if (p >= string + ifsp->endoff) {
6406                                                         break;
6407                                                 }
6408                                                 q = p;
6409                                                 if (*p == CTLESC)
6410                                                         p++;
6411                                                 if (strchr(ifs, *p) == NULL) {
6412                                                         p = q;
6413                                                         break;
6414                                                 }
6415                                                 if (strchr(defifs, *p) == NULL) {
6416                                                         if (ifsspc) {
6417                                                                 p++;
6418                                                                 ifsspc = 0;
6419                                                         } else {
6420                                                                 p = q;
6421                                                                 break;
6422                                                         }
6423                                                 } else
6424                                                         p++;
6425                                         }
6426                                 }
6427                                 start = p;
6428                         } /* while */
6429                         ifsp = ifsp->next;
6430                 } while (ifsp != NULL);
6431                 if (nulonly)
6432                         goto add;
6433         }
6434
6435         if (!*start)
6436                 return;
6437
6438  add:
6439         sp = stzalloc(sizeof(*sp));
6440         sp->text = start;
6441         *arglist->lastp = sp;
6442         arglist->lastp = &sp->next;
6443 }
6444
6445 static void
6446 ifsfree(void)
6447 {
6448         struct ifsregion *p;
6449
6450         INT_OFF;
6451         p = ifsfirst.next;
6452         do {
6453                 struct ifsregion *ifsp;
6454                 ifsp = p->next;
6455                 free(p);
6456                 p = ifsp;
6457         } while (p);
6458         ifslastp = NULL;
6459         ifsfirst.next = NULL;
6460         INT_ON;
6461 }
6462
6463 /*
6464  * Add a file name to the list.
6465  */
6466 static void
6467 addfname(const char *name)
6468 {
6469         struct strlist *sp;
6470
6471         sp = stzalloc(sizeof(*sp));
6472         sp->text = ststrdup(name);
6473         *exparg.lastp = sp;
6474         exparg.lastp = &sp->next;
6475 }
6476
6477 static char *expdir;
6478
6479 /*
6480  * Do metacharacter (i.e. *, ?, [...]) expansion.
6481  */
6482 static void
6483 expmeta(char *enddir, char *name)
6484 {
6485         char *p;
6486         const char *cp;
6487         char *start;
6488         char *endname;
6489         int metaflag;
6490         struct stat statb;
6491         DIR *dirp;
6492         struct dirent *dp;
6493         int atend;
6494         int matchdot;
6495
6496         metaflag = 0;
6497         start = name;
6498         for (p = name; *p; p++) {
6499                 if (*p == '*' || *p == '?')
6500                         metaflag = 1;
6501                 else if (*p == '[') {
6502                         char *q = p + 1;
6503                         if (*q == '!')
6504                                 q++;
6505                         for (;;) {
6506                                 if (*q == '\\')
6507                                         q++;
6508                                 if (*q == '/' || *q == '\0')
6509                                         break;
6510                                 if (*++q == ']') {
6511                                         metaflag = 1;
6512                                         break;
6513                                 }
6514                         }
6515                 } else if (*p == '\\')
6516                         p++;
6517                 else if (*p == '/') {
6518                         if (metaflag)
6519                                 goto out;
6520                         start = p + 1;
6521                 }
6522         }
6523  out:
6524         if (metaflag == 0) {    /* we've reached the end of the file name */
6525                 if (enddir != expdir)
6526                         metaflag++;
6527                 p = name;
6528                 do {
6529                         if (*p == '\\')
6530                                 p++;
6531                         *enddir++ = *p;
6532                 } while (*p++);
6533                 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
6534                         addfname(expdir);
6535                 return;
6536         }
6537         endname = p;
6538         if (name < start) {
6539                 p = name;
6540                 do {
6541                         if (*p == '\\')
6542                                 p++;
6543                         *enddir++ = *p++;
6544                 } while (p < start);
6545         }
6546         if (enddir == expdir) {
6547                 cp = ".";
6548         } else if (enddir == expdir + 1 && *expdir == '/') {
6549                 cp = "/";
6550         } else {
6551                 cp = expdir;
6552                 enddir[-1] = '\0';
6553         }
6554         dirp = opendir(cp);
6555         if (dirp == NULL)
6556                 return;
6557         if (enddir != expdir)
6558                 enddir[-1] = '/';
6559         if (*endname == 0) {
6560                 atend = 1;
6561         } else {
6562                 atend = 0;
6563                 *endname++ = '\0';
6564         }
6565         matchdot = 0;
6566         p = start;
6567         if (*p == '\\')
6568                 p++;
6569         if (*p == '.')
6570                 matchdot++;
6571         while (!intpending && (dp = readdir(dirp)) != NULL) {
6572                 if (dp->d_name[0] == '.' && ! matchdot)
6573                         continue;
6574                 if (pmatch(start, dp->d_name)) {
6575                         if (atend) {
6576                                 strcpy(enddir, dp->d_name);
6577                                 addfname(expdir);
6578                         } else {
6579                                 for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';)
6580                                         continue;
6581                                 p[-1] = '/';
6582                                 expmeta(p, endname);
6583                         }
6584                 }
6585         }
6586         closedir(dirp);
6587         if (! atend)
6588                 endname[-1] = '/';
6589 }
6590
6591 static struct strlist *
6592 msort(struct strlist *list, int len)
6593 {
6594         struct strlist *p, *q = NULL;
6595         struct strlist **lpp;
6596         int half;
6597         int n;
6598
6599         if (len <= 1)
6600                 return list;
6601         half = len >> 1;
6602         p = list;
6603         for (n = half; --n >= 0;) {
6604                 q = p;
6605                 p = p->next;
6606         }
6607         q->next = NULL;                 /* terminate first half of list */
6608         q = msort(list, half);          /* sort first half of list */
6609         p = msort(p, len - half);               /* sort second half */
6610         lpp = &list;
6611         for (;;) {
6612 #if ENABLE_LOCALE_SUPPORT
6613                 if (strcoll(p->text, q->text) < 0)
6614 #else
6615                 if (strcmp(p->text, q->text) < 0)
6616 #endif
6617                                                 {
6618                         *lpp = p;
6619                         lpp = &p->next;
6620                         p = *lpp;
6621                         if (p == NULL) {
6622                                 *lpp = q;
6623                                 break;
6624                         }
6625                 } else {
6626                         *lpp = q;
6627                         lpp = &q->next;
6628                         q = *lpp;
6629                         if (q == NULL) {
6630                                 *lpp = p;
6631                                 break;
6632                         }
6633                 }
6634         }
6635         return list;
6636 }
6637
6638 /*
6639  * Sort the results of file name expansion.  It calculates the number of
6640  * strings to sort and then calls msort (short for merge sort) to do the
6641  * work.
6642  */
6643 static struct strlist *
6644 expsort(struct strlist *str)
6645 {
6646         int len;
6647         struct strlist *sp;
6648
6649         len = 0;
6650         for (sp = str; sp; sp = sp->next)
6651                 len++;
6652         return msort(str, len);
6653 }
6654
6655 static void
6656 expandmeta(struct strlist *str /*, int flag*/)
6657 {
6658         static const char metachars[] ALIGN1 = {
6659                 '*', '?', '[', 0
6660         };
6661         /* TODO - EXP_REDIR */
6662
6663         while (str) {
6664                 struct strlist **savelastp;
6665                 struct strlist *sp;
6666                 char *p;
6667
6668                 if (fflag)
6669                         goto nometa;
6670                 if (!strpbrk(str->text, metachars))
6671                         goto nometa;
6672                 savelastp = exparg.lastp;
6673
6674                 INT_OFF;
6675                 p = preglob(str->text, 0, RMESCAPE_ALLOC | RMESCAPE_HEAP);
6676                 {
6677                         int i = strlen(str->text);
6678                         expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
6679                 }
6680
6681                 expmeta(expdir, p);
6682                 free(expdir);
6683                 if (p != str->text)
6684                         free(p);
6685                 INT_ON;
6686                 if (exparg.lastp == savelastp) {
6687                         /*
6688                          * no matches
6689                          */
6690  nometa:
6691                         *exparg.lastp = str;
6692                         rmescapes(str->text);
6693                         exparg.lastp = &str->next;
6694                 } else {
6695                         *exparg.lastp = NULL;
6696                         *savelastp = sp = expsort(*savelastp);
6697                         while (sp->next != NULL)
6698                                 sp = sp->next;
6699                         exparg.lastp = &sp->next;
6700                 }
6701                 str = str->next;
6702         }
6703 }
6704
6705 /*
6706  * Perform variable substitution and command substitution on an argument,
6707  * placing the resulting list of arguments in arglist.  If EXP_FULL is true,
6708  * perform splitting and file name expansion.  When arglist is NULL, perform
6709  * here document expansion.
6710  */
6711 static void
6712 expandarg(union node *arg, struct arglist *arglist, int flag)
6713 {
6714         struct strlist *sp;
6715         char *p;
6716
6717         argbackq = arg->narg.backquote;
6718         STARTSTACKSTR(expdest);
6719         ifsfirst.next = NULL;
6720         ifslastp = NULL;
6721         argstr(arg->narg.text, flag,
6722                         /* var_str_list: */ arglist ? arglist->list : NULL);
6723         p = _STPUTC('\0', expdest);
6724         expdest = p - 1;
6725         if (arglist == NULL) {
6726                 return;                 /* here document expanded */
6727         }
6728         p = grabstackstr(p);
6729         exparg.lastp = &exparg.list;
6730         /*
6731          * TODO - EXP_REDIR
6732          */
6733         if (flag & EXP_FULL) {
6734                 ifsbreakup(p, &exparg);
6735                 *exparg.lastp = NULL;
6736                 exparg.lastp = &exparg.list;
6737                 expandmeta(exparg.list /*, flag*/);
6738         } else {
6739                 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
6740                         rmescapes(p);
6741                 sp = stzalloc(sizeof(*sp));
6742                 sp->text = p;
6743                 *exparg.lastp = sp;
6744                 exparg.lastp = &sp->next;
6745         }
6746         if (ifsfirst.next)
6747                 ifsfree();
6748         *exparg.lastp = NULL;
6749         if (exparg.list) {
6750                 *arglist->lastp = exparg.list;
6751                 arglist->lastp = exparg.lastp;
6752         }
6753 }
6754
6755 /*
6756  * Expand shell variables and backquotes inside a here document.
6757  */
6758 static void
6759 expandhere(union node *arg, int fd)
6760 {
6761         herefd = fd;
6762         expandarg(arg, (struct arglist *)NULL, 0);
6763         full_write(fd, stackblock(), expdest - (char *)stackblock());
6764 }
6765
6766 /*
6767  * Returns true if the pattern matches the string.
6768  */
6769 static int
6770 patmatch(char *pattern, const char *string)
6771 {
6772         return pmatch(preglob(pattern, 0, 0), string);
6773 }
6774
6775 /*
6776  * See if a pattern matches in a case statement.
6777  */
6778 static int
6779 casematch(union node *pattern, char *val)
6780 {
6781         struct stackmark smark;
6782         int result;
6783
6784         setstackmark(&smark);
6785         argbackq = pattern->narg.backquote;
6786         STARTSTACKSTR(expdest);
6787         ifslastp = NULL;
6788         argstr(pattern->narg.text, EXP_TILDE | EXP_CASE,
6789                         /* var_str_list: */ NULL);
6790         STACKSTRNUL(expdest);
6791         result = patmatch(stackblock(), val);
6792         popstackmark(&smark);
6793         return result;
6794 }
6795
6796
6797 /* ============ find_command */
6798
6799 struct builtincmd {
6800         const char *name;
6801         int (*builtin)(int, char **);
6802         /* unsigned flags; */
6803 };
6804 #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1)
6805 /* "regular" builtins always take precedence over commands,
6806  * regardless of PATH=....%builtin... position */
6807 #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2)
6808 #define IS_BUILTIN_ASSIGN(b)  ((b)->name[0] & 4)
6809
6810 struct cmdentry {
6811         smallint cmdtype;       /* CMDxxx */
6812         union param {
6813                 int index;
6814                 /* index >= 0 for commands without path (slashes) */
6815                 /* (TODO: what exactly does the value mean? PATH position?) */
6816                 /* index == -1 for commands with slashes */
6817                 /* index == (-2 - applet_no) for NOFORK applets */
6818                 const struct builtincmd *cmd;
6819                 struct funcnode *func;
6820         } u;
6821 };
6822 /* values of cmdtype */
6823 #define CMDUNKNOWN      -1      /* no entry in table for command */
6824 #define CMDNORMAL       0       /* command is an executable program */
6825 #define CMDFUNCTION     1       /* command is a shell function */
6826 #define CMDBUILTIN      2       /* command is a shell builtin */
6827
6828 /* action to find_command() */
6829 #define DO_ERR          0x01    /* prints errors */
6830 #define DO_ABS          0x02    /* checks absolute paths */
6831 #define DO_NOFUNC       0x04    /* don't return shell functions, for command */
6832 #define DO_ALTPATH      0x08    /* using alternate path */
6833 #define DO_ALTBLTIN     0x20    /* %builtin in alt. path */
6834
6835 static void find_command(char *, struct cmdentry *, int, const char *);
6836
6837
6838 /* ============ Hashing commands */
6839
6840 /*
6841  * When commands are first encountered, they are entered in a hash table.
6842  * This ensures that a full path search will not have to be done for them
6843  * on each invocation.
6844  *
6845  * We should investigate converting to a linear search, even though that
6846  * would make the command name "hash" a misnomer.
6847  */
6848
6849 struct tblentry {
6850         struct tblentry *next;  /* next entry in hash chain */
6851         union param param;      /* definition of builtin function */
6852         smallint cmdtype;       /* CMDxxx */
6853         char rehash;            /* if set, cd done since entry created */
6854         char cmdname[1];        /* name of command */
6855 };
6856
6857 static struct tblentry **cmdtable;
6858 #define INIT_G_cmdtable() do { \
6859         cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \
6860 } while (0)
6861
6862 static int builtinloc = -1;     /* index in path of %builtin, or -1 */
6863
6864
6865 static void
6866 tryexec(USE_FEATURE_SH_STANDALONE(int applet_no,) char *cmd, char **argv, char **envp)
6867 {
6868         int repeated = 0;
6869
6870 #if ENABLE_FEATURE_SH_STANDALONE
6871         if (applet_no >= 0) {
6872                 if (APPLET_IS_NOEXEC(applet_no))
6873                         run_applet_no_and_exit(applet_no, argv);
6874                 /* re-exec ourselves with the new arguments */
6875                 execve(bb_busybox_exec_path, argv, envp);
6876                 /* If they called chroot or otherwise made the binary no longer
6877                  * executable, fall through */
6878         }
6879 #endif
6880
6881  repeat:
6882 #ifdef SYSV
6883         do {
6884                 execve(cmd, argv, envp);
6885         } while (errno == EINTR);
6886 #else
6887         execve(cmd, argv, envp);
6888 #endif
6889         if (repeated) {
6890                 free(argv);
6891                 return;
6892         }
6893         if (errno == ENOEXEC) {
6894                 char **ap;
6895                 char **new;
6896
6897                 for (ap = argv; *ap; ap++)
6898                         continue;
6899                 ap = new = ckmalloc((ap - argv + 2) * sizeof(ap[0]));
6900                 ap[1] = cmd;
6901                 ap[0] = cmd = (char *)DEFAULT_SHELL;
6902                 ap += 2;
6903                 argv++;
6904                 while ((*ap++ = *argv++) != NULL)
6905                         continue;
6906                 argv = new;
6907                 repeated++;
6908                 goto repeat;
6909         }
6910 }
6911
6912 /*
6913  * Exec a program.  Never returns.  If you change this routine, you may
6914  * have to change the find_command routine as well.
6915  */
6916 static void shellexec(char **, const char *, int) ATTRIBUTE_NORETURN;
6917 static void
6918 shellexec(char **argv, const char *path, int idx)
6919 {
6920         char *cmdname;
6921         int e;
6922         char **envp;
6923         int exerrno;
6924 #if ENABLE_FEATURE_SH_STANDALONE
6925         int applet_no = -1;
6926 #endif
6927
6928         clearredir(1);
6929         envp = listvars(VEXPORT, VUNSET, 0);
6930         if (strchr(argv[0], '/') != NULL
6931 #if ENABLE_FEATURE_SH_STANDALONE
6932          || (applet_no = find_applet_by_name(argv[0])) >= 0
6933 #endif
6934         ) {
6935                 tryexec(USE_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp);
6936                 e = errno;
6937         } else {
6938                 e = ENOENT;
6939                 while ((cmdname = padvance(&path, argv[0])) != NULL) {
6940                         if (--idx < 0 && pathopt == NULL) {
6941                                 tryexec(USE_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp);
6942                                 if (errno != ENOENT && errno != ENOTDIR)
6943                                         e = errno;
6944                         }
6945                         stunalloc(cmdname);
6946                 }
6947         }
6948
6949         /* Map to POSIX errors */
6950         switch (e) {
6951         case EACCES:
6952                 exerrno = 126;
6953                 break;
6954         case ENOENT:
6955                 exerrno = 127;
6956                 break;
6957         default:
6958                 exerrno = 2;
6959                 break;
6960         }
6961         exitstatus = exerrno;
6962         TRACE(("shellexec failed for %s, errno %d, suppressint %d\n",
6963                 argv[0], e, suppressint));
6964         ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found"));
6965         /* NOTREACHED */
6966 }
6967
6968 static void
6969 printentry(struct tblentry *cmdp)
6970 {
6971         int idx;
6972         const char *path;
6973         char *name;
6974
6975         idx = cmdp->param.index;
6976         path = pathval();
6977         do {
6978                 name = padvance(&path, cmdp->cmdname);
6979                 stunalloc(name);
6980         } while (--idx >= 0);
6981         out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr));
6982 }
6983
6984 /*
6985  * Clear out command entries.  The argument specifies the first entry in
6986  * PATH which has changed.
6987  */
6988 static void
6989 clearcmdentry(int firstchange)
6990 {
6991         struct tblentry **tblp;
6992         struct tblentry **pp;
6993         struct tblentry *cmdp;
6994
6995         INT_OFF;
6996         for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) {
6997                 pp = tblp;
6998                 while ((cmdp = *pp) != NULL) {
6999                         if ((cmdp->cmdtype == CMDNORMAL &&
7000                              cmdp->param.index >= firstchange)
7001                          || (cmdp->cmdtype == CMDBUILTIN &&
7002                              builtinloc >= firstchange)
7003                         ) {
7004                                 *pp = cmdp->next;
7005                                 free(cmdp);
7006                         } else {
7007                                 pp = &cmdp->next;
7008                         }
7009                 }
7010         }
7011         INT_ON;
7012 }
7013
7014 /*
7015  * Locate a command in the command hash table.  If "add" is nonzero,
7016  * add the command to the table if it is not already present.  The
7017  * variable "lastcmdentry" is set to point to the address of the link
7018  * pointing to the entry, so that delete_cmd_entry can delete the
7019  * entry.
7020  *
7021  * Interrupts must be off if called with add != 0.
7022  */
7023 static struct tblentry **lastcmdentry;
7024
7025 static struct tblentry *
7026 cmdlookup(const char *name, int add)
7027 {
7028         unsigned int hashval;
7029         const char *p;
7030         struct tblentry *cmdp;
7031         struct tblentry **pp;
7032
7033         p = name;
7034         hashval = (unsigned char)*p << 4;
7035         while (*p)
7036                 hashval += (unsigned char)*p++;
7037         hashval &= 0x7FFF;
7038         pp = &cmdtable[hashval % CMDTABLESIZE];
7039         for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
7040                 if (strcmp(cmdp->cmdname, name) == 0)
7041                         break;
7042                 pp = &cmdp->next;
7043         }
7044         if (add && cmdp == NULL) {
7045                 cmdp = *pp = ckzalloc(sizeof(struct tblentry)
7046                                 + strlen(name)
7047                                 /* + 1 - already done because
7048                                  * tblentry::cmdname is char[1] */);
7049                 /*cmdp->next = NULL; - ckzalloc did it */
7050                 cmdp->cmdtype = CMDUNKNOWN;
7051                 strcpy(cmdp->cmdname, name);
7052         }
7053         lastcmdentry = pp;
7054         return cmdp;
7055 }
7056
7057 /*
7058  * Delete the command entry returned on the last lookup.
7059  */
7060 static void
7061 delete_cmd_entry(void)
7062 {
7063         struct tblentry *cmdp;
7064
7065         INT_OFF;
7066         cmdp = *lastcmdentry;
7067         *lastcmdentry = cmdp->next;
7068         if (cmdp->cmdtype == CMDFUNCTION)
7069                 freefunc(cmdp->param.func);
7070         free(cmdp);
7071         INT_ON;
7072 }
7073
7074 /*
7075  * Add a new command entry, replacing any existing command entry for
7076  * the same name - except special builtins.
7077  */
7078 static void
7079 addcmdentry(char *name, struct cmdentry *entry)
7080 {
7081         struct tblentry *cmdp;
7082
7083         cmdp = cmdlookup(name, 1);
7084         if (cmdp->cmdtype == CMDFUNCTION) {
7085                 freefunc(cmdp->param.func);
7086         }
7087         cmdp->cmdtype = entry->cmdtype;
7088         cmdp->param = entry->u;
7089         cmdp->rehash = 0;
7090 }
7091
7092 static int
7093 hashcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
7094 {
7095         struct tblentry **pp;
7096         struct tblentry *cmdp;
7097         int c;
7098         struct cmdentry entry;
7099         char *name;
7100
7101         if (nextopt("r") != '\0') {
7102                 clearcmdentry(0);
7103                 return 0;
7104         }
7105
7106         if (*argptr == NULL) {
7107                 for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
7108                         for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
7109                                 if (cmdp->cmdtype == CMDNORMAL)
7110                                         printentry(cmdp);
7111                         }
7112                 }
7113                 return 0;
7114         }
7115
7116         c = 0;
7117         while ((name = *argptr) != NULL) {
7118                 cmdp = cmdlookup(name, 0);
7119                 if (cmdp != NULL
7120                  && (cmdp->cmdtype == CMDNORMAL
7121                      || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
7122                 ) {
7123                         delete_cmd_entry();
7124                 }
7125                 find_command(name, &entry, DO_ERR, pathval());
7126                 if (entry.cmdtype == CMDUNKNOWN)
7127                         c = 1;
7128                 argptr++;
7129         }
7130         return c;
7131 }
7132
7133 /*
7134  * Called when a cd is done.  Marks all commands so the next time they
7135  * are executed they will be rehashed.
7136  */
7137 static void
7138 hashcd(void)
7139 {
7140         struct tblentry **pp;
7141         struct tblentry *cmdp;
7142
7143         for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
7144                 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
7145                         if (cmdp->cmdtype == CMDNORMAL
7146                          || (cmdp->cmdtype == CMDBUILTIN
7147                              && !IS_BUILTIN_REGULAR(cmdp->param.cmd)
7148                              && builtinloc > 0)
7149                         ) {
7150                                 cmdp->rehash = 1;
7151                         }
7152                 }
7153         }
7154 }
7155
7156 /*
7157  * Fix command hash table when PATH changed.
7158  * Called before PATH is changed.  The argument is the new value of PATH;
7159  * pathval() still returns the old value at this point.
7160  * Called with interrupts off.
7161  */
7162 static void
7163 changepath(const char *new)
7164 {
7165         const char *old;
7166         int firstchange;
7167         int idx;
7168         int idx_bltin;
7169
7170         old = pathval();
7171         firstchange = 9999;     /* assume no change */
7172         idx = 0;
7173         idx_bltin = -1;
7174         for (;;) {
7175                 if (*old != *new) {
7176                         firstchange = idx;
7177                         if ((*old == '\0' && *new == ':')
7178                          || (*old == ':' && *new == '\0'))
7179                                 firstchange++;
7180                         old = new;      /* ignore subsequent differences */
7181                 }
7182                 if (*new == '\0')
7183                         break;
7184                 if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin"))
7185                         idx_bltin = idx;
7186                 if (*new == ':')
7187                         idx++;
7188                 new++, old++;
7189         }
7190         if (builtinloc < 0 && idx_bltin >= 0)
7191                 builtinloc = idx_bltin;             /* zap builtins */
7192         if (builtinloc >= 0 && idx_bltin < 0)
7193                 firstchange = 0;
7194         clearcmdentry(firstchange);
7195         builtinloc = idx_bltin;
7196 }
7197
7198 #define TEOF 0
7199 #define TNL 1
7200 #define TREDIR 2
7201 #define TWORD 3
7202 #define TSEMI 4
7203 #define TBACKGND 5
7204 #define TAND 6
7205 #define TOR 7
7206 #define TPIPE 8
7207 #define TLP 9
7208 #define TRP 10
7209 #define TENDCASE 11
7210 #define TENDBQUOTE 12
7211 #define TNOT 13
7212 #define TCASE 14
7213 #define TDO 15
7214 #define TDONE 16
7215 #define TELIF 17
7216 #define TELSE 18
7217 #define TESAC 19
7218 #define TFI 20
7219 #define TFOR 21
7220 #define TIF 22
7221 #define TIN 23
7222 #define TTHEN 24
7223 #define TUNTIL 25
7224 #define TWHILE 26
7225 #define TBEGIN 27
7226 #define TEND 28
7227 typedef smallint token_id_t;
7228
7229 /* first char is indicating which tokens mark the end of a list */
7230 static const char *const tokname_array[] = {
7231         "\1end of file",
7232         "\0newline",
7233         "\0redirection",
7234         "\0word",
7235         "\0;",
7236         "\0&",
7237         "\0&&",
7238         "\0||",
7239         "\0|",
7240         "\0(",
7241         "\1)",
7242         "\1;;",
7243         "\1`",
7244 #define KWDOFFSET 13
7245         /* the following are keywords */
7246         "\0!",
7247         "\0case",
7248         "\1do",
7249         "\1done",
7250         "\1elif",
7251         "\1else",
7252         "\1esac",
7253         "\1fi",
7254         "\0for",
7255         "\0if",
7256         "\0in",
7257         "\1then",
7258         "\0until",
7259         "\0while",
7260         "\0{",
7261         "\1}",
7262 };
7263
7264 static const char *
7265 tokname(int tok)
7266 {
7267         static char buf[16];
7268
7269 //try this:
7270 //if (tok < TSEMI) return tokname_array[tok] + 1;
7271 //sprintf(buf, "\"%s\"", tokname_array[tok] + 1);
7272 //return buf;
7273
7274         if (tok >= TSEMI)
7275                 buf[0] = '"';
7276         sprintf(buf + (tok >= TSEMI), "%s%c",
7277                         tokname_array[tok] + 1, (tok >= TSEMI ? '"' : 0));
7278         return buf;
7279 }
7280
7281 /* Wrapper around strcmp for qsort/bsearch/... */
7282 static int
7283 pstrcmp(const void *a, const void *b)
7284 {
7285         return strcmp((char*) a, (*(char**) b) + 1);
7286 }
7287
7288 static const char *const *
7289 findkwd(const char *s)
7290 {
7291         return bsearch(s, tokname_array + KWDOFFSET,
7292                         ARRAY_SIZE(tokname_array) - KWDOFFSET,
7293                         sizeof(tokname_array[0]), pstrcmp);
7294 }
7295
7296 /*
7297  * Locate and print what a word is...
7298  */
7299 static int
7300 describe_command(char *command, int describe_command_verbose)
7301 {
7302         struct cmdentry entry;
7303         struct tblentry *cmdp;
7304 #if ENABLE_ASH_ALIAS
7305         const struct alias *ap;
7306 #endif
7307         const char *path = pathval();
7308
7309         if (describe_command_verbose) {
7310                 out1str(command);
7311         }
7312
7313         /* First look at the keywords */
7314         if (findkwd(command)) {
7315                 out1str(describe_command_verbose ? " is a shell keyword" : command);
7316                 goto out;
7317         }
7318
7319 #if ENABLE_ASH_ALIAS
7320         /* Then look at the aliases */
7321         ap = lookupalias(command, 0);
7322         if (ap != NULL) {
7323                 if (!describe_command_verbose) {
7324                         out1str("alias ");
7325                         printalias(ap);
7326                         return 0;
7327                 }
7328                 out1fmt(" is an alias for %s", ap->val);
7329                 goto out;
7330         }
7331 #endif
7332         /* Then check if it is a tracked alias */
7333         cmdp = cmdlookup(command, 0);
7334         if (cmdp != NULL) {
7335                 entry.cmdtype = cmdp->cmdtype;
7336                 entry.u = cmdp->param;
7337         } else {
7338                 /* Finally use brute force */
7339                 find_command(command, &entry, DO_ABS, path);
7340         }
7341
7342         switch (entry.cmdtype) {
7343         case CMDNORMAL: {
7344                 int j = entry.u.index;
7345                 char *p;
7346                 if (j < 0) {
7347                         p = command;
7348                 } else {
7349                         do {
7350                                 p = padvance(&path, command);
7351                                 stunalloc(p);
7352                         } while (--j >= 0);
7353                 }
7354                 if (describe_command_verbose) {
7355                         out1fmt(" is%s %s",
7356                                 (cmdp ? " a tracked alias for" : nullstr), p
7357                         );
7358                 } else {
7359                         out1str(p);
7360                 }
7361                 break;
7362         }
7363
7364         case CMDFUNCTION:
7365                 if (describe_command_verbose) {
7366                         out1str(" is a shell function");
7367                 } else {
7368                         out1str(command);
7369                 }
7370                 break;
7371
7372         case CMDBUILTIN:
7373                 if (describe_command_verbose) {
7374                         out1fmt(" is a %sshell builtin",
7375                                 IS_BUILTIN_SPECIAL(entry.u.cmd) ?
7376                                         "special " : nullstr
7377                         );
7378                 } else {
7379                         out1str(command);
7380                 }
7381                 break;
7382
7383         default:
7384                 if (describe_command_verbose) {
7385                         out1str(": not found\n");
7386                 }
7387                 return 127;
7388         }
7389  out:
7390         outstr("\n", stdout);
7391         return 0;
7392 }
7393
7394 static int
7395 typecmd(int argc ATTRIBUTE_UNUSED, char **argv)
7396 {
7397         int i = 1;
7398         int err = 0;
7399         int verbose = 1;
7400
7401         /* type -p ... ? (we don't bother checking for 'p') */
7402         if (argv[1] && argv[1][0] == '-') {
7403                 i++;
7404                 verbose = 0;
7405         }
7406         while (argv[i]) {
7407                 err |= describe_command(argv[i++], verbose);
7408         }
7409         return err;
7410 }
7411
7412 #if ENABLE_ASH_CMDCMD
7413 static int
7414 commandcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
7415 {
7416         int c;
7417         enum {
7418                 VERIFY_BRIEF = 1,
7419                 VERIFY_VERBOSE = 2,
7420         } verify = 0;
7421
7422         while ((c = nextopt("pvV")) != '\0')
7423                 if (c == 'V')
7424                         verify |= VERIFY_VERBOSE;
7425                 else if (c == 'v')
7426                         verify |= VERIFY_BRIEF;
7427 #if DEBUG
7428                 else if (c != 'p')
7429                         abort();
7430 #endif
7431         if (verify)
7432                 return describe_command(*argptr, verify - VERIFY_BRIEF);
7433
7434         return 0;
7435 }
7436 #endif
7437
7438
7439 /* ============ eval.c */
7440
7441 static int funcblocksize;          /* size of structures in function */
7442 static int funcstringsize;         /* size of strings in node */
7443 static void *funcblock;            /* block to allocate function from */
7444 static char *funcstring;           /* block to allocate strings from */
7445
7446 /* flags in argument to evaltree */
7447 #define EV_EXIT 01              /* exit after evaluating tree */
7448 #define EV_TESTED 02            /* exit status is checked; ignore -e flag */
7449 #define EV_BACKCMD 04           /* command executing within back quotes */
7450
7451 static const short nodesize[26] = {
7452         SHELL_ALIGN(sizeof(struct ncmd)),
7453         SHELL_ALIGN(sizeof(struct npipe)),
7454         SHELL_ALIGN(sizeof(struct nredir)),
7455         SHELL_ALIGN(sizeof(struct nredir)),
7456         SHELL_ALIGN(sizeof(struct nredir)),
7457         SHELL_ALIGN(sizeof(struct nbinary)),
7458         SHELL_ALIGN(sizeof(struct nbinary)),
7459         SHELL_ALIGN(sizeof(struct nbinary)),
7460         SHELL_ALIGN(sizeof(struct nif)),
7461         SHELL_ALIGN(sizeof(struct nbinary)),
7462         SHELL_ALIGN(sizeof(struct nbinary)),
7463         SHELL_ALIGN(sizeof(struct nfor)),
7464         SHELL_ALIGN(sizeof(struct ncase)),
7465         SHELL_ALIGN(sizeof(struct nclist)),
7466         SHELL_ALIGN(sizeof(struct narg)),
7467         SHELL_ALIGN(sizeof(struct narg)),
7468         SHELL_ALIGN(sizeof(struct nfile)),
7469         SHELL_ALIGN(sizeof(struct nfile)),
7470         SHELL_ALIGN(sizeof(struct nfile)),
7471         SHELL_ALIGN(sizeof(struct nfile)),
7472         SHELL_ALIGN(sizeof(struct nfile)),
7473         SHELL_ALIGN(sizeof(struct ndup)),
7474         SHELL_ALIGN(sizeof(struct ndup)),
7475         SHELL_ALIGN(sizeof(struct nhere)),
7476         SHELL_ALIGN(sizeof(struct nhere)),
7477         SHELL_ALIGN(sizeof(struct nnot)),
7478 };
7479
7480 static void calcsize(union node *n);
7481
7482 static void
7483 sizenodelist(struct nodelist *lp)
7484 {
7485         while (lp) {
7486                 funcblocksize += SHELL_ALIGN(sizeof(struct nodelist));
7487                 calcsize(lp->n);
7488                 lp = lp->next;
7489         }
7490 }
7491
7492 static void
7493 calcsize(union node *n)
7494 {
7495         if (n == NULL)
7496                 return;
7497         funcblocksize += nodesize[n->type];
7498         switch (n->type) {
7499         case NCMD:
7500                 calcsize(n->ncmd.redirect);
7501                 calcsize(n->ncmd.args);
7502                 calcsize(n->ncmd.assign);
7503                 break;
7504         case NPIPE:
7505                 sizenodelist(n->npipe.cmdlist);
7506                 break;
7507         case NREDIR:
7508         case NBACKGND:
7509         case NSUBSHELL:
7510                 calcsize(n->nredir.redirect);
7511                 calcsize(n->nredir.n);
7512                 break;
7513         case NAND:
7514         case NOR:
7515         case NSEMI:
7516         case NWHILE:
7517         case NUNTIL:
7518                 calcsize(n->nbinary.ch2);
7519                 calcsize(n->nbinary.ch1);
7520                 break;
7521         case NIF:
7522                 calcsize(n->nif.elsepart);
7523                 calcsize(n->nif.ifpart);
7524                 calcsize(n->nif.test);
7525                 break;
7526         case NFOR:
7527                 funcstringsize += strlen(n->nfor.var) + 1;
7528                 calcsize(n->nfor.body);
7529                 calcsize(n->nfor.args);
7530                 break;
7531         case NCASE:
7532                 calcsize(n->ncase.cases);
7533                 calcsize(n->ncase.expr);
7534                 break;
7535         case NCLIST:
7536                 calcsize(n->nclist.body);
7537                 calcsize(n->nclist.pattern);
7538                 calcsize(n->nclist.next);
7539                 break;
7540         case NDEFUN:
7541         case NARG:
7542                 sizenodelist(n->narg.backquote);
7543                 funcstringsize += strlen(n->narg.text) + 1;
7544                 calcsize(n->narg.next);
7545                 break;
7546         case NTO:
7547         case NCLOBBER:
7548         case NFROM:
7549         case NFROMTO:
7550         case NAPPEND:
7551                 calcsize(n->nfile.fname);
7552                 calcsize(n->nfile.next);
7553                 break;
7554         case NTOFD:
7555         case NFROMFD:
7556                 calcsize(n->ndup.vname);
7557                 calcsize(n->ndup.next);
7558         break;
7559         case NHERE:
7560         case NXHERE:
7561                 calcsize(n->nhere.doc);
7562                 calcsize(n->nhere.next);
7563                 break;
7564         case NNOT:
7565                 calcsize(n->nnot.com);
7566                 break;
7567         };
7568 }
7569
7570 static char *
7571 nodeckstrdup(char *s)
7572 {
7573         char *rtn = funcstring;
7574
7575         strcpy(funcstring, s);
7576         funcstring += strlen(s) + 1;
7577         return rtn;
7578 }
7579
7580 static union node *copynode(union node *);
7581
7582 static struct nodelist *
7583 copynodelist(struct nodelist *lp)
7584 {
7585         struct nodelist *start;
7586         struct nodelist **lpp;
7587
7588         lpp = &start;
7589         while (lp) {
7590                 *lpp = funcblock;
7591                 funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist));
7592                 (*lpp)->n = copynode(lp->n);
7593                 lp = lp->next;
7594                 lpp = &(*lpp)->next;
7595         }
7596         *lpp = NULL;
7597         return start;
7598 }
7599
7600 static union node *
7601 copynode(union node *n)
7602 {
7603         union node *new;
7604
7605         if (n == NULL)
7606                 return NULL;
7607         new = funcblock;
7608         funcblock = (char *) funcblock + nodesize[n->type];
7609
7610         switch (n->type) {
7611         case NCMD:
7612                 new->ncmd.redirect = copynode(n->ncmd.redirect);
7613                 new->ncmd.args = copynode(n->ncmd.args);
7614                 new->ncmd.assign = copynode(n->ncmd.assign);
7615                 break;
7616         case NPIPE:
7617                 new->npipe.cmdlist = copynodelist(n->npipe.cmdlist);
7618                 new->npipe.backgnd = n->npipe.backgnd;
7619                 break;
7620         case NREDIR:
7621         case NBACKGND:
7622         case NSUBSHELL:
7623                 new->nredir.redirect = copynode(n->nredir.redirect);
7624                 new->nredir.n = copynode(n->nredir.n);
7625                 break;
7626         case NAND:
7627         case NOR:
7628         case NSEMI:
7629         case NWHILE:
7630         case NUNTIL:
7631                 new->nbinary.ch2 = copynode(n->nbinary.ch2);
7632                 new->nbinary.ch1 = copynode(n->nbinary.ch1);
7633                 break;
7634         case NIF:
7635                 new->nif.elsepart = copynode(n->nif.elsepart);
7636                 new->nif.ifpart = copynode(n->nif.ifpart);
7637                 new->nif.test = copynode(n->nif.test);
7638                 break;
7639         case NFOR:
7640                 new->nfor.var = nodeckstrdup(n->nfor.var);
7641                 new->nfor.body = copynode(n->nfor.body);
7642                 new->nfor.args = copynode(n->nfor.args);
7643                 break;
7644         case NCASE:
7645                 new->ncase.cases = copynode(n->ncase.cases);
7646                 new->ncase.expr = copynode(n->ncase.expr);
7647                 break;
7648         case NCLIST:
7649                 new->nclist.body = copynode(n->nclist.body);
7650                 new->nclist.pattern = copynode(n->nclist.pattern);
7651                 new->nclist.next = copynode(n->nclist.next);
7652                 break;
7653         case NDEFUN:
7654         case NARG:
7655                 new->narg.backquote = copynodelist(n->narg.backquote);
7656                 new->narg.text = nodeckstrdup(n->narg.text);
7657                 new->narg.next = copynode(n->narg.next);
7658                 break;
7659         case NTO:
7660         case NCLOBBER:
7661         case NFROM:
7662         case NFROMTO:
7663         case NAPPEND:
7664                 new->nfile.fname = copynode(n->nfile.fname);
7665                 new->nfile.fd = n->nfile.fd;
7666                 new->nfile.next = copynode(n->nfile.next);
7667                 break;
7668         case NTOFD:
7669         case NFROMFD:
7670                 new->ndup.vname = copynode(n->ndup.vname);
7671                 new->ndup.dupfd = n->ndup.dupfd;
7672                 new->ndup.fd = n->ndup.fd;
7673                 new->ndup.next = copynode(n->ndup.next);
7674                 break;
7675         case NHERE:
7676         case NXHERE:
7677                 new->nhere.doc = copynode(n->nhere.doc);
7678                 new->nhere.fd = n->nhere.fd;
7679                 new->nhere.next = copynode(n->nhere.next);
7680                 break;
7681         case NNOT:
7682                 new->nnot.com = copynode(n->nnot.com);
7683                 break;
7684         };
7685         new->type = n->type;
7686         return new;
7687 }
7688
7689 /*
7690  * Make a copy of a parse tree.
7691  */
7692 static struct funcnode *
7693 copyfunc(union node *n)
7694 {
7695         struct funcnode *f;
7696         size_t blocksize;
7697
7698         funcblocksize = offsetof(struct funcnode, n);
7699         funcstringsize = 0;
7700         calcsize(n);
7701         blocksize = funcblocksize;
7702         f = ckmalloc(blocksize + funcstringsize);
7703         funcblock = (char *) f + offsetof(struct funcnode, n);
7704         funcstring = (char *) f + blocksize;
7705         copynode(n);
7706         f->count = 0;
7707         return f;
7708 }
7709
7710 /*
7711  * Define a shell function.
7712  */
7713 static void
7714 defun(char *name, union node *func)
7715 {
7716         struct cmdentry entry;
7717
7718         INT_OFF;
7719         entry.cmdtype = CMDFUNCTION;
7720         entry.u.func = copyfunc(func);
7721         addcmdentry(name, &entry);
7722         INT_ON;
7723 }
7724
7725 static int evalskip;            /* set if we are skipping commands */
7726 /* reasons for skipping commands (see comment on breakcmd routine) */
7727 #define SKIPBREAK      (1 << 0)
7728 #define SKIPCONT       (1 << 1)
7729 #define SKIPFUNC       (1 << 2)
7730 #define SKIPFILE       (1 << 3)
7731 #define SKIPEVAL       (1 << 4)
7732 static int skipcount;           /* number of levels to skip */
7733 static int funcnest;            /* depth of function calls */
7734 static int loopnest;            /* current loop nesting level */
7735
7736 /* forward decl way out to parsing code - dotrap needs it */
7737 static int evalstring(char *s, int mask);
7738
7739 /*
7740  * Called to execute a trap.  Perhaps we should avoid entering new trap
7741  * handlers while we are executing a trap handler.
7742  */
7743 static int
7744 dotrap(void)
7745 {
7746         char *p;
7747         char *q;
7748         int i;
7749         int savestatus;
7750         int skip;
7751
7752         savestatus = exitstatus;
7753         pendingsig = 0;
7754         xbarrier();
7755
7756         for (i = 0, q = gotsig; i < NSIG - 1; i++, q++) {
7757                 if (!*q)
7758                         continue;
7759                 *q = '\0';
7760
7761                 p = trap[i + 1];
7762                 if (!p)
7763                         continue;
7764                 skip = evalstring(p, SKIPEVAL);
7765                 exitstatus = savestatus;
7766                 if (skip)
7767                         return skip;
7768         }
7769
7770         return 0;
7771 }
7772
7773 /* forward declarations - evaluation is fairly recursive business... */
7774 static void evalloop(union node *, int);
7775 static void evalfor(union node *, int);
7776 static void evalcase(union node *, int);
7777 static void evalsubshell(union node *, int);
7778 static void expredir(union node *);
7779 static void evalpipe(union node *, int);
7780 static void evalcommand(union node *, int);
7781 static int evalbltin(const struct builtincmd *, int, char **);
7782 static void prehash(union node *);
7783
7784 /*
7785  * Evaluate a parse tree.  The value is left in the global variable
7786  * exitstatus.
7787  */
7788 static void
7789 evaltree(union node *n, int flags)
7790 {
7791         int checkexit = 0;
7792         void (*evalfn)(union node *, int);
7793         unsigned isor;
7794         int status;
7795         if (n == NULL) {
7796                 TRACE(("evaltree(NULL) called\n"));
7797                 goto out;
7798         }
7799         TRACE(("pid %d, evaltree(%p: %d, %d) called\n",
7800                         getpid(), n, n->type, flags));
7801         switch (n->type) {
7802         default:
7803 #if DEBUG
7804                 out1fmt("Node type = %d\n", n->type);
7805                 fflush(stdout);
7806                 break;
7807 #endif
7808         case NNOT:
7809                 evaltree(n->nnot.com, EV_TESTED);
7810                 status = !exitstatus;
7811                 goto setstatus;
7812         case NREDIR:
7813                 expredir(n->nredir.redirect);
7814                 status = redirectsafe(n->nredir.redirect, REDIR_PUSH);
7815                 if (!status) {
7816                         evaltree(n->nredir.n, flags & EV_TESTED);
7817                         status = exitstatus;
7818                 }
7819                 popredir(0);
7820                 goto setstatus;
7821         case NCMD:
7822                 evalfn = evalcommand;
7823  checkexit:
7824                 if (eflag && !(flags & EV_TESTED))
7825                         checkexit = ~0;
7826                 goto calleval;
7827         case NFOR:
7828                 evalfn = evalfor;
7829                 goto calleval;
7830         case NWHILE:
7831         case NUNTIL:
7832                 evalfn = evalloop;
7833                 goto calleval;
7834         case NSUBSHELL:
7835         case NBACKGND:
7836                 evalfn = evalsubshell;
7837                 goto calleval;
7838         case NPIPE:
7839                 evalfn = evalpipe;
7840                 goto checkexit;
7841         case NCASE:
7842                 evalfn = evalcase;
7843                 goto calleval;
7844         case NAND:
7845         case NOR:
7846         case NSEMI:
7847 #if NAND + 1 != NOR
7848 #error NAND + 1 != NOR
7849 #endif
7850 #if NOR + 1 != NSEMI
7851 #error NOR + 1 != NSEMI
7852 #endif
7853                 isor = n->type - NAND;
7854                 evaltree(
7855                         n->nbinary.ch1,
7856                         (flags | ((isor >> 1) - 1)) & EV_TESTED
7857                 );
7858                 if (!exitstatus == isor)
7859                         break;
7860                 if (!evalskip) {
7861                         n = n->nbinary.ch2;
7862  evaln:
7863                         evalfn = evaltree;
7864  calleval:
7865                         evalfn(n, flags);
7866                         break;
7867                 }
7868                 break;
7869         case NIF:
7870                 evaltree(n->nif.test, EV_TESTED);
7871                 if (evalskip)
7872                         break;
7873                 if (exitstatus == 0) {
7874                         n = n->nif.ifpart;
7875                         goto evaln;
7876                 } else if (n->nif.elsepart) {
7877                         n = n->nif.elsepart;
7878                         goto evaln;
7879                 }
7880                 goto success;
7881         case NDEFUN:
7882                 defun(n->narg.text, n->narg.next);
7883  success:
7884                 status = 0;
7885  setstatus:
7886                 exitstatus = status;
7887                 break;
7888         }
7889  out:
7890         if ((checkexit & exitstatus))
7891                 evalskip |= SKIPEVAL;
7892         else if (pendingsig && dotrap())
7893                 goto exexit;
7894
7895         if (flags & EV_EXIT) {
7896  exexit:
7897                 raise_exception(EXEXIT);
7898         }
7899 }
7900
7901 #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3)
7902 static
7903 #endif
7904 void evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__));
7905
7906 static void
7907 evalloop(union node *n, int flags)
7908 {
7909         int status;
7910
7911         loopnest++;
7912         status = 0;
7913         flags &= EV_TESTED;
7914         for (;;) {
7915                 int i;
7916
7917                 evaltree(n->nbinary.ch1, EV_TESTED);
7918                 if (evalskip) {
7919  skipping:
7920                         if (evalskip == SKIPCONT && --skipcount <= 0) {
7921                                 evalskip = 0;
7922                                 continue;
7923                         }
7924                         if (evalskip == SKIPBREAK && --skipcount <= 0)
7925                                 evalskip = 0;
7926                         break;
7927                 }
7928                 i = exitstatus;
7929                 if (n->type != NWHILE)
7930                         i = !i;
7931                 if (i != 0)
7932                         break;
7933                 evaltree(n->nbinary.ch2, flags);
7934                 status = exitstatus;
7935                 if (evalskip)
7936                         goto skipping;
7937         }
7938         loopnest--;
7939         exitstatus = status;
7940 }
7941
7942 static void
7943 evalfor(union node *n, int flags)
7944 {
7945         struct arglist arglist;
7946         union node *argp;
7947         struct strlist *sp;
7948         struct stackmark smark;
7949
7950         setstackmark(&smark);
7951         arglist.list = NULL;
7952         arglist.lastp = &arglist.list;
7953         for (argp = n->nfor.args; argp; argp = argp->narg.next) {
7954                 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD);
7955                 /* XXX */
7956                 if (evalskip)
7957                         goto out;
7958         }
7959         *arglist.lastp = NULL;
7960
7961         exitstatus = 0;
7962         loopnest++;
7963         flags &= EV_TESTED;
7964         for (sp = arglist.list; sp; sp = sp->next) {
7965                 setvar(n->nfor.var, sp->text, 0);
7966                 evaltree(n->nfor.body, flags);
7967                 if (evalskip) {
7968                         if (evalskip == SKIPCONT && --skipcount <= 0) {
7969                                 evalskip = 0;
7970                                 continue;
7971                         }
7972                         if (evalskip == SKIPBREAK && --skipcount <= 0)
7973                                 evalskip = 0;
7974                         break;
7975                 }
7976         }
7977         loopnest--;
7978  out:
7979         popstackmark(&smark);
7980 }
7981
7982 static void
7983 evalcase(union node *n, int flags)
7984 {
7985         union node *cp;
7986         union node *patp;
7987         struct arglist arglist;
7988         struct stackmark smark;
7989
7990         setstackmark(&smark);
7991         arglist.list = NULL;
7992         arglist.lastp = &arglist.list;
7993         expandarg(n->ncase.expr, &arglist, EXP_TILDE);
7994         exitstatus = 0;
7995         for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) {
7996                 for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) {
7997                         if (casematch(patp, arglist.list->text)) {
7998                                 if (evalskip == 0) {
7999                                         evaltree(cp->nclist.body, flags);
8000                                 }
8001                                 goto out;
8002                         }
8003                 }
8004         }
8005  out:
8006         popstackmark(&smark);
8007 }
8008
8009 /*
8010  * Kick off a subshell to evaluate a tree.
8011  */
8012 static void
8013 evalsubshell(union node *n, int flags)
8014 {
8015         struct job *jp;
8016         int backgnd = (n->type == NBACKGND);
8017         int status;
8018
8019         expredir(n->nredir.redirect);
8020         if (!backgnd && flags & EV_EXIT && !trap[0])
8021                 goto nofork;
8022         INT_OFF;
8023         jp = makejob(/*n,*/ 1);
8024         if (forkshell(jp, n, backgnd) == 0) {
8025                 INT_ON;
8026                 flags |= EV_EXIT;
8027                 if (backgnd)
8028                         flags &=~ EV_TESTED;
8029  nofork:
8030                 redirect(n->nredir.redirect, 0);
8031                 evaltreenr(n->nredir.n, flags);
8032                 /* never returns */
8033         }
8034         status = 0;
8035         if (! backgnd)
8036                 status = waitforjob(jp);
8037         exitstatus = status;
8038         INT_ON;
8039 }
8040
8041 /*
8042  * Compute the names of the files in a redirection list.
8043  */
8044 static void fixredir(union node *, const char *, int);
8045 static void
8046 expredir(union node *n)
8047 {
8048         union node *redir;
8049
8050         for (redir = n; redir; redir = redir->nfile.next) {
8051                 struct arglist fn;
8052
8053                 fn.list = NULL;
8054                 fn.lastp = &fn.list;
8055                 switch (redir->type) {
8056                 case NFROMTO:
8057                 case NFROM:
8058                 case NTO:
8059                 case NCLOBBER:
8060                 case NAPPEND:
8061                         expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
8062                         redir->nfile.expfname = fn.list->text;
8063                         break;
8064                 case NFROMFD:
8065                 case NTOFD:
8066                         if (redir->ndup.vname) {
8067                                 expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
8068                                 if (fn.list == NULL)
8069                                         ash_msg_and_raise_error("redir error");
8070                                 fixredir(redir, fn.list->text, 1);
8071                         }
8072                         break;
8073                 }
8074         }
8075 }
8076
8077 /*
8078  * Evaluate a pipeline.  All the processes in the pipeline are children
8079  * of the process creating the pipeline.  (This differs from some versions
8080  * of the shell, which make the last process in a pipeline the parent
8081  * of all the rest.)
8082  */
8083 static void
8084 evalpipe(union node *n, int flags)
8085 {
8086         struct job *jp;
8087         struct nodelist *lp;
8088         int pipelen;
8089         int prevfd;
8090         int pip[2];
8091
8092         TRACE(("evalpipe(0x%lx) called\n", (long)n));
8093         pipelen = 0;
8094         for (lp = n->npipe.cmdlist; lp; lp = lp->next)
8095                 pipelen++;
8096         flags |= EV_EXIT;
8097         INT_OFF;
8098         jp = makejob(/*n,*/ pipelen);
8099         prevfd = -1;
8100         for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
8101                 prehash(lp->n);
8102                 pip[1] = -1;
8103                 if (lp->next) {
8104                         if (pipe(pip) < 0) {
8105                                 close(prevfd);
8106                                 ash_msg_and_raise_error("pipe call failed");
8107                         }
8108                 }
8109                 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
8110                         INT_ON;
8111                         if (pip[1] >= 0) {
8112                                 close(pip[0]);
8113                         }
8114                         if (prevfd > 0) {
8115                                 dup2(prevfd, 0);
8116                                 close(prevfd);
8117                         }
8118                         if (pip[1] > 1) {
8119                                 dup2(pip[1], 1);
8120                                 close(pip[1]);
8121                         }
8122                         evaltreenr(lp->n, flags);
8123                         /* never returns */
8124                 }
8125                 if (prevfd >= 0)
8126                         close(prevfd);
8127                 prevfd = pip[0];
8128                 close(pip[1]);
8129         }
8130         if (n->npipe.backgnd == 0) {
8131                 exitstatus = waitforjob(jp);
8132                 TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
8133         }
8134         INT_ON;
8135 }
8136
8137 /*
8138  * Controls whether the shell is interactive or not.
8139  */
8140 static void
8141 setinteractive(int on)
8142 {
8143         static smallint is_interactive;
8144
8145         if (++on == is_interactive)
8146                 return;
8147         is_interactive = on;
8148         setsignal(SIGINT);
8149         setsignal(SIGQUIT);
8150         setsignal(SIGTERM);
8151 #if !ENABLE_FEATURE_SH_EXTRA_QUIET
8152         if (is_interactive > 1) {
8153                 /* Looks like they want an interactive shell */
8154                 static smallint did_banner;
8155
8156                 if (!did_banner) {
8157                         out1fmt(
8158                                 "\n\n"
8159                                 "%s built-in shell (ash)\n"
8160                                 "Enter 'help' for a list of built-in commands."
8161                                 "\n\n",
8162                                 bb_banner);
8163                         did_banner = 1;
8164                 }
8165         }
8166 #endif
8167 }
8168
8169 static void
8170 optschanged(void)
8171 {
8172 #if DEBUG
8173         opentrace();
8174 #endif
8175         setinteractive(iflag);
8176         setjobctl(mflag);
8177 #if ENABLE_FEATURE_EDITING_VI
8178         if (viflag)
8179                 line_input_state->flags |= VI_MODE;
8180         else
8181                 line_input_state->flags &= ~VI_MODE;
8182 #else
8183         viflag = 0; /* forcibly keep the option off */
8184 #endif
8185 }
8186
8187 static struct localvar *localvars;
8188
8189 /*
8190  * Called after a function returns.
8191  * Interrupts must be off.
8192  */
8193 static void
8194 poplocalvars(void)
8195 {
8196         struct localvar *lvp;
8197         struct var *vp;
8198
8199         while ((lvp = localvars) != NULL) {
8200                 localvars = lvp->next;
8201                 vp = lvp->vp;
8202                 TRACE(("poplocalvar %s", vp ? vp->text : "-"));
8203                 if (vp == NULL) {       /* $- saved */
8204                         memcpy(optlist, lvp->text, sizeof(optlist));
8205                         free((char*)lvp->text);
8206                         optschanged();
8207                 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
8208                         unsetvar(vp->text);
8209                 } else {
8210                         if (vp->func)
8211                                 (*vp->func)(strchrnul(lvp->text, '=') + 1);
8212                         if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
8213                                 free((char*)vp->text);
8214                         vp->flags = lvp->flags;
8215                         vp->text = lvp->text;
8216                 }
8217                 free(lvp);
8218         }
8219 }
8220
8221 static int
8222 evalfun(struct funcnode *func, int argc, char **argv, int flags)
8223 {
8224         volatile struct shparam saveparam;
8225         struct localvar *volatile savelocalvars;
8226         struct jmploc *volatile savehandler;
8227         struct jmploc jmploc;
8228         int e;
8229
8230         saveparam = shellparam;
8231         savelocalvars = localvars;
8232         e = setjmp(jmploc.loc);
8233         if (e) {
8234                 goto funcdone;
8235         }
8236         INT_OFF;
8237         savehandler = exception_handler;
8238         exception_handler = &jmploc;
8239         localvars = NULL;
8240         shellparam.malloced = 0;
8241         func->count++;
8242         funcnest++;
8243         INT_ON;
8244         shellparam.nparam = argc - 1;
8245         shellparam.p = argv + 1;
8246 #if ENABLE_ASH_GETOPTS
8247         shellparam.optind = 1;
8248         shellparam.optoff = -1;
8249 #endif
8250         evaltree(&func->n, flags & EV_TESTED);
8251  funcdone:
8252         INT_OFF;
8253         funcnest--;
8254         freefunc(func);
8255         poplocalvars();
8256         localvars = savelocalvars;
8257         freeparam(&shellparam);
8258         shellparam = saveparam;
8259         exception_handler = savehandler;
8260         INT_ON;
8261         evalskip &= ~SKIPFUNC;
8262         return e;
8263 }
8264
8265 #if ENABLE_ASH_CMDCMD
8266 static char **
8267 parse_command_args(char **argv, const char **path)
8268 {
8269         char *cp, c;
8270
8271         for (;;) {
8272                 cp = *++argv;
8273                 if (!cp)
8274                         return 0;
8275                 if (*cp++ != '-')
8276                         break;
8277                 c = *cp++;
8278                 if (!c)
8279                         break;
8280                 if (c == '-' && !*cp) {
8281                         argv++;
8282                         break;
8283                 }
8284                 do {
8285                         switch (c) {
8286                         case 'p':
8287                                 *path = bb_default_path;
8288                                 break;
8289                         default:
8290                                 /* run 'typecmd' for other options */
8291                                 return 0;
8292                         }
8293                         c = *cp++;
8294                 } while (c);
8295         }
8296         return argv;
8297 }
8298 #endif
8299
8300 /*
8301  * Make a variable a local variable.  When a variable is made local, it's
8302  * value and flags are saved in a localvar structure.  The saved values
8303  * will be restored when the shell function returns.  We handle the name
8304  * "-" as a special case.
8305  */
8306 static void
8307 mklocal(char *name)
8308 {
8309         struct localvar *lvp;
8310         struct var **vpp;
8311         struct var *vp;
8312
8313         INT_OFF;
8314         lvp = ckzalloc(sizeof(struct localvar));
8315         if (LONE_DASH(name)) {
8316                 char *p;
8317                 p = ckmalloc(sizeof(optlist));
8318                 lvp->text = memcpy(p, optlist, sizeof(optlist));
8319                 vp = NULL;
8320         } else {
8321                 char *eq;
8322
8323                 vpp = hashvar(name);
8324                 vp = *findvar(vpp, name);
8325                 eq = strchr(name, '=');
8326                 if (vp == NULL) {
8327                         if (eq)
8328                                 setvareq(name, VSTRFIXED);
8329                         else
8330                                 setvar(name, NULL, VSTRFIXED);
8331                         vp = *vpp;      /* the new variable */
8332                         lvp->flags = VUNSET;
8333                 } else {
8334                         lvp->text = vp->text;
8335                         lvp->flags = vp->flags;
8336                         vp->flags |= VSTRFIXED|VTEXTFIXED;
8337                         if (eq)
8338                                 setvareq(name, 0);
8339                 }
8340         }
8341         lvp->vp = vp;
8342         lvp->next = localvars;
8343         localvars = lvp;
8344         INT_ON;
8345 }
8346
8347 /*
8348  * The "local" command.
8349  */
8350 static int
8351 localcmd(int argc ATTRIBUTE_UNUSED, char **argv)
8352 {
8353         char *name;
8354
8355         argv = argptr;
8356         while ((name = *argv++) != NULL) {
8357                 mklocal(name);
8358         }
8359         return 0;
8360 }
8361
8362 static int
8363 falsecmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
8364 {
8365         return 1;
8366 }
8367
8368 static int
8369 truecmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
8370 {
8371         return 0;
8372 }
8373
8374 static int
8375 execcmd(int argc ATTRIBUTE_UNUSED, char **argv)
8376 {
8377         if (argv[1]) {
8378                 iflag = 0;              /* exit on error */
8379                 mflag = 0;
8380                 optschanged();
8381                 shellexec(argv + 1, pathval(), 0);
8382         }
8383         return 0;
8384 }
8385
8386 /*
8387  * The return command.
8388  */
8389 static int
8390 returncmd(int argc ATTRIBUTE_UNUSED, char **argv)
8391 {
8392         /*
8393          * If called outside a function, do what ksh does;
8394          * skip the rest of the file.
8395          */
8396         evalskip = funcnest ? SKIPFUNC : SKIPFILE;
8397         return argv[1] ? number(argv[1]) : exitstatus;
8398 }
8399
8400 /* Forward declarations for builtintab[] */
8401 static int breakcmd(int, char **);
8402 static int dotcmd(int, char **);
8403 static int evalcmd(int, char **);
8404 static int exitcmd(int, char **);
8405 static int exportcmd(int, char **);
8406 #if ENABLE_ASH_GETOPTS
8407 static int getoptscmd(int, char **);
8408 #endif
8409 #if !ENABLE_FEATURE_SH_EXTRA_QUIET
8410 static int helpcmd(int, char **);
8411 #endif
8412 #if ENABLE_ASH_MATH_SUPPORT
8413 static int letcmd(int, char **);
8414 #endif
8415 static int readcmd(int, char **);
8416 static int setcmd(int, char **);
8417 static int shiftcmd(int, char **);
8418 static int timescmd(int, char **);
8419 static int trapcmd(int, char **);
8420 static int umaskcmd(int, char **);
8421 static int unsetcmd(int, char **);
8422 static int ulimitcmd(int, char **);
8423
8424 #define BUILTIN_NOSPEC          "0"
8425 #define BUILTIN_SPECIAL         "1"
8426 #define BUILTIN_REGULAR         "2"
8427 #define BUILTIN_SPEC_REG        "3"
8428 #define BUILTIN_ASSIGN          "4"
8429 #define BUILTIN_SPEC_ASSG       "5"
8430 #define BUILTIN_REG_ASSG        "6"
8431 #define BUILTIN_SPEC_REG_ASSG   "7"
8432
8433 /* We do not handle [[ expr ]] bashism bash-compatibly,
8434  * we make it a synonym of [ expr ].
8435  * Basically, word splitting and pathname expansion should NOT be performed
8436  * Examples:
8437  * no word splitting:     a="a b"; [[ $a = "a b" ]]; echo $? should print "0"
8438  * no pathname expansion: [[ /bin/m* = "/bin/m*" ]]; echo $? should print "0"
8439  * Additional operators:
8440  * || and && should work as -o and -a
8441  * =~ regexp match
8442  * Apart from the above, [[ expr ]] should work as [ expr ]
8443  */
8444
8445 #define echocmd   echo_main
8446 #define printfcmd printf_main
8447 #define testcmd   test_main
8448
8449 /* Keep these in proper order since it is searched via bsearch() */
8450 static const struct builtincmd builtintab[] = {
8451         { BUILTIN_SPEC_REG      ".", dotcmd },
8452         { BUILTIN_SPEC_REG      ":", truecmd },
8453 #if ENABLE_ASH_BUILTIN_TEST
8454         { BUILTIN_REGULAR       "[", testcmd },
8455 #if ENABLE_ASH_BASH_COMPAT
8456         { BUILTIN_REGULAR       "[[", testcmd },
8457 #endif
8458 #endif
8459 #if ENABLE_ASH_ALIAS
8460         { BUILTIN_REG_ASSG      "alias", aliascmd },
8461 #endif
8462 #if JOBS
8463         { BUILTIN_REGULAR       "bg", fg_bgcmd },
8464 #endif
8465         { BUILTIN_SPEC_REG      "break", breakcmd },
8466         { BUILTIN_REGULAR       "cd", cdcmd },
8467         { BUILTIN_NOSPEC        "chdir", cdcmd },
8468 #if ENABLE_ASH_CMDCMD
8469         { BUILTIN_REGULAR       "command", commandcmd },
8470 #endif
8471         { BUILTIN_SPEC_REG      "continue", breakcmd },
8472 #if ENABLE_ASH_BUILTIN_ECHO
8473         { BUILTIN_REGULAR       "echo", echocmd },
8474 #endif
8475         { BUILTIN_SPEC_REG      "eval", evalcmd },
8476         { BUILTIN_SPEC_REG      "exec", execcmd },
8477         { BUILTIN_SPEC_REG      "exit", exitcmd },
8478         { BUILTIN_SPEC_REG_ASSG "export", exportcmd },
8479         { BUILTIN_REGULAR       "false", falsecmd },
8480 #if JOBS
8481         { BUILTIN_REGULAR       "fg", fg_bgcmd },
8482 #endif
8483 #if ENABLE_ASH_GETOPTS
8484         { BUILTIN_REGULAR       "getopts", getoptscmd },
8485 #endif
8486         { BUILTIN_NOSPEC        "hash", hashcmd },
8487 #if !ENABLE_FEATURE_SH_EXTRA_QUIET
8488         { BUILTIN_NOSPEC        "help", helpcmd },
8489 #endif
8490 #if JOBS
8491         { BUILTIN_REGULAR       "jobs", jobscmd },
8492         { BUILTIN_REGULAR       "kill", killcmd },
8493 #endif
8494 #if ENABLE_ASH_MATH_SUPPORT
8495         { BUILTIN_NOSPEC        "let", letcmd },
8496 #endif
8497         { BUILTIN_ASSIGN        "local", localcmd },
8498 #if ENABLE_ASH_BUILTIN_PRINTF
8499         { BUILTIN_REGULAR       "printf", printfcmd },
8500 #endif
8501         { BUILTIN_NOSPEC        "pwd", pwdcmd },
8502         { BUILTIN_REGULAR       "read", readcmd },
8503         { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd },
8504         { BUILTIN_SPEC_REG      "return", returncmd },
8505         { BUILTIN_SPEC_REG      "set", setcmd },
8506         { BUILTIN_SPEC_REG      "shift", shiftcmd },
8507         { BUILTIN_SPEC_REG      "source", dotcmd },
8508 #if ENABLE_ASH_BUILTIN_TEST
8509         { BUILTIN_REGULAR       "test", testcmd },
8510 #endif
8511         { BUILTIN_SPEC_REG      "times", timescmd },
8512         { BUILTIN_SPEC_REG      "trap", trapcmd },
8513         { BUILTIN_REGULAR       "true", truecmd },
8514         { BUILTIN_NOSPEC        "type", typecmd },
8515         { BUILTIN_NOSPEC        "ulimit", ulimitcmd },
8516         { BUILTIN_REGULAR       "umask", umaskcmd },
8517 #if ENABLE_ASH_ALIAS
8518         { BUILTIN_REGULAR       "unalias", unaliascmd },
8519 #endif
8520         { BUILTIN_SPEC_REG      "unset", unsetcmd },
8521         { BUILTIN_REGULAR       "wait", waitcmd },
8522 };
8523
8524 /* Should match the above table! */
8525 #define COMMANDCMD (builtintab + \
8526         2 + \
8527         1 * ENABLE_ASH_BUILTIN_TEST + \
8528         1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \
8529         1 * ENABLE_ASH_ALIAS + \
8530         1 * ENABLE_ASH_JOB_CONTROL + \
8531         3)
8532 #define EXECCMD (builtintab + \
8533         2 + \
8534         1 * ENABLE_ASH_BUILTIN_TEST + \
8535         1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \
8536         1 * ENABLE_ASH_ALIAS + \
8537         1 * ENABLE_ASH_JOB_CONTROL + \
8538         3 + \
8539         1 * ENABLE_ASH_CMDCMD + \
8540         1 + \
8541         ENABLE_ASH_BUILTIN_ECHO + \
8542         1)
8543
8544 /*
8545  * Search the table of builtin commands.
8546  */
8547 static struct builtincmd *
8548 find_builtin(const char *name)
8549 {
8550         struct builtincmd *bp;
8551
8552         bp = bsearch(
8553                 name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]),
8554                 pstrcmp
8555         );
8556         return bp;
8557 }
8558
8559 /*
8560  * Execute a simple command.
8561  */
8562 static int
8563 isassignment(const char *p)
8564 {
8565         const char *q = endofname(p);
8566         if (p == q)
8567                 return 0;
8568         return *q == '=';
8569 }
8570 static int
8571 bltincmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
8572 {
8573         /* Preserve exitstatus of a previous possible redirection
8574          * as POSIX mandates */
8575         return back_exitstatus;
8576 }
8577 static void
8578 evalcommand(union node *cmd, int flags)
8579 {
8580         static const struct builtincmd null_bltin = {
8581                 "\0\0", bltincmd /* why three NULs? */
8582         };
8583         struct stackmark smark;
8584         union node *argp;
8585         struct arglist arglist;
8586         struct arglist varlist;
8587         char **argv;
8588         int argc;
8589         const struct strlist *sp;
8590         struct cmdentry cmdentry;
8591         struct job *jp;
8592         char *lastarg;
8593         const char *path;
8594         int spclbltin;
8595         int cmd_is_exec;
8596         int status;
8597         char **nargv;
8598         struct builtincmd *bcmd;
8599         int pseudovarflag = 0;
8600
8601         /* First expand the arguments. */
8602         TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
8603         setstackmark(&smark);
8604         back_exitstatus = 0;
8605
8606         cmdentry.cmdtype = CMDBUILTIN;
8607         cmdentry.u.cmd = &null_bltin;
8608         varlist.lastp = &varlist.list;
8609         *varlist.lastp = NULL;
8610         arglist.lastp = &arglist.list;
8611         *arglist.lastp = NULL;
8612
8613         argc = 0;
8614         if (cmd->ncmd.args) {
8615                 bcmd = find_builtin(cmd->ncmd.args->narg.text);
8616                 pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd);
8617         }
8618
8619         for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) {
8620                 struct strlist **spp;
8621
8622                 spp = arglist.lastp;
8623                 if (pseudovarflag && isassignment(argp->narg.text))
8624                         expandarg(argp, &arglist, EXP_VARTILDE);
8625                 else
8626                         expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
8627
8628                 for (sp = *spp; sp; sp = sp->next)
8629                         argc++;
8630         }
8631
8632         argv = nargv = stalloc(sizeof(char *) * (argc + 1));
8633         for (sp = arglist.list; sp; sp = sp->next) {
8634                 TRACE(("evalcommand arg: %s\n", sp->text));
8635                 *nargv++ = sp->text;
8636         }
8637         *nargv = NULL;
8638
8639         lastarg = NULL;
8640         if (iflag && funcnest == 0 && argc > 0)
8641                 lastarg = nargv[-1];
8642
8643         preverrout_fd = 2;
8644         expredir(cmd->ncmd.redirect);
8645         status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2);
8646
8647         path = vpath.text;
8648         for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) {
8649                 struct strlist **spp;
8650                 char *p;
8651
8652                 spp = varlist.lastp;
8653                 expandarg(argp, &varlist, EXP_VARTILDE);
8654
8655                 /*
8656                  * Modify the command lookup path, if a PATH= assignment
8657                  * is present
8658                  */
8659                 p = (*spp)->text;
8660                 if (varequal(p, path))
8661                         path = p;
8662         }
8663
8664         /* Print the command if xflag is set. */
8665         if (xflag) {
8666                 int n;
8667                 const char *p = " %s";
8668
8669                 p++;
8670                 fdprintf(preverrout_fd, p, expandstr(ps4val()));
8671
8672                 sp = varlist.list;
8673                 for (n = 0; n < 2; n++) {
8674                         while (sp) {
8675                                 fdprintf(preverrout_fd, p, sp->text);
8676                                 sp = sp->next;
8677                                 if (*p == '%') {
8678                                         p--;
8679                                 }
8680                         }
8681                         sp = arglist.list;
8682                 }
8683                 safe_write(preverrout_fd, "\n", 1);
8684         }
8685
8686         cmd_is_exec = 0;
8687         spclbltin = -1;
8688
8689         /* Now locate the command. */
8690         if (argc) {
8691                 const char *oldpath;
8692                 int cmd_flag = DO_ERR;
8693
8694                 path += 5;
8695                 oldpath = path;
8696                 for (;;) {
8697                         find_command(argv[0], &cmdentry, cmd_flag, path);
8698                         if (cmdentry.cmdtype == CMDUNKNOWN) {
8699                                 status = 127;
8700                                 flush_stderr();
8701                                 goto bail;
8702                         }
8703
8704                         /* implement bltin and command here */
8705                         if (cmdentry.cmdtype != CMDBUILTIN)
8706                                 break;
8707                         if (spclbltin < 0)
8708                                 spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd);
8709                         if (cmdentry.u.cmd == EXECCMD)
8710                                 cmd_is_exec++;
8711 #if ENABLE_ASH_CMDCMD
8712                         if (cmdentry.u.cmd == COMMANDCMD) {
8713                                 path = oldpath;
8714                                 nargv = parse_command_args(argv, &path);
8715                                 if (!nargv)
8716                                         break;
8717                                 argc -= nargv - argv;
8718                                 argv = nargv;
8719                                 cmd_flag |= DO_NOFUNC;
8720                         } else
8721 #endif
8722                                 break;
8723                 }
8724         }
8725
8726         if (status) {
8727                 /* We have a redirection error. */
8728                 if (spclbltin > 0)
8729                         raise_exception(EXERROR);
8730  bail:
8731                 exitstatus = status;
8732                 goto out;
8733         }
8734
8735         /* Execute the command. */
8736         switch (cmdentry.cmdtype) {
8737         default:
8738 #if ENABLE_FEATURE_SH_NOFORK
8739         {
8740                 /* find_command() encodes applet_no as (-2 - applet_no) */
8741                 int applet_no = (- cmdentry.u.index - 2);
8742                 if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) {
8743                         listsetvar(varlist.list, VEXPORT|VSTACK);
8744                         /* run <applet>_main() */
8745                         exitstatus = run_nofork_applet(applet_no, argv);
8746                         break;
8747                 }
8748         }
8749 #endif
8750
8751                 /* Fork off a child process if necessary. */
8752                 if (!(flags & EV_EXIT) || trap[0]) {
8753                         INT_OFF;
8754                         jp = makejob(/*cmd,*/ 1);
8755                         if (forkshell(jp, cmd, FORK_FG) != 0) {
8756                                 exitstatus = waitforjob(jp);
8757                                 INT_ON;
8758                                 break;
8759                         }
8760                         FORCE_INT_ON;
8761                 }
8762                 listsetvar(varlist.list, VEXPORT|VSTACK);
8763                 shellexec(argv, path, cmdentry.u.index);
8764                 /* NOTREACHED */
8765
8766         case CMDBUILTIN:
8767                 cmdenviron = varlist.list;
8768                 if (cmdenviron) {
8769                         struct strlist *list = cmdenviron;
8770                         int i = VNOSET;
8771                         if (spclbltin > 0 || argc == 0) {
8772                                 i = 0;
8773                                 if (cmd_is_exec && argc > 1)
8774                                         i = VEXPORT;
8775                         }
8776                         listsetvar(list, i);
8777                 }
8778                 if (evalbltin(cmdentry.u.cmd, argc, argv)) {
8779                         int exit_status;
8780                         int i = exception;
8781                         if (i == EXEXIT)
8782                                 goto raise;
8783                         exit_status = 2;
8784                         if (i == EXINT)
8785                                 exit_status = 128 + SIGINT;
8786                         if (i == EXSIG)
8787                                 exit_status = 128 + pendingsig;
8788                         exitstatus = exit_status;
8789                         if (i == EXINT || spclbltin > 0) {
8790  raise:
8791                                 longjmp(exception_handler->loc, 1);
8792                         }
8793                         FORCE_INT_ON;
8794                 }
8795                 break;
8796
8797         case CMDFUNCTION:
8798                 listsetvar(varlist.list, 0);
8799                 if (evalfun(cmdentry.u.func, argc, argv, flags))
8800                         goto raise;
8801                 break;
8802         }
8803
8804  out:
8805         popredir(cmd_is_exec);
8806         if (lastarg)
8807                 /* dsl: I think this is intended to be used to support
8808                  * '_' in 'vi' command mode during line editing...
8809                  * However I implemented that within libedit itself.
8810                  */
8811                 setvar("_", lastarg, 0);
8812         popstackmark(&smark);
8813 }
8814
8815 static int
8816 evalbltin(const struct builtincmd *cmd, int argc, char **argv)
8817 {
8818         char *volatile savecmdname;
8819         struct jmploc *volatile savehandler;
8820         struct jmploc jmploc;
8821         int i;
8822
8823         savecmdname = commandname;
8824         i = setjmp(jmploc.loc);
8825         if (i)
8826                 goto cmddone;
8827         savehandler = exception_handler;
8828         exception_handler = &jmploc;
8829         commandname = argv[0];
8830         argptr = argv + 1;
8831         optptr = NULL;                  /* initialize nextopt */
8832         exitstatus = (*cmd->builtin)(argc, argv);
8833         flush_stdout_stderr();
8834  cmddone:
8835         exitstatus |= ferror(stdout);
8836         clearerr(stdout);
8837         commandname = savecmdname;
8838 //      exsig = 0;
8839         exception_handler = savehandler;
8840
8841         return i;
8842 }
8843
8844 static int
8845 goodname(const char *p)
8846 {
8847         return !*endofname(p);
8848 }
8849
8850
8851 /*
8852  * Search for a command.  This is called before we fork so that the
8853  * location of the command will be available in the parent as well as
8854  * the child.  The check for "goodname" is an overly conservative
8855  * check that the name will not be subject to expansion.
8856  */
8857 static void
8858 prehash(union node *n)
8859 {
8860         struct cmdentry entry;
8861
8862         if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text))
8863                 find_command(n->ncmd.args->narg.text, &entry, 0, pathval());
8864 }
8865
8866
8867 /* ============ Builtin commands
8868  *
8869  * Builtin commands whose functions are closely tied to evaluation
8870  * are implemented here.
8871  */
8872
8873 /*
8874  * Handle break and continue commands.  Break, continue, and return are
8875  * all handled by setting the evalskip flag.  The evaluation routines
8876  * above all check this flag, and if it is set they start skipping
8877  * commands rather than executing them.  The variable skipcount is
8878  * the number of loops to break/continue, or the number of function
8879  * levels to return.  (The latter is always 1.)  It should probably
8880  * be an error to break out of more loops than exist, but it isn't
8881  * in the standard shell so we don't make it one here.
8882  */
8883 static int
8884 breakcmd(int argc ATTRIBUTE_UNUSED, char **argv)
8885 {
8886         int n = argv[1] ? number(argv[1]) : 1;
8887
8888         if (n <= 0)
8889                 ash_msg_and_raise_error(illnum, argv[1]);
8890         if (n > loopnest)
8891                 n = loopnest;
8892         if (n > 0) {
8893                 evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK;
8894                 skipcount = n;
8895         }
8896         return 0;
8897 }
8898
8899
8900 /* ============ input.c
8901  *
8902  * This implements the input routines used by the parser.
8903  */
8904
8905 #define EOF_NLEFT -99           /* value of parsenleft when EOF pushed back */
8906
8907 enum {
8908         INPUT_PUSH_FILE = 1,
8909         INPUT_NOFILE_OK = 2,
8910 };
8911
8912 static int plinno = 1;                  /* input line number */
8913 /* number of characters left in input buffer */
8914 static int parsenleft;                  /* copy of parsefile->nleft */
8915 static int parselleft;                  /* copy of parsefile->lleft */
8916 /* next character in input buffer */
8917 static char *parsenextc;                /* copy of parsefile->nextc */
8918
8919 static smallint checkkwd;
8920 /* values of checkkwd variable */
8921 #define CHKALIAS        0x1
8922 #define CHKKWD          0x2
8923 #define CHKNL           0x4
8924
8925 static void
8926 popstring(void)
8927 {
8928         struct strpush *sp = g_parsefile->strpush;
8929
8930         INT_OFF;
8931 #if ENABLE_ASH_ALIAS
8932         if (sp->ap) {
8933                 if (parsenextc[-1] == ' ' || parsenextc[-1] == '\t') {
8934                         checkkwd |= CHKALIAS;
8935                 }
8936                 if (sp->string != sp->ap->val) {
8937                         free(sp->string);
8938                 }
8939                 sp->ap->flag &= ~ALIASINUSE;
8940                 if (sp->ap->flag & ALIASDEAD) {
8941                         unalias(sp->ap->name);
8942                 }
8943         }
8944 #endif
8945         parsenextc = sp->prevstring;
8946         parsenleft = sp->prevnleft;
8947 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
8948         g_parsefile->strpush = sp->prev;
8949         if (sp != &(g_parsefile->basestrpush))
8950                 free(sp);
8951         INT_ON;
8952 }
8953
8954 static int
8955 preadfd(void)
8956 {
8957         int nr;
8958         char *buf =  g_parsefile->buf;
8959         parsenextc = buf;
8960
8961 #if ENABLE_FEATURE_EDITING
8962  retry:
8963         if (!iflag || g_parsefile->fd)
8964                 nr = nonblock_safe_read(g_parsefile->fd, buf, BUFSIZ - 1);
8965         else {
8966 #if ENABLE_FEATURE_TAB_COMPLETION
8967                 line_input_state->path_lookup = pathval();
8968 #endif
8969                 nr = read_line_input(cmdedit_prompt, buf, BUFSIZ, line_input_state);
8970                 if (nr == 0) {
8971                         /* Ctrl+C pressed */
8972                         if (trap[SIGINT]) {
8973                                 buf[0] = '\n';
8974                                 buf[1] = '\0';
8975                                 raise(SIGINT);
8976                                 return 1;
8977                         }
8978                         goto retry;
8979                 }
8980                 if (nr < 0 && errno == 0) {
8981                         /* Ctrl+D pressed */
8982                         nr = 0;
8983                 }
8984         }
8985 #else
8986         nr = nonblock_safe_read(g_parsefile->fd, buf, BUFSIZ - 1);
8987 #endif
8988
8989 #if 0
8990 /* nonblock_safe_read() handles this problem */
8991         if (nr < 0) {
8992                 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
8993                         int flags = fcntl(0, F_GETFL);
8994                         if (flags >= 0 && (flags & O_NONBLOCK)) {
8995                                 flags &= ~O_NONBLOCK;
8996                                 if (fcntl(0, F_SETFL, flags) >= 0) {
8997                                         out2str("sh: turning off NDELAY mode\n");
8998                                         goto retry;
8999                                 }
9000                         }
9001                 }
9002         }
9003 #endif
9004         return nr;
9005 }
9006
9007 /*
9008  * Refill the input buffer and return the next input character:
9009  *
9010  * 1) If a string was pushed back on the input, pop it;
9011  * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
9012  *    from a string so we can't refill the buffer, return EOF.
9013  * 3) If the is more stuff in this buffer, use it else call read to fill it.
9014  * 4) Process input up to the next newline, deleting nul characters.
9015  */
9016 static int
9017 preadbuffer(void)
9018 {
9019         char *q;
9020         int more;
9021         char savec;
9022
9023         while (g_parsefile->strpush) {
9024 #if ENABLE_ASH_ALIAS
9025                 if (parsenleft == -1 && g_parsefile->strpush->ap &&
9026                         parsenextc[-1] != ' ' && parsenextc[-1] != '\t') {
9027                         return PEOA;
9028                 }
9029 #endif
9030                 popstring();
9031                 if (--parsenleft >= 0)
9032                         return signed_char2int(*parsenextc++);
9033         }
9034         if (parsenleft == EOF_NLEFT || g_parsefile->buf == NULL)
9035                 return PEOF;
9036         flush_stdout_stderr();
9037
9038         more = parselleft;
9039         if (more <= 0) {
9040  again:
9041                 more = preadfd();
9042                 if (more <= 0) {
9043                         parselleft = parsenleft = EOF_NLEFT;
9044                         return PEOF;
9045                 }
9046         }
9047
9048         q = parsenextc;
9049
9050         /* delete nul characters */
9051         for (;;) {
9052                 int c;
9053
9054                 more--;
9055                 c = *q;
9056
9057                 if (!c)
9058                         memmove(q, q + 1, more);
9059                 else {
9060                         q++;
9061                         if (c == '\n') {
9062                                 parsenleft = q - parsenextc - 1;
9063                                 break;
9064                         }
9065                 }
9066
9067                 if (more <= 0) {
9068                         parsenleft = q - parsenextc - 1;
9069                         if (parsenleft < 0)
9070                                 goto again;
9071                         break;
9072                 }
9073         }
9074         parselleft = more;
9075
9076         savec = *q;
9077         *q = '\0';
9078
9079         if (vflag) {
9080                 out2str(parsenextc);
9081         }
9082
9083         *q = savec;
9084
9085         return signed_char2int(*parsenextc++);
9086 }
9087
9088 #define pgetc_as_macro() (--parsenleft >= 0? signed_char2int(*parsenextc++) : preadbuffer())
9089 static int
9090 pgetc(void)
9091 {
9092         return pgetc_as_macro();
9093 }
9094
9095 #if ENABLE_ASH_OPTIMIZE_FOR_SIZE
9096 #define pgetc_macro() pgetc()
9097 #else
9098 #define pgetc_macro() pgetc_as_macro()
9099 #endif
9100
9101 /*
9102  * Same as pgetc(), but ignores PEOA.
9103  */
9104 #if ENABLE_ASH_ALIAS
9105 static int
9106 pgetc2(void)
9107 {
9108         int c;
9109
9110         do {
9111                 c = pgetc_macro();
9112         } while (c == PEOA);
9113         return c;
9114 }
9115 #else
9116 static int
9117 pgetc2(void)
9118 {
9119         return pgetc_macro();
9120 }
9121 #endif
9122
9123 /*
9124  * Read a line from the script.
9125  */
9126 static char *
9127 pfgets(char *line, int len)
9128 {
9129         char *p = line;
9130         int nleft = len;
9131         int c;
9132
9133         while (--nleft > 0) {
9134                 c = pgetc2();
9135                 if (c == PEOF) {
9136                         if (p == line)
9137                                 return NULL;
9138                         break;
9139                 }
9140                 *p++ = c;
9141                 if (c == '\n')
9142                         break;
9143         }
9144         *p = '\0';
9145         return line;
9146 }
9147
9148 /*
9149  * Undo the last call to pgetc.  Only one character may be pushed back.
9150  * PEOF may be pushed back.
9151  */
9152 static void
9153 pungetc(void)
9154 {
9155         parsenleft++;
9156         parsenextc--;
9157 }
9158
9159 /*
9160  * Push a string back onto the input at this current parsefile level.
9161  * We handle aliases this way.
9162  */
9163 #if !ENABLE_ASH_ALIAS
9164 #define pushstring(s, ap) pushstring(s)
9165 #endif
9166 static void
9167 pushstring(char *s, struct alias *ap)
9168 {
9169         struct strpush *sp;
9170         size_t len;
9171
9172         len = strlen(s);
9173         INT_OFF;
9174 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
9175         if (g_parsefile->strpush) {
9176                 sp = ckzalloc(sizeof(struct strpush));
9177                 sp->prev = g_parsefile->strpush;
9178                 g_parsefile->strpush = sp;
9179         } else
9180                 sp = g_parsefile->strpush = &(g_parsefile->basestrpush);
9181         sp->prevstring = parsenextc;
9182         sp->prevnleft = parsenleft;
9183 #if ENABLE_ASH_ALIAS
9184         sp->ap = ap;
9185         if (ap) {
9186                 ap->flag |= ALIASINUSE;
9187                 sp->string = s;
9188         }
9189 #endif
9190         parsenextc = s;
9191         parsenleft = len;
9192         INT_ON;
9193 }
9194
9195 /*
9196  * To handle the "." command, a stack of input files is used.  Pushfile
9197  * adds a new entry to the stack and popfile restores the previous level.
9198  */
9199 static void
9200 pushfile(void)
9201 {
9202         struct parsefile *pf;
9203
9204         g_parsefile->nleft = parsenleft;
9205         g_parsefile->lleft = parselleft;
9206         g_parsefile->nextc = parsenextc;
9207         g_parsefile->linno = plinno;
9208         pf = ckzalloc(sizeof(*pf));
9209         pf->prev = g_parsefile;
9210         pf->fd = -1;
9211         /*pf->strpush = NULL; - ckzalloc did it */
9212         /*pf->basestrpush.prev = NULL;*/
9213         g_parsefile = pf;
9214 }
9215
9216 static void
9217 popfile(void)
9218 {
9219         struct parsefile *pf = g_parsefile;
9220
9221         INT_OFF;
9222         if (pf->fd >= 0)
9223                 close(pf->fd);
9224         free(pf->buf);
9225         while (pf->strpush)
9226                 popstring();
9227         g_parsefile = pf->prev;
9228         free(pf);
9229         parsenleft = g_parsefile->nleft;
9230         parselleft = g_parsefile->lleft;
9231         parsenextc = g_parsefile->nextc;
9232         plinno = g_parsefile->linno;
9233         INT_ON;
9234 }
9235
9236 /*
9237  * Return to top level.
9238  */
9239 static void
9240 popallfiles(void)
9241 {
9242         while (g_parsefile != &basepf)
9243                 popfile();
9244 }
9245
9246 /*
9247  * Close the file(s) that the shell is reading commands from.  Called
9248  * after a fork is done.
9249  */
9250 static void
9251 closescript(void)
9252 {
9253         popallfiles();
9254         if (g_parsefile->fd > 0) {
9255                 close(g_parsefile->fd);
9256                 g_parsefile->fd = 0;
9257         }
9258 }
9259
9260 /*
9261  * Like setinputfile, but takes an open file descriptor.  Call this with
9262  * interrupts off.
9263  */
9264 static void
9265 setinputfd(int fd, int push)
9266 {
9267         close_on_exec_on(fd);
9268         if (push) {
9269                 pushfile();
9270                 g_parsefile->buf = 0;
9271         }
9272         g_parsefile->fd = fd;
9273         if (g_parsefile->buf == NULL)
9274                 g_parsefile->buf = ckmalloc(IBUFSIZ);
9275         parselleft = parsenleft = 0;
9276         plinno = 1;
9277 }
9278
9279 /*
9280  * Set the input to take input from a file.  If push is set, push the
9281  * old input onto the stack first.
9282  */
9283 static int
9284 setinputfile(const char *fname, int flags)
9285 {
9286         int fd;
9287         int fd2;
9288
9289         INT_OFF;
9290         fd = open(fname, O_RDONLY);
9291         if (fd < 0) {
9292                 if (flags & INPUT_NOFILE_OK)
9293                         goto out;
9294                 ash_msg_and_raise_error("can't open %s", fname);
9295         }
9296         if (fd < 10) {
9297                 fd2 = copyfd(fd, 10);
9298                 close(fd);
9299                 if (fd2 < 0)
9300                         ash_msg_and_raise_error("out of file descriptors");
9301                 fd = fd2;
9302         }
9303         setinputfd(fd, flags & INPUT_PUSH_FILE);
9304  out:
9305         INT_ON;
9306         return fd;
9307 }
9308
9309 /*
9310  * Like setinputfile, but takes input from a string.
9311  */
9312 static void
9313 setinputstring(char *string)
9314 {
9315         INT_OFF;
9316         pushfile();
9317         parsenextc = string;
9318         parsenleft = strlen(string);
9319         g_parsefile->buf = NULL;
9320         plinno = 1;
9321         INT_ON;
9322 }
9323
9324
9325 /* ============ mail.c
9326  *
9327  * Routines to check for mail.
9328  */
9329
9330 #if ENABLE_ASH_MAIL
9331
9332 #define MAXMBOXES 10
9333
9334 /* times of mailboxes */
9335 static time_t mailtime[MAXMBOXES];
9336 /* Set if MAIL or MAILPATH is changed. */
9337 static smallint mail_var_path_changed;
9338
9339 /*
9340  * Print appropriate message(s) if mail has arrived.
9341  * If mail_var_path_changed is set,
9342  * then the value of MAIL has mail_var_path_changed,
9343  * so we just update the values.
9344  */
9345 static void
9346 chkmail(void)
9347 {
9348         const char *mpath;
9349         char *p;
9350         char *q;
9351         time_t *mtp;
9352         struct stackmark smark;
9353         struct stat statb;
9354
9355         setstackmark(&smark);
9356         mpath = mpathset() ? mpathval() : mailval();
9357         for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) {
9358                 p = padvance(&mpath, nullstr);
9359                 if (p == NULL)
9360                         break;
9361                 if (*p == '\0')
9362                         continue;
9363                 for (q = p; *q; q++)
9364                         continue;
9365 #if DEBUG
9366                 if (q[-1] != '/')
9367                         abort();
9368 #endif
9369                 q[-1] = '\0';                   /* delete trailing '/' */
9370                 if (stat(p, &statb) < 0) {
9371                         *mtp = 0;
9372                         continue;
9373                 }
9374                 if (!mail_var_path_changed && statb.st_mtime != *mtp) {
9375                         fprintf(
9376                                 stderr, snlfmt,
9377                                 pathopt ? pathopt : "you have mail"
9378                         );
9379                 }
9380                 *mtp = statb.st_mtime;
9381         }
9382         mail_var_path_changed = 0;
9383         popstackmark(&smark);
9384 }
9385
9386 static void
9387 changemail(const char *val ATTRIBUTE_UNUSED)
9388 {
9389         mail_var_path_changed = 1;
9390 }
9391
9392 #endif /* ASH_MAIL */
9393
9394
9395 /* ============ ??? */
9396
9397 /*
9398  * Set the shell parameters.
9399  */
9400 static void
9401 setparam(char **argv)
9402 {
9403         char **newparam;
9404         char **ap;
9405         int nparam;
9406
9407         for (nparam = 0; argv[nparam]; nparam++)
9408                 continue;
9409         ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap));
9410         while (*argv) {
9411                 *ap++ = ckstrdup(*argv++);
9412         }
9413         *ap = NULL;
9414         freeparam(&shellparam);
9415         shellparam.malloced = 1;
9416         shellparam.nparam = nparam;
9417         shellparam.p = newparam;
9418 #if ENABLE_ASH_GETOPTS
9419         shellparam.optind = 1;
9420         shellparam.optoff = -1;
9421 #endif
9422 }
9423
9424 /*
9425  * Process shell options.  The global variable argptr contains a pointer
9426  * to the argument list; we advance it past the options.
9427  *
9428  * SUSv3 section 2.8.1 "Consequences of Shell Errors" says:
9429  * For a non-interactive shell, an error condition encountered
9430  * by a special built-in ... shall cause the shell to write a diagnostic message
9431  * to standard error and exit as shown in the following table:
9432  * Error                                           Special Built-In
9433  * ...
9434  * Utility syntax error (option or operand error)  Shall exit
9435  * ...
9436  * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142)
9437  * we see that bash does not do that (set "finishes" with error code 1 instead,
9438  * and shell continues), and people rely on this behavior!
9439  * Testcase:
9440  * set -o barfoo 2>/dev/null
9441  * echo $?
9442  *
9443  * Oh well. Let's mimic that.
9444  */
9445 static int
9446 plus_minus_o(char *name, int val)
9447 {
9448         int i;
9449
9450         if (name) {
9451                 for (i = 0; i < NOPTS; i++) {
9452                         if (strcmp(name, optnames(i)) == 0) {
9453                                 optlist[i] = val;
9454                                 return 0;
9455                         }
9456                 }
9457                 ash_msg("illegal option %co %s", val ? '-' : '+', name);
9458                 return 1;
9459         }
9460         for (i = 0; i < NOPTS; i++) {
9461                 if (val) {
9462                         out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off");
9463                 } else {
9464                         out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i));
9465                 }
9466         }
9467         return 0;
9468 }
9469 static void
9470 setoption(int flag, int val)
9471 {
9472         int i;
9473
9474         for (i = 0; i < NOPTS; i++) {
9475                 if (optletters(i) == flag) {
9476                         optlist[i] = val;
9477                         return;
9478                 }
9479         }
9480         ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag);
9481         /* NOTREACHED */
9482 }
9483 static int
9484 options(int cmdline)
9485 {
9486         char *p;
9487         int val;
9488         int c;
9489
9490         if (cmdline)
9491                 minusc = NULL;
9492         while ((p = *argptr) != NULL) {
9493                 c = *p++;
9494                 if (c != '-' && c != '+')
9495                         break;
9496                 argptr++;
9497                 val = 0; /* val = 0 if c == '+' */
9498                 if (c == '-') {
9499                         val = 1;
9500                         if (p[0] == '\0' || LONE_DASH(p)) {
9501                                 if (!cmdline) {
9502                                         /* "-" means turn off -x and -v */
9503                                         if (p[0] == '\0')
9504                                                 xflag = vflag = 0;
9505                                         /* "--" means reset params */
9506                                         else if (*argptr == NULL)
9507                                                 setparam(argptr);
9508                                 }
9509                                 break;    /* "-" or  "--" terminates options */
9510                         }
9511                 }
9512                 /* first char was + or - */
9513                 while ((c = *p++) != '\0') {
9514                         /* bash 3.2 indeed handles -c CMD and +c CMD the same */
9515                         if (c == 'c' && cmdline) {
9516                                 minusc = p;     /* command is after shell args */
9517                         } else if (c == 'o') {
9518                                 if (plus_minus_o(*argptr, val)) {
9519                                         /* it already printed err message */
9520                                         return 1; /* error */
9521                                 }
9522                                 if (*argptr)
9523                                         argptr++;
9524                         } else if (cmdline && (c == 'l')) { /* -l or +l == --login */
9525                                 isloginsh = 1;
9526                         /* bash does not accept +-login, we also won't */
9527                         } else if (cmdline && val && (c == '-')) { /* long options */
9528                                 if (strcmp(p, "login") == 0)
9529                                         isloginsh = 1;
9530                                 break;
9531                         } else {
9532                                 setoption(c, val);
9533                         }
9534                 }
9535         }
9536         return 0;
9537 }
9538
9539 /*
9540  * The shift builtin command.
9541  */
9542 static int
9543 shiftcmd(int argc ATTRIBUTE_UNUSED, char **argv)
9544 {
9545         int n;
9546         char **ap1, **ap2;
9547
9548         n = 1;
9549         if (argv[1])
9550                 n = number(argv[1]);
9551         if (n > shellparam.nparam)
9552                 n = shellparam.nparam;
9553         INT_OFF;
9554         shellparam.nparam -= n;
9555         for (ap1 = shellparam.p; --n >= 0; ap1++) {
9556                 if (shellparam.malloced)
9557                         free(*ap1);
9558         }
9559         ap2 = shellparam.p;
9560         while ((*ap2++ = *ap1++) != NULL)
9561                 continue;
9562 #if ENABLE_ASH_GETOPTS
9563         shellparam.optind = 1;
9564         shellparam.optoff = -1;
9565 #endif
9566         INT_ON;
9567         return 0;
9568 }
9569
9570 /*
9571  * POSIX requires that 'set' (but not export or readonly) output the
9572  * variables in lexicographic order - by the locale's collating order (sigh).
9573  * Maybe we could keep them in an ordered balanced binary tree
9574  * instead of hashed lists.
9575  * For now just roll 'em through qsort for printing...
9576  */
9577 static int
9578 showvars(const char *sep_prefix, int on, int off)
9579 {
9580         const char *sep;
9581         char **ep, **epend;
9582
9583         ep = listvars(on, off, &epend);
9584         qsort(ep, epend - ep, sizeof(char *), vpcmp);
9585
9586         sep = *sep_prefix ? " " : sep_prefix;
9587
9588         for (; ep < epend; ep++) {
9589                 const char *p;
9590                 const char *q;
9591
9592                 p = strchrnul(*ep, '=');
9593                 q = nullstr;
9594                 if (*p)
9595                         q = single_quote(++p);
9596                 out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q);
9597         }
9598         return 0;
9599 }
9600
9601 /*
9602  * The set command builtin.
9603  */
9604 static int
9605 setcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
9606 {
9607         int retval;
9608
9609         if (!argv[1])
9610                 return showvars(nullstr, 0, VUNSET);
9611         INT_OFF;
9612         retval = 1;
9613         if (!options(0)) { /* if no parse error... */
9614                 retval = 0;
9615                 optschanged();
9616                 if (*argptr != NULL) {
9617                         setparam(argptr);
9618                 }
9619         }
9620         INT_ON;
9621         return retval;
9622 }
9623
9624 #if ENABLE_ASH_RANDOM_SUPPORT
9625 /* Roughly copied from bash.. */
9626 static void
9627 change_random(const char *value)
9628 {
9629         if (value == NULL) {
9630                 /* "get", generate */
9631                 char buf[16];
9632
9633                 rseed = rseed * 1103515245 + 12345;
9634                 sprintf(buf, "%d", (unsigned int)((rseed & 32767)));
9635                 /* set without recursion */
9636                 setvar(vrandom.text, buf, VNOFUNC);
9637                 vrandom.flags &= ~VNOFUNC;
9638         } else {
9639                 /* set/reset */
9640                 rseed = strtoul(value, (char **)NULL, 10);
9641         }
9642 }
9643 #endif
9644
9645 #if ENABLE_ASH_GETOPTS
9646 static int
9647 getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *optoff)
9648 {
9649         char *p, *q;
9650         char c = '?';
9651         int done = 0;
9652         int err = 0;
9653         char s[12];
9654         char **optnext;
9655
9656         if (*param_optind < 1)
9657                 return 1;
9658         optnext = optfirst + *param_optind - 1;
9659
9660         if (*param_optind <= 1 || *optoff < 0 || (int)strlen(optnext[-1]) < *optoff)
9661                 p = NULL;
9662         else
9663                 p = optnext[-1] + *optoff;
9664         if (p == NULL || *p == '\0') {
9665                 /* Current word is done, advance */
9666                 p = *optnext;
9667                 if (p == NULL || *p != '-' || *++p == '\0') {
9668  atend:
9669                         p = NULL;
9670                         done = 1;
9671                         goto out;
9672                 }
9673                 optnext++;
9674                 if (LONE_DASH(p))        /* check for "--" */
9675                         goto atend;
9676         }
9677
9678         c = *p++;
9679         for (q = optstr; *q != c;) {
9680                 if (*q == '\0') {
9681                         if (optstr[0] == ':') {
9682                                 s[0] = c;
9683                                 s[1] = '\0';
9684                                 err |= setvarsafe("OPTARG", s, 0);
9685                         } else {
9686                                 fprintf(stderr, "Illegal option -%c\n", c);
9687                                 unsetvar("OPTARG");
9688                         }
9689                         c = '?';
9690                         goto out;
9691                 }
9692                 if (*++q == ':')
9693                         q++;
9694         }
9695
9696         if (*++q == ':') {
9697                 if (*p == '\0' && (p = *optnext) == NULL) {
9698                         if (optstr[0] == ':') {
9699                                 s[0] = c;
9700                                 s[1] = '\0';
9701                                 err |= setvarsafe("OPTARG", s, 0);
9702                                 c = ':';
9703                         } else {
9704                                 fprintf(stderr, "No arg for -%c option\n", c);
9705                                 unsetvar("OPTARG");
9706                                 c = '?';
9707                         }
9708                         goto out;
9709                 }
9710
9711                 if (p == *optnext)
9712                         optnext++;
9713                 err |= setvarsafe("OPTARG", p, 0);
9714                 p = NULL;
9715         } else
9716                 err |= setvarsafe("OPTARG", nullstr, 0);
9717  out:
9718         *optoff = p ? p - *(optnext - 1) : -1;
9719         *param_optind = optnext - optfirst + 1;
9720         fmtstr(s, sizeof(s), "%d", *param_optind);
9721         err |= setvarsafe("OPTIND", s, VNOFUNC);
9722         s[0] = c;
9723         s[1] = '\0';
9724         err |= setvarsafe(optvar, s, 0);
9725         if (err) {
9726                 *param_optind = 1;
9727                 *optoff = -1;
9728                 flush_stdout_stderr();
9729                 raise_exception(EXERROR);
9730         }
9731         return done;
9732 }
9733
9734 /*
9735  * The getopts builtin.  Shellparam.optnext points to the next argument
9736  * to be processed.  Shellparam.optptr points to the next character to
9737  * be processed in the current argument.  If shellparam.optnext is NULL,
9738  * then it's the first time getopts has been called.
9739  */
9740 static int
9741 getoptscmd(int argc, char **argv)
9742 {
9743         char **optbase;
9744
9745         if (argc < 3)
9746                 ash_msg_and_raise_error("usage: getopts optstring var [arg]");
9747         if (argc == 3) {
9748                 optbase = shellparam.p;
9749                 if (shellparam.optind > shellparam.nparam + 1) {
9750                         shellparam.optind = 1;
9751                         shellparam.optoff = -1;
9752                 }
9753         } else {
9754                 optbase = &argv[3];
9755                 if (shellparam.optind > argc - 2) {
9756                         shellparam.optind = 1;
9757                         shellparam.optoff = -1;
9758                 }
9759         }
9760
9761         return getopts(argv[1], argv[2], optbase, &shellparam.optind,
9762                         &shellparam.optoff);
9763 }
9764 #endif /* ASH_GETOPTS */
9765
9766
9767 /* ============ Shell parser */
9768
9769 struct heredoc {
9770         struct heredoc *next;   /* next here document in list */
9771         union node *here;       /* redirection node */
9772         char *eofmark;          /* string indicating end of input */
9773         smallint striptabs;     /* if set, strip leading tabs */
9774 };
9775
9776 static smallint tokpushback;           /* last token pushed back */
9777 static smallint parsebackquote;        /* nonzero if we are inside backquotes */
9778 static smallint quoteflag;             /* set if (part of) last token was quoted */
9779 static token_id_t lasttoken;           /* last token read (integer id Txxx) */
9780 static struct heredoc *heredoclist;    /* list of here documents to read */
9781 static char *wordtext;                 /* text of last word returned by readtoken */
9782 static struct nodelist *backquotelist;
9783 static union node *redirnode;
9784 static struct heredoc *heredoc;
9785 /*
9786  * NEOF is returned by parsecmd when it encounters an end of file.  It
9787  * must be distinct from NULL, so we use the address of a variable that
9788  * happens to be handy.
9789  */
9790 #define NEOF ((union node *)&tokpushback)
9791
9792 static void raise_error_syntax(const char *) ATTRIBUTE_NORETURN;
9793 static void
9794 raise_error_syntax(const char *msg)
9795 {
9796         ash_msg_and_raise_error("syntax error: %s", msg);
9797         /* NOTREACHED */
9798 }
9799
9800 /*
9801  * Called when an unexpected token is read during the parse.  The argument
9802  * is the token that is expected, or -1 if more than one type of token can
9803  * occur at this point.
9804  */
9805 static void raise_error_unexpected_syntax(int) ATTRIBUTE_NORETURN;
9806 static void
9807 raise_error_unexpected_syntax(int token)
9808 {
9809         char msg[64];
9810         int l;
9811
9812         l = sprintf(msg, "%s unexpected", tokname(lasttoken));
9813         if (token >= 0)
9814                 sprintf(msg + l, " (expecting %s)", tokname(token));
9815         raise_error_syntax(msg);
9816         /* NOTREACHED */
9817 }
9818
9819 #define EOFMARKLEN 79
9820
9821 /* parsing is heavily cross-recursive, need these forward decls */
9822 static union node *andor(void);
9823 static union node *pipeline(void);
9824 static union node *parse_command(void);
9825 static void parseheredoc(void);
9826 static char peektoken(void);
9827 static int readtoken(void);
9828
9829 static union node *
9830 list(int nlflag)
9831 {
9832         union node *n1, *n2, *n3;
9833         int tok;
9834
9835         checkkwd = CHKNL | CHKKWD | CHKALIAS;
9836         if (nlflag == 2 && peektoken())
9837                 return NULL;
9838         n1 = NULL;
9839         for (;;) {
9840                 n2 = andor();
9841                 tok = readtoken();
9842                 if (tok == TBACKGND) {
9843                         if (n2->type == NPIPE) {
9844                                 n2->npipe.backgnd = 1;
9845                         } else {
9846                                 if (n2->type != NREDIR) {
9847                                         n3 = stzalloc(sizeof(struct nredir));
9848                                         n3->nredir.n = n2;
9849                                         /*n3->nredir.redirect = NULL; - stzalloc did it */
9850                                         n2 = n3;
9851                                 }
9852                                 n2->type = NBACKGND;
9853                         }
9854                 }
9855                 if (n1 == NULL) {
9856                         n1 = n2;
9857                 } else {
9858                         n3 = stzalloc(sizeof(struct nbinary));
9859                         n3->type = NSEMI;
9860                         n3->nbinary.ch1 = n1;
9861                         n3->nbinary.ch2 = n2;
9862                         n1 = n3;
9863                 }
9864                 switch (tok) {
9865                 case TBACKGND:
9866                 case TSEMI:
9867                         tok = readtoken();
9868                         /* fall through */
9869                 case TNL:
9870                         if (tok == TNL) {
9871                                 parseheredoc();
9872                                 if (nlflag == 1)
9873                                         return n1;
9874                         } else {
9875                                 tokpushback = 1;
9876                         }
9877                         checkkwd = CHKNL | CHKKWD | CHKALIAS;
9878                         if (peektoken())
9879                                 return n1;
9880                         break;
9881                 case TEOF:
9882                         if (heredoclist)
9883                                 parseheredoc();
9884                         else
9885                                 pungetc();              /* push back EOF on input */
9886                         return n1;
9887                 default:
9888                         if (nlflag == 1)
9889                                 raise_error_unexpected_syntax(-1);
9890                         tokpushback = 1;
9891                         return n1;
9892                 }
9893         }
9894 }
9895
9896 static union node *
9897 andor(void)
9898 {
9899         union node *n1, *n2, *n3;
9900         int t;
9901
9902         n1 = pipeline();
9903         for (;;) {
9904                 t = readtoken();
9905                 if (t == TAND) {
9906                         t = NAND;
9907                 } else if (t == TOR) {
9908                         t = NOR;
9909                 } else {
9910                         tokpushback = 1;
9911                         return n1;
9912                 }
9913                 checkkwd = CHKNL | CHKKWD | CHKALIAS;
9914                 n2 = pipeline();
9915                 n3 = stzalloc(sizeof(struct nbinary));
9916                 n3->type = t;
9917                 n3->nbinary.ch1 = n1;
9918                 n3->nbinary.ch2 = n2;
9919                 n1 = n3;
9920         }
9921 }
9922
9923 static union node *
9924 pipeline(void)
9925 {
9926         union node *n1, *n2, *pipenode;
9927         struct nodelist *lp, *prev;
9928         int negate;
9929
9930         negate = 0;
9931         TRACE(("pipeline: entered\n"));
9932         if (readtoken() == TNOT) {
9933                 negate = !negate;
9934                 checkkwd = CHKKWD | CHKALIAS;
9935         } else
9936                 tokpushback = 1;
9937         n1 = parse_command();
9938         if (readtoken() == TPIPE) {
9939                 pipenode = stzalloc(sizeof(struct npipe));
9940                 pipenode->type = NPIPE;
9941                 /*pipenode->npipe.backgnd = 0; - stzalloc did it */
9942                 lp = stzalloc(sizeof(struct nodelist));
9943                 pipenode->npipe.cmdlist = lp;
9944                 lp->n = n1;
9945                 do {
9946                         prev = lp;
9947                         lp = stzalloc(sizeof(struct nodelist));
9948                         checkkwd = CHKNL | CHKKWD | CHKALIAS;
9949                         lp->n = parse_command();
9950                         prev->next = lp;
9951                 } while (readtoken() == TPIPE);
9952                 lp->next = NULL;
9953                 n1 = pipenode;
9954         }
9955         tokpushback = 1;
9956         if (negate) {
9957                 n2 = stzalloc(sizeof(struct nnot));
9958                 n2->type = NNOT;
9959                 n2->nnot.com = n1;
9960                 return n2;
9961         }
9962         return n1;
9963 }
9964
9965 static union node *
9966 makename(void)
9967 {
9968         union node *n;
9969
9970         n = stzalloc(sizeof(struct narg));
9971         n->type = NARG;
9972         /*n->narg.next = NULL; - stzalloc did it */
9973         n->narg.text = wordtext;
9974         n->narg.backquote = backquotelist;
9975         return n;
9976 }
9977
9978 static void
9979 fixredir(union node *n, const char *text, int err)
9980 {
9981         TRACE(("Fix redir %s %d\n", text, err));
9982         if (!err)
9983                 n->ndup.vname = NULL;
9984
9985         if (isdigit(text[0]) && text[1] == '\0')
9986                 n->ndup.dupfd = text[0] - '0';
9987         else if (LONE_DASH(text))
9988                 n->ndup.dupfd = -1;
9989         else {
9990                 if (err)
9991                         raise_error_syntax("Bad fd number");
9992                 n->ndup.vname = makename();
9993         }
9994 }
9995
9996 /*
9997  * Returns true if the text contains nothing to expand (no dollar signs
9998  * or backquotes).
9999  */
10000 static int
10001 noexpand(char *text)
10002 {
10003         char *p;
10004         char c;
10005
10006         p = text;
10007         while ((c = *p++) != '\0') {
10008                 if (c == CTLQUOTEMARK)
10009                         continue;
10010                 if (c == CTLESC)
10011                         p++;
10012                 else if (SIT(c, BASESYNTAX) == CCTL)
10013                         return 0;
10014         }
10015         return 1;
10016 }
10017
10018 static void
10019 parsefname(void)
10020 {
10021         union node *n = redirnode;
10022
10023         if (readtoken() != TWORD)
10024                 raise_error_unexpected_syntax(-1);
10025         if (n->type == NHERE) {
10026                 struct heredoc *here = heredoc;
10027                 struct heredoc *p;
10028                 int i;
10029
10030                 if (quoteflag == 0)
10031                         n->type = NXHERE;
10032                 TRACE(("Here document %d\n", n->type));
10033                 if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
10034                         raise_error_syntax("Illegal eof marker for << redirection");
10035                 rmescapes(wordtext);
10036                 here->eofmark = wordtext;
10037                 here->next = NULL;
10038                 if (heredoclist == NULL)
10039                         heredoclist = here;
10040                 else {
10041                         for (p = heredoclist; p->next; p = p->next)
10042                                 continue;
10043                         p->next = here;
10044                 }
10045         } else if (n->type == NTOFD || n->type == NFROMFD) {
10046                 fixredir(n, wordtext, 0);
10047         } else {
10048                 n->nfile.fname = makename();
10049         }
10050 }
10051
10052 static union node *
10053 simplecmd(void)
10054 {
10055         union node *args, **app;
10056         union node *n = NULL;
10057         union node *vars, **vpp;
10058         union node **rpp, *redir;
10059         int savecheckkwd;
10060 #if ENABLE_ASH_BASH_COMPAT
10061         smallint double_brackets_flag = 0;
10062 #endif
10063
10064         args = NULL;
10065         app = &args;
10066         vars = NULL;
10067         vpp = &vars;
10068         redir = NULL;
10069         rpp = &redir;
10070
10071         savecheckkwd = CHKALIAS;
10072         for (;;) {
10073                 int t;
10074                 checkkwd = savecheckkwd;
10075                 t = readtoken();
10076                 switch (t) {
10077 #if ENABLE_ASH_BASH_COMPAT
10078                 case TAND: /* "&&" */
10079                 case TOR: /* "||" */
10080                         if (!double_brackets_flag) {
10081                                 tokpushback = 1;
10082                                 goto out;
10083                         }
10084                         wordtext = (char *) (t == TAND ? "-a" : "-o");
10085 #endif
10086                 case TWORD:
10087                         n = stzalloc(sizeof(struct narg));
10088                         n->type = NARG;
10089                         /*n->narg.next = NULL; - stzalloc did it */
10090                         n->narg.text = wordtext;
10091 #if ENABLE_ASH_BASH_COMPAT
10092                         if (strcmp("[[", wordtext) == 0)
10093                                 double_brackets_flag = 1;
10094                         else if (strcmp("]]", wordtext) == 0)
10095                                 double_brackets_flag = 0;
10096 #endif
10097                         n->narg.backquote = backquotelist;
10098                         if (savecheckkwd && isassignment(wordtext)) {
10099                                 *vpp = n;
10100                                 vpp = &n->narg.next;
10101                         } else {
10102                                 *app = n;
10103                                 app = &n->narg.next;
10104                                 savecheckkwd = 0;
10105                         }
10106                         break;
10107                 case TREDIR:
10108                         *rpp = n = redirnode;
10109                         rpp = &n->nfile.next;
10110                         parsefname();   /* read name of redirection file */
10111                         break;
10112                 case TLP:
10113                         if (args && app == &args->narg.next
10114                          && !vars && !redir
10115                         ) {
10116                                 struct builtincmd *bcmd;
10117                                 const char *name;
10118
10119                                 /* We have a function */
10120                                 if (readtoken() != TRP)
10121                                         raise_error_unexpected_syntax(TRP);
10122                                 name = n->narg.text;
10123                                 if (!goodname(name)
10124                                  || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd))
10125                                 ) {
10126                                         raise_error_syntax("Bad function name");
10127                                 }
10128                                 n->type = NDEFUN;
10129                                 checkkwd = CHKNL | CHKKWD | CHKALIAS;
10130                                 n->narg.next = parse_command();
10131                                 return n;
10132                         }
10133                         /* fall through */
10134                 default:
10135                         tokpushback = 1;
10136                         goto out;
10137                 }
10138         }
10139  out:
10140         *app = NULL;
10141         *vpp = NULL;
10142         *rpp = NULL;
10143         n = stzalloc(sizeof(struct ncmd));
10144         n->type = NCMD;
10145         n->ncmd.args = args;
10146         n->ncmd.assign = vars;
10147         n->ncmd.redirect = redir;
10148         return n;
10149 }
10150
10151 static union node *
10152 parse_command(void)
10153 {
10154         union node *n1, *n2;
10155         union node *ap, **app;
10156         union node *cp, **cpp;
10157         union node *redir, **rpp;
10158         union node **rpp2;
10159         int t;
10160
10161         redir = NULL;
10162         rpp2 = &redir;
10163
10164         switch (readtoken()) {
10165         default:
10166                 raise_error_unexpected_syntax(-1);
10167                 /* NOTREACHED */
10168         case TIF:
10169                 n1 = stzalloc(sizeof(struct nif));
10170                 n1->type = NIF;
10171                 n1->nif.test = list(0);
10172                 if (readtoken() != TTHEN)
10173                         raise_error_unexpected_syntax(TTHEN);
10174                 n1->nif.ifpart = list(0);
10175                 n2 = n1;
10176                 while (readtoken() == TELIF) {
10177                         n2->nif.elsepart = stzalloc(sizeof(struct nif));
10178                         n2 = n2->nif.elsepart;
10179                         n2->type = NIF;
10180                         n2->nif.test = list(0);
10181                         if (readtoken() != TTHEN)
10182                                 raise_error_unexpected_syntax(TTHEN);
10183                         n2->nif.ifpart = list(0);
10184                 }
10185                 if (lasttoken == TELSE)
10186                         n2->nif.elsepart = list(0);
10187                 else {
10188                         n2->nif.elsepart = NULL;
10189                         tokpushback = 1;
10190                 }
10191                 t = TFI;
10192                 break;
10193         case TWHILE:
10194         case TUNTIL: {
10195                 int got;
10196                 n1 = stzalloc(sizeof(struct nbinary));
10197                 n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL;
10198                 n1->nbinary.ch1 = list(0);
10199                 got = readtoken();
10200                 if (got != TDO) {
10201                         TRACE(("expecting DO got %s %s\n", tokname(got),
10202                                         got == TWORD ? wordtext : ""));
10203                         raise_error_unexpected_syntax(TDO);
10204                 }
10205                 n1->nbinary.ch2 = list(0);
10206                 t = TDONE;
10207                 break;
10208         }
10209         case TFOR:
10210                 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
10211                         raise_error_syntax("Bad for loop variable");
10212                 n1 = stzalloc(sizeof(struct nfor));
10213                 n1->type = NFOR;
10214                 n1->nfor.var = wordtext;
10215                 checkkwd = CHKKWD | CHKALIAS;
10216                 if (readtoken() == TIN) {
10217                         app = &ap;
10218                         while (readtoken() == TWORD) {
10219                                 n2 = stzalloc(sizeof(struct narg));
10220                                 n2->type = NARG;
10221                                 /*n2->narg.next = NULL; - stzalloc did it */
10222                                 n2->narg.text = wordtext;
10223                                 n2->narg.backquote = backquotelist;
10224                                 *app = n2;
10225                                 app = &n2->narg.next;
10226                         }
10227                         *app = NULL;
10228                         n1->nfor.args = ap;
10229                         if (lasttoken != TNL && lasttoken != TSEMI)
10230                                 raise_error_unexpected_syntax(-1);
10231                 } else {
10232                         n2 = stzalloc(sizeof(struct narg));
10233                         n2->type = NARG;
10234                         /*n2->narg.next = NULL; - stzalloc did it */
10235                         n2->narg.text = (char *)dolatstr;
10236                         /*n2->narg.backquote = NULL;*/
10237                         n1->nfor.args = n2;
10238                         /*
10239                          * Newline or semicolon here is optional (but note
10240                          * that the original Bourne shell only allowed NL).
10241                          */
10242                         if (lasttoken != TNL && lasttoken != TSEMI)
10243                                 tokpushback = 1;
10244                 }
10245                 checkkwd = CHKNL | CHKKWD | CHKALIAS;
10246                 if (readtoken() != TDO)
10247                         raise_error_unexpected_syntax(TDO);
10248                 n1->nfor.body = list(0);
10249                 t = TDONE;
10250                 break;
10251         case TCASE:
10252                 n1 = stzalloc(sizeof(struct ncase));
10253                 n1->type = NCASE;
10254                 if (readtoken() != TWORD)
10255                         raise_error_unexpected_syntax(TWORD);
10256                 n1->ncase.expr = n2 = stzalloc(sizeof(struct narg));
10257                 n2->type = NARG;
10258                 /*n2->narg.next = NULL; - stzalloc did it */
10259                 n2->narg.text = wordtext;
10260                 n2->narg.backquote = backquotelist;
10261                 do {
10262                         checkkwd = CHKKWD | CHKALIAS;
10263                 } while (readtoken() == TNL);
10264                 if (lasttoken != TIN)
10265                         raise_error_unexpected_syntax(TIN);
10266                 cpp = &n1->ncase.cases;
10267  next_case:
10268                 checkkwd = CHKNL | CHKKWD;
10269                 t = readtoken();
10270                 while (t != TESAC) {
10271                         if (lasttoken == TLP)
10272                                 readtoken();
10273                         *cpp = cp = stzalloc(sizeof(struct nclist));
10274                         cp->type = NCLIST;
10275                         app = &cp->nclist.pattern;
10276                         for (;;) {
10277                                 *app = ap = stzalloc(sizeof(struct narg));
10278                                 ap->type = NARG;
10279                                 /*ap->narg.next = NULL; - stzalloc did it */
10280                                 ap->narg.text = wordtext;
10281                                 ap->narg.backquote = backquotelist;
10282                                 if (readtoken() != TPIPE)
10283                                         break;
10284                                 app = &ap->narg.next;
10285                                 readtoken();
10286                         }
10287                         //ap->narg.next = NULL;
10288                         if (lasttoken != TRP)
10289                                 raise_error_unexpected_syntax(TRP);
10290                         cp->nclist.body = list(2);
10291
10292                         cpp = &cp->nclist.next;
10293
10294                         checkkwd = CHKNL | CHKKWD;
10295                         t = readtoken();
10296                         if (t != TESAC) {
10297                                 if (t != TENDCASE)
10298                                         raise_error_unexpected_syntax(TENDCASE);
10299                                 goto next_case;
10300                         }
10301                 }
10302                 *cpp = NULL;
10303                 goto redir;
10304         case TLP:
10305                 n1 = stzalloc(sizeof(struct nredir));
10306                 n1->type = NSUBSHELL;
10307                 n1->nredir.n = list(0);
10308                 /*n1->nredir.redirect = NULL; - stzalloc did it */
10309                 t = TRP;
10310                 break;
10311         case TBEGIN:
10312                 n1 = list(0);
10313                 t = TEND;
10314                 break;
10315         case TWORD:
10316         case TREDIR:
10317                 tokpushback = 1;
10318                 return simplecmd();
10319         }
10320
10321         if (readtoken() != t)
10322                 raise_error_unexpected_syntax(t);
10323
10324  redir:
10325         /* Now check for redirection which may follow command */
10326         checkkwd = CHKKWD | CHKALIAS;
10327         rpp = rpp2;
10328         while (readtoken() == TREDIR) {
10329                 *rpp = n2 = redirnode;
10330                 rpp = &n2->nfile.next;
10331                 parsefname();
10332         }
10333         tokpushback = 1;
10334         *rpp = NULL;
10335         if (redir) {
10336                 if (n1->type != NSUBSHELL) {
10337                         n2 = stzalloc(sizeof(struct nredir));
10338                         n2->type = NREDIR;
10339                         n2->nredir.n = n1;
10340                         n1 = n2;
10341                 }
10342                 n1->nredir.redirect = redir;
10343         }
10344         return n1;
10345 }
10346
10347 #if ENABLE_ASH_BASH_COMPAT
10348 static int decode_dollar_squote(void)
10349 {
10350         static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567";
10351         int c, cnt;
10352         char *p;
10353         char buf[4];
10354
10355         c = pgetc();
10356         p = strchr(C_escapes, c);
10357         if (p) {
10358                 buf[0] = c;
10359                 p = buf;
10360                 cnt = 3;
10361                 if ((unsigned char)(c - '0') <= 7) { /* \ooo */
10362                         do {
10363                                 c = pgetc();
10364                                 *++p = c;
10365                         } while ((unsigned char)(c - '0') <= 7 && --cnt);
10366                         pungetc();
10367                 } else if (c == 'x') { /* \xHH */
10368                         do {
10369                                 c = pgetc();
10370                                 *++p = c;
10371                         } while (isxdigit(c) && --cnt);
10372                         pungetc();
10373                         if (cnt == 3) { /* \x but next char is "bad" */
10374                                 c = 'x';
10375                                 goto unrecognized;
10376                         }
10377                 } else { /* simple seq like \\ or \t */
10378                         p++;
10379                 }
10380                 *p = '\0';
10381                 p = buf;
10382                 c = bb_process_escape_sequence((void*)&p);
10383         } else { /* unrecognized "\z": print both chars unless ' or " */
10384                 if (c != '\'' && c != '"') {
10385  unrecognized:
10386                         c |= 0x100; /* "please encode \, then me" */
10387                 }
10388         }
10389         return c;
10390 }
10391 #endif
10392
10393 /*
10394  * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
10395  * is not NULL, read a here document.  In the latter case, eofmark is the
10396  * word which marks the end of the document and striptabs is true if
10397  * leading tabs should be stripped from the document.  The argument firstc
10398  * is the first character of the input token or document.
10399  *
10400  * Because C does not have internal subroutines, I have simulated them
10401  * using goto's to implement the subroutine linkage.  The following macros
10402  * will run code that appears at the end of readtoken1.
10403  */
10404 #define CHECKEND()      {goto checkend; checkend_return:;}
10405 #define PARSEREDIR()    {goto parseredir; parseredir_return:;}
10406 #define PARSESUB()      {goto parsesub; parsesub_return:;}
10407 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
10408 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
10409 #define PARSEARITH()    {goto parsearith; parsearith_return:;}
10410 static int
10411 readtoken1(int firstc, int syntax, char *eofmark, int striptabs)
10412 {
10413         /* NB: syntax parameter fits into smallint */
10414         int c = firstc;
10415         char *out;
10416         int len;
10417         char line[EOFMARKLEN + 1];
10418         struct nodelist *bqlist;
10419         smallint quotef;
10420         smallint dblquote;
10421         smallint oldstyle;
10422         smallint prevsyntax; /* syntax before arithmetic */
10423 #if ENABLE_ASH_EXPAND_PRMT
10424         smallint pssyntax;   /* we are expanding a prompt string */
10425 #endif
10426         int varnest;         /* levels of variables expansion */
10427         int arinest;         /* levels of arithmetic expansion */
10428         int parenlevel;      /* levels of parens in arithmetic */
10429         int dqvarnest;       /* levels of variables expansion within double quotes */
10430
10431         USE_ASH_BASH_COMPAT(smallint bash_dollar_squote = 0;)
10432
10433 #if __GNUC__
10434         /* Avoid longjmp clobbering */
10435         (void) &out;
10436         (void) &quotef;
10437         (void) &dblquote;
10438         (void) &varnest;
10439         (void) &arinest;
10440         (void) &parenlevel;
10441         (void) &dqvarnest;
10442         (void) &oldstyle;
10443         (void) &prevsyntax;
10444         (void) &syntax;
10445 #endif
10446         startlinno = plinno;
10447         bqlist = NULL;
10448         quotef = 0;
10449         oldstyle = 0;
10450         prevsyntax = 0;
10451 #if ENABLE_ASH_EXPAND_PRMT
10452         pssyntax = (syntax == PSSYNTAX);
10453         if (pssyntax)
10454                 syntax = DQSYNTAX;
10455 #endif
10456         dblquote = (syntax == DQSYNTAX);
10457         varnest = 0;
10458         arinest = 0;
10459         parenlevel = 0;
10460         dqvarnest = 0;
10461
10462         STARTSTACKSTR(out);
10463         loop: { /* for each line, until end of word */
10464                 CHECKEND();     /* set c to PEOF if at end of here document */
10465                 for (;;) {      /* until end of line or end of word */
10466                         CHECKSTRSPACE(4, out);  /* permit 4 calls to USTPUTC */
10467                         switch (SIT(c, syntax)) {
10468                         case CNL:       /* '\n' */
10469                                 if (syntax == BASESYNTAX)
10470                                         goto endword;   /* exit outer loop */
10471                                 USTPUTC(c, out);
10472                                 plinno++;
10473                                 if (doprompt)
10474                                         setprompt(2);
10475                                 c = pgetc();
10476                                 goto loop;              /* continue outer loop */
10477                         case CWORD:
10478                                 USTPUTC(c, out);
10479                                 break;
10480                         case CCTL:
10481                                 if (eofmark == NULL || dblquote)
10482                                         USTPUTC(CTLESC, out);
10483 #if ENABLE_ASH_BASH_COMPAT
10484                                 if (c == '\\' && bash_dollar_squote) {
10485                                         c = decode_dollar_squote();
10486                                         if (c & 0x100) {
10487                                                 USTPUTC('\\', out);
10488                                                 c = (unsigned char)c;
10489                                         }
10490                                 }
10491 #endif
10492                                 USTPUTC(c, out);
10493                                 break;
10494                         case CBACK:     /* backslash */
10495                                 c = pgetc2();
10496                                 if (c == PEOF) {
10497                                         USTPUTC(CTLESC, out);
10498                                         USTPUTC('\\', out);
10499                                         pungetc();
10500                                 } else if (c == '\n') {
10501                                         if (doprompt)
10502                                                 setprompt(2);
10503                                 } else {
10504 #if ENABLE_ASH_EXPAND_PRMT
10505                                         if (c == '$' && pssyntax) {
10506                                                 USTPUTC(CTLESC, out);
10507                                                 USTPUTC('\\', out);
10508                                         }
10509 #endif
10510                                         if (dblquote && c != '\\'
10511                                          && c != '`' && c != '$'
10512                                          && (c != '"' || eofmark != NULL)
10513                                         ) {
10514                                                 USTPUTC(CTLESC, out);
10515                                                 USTPUTC('\\', out);
10516                                         }
10517                                         if (SIT(c, SQSYNTAX) == CCTL)
10518                                                 USTPUTC(CTLESC, out);
10519                                         USTPUTC(c, out);
10520                                         quotef = 1;
10521                                 }
10522                                 break;
10523                         case CSQUOTE:
10524                                 syntax = SQSYNTAX;
10525  quotemark:
10526                                 if (eofmark == NULL) {
10527                                         USTPUTC(CTLQUOTEMARK, out);
10528                                 }
10529                                 break;
10530                         case CDQUOTE:
10531                                 syntax = DQSYNTAX;
10532                                 dblquote = 1;
10533                                 goto quotemark;
10534                         case CENDQUOTE:
10535                                 USE_ASH_BASH_COMPAT(bash_dollar_squote = 0;)
10536                                 if (eofmark != NULL && arinest == 0
10537                                  && varnest == 0
10538                                 ) {
10539                                         USTPUTC(c, out);
10540                                 } else {
10541                                         if (dqvarnest == 0) {
10542                                                 syntax = BASESYNTAX;
10543                                                 dblquote = 0;
10544                                         }
10545                                         quotef = 1;
10546                                         goto quotemark;
10547                                 }
10548                                 break;
10549                         case CVAR:      /* '$' */
10550                                 PARSESUB();             /* parse substitution */
10551                                 break;
10552                         case CENDVAR:   /* '}' */
10553                                 if (varnest > 0) {
10554                                         varnest--;
10555                                         if (dqvarnest > 0) {
10556                                                 dqvarnest--;
10557                                         }
10558                                         USTPUTC(CTLENDVAR, out);
10559                                 } else {
10560                                         USTPUTC(c, out);
10561                                 }
10562                                 break;
10563 #if ENABLE_ASH_MATH_SUPPORT
10564                         case CLP:       /* '(' in arithmetic */
10565                                 parenlevel++;
10566                                 USTPUTC(c, out);
10567                                 break;
10568                         case CRP:       /* ')' in arithmetic */
10569                                 if (parenlevel > 0) {
10570                                         USTPUTC(c, out);
10571                                         --parenlevel;
10572                                 } else {
10573                                         if (pgetc() == ')') {
10574                                                 if (--arinest == 0) {
10575                                                         USTPUTC(CTLENDARI, out);
10576                                                         syntax = prevsyntax;
10577                                                         dblquote = (syntax == DQSYNTAX);
10578                                                 } else
10579                                                         USTPUTC(')', out);
10580                                         } else {
10581                                                 /*
10582                                                  * unbalanced parens
10583                                                  *  (don't 2nd guess - no error)
10584                                                  */
10585                                                 pungetc();
10586                                                 USTPUTC(')', out);
10587                                         }
10588                                 }
10589                                 break;
10590 #endif
10591                         case CBQUOTE:   /* '`' */
10592                                 PARSEBACKQOLD();
10593                                 break;
10594                         case CENDFILE:
10595                                 goto endword;           /* exit outer loop */
10596                         case CIGN:
10597                                 break;
10598                         default:
10599                                 if (varnest == 0)
10600                                         goto endword;   /* exit outer loop */
10601 #if ENABLE_ASH_ALIAS
10602                                 if (c != PEOA)
10603 #endif
10604                                         USTPUTC(c, out);
10605
10606                         }
10607                         c = pgetc_macro();
10608                 } /* for(;;) */
10609         }
10610  endword:
10611 #if ENABLE_ASH_MATH_SUPPORT
10612         if (syntax == ARISYNTAX)
10613                 raise_error_syntax("Missing '))'");
10614 #endif
10615         if (syntax != BASESYNTAX && !parsebackquote && eofmark == NULL)
10616                 raise_error_syntax("Unterminated quoted string");
10617         if (varnest != 0) {
10618                 startlinno = plinno;
10619                 /* { */
10620                 raise_error_syntax("Missing '}'");
10621         }
10622         USTPUTC('\0', out);
10623         len = out - (char *)stackblock();
10624         out = stackblock();
10625         if (eofmark == NULL) {
10626                 if ((c == '>' || c == '<')
10627                  && quotef == 0
10628                  && len <= 2
10629                  && (*out == '\0' || isdigit(*out))
10630                 ) {
10631                         PARSEREDIR();
10632                         lasttoken = TREDIR;
10633                         return lasttoken;
10634                 }
10635                 pungetc();
10636         }
10637         quoteflag = quotef;
10638         backquotelist = bqlist;
10639         grabstackblock(len);
10640         wordtext = out;
10641         lasttoken = TWORD;
10642         return lasttoken;
10643 /* end of readtoken routine */
10644
10645 /*
10646  * Check to see whether we are at the end of the here document.  When this
10647  * is called, c is set to the first character of the next input line.  If
10648  * we are at the end of the here document, this routine sets the c to PEOF.
10649  */
10650 checkend: {
10651         if (eofmark) {
10652 #if ENABLE_ASH_ALIAS
10653                 if (c == PEOA) {
10654                         c = pgetc2();
10655                 }
10656 #endif
10657                 if (striptabs) {
10658                         while (c == '\t') {
10659                                 c = pgetc2();
10660                         }
10661                 }
10662                 if (c == *eofmark) {
10663                         if (pfgets(line, sizeof(line)) != NULL) {
10664                                 char *p, *q;
10665
10666                                 p = line;
10667                                 for (q = eofmark + 1; *q && *p == *q; p++, q++)
10668                                         continue;
10669                                 if (*p == '\n' && *q == '\0') {
10670                                         c = PEOF;
10671                                         plinno++;
10672                                         needprompt = doprompt;
10673                                 } else {
10674                                         pushstring(line, NULL);
10675                                 }
10676                         }
10677                 }
10678         }
10679         goto checkend_return;
10680 }
10681
10682 /*
10683  * Parse a redirection operator.  The variable "out" points to a string
10684  * specifying the fd to be redirected.  The variable "c" contains the
10685  * first character of the redirection operator.
10686  */
10687 parseredir: {
10688         char fd = *out;
10689         union node *np;
10690
10691         np = stzalloc(sizeof(struct nfile));
10692         if (c == '>') {
10693                 np->nfile.fd = 1;
10694                 c = pgetc();
10695                 if (c == '>')
10696                         np->type = NAPPEND;
10697                 else if (c == '|')
10698                         np->type = NCLOBBER;
10699                 else if (c == '&')
10700                         np->type = NTOFD;
10701                 else {
10702                         np->type = NTO;
10703                         pungetc();
10704                 }
10705         } else {        /* c == '<' */
10706                 /*np->nfile.fd = 0; - stzalloc did it */
10707                 c = pgetc();
10708                 switch (c) {
10709                 case '<':
10710                         if (sizeof(struct nfile) != sizeof(struct nhere)) {
10711                                 np = stzalloc(sizeof(struct nhere));
10712                                 /*np->nfile.fd = 0; - stzalloc did it */
10713                         }
10714                         np->type = NHERE;
10715                         heredoc = stzalloc(sizeof(struct heredoc));
10716                         heredoc->here = np;
10717                         c = pgetc();
10718                         if (c == '-') {
10719                                 heredoc->striptabs = 1;
10720                         } else {
10721                                 /*heredoc->striptabs = 0; - stzalloc did it */
10722                                 pungetc();
10723                         }
10724                         break;
10725
10726                 case '&':
10727                         np->type = NFROMFD;
10728                         break;
10729
10730                 case '>':
10731                         np->type = NFROMTO;
10732                         break;
10733
10734                 default:
10735                         np->type = NFROM;
10736                         pungetc();
10737                         break;
10738                 }
10739         }
10740         if (fd != '\0')
10741                 np->nfile.fd = fd - '0';
10742         redirnode = np;
10743         goto parseredir_return;
10744 }
10745
10746 /*
10747  * Parse a substitution.  At this point, we have read the dollar sign
10748  * and nothing else.
10749  */
10750
10751 /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise
10752  * (assuming ascii char codes, as the original implementation did) */
10753 #define is_special(c) \
10754         (((unsigned)(c) - 33 < 32) \
10755                         && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1))
10756 parsesub: {
10757         int subtype;
10758         int typeloc;
10759         int flags;
10760         char *p;
10761         static const char types[] ALIGN1 = "}-+?=";
10762
10763         c = pgetc();
10764         if (c <= PEOA_OR_PEOF
10765          || (c != '(' && c != '{' && !is_name(c) && !is_special(c))
10766         ) {
10767 #if ENABLE_ASH_BASH_COMPAT
10768                 if (c == '\'')
10769                         bash_dollar_squote = 1;
10770                 else
10771 #endif
10772                         USTPUTC('$', out);
10773                 pungetc();
10774         } else if (c == '(') {  /* $(command) or $((arith)) */
10775                 if (pgetc() == '(') {
10776 #if ENABLE_ASH_MATH_SUPPORT
10777                         PARSEARITH();
10778 #else
10779                         raise_error_syntax("you disabled math support for $((arith)) syntax");
10780 #endif
10781                 } else {
10782                         pungetc();
10783                         PARSEBACKQNEW();
10784                 }
10785         } else {
10786                 USTPUTC(CTLVAR, out);
10787                 typeloc = out - (char *)stackblock();
10788                 USTPUTC(VSNORMAL, out);
10789                 subtype = VSNORMAL;
10790                 if (c == '{') {
10791                         c = pgetc();
10792                         if (c == '#') {
10793                                 c = pgetc();
10794                                 if (c == '}')
10795                                         c = '#';
10796                                 else
10797                                         subtype = VSLENGTH;
10798                         } else
10799                                 subtype = 0;
10800                 }
10801                 if (c > PEOA_OR_PEOF && is_name(c)) {
10802                         do {
10803                                 STPUTC(c, out);
10804                                 c = pgetc();
10805                         } while (c > PEOA_OR_PEOF && is_in_name(c));
10806                 } else if (isdigit(c)) {
10807                         do {
10808                                 STPUTC(c, out);
10809                                 c = pgetc();
10810                         } while (isdigit(c));
10811                 } else if (is_special(c)) {
10812                         USTPUTC(c, out);
10813                         c = pgetc();
10814                 } else
10815  badsub:                raise_error_syntax("Bad substitution");
10816
10817                 STPUTC('=', out);
10818                 flags = 0;
10819                 if (subtype == 0) {
10820                         switch (c) {
10821                         case ':':
10822                                 c = pgetc();
10823 #if ENABLE_ASH_BASH_COMPAT
10824                                 if (c == ':' || c == '$' || isdigit(c)) {
10825                                         pungetc();
10826                                         subtype = VSSUBSTR;
10827                                         break;
10828                                 }
10829 #endif
10830                                 flags = VSNUL;
10831                                 /*FALLTHROUGH*/
10832                         default:
10833                                 p = strchr(types, c);
10834                                 if (p == NULL)
10835                                         goto badsub;
10836                                 subtype = p - types + VSNORMAL;
10837                                 break;
10838                         case '%':
10839                         case '#': {
10840                                 int cc = c;
10841                                 subtype = c == '#' ? VSTRIMLEFT : VSTRIMRIGHT;
10842                                 c = pgetc();
10843                                 if (c == cc)
10844                                         subtype++;
10845                                 else
10846                                         pungetc();
10847                                 break;
10848                         }
10849 #if ENABLE_ASH_BASH_COMPAT
10850                         case '/':
10851                                 subtype = VSREPLACE;
10852                                 c = pgetc();
10853                                 if (c == '/')
10854                                         subtype++; /* VSREPLACEALL */
10855                                 else
10856                                         pungetc();
10857                                 break;
10858 #endif
10859                         }
10860                 } else {
10861                         pungetc();
10862                 }
10863                 if (dblquote || arinest)
10864                         flags |= VSQUOTE;
10865                 *((char *)stackblock() + typeloc) = subtype | flags;
10866                 if (subtype != VSNORMAL) {
10867                         varnest++;
10868                         if (dblquote || arinest) {
10869                                 dqvarnest++;
10870                         }
10871                 }
10872         }
10873         goto parsesub_return;
10874 }
10875
10876 /*
10877  * Called to parse command substitutions.  Newstyle is set if the command
10878  * is enclosed inside $(...); nlpp is a pointer to the head of the linked
10879  * list of commands (passed by reference), and savelen is the number of
10880  * characters on the top of the stack which must be preserved.
10881  */
10882 parsebackq: {
10883         struct nodelist **nlpp;
10884         smallint savepbq;
10885         union node *n;
10886         char *volatile str;
10887         struct jmploc jmploc;
10888         struct jmploc *volatile savehandler;
10889         size_t savelen;
10890         smallint saveprompt = 0;
10891
10892 #ifdef __GNUC__
10893         (void) &saveprompt;
10894 #endif
10895         savepbq = parsebackquote;
10896         if (setjmp(jmploc.loc)) {
10897                 free(str);
10898                 parsebackquote = 0;
10899                 exception_handler = savehandler;
10900                 longjmp(exception_handler->loc, 1);
10901         }
10902         INT_OFF;
10903         str = NULL;
10904         savelen = out - (char *)stackblock();
10905         if (savelen > 0) {
10906                 str = ckmalloc(savelen);
10907                 memcpy(str, stackblock(), savelen);
10908         }
10909         savehandler = exception_handler;
10910         exception_handler = &jmploc;
10911         INT_ON;
10912         if (oldstyle) {
10913                 /* We must read until the closing backquote, giving special
10914                    treatment to some slashes, and then push the string and
10915                    reread it as input, interpreting it normally.  */
10916                 char *pout;
10917                 int pc;
10918                 size_t psavelen;
10919                 char *pstr;
10920
10921
10922                 STARTSTACKSTR(pout);
10923                 for (;;) {
10924                         if (needprompt) {
10925                                 setprompt(2);
10926                         }
10927                         pc = pgetc();
10928                         switch (pc) {
10929                         case '`':
10930                                 goto done;
10931
10932                         case '\\':
10933                                 pc = pgetc();
10934                                 if (pc == '\n') {
10935                                         plinno++;
10936                                         if (doprompt)
10937                                                 setprompt(2);
10938                                         /*
10939                                          * If eating a newline, avoid putting
10940                                          * the newline into the new character
10941                                          * stream (via the STPUTC after the
10942                                          * switch).
10943                                          */
10944                                         continue;
10945                                 }
10946                                 if (pc != '\\' && pc != '`' && pc != '$'
10947                                  && (!dblquote || pc != '"'))
10948                                         STPUTC('\\', pout);
10949                                 if (pc > PEOA_OR_PEOF) {
10950                                         break;
10951                                 }
10952                                 /* fall through */
10953
10954                         case PEOF:
10955 #if ENABLE_ASH_ALIAS
10956                         case PEOA:
10957 #endif
10958                                 startlinno = plinno;
10959                                 raise_error_syntax("EOF in backquote substitution");
10960
10961                         case '\n':
10962                                 plinno++;
10963                                 needprompt = doprompt;
10964                                 break;
10965
10966                         default:
10967                                 break;
10968                         }
10969                         STPUTC(pc, pout);
10970                 }
10971  done:
10972                 STPUTC('\0', pout);
10973                 psavelen = pout - (char *)stackblock();
10974                 if (psavelen > 0) {
10975                         pstr = grabstackstr(pout);
10976                         setinputstring(pstr);
10977                 }
10978         }
10979         nlpp = &bqlist;
10980         while (*nlpp)
10981                 nlpp = &(*nlpp)->next;
10982         *nlpp = stzalloc(sizeof(**nlpp));
10983         /* (*nlpp)->next = NULL; - stzalloc did it */
10984         parsebackquote = oldstyle;
10985
10986         if (oldstyle) {
10987                 saveprompt = doprompt;
10988                 doprompt = 0;
10989         }
10990
10991         n = list(2);
10992
10993         if (oldstyle)
10994                 doprompt = saveprompt;
10995         else if (readtoken() != TRP)
10996                 raise_error_unexpected_syntax(TRP);
10997
10998         (*nlpp)->n = n;
10999         if (oldstyle) {
11000                 /*
11001                  * Start reading from old file again, ignoring any pushed back
11002                  * tokens left from the backquote parsing
11003                  */
11004                 popfile();
11005                 tokpushback = 0;
11006         }
11007         while (stackblocksize() <= savelen)
11008                 growstackblock();
11009         STARTSTACKSTR(out);
11010         if (str) {
11011                 memcpy(out, str, savelen);
11012                 STADJUST(savelen, out);
11013                 INT_OFF;
11014                 free(str);
11015                 str = NULL;
11016                 INT_ON;
11017         }
11018         parsebackquote = savepbq;
11019         exception_handler = savehandler;
11020         if (arinest || dblquote)
11021                 USTPUTC(CTLBACKQ | CTLQUOTE, out);
11022         else
11023                 USTPUTC(CTLBACKQ, out);
11024         if (oldstyle)
11025                 goto parsebackq_oldreturn;
11026         goto parsebackq_newreturn;
11027 }
11028
11029 #if ENABLE_ASH_MATH_SUPPORT
11030 /*
11031  * Parse an arithmetic expansion (indicate start of one and set state)
11032  */
11033 parsearith: {
11034         if (++arinest == 1) {
11035                 prevsyntax = syntax;
11036                 syntax = ARISYNTAX;
11037                 USTPUTC(CTLARI, out);
11038                 if (dblquote)
11039                         USTPUTC('"', out);
11040                 else
11041                         USTPUTC(' ', out);
11042         } else {
11043                 /*
11044                  * we collapse embedded arithmetic expansion to
11045                  * parenthesis, which should be equivalent
11046                  */
11047                 USTPUTC('(', out);
11048         }
11049         goto parsearith_return;
11050 }
11051 #endif
11052
11053 } /* end of readtoken */
11054
11055 /*
11056  * Read the next input token.
11057  * If the token is a word, we set backquotelist to the list of cmds in
11058  *      backquotes.  We set quoteflag to true if any part of the word was
11059  *      quoted.
11060  * If the token is TREDIR, then we set redirnode to a structure containing
11061  *      the redirection.
11062  * In all cases, the variable startlinno is set to the number of the line
11063  *      on which the token starts.
11064  *
11065  * [Change comment:  here documents and internal procedures]
11066  * [Readtoken shouldn't have any arguments.  Perhaps we should make the
11067  *  word parsing code into a separate routine.  In this case, readtoken
11068  *  doesn't need to have any internal procedures, but parseword does.
11069  *  We could also make parseoperator in essence the main routine, and
11070  *  have parseword (readtoken1?) handle both words and redirection.]
11071  */
11072 #define NEW_xxreadtoken
11073 #ifdef NEW_xxreadtoken
11074 /* singles must be first! */
11075 static const char xxreadtoken_chars[7] ALIGN1 = {
11076         '\n', '(', ')', '&', '|', ';', 0
11077 };
11078
11079 static const char xxreadtoken_tokens[] ALIGN1 = {
11080         TNL, TLP, TRP,          /* only single occurrence allowed */
11081         TBACKGND, TPIPE, TSEMI, /* if single occurrence */
11082         TEOF,                   /* corresponds to trailing nul */
11083         TAND, TOR, TENDCASE     /* if double occurrence */
11084 };
11085
11086 #define xxreadtoken_doubles \
11087         (sizeof(xxreadtoken_tokens) - sizeof(xxreadtoken_chars))
11088 #define xxreadtoken_singles \
11089         (sizeof(xxreadtoken_chars) - xxreadtoken_doubles - 1)
11090
11091 static int
11092 xxreadtoken(void)
11093 {
11094         int c;
11095
11096         if (tokpushback) {
11097                 tokpushback = 0;
11098                 return lasttoken;
11099         }
11100         if (needprompt) {
11101                 setprompt(2);
11102         }
11103         startlinno = plinno;
11104         for (;;) {                      /* until token or start of word found */
11105                 c = pgetc_macro();
11106
11107                 if ((c != ' ') && (c != '\t')
11108 #if ENABLE_ASH_ALIAS
11109                  && (c != PEOA)
11110 #endif
11111                 ) {
11112                         if (c == '#') {
11113                                 while ((c = pgetc()) != '\n' && c != PEOF)
11114                                         continue;
11115                                 pungetc();
11116                         } else if (c == '\\') {
11117                                 if (pgetc() != '\n') {
11118                                         pungetc();
11119                                         goto READTOKEN1;
11120                                 }
11121                                 startlinno = ++plinno;
11122                                 if (doprompt)
11123                                         setprompt(2);
11124                         } else {
11125                                 const char *p
11126                                         = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1;
11127
11128                                 if (c != PEOF) {
11129                                         if (c == '\n') {
11130                                                 plinno++;
11131                                                 needprompt = doprompt;
11132                                         }
11133
11134                                         p = strchr(xxreadtoken_chars, c);
11135                                         if (p == NULL) {
11136  READTOKEN1:
11137                                                 return readtoken1(c, BASESYNTAX, (char *) NULL, 0);
11138                                         }
11139
11140                                         if ((size_t)(p - xxreadtoken_chars) >= xxreadtoken_singles) {
11141                                                 if (pgetc() == *p) {    /* double occurrence? */
11142                                                         p += xxreadtoken_doubles + 1;
11143                                                 } else {
11144                                                         pungetc();
11145                                                 }
11146                                         }
11147                                 }
11148                                 lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars];
11149                                 return lasttoken;
11150                         }
11151                 }
11152         } /* for */
11153 }
11154 #else
11155 #define RETURN(token)   return lasttoken = token
11156 static int
11157 xxreadtoken(void)
11158 {
11159         int c;
11160
11161         if (tokpushback) {
11162                 tokpushback = 0;
11163                 return lasttoken;
11164         }
11165         if (needprompt) {
11166                 setprompt(2);
11167         }
11168         startlinno = plinno;
11169         for (;;) {      /* until token or start of word found */
11170                 c = pgetc_macro();
11171                 switch (c) {
11172                 case ' ': case '\t':
11173 #if ENABLE_ASH_ALIAS
11174                 case PEOA:
11175 #endif
11176                         continue;
11177                 case '#':
11178                         while ((c = pgetc()) != '\n' && c != PEOF)
11179                                 continue;
11180                         pungetc();
11181                         continue;
11182                 case '\\':
11183                         if (pgetc() == '\n') {
11184                                 startlinno = ++plinno;
11185                                 if (doprompt)
11186                                         setprompt(2);
11187                                 continue;
11188                         }
11189                         pungetc();
11190                         goto breakloop;
11191                 case '\n':
11192                         plinno++;
11193                         needprompt = doprompt;
11194                         RETURN(TNL);
11195                 case PEOF:
11196                         RETURN(TEOF);
11197                 case '&':
11198                         if (pgetc() == '&')
11199                                 RETURN(TAND);
11200                         pungetc();
11201                         RETURN(TBACKGND);
11202                 case '|':
11203                         if (pgetc() == '|')
11204                                 RETURN(TOR);
11205                         pungetc();
11206                         RETURN(TPIPE);
11207                 case ';':
11208                         if (pgetc() == ';')
11209                                 RETURN(TENDCASE);
11210                         pungetc();
11211                         RETURN(TSEMI);
11212                 case '(':
11213                         RETURN(TLP);
11214                 case ')':
11215                         RETURN(TRP);
11216                 default:
11217                         goto breakloop;
11218                 }
11219         }
11220  breakloop:
11221         return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
11222 #undef RETURN
11223 }
11224 #endif /* NEW_xxreadtoken */
11225
11226 static int
11227 readtoken(void)
11228 {
11229         int t;
11230 #if DEBUG
11231         smallint alreadyseen = tokpushback;
11232 #endif
11233
11234 #if ENABLE_ASH_ALIAS
11235  top:
11236 #endif
11237
11238         t = xxreadtoken();
11239
11240         /*
11241          * eat newlines
11242          */
11243         if (checkkwd & CHKNL) {
11244                 while (t == TNL) {
11245                         parseheredoc();
11246                         t = xxreadtoken();
11247                 }
11248         }
11249
11250         if (t != TWORD || quoteflag) {
11251                 goto out;
11252         }
11253
11254         /*
11255          * check for keywords
11256          */
11257         if (checkkwd & CHKKWD) {
11258                 const char *const *pp;
11259
11260                 pp = findkwd(wordtext);
11261                 if (pp) {
11262                         lasttoken = t = pp - tokname_array;
11263                         TRACE(("keyword %s recognized\n", tokname(t)));
11264                         goto out;
11265                 }
11266         }
11267
11268         if (checkkwd & CHKALIAS) {
11269 #if ENABLE_ASH_ALIAS
11270                 struct alias *ap;
11271                 ap = lookupalias(wordtext, 1);
11272                 if (ap != NULL) {
11273                         if (*ap->val) {
11274                                 pushstring(ap->val, ap);
11275                         }
11276                         goto top;
11277                 }
11278 #endif
11279         }
11280  out:
11281         checkkwd = 0;
11282 #if DEBUG
11283         if (!alreadyseen)
11284                 TRACE(("token %s %s\n", tokname(t), t == TWORD ? wordtext : ""));
11285         else
11286                 TRACE(("reread token %s %s\n", tokname(t), t == TWORD ? wordtext : ""));
11287 #endif
11288         return t;
11289 }
11290
11291 static char
11292 peektoken(void)
11293 {
11294         int t;
11295
11296         t = readtoken();
11297         tokpushback = 1;
11298         return tokname_array[t][0];
11299 }
11300
11301 /*
11302  * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
11303  * valid parse tree indicating a blank line.)
11304  */
11305 static union node *
11306 parsecmd(int interact)
11307 {
11308         int t;
11309
11310         tokpushback = 0;
11311         doprompt = interact;
11312         if (doprompt)
11313                 setprompt(doprompt);
11314         needprompt = 0;
11315         t = readtoken();
11316         if (t == TEOF)
11317                 return NEOF;
11318         if (t == TNL)
11319                 return NULL;
11320         tokpushback = 1;
11321         return list(1);
11322 }
11323
11324 /*
11325  * Input any here documents.
11326  */
11327 static void
11328 parseheredoc(void)
11329 {
11330         struct heredoc *here;
11331         union node *n;
11332
11333         here = heredoclist;
11334         heredoclist = NULL;
11335
11336         while (here) {
11337                 if (needprompt) {
11338                         setprompt(2);
11339                 }
11340                 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
11341                                 here->eofmark, here->striptabs);
11342                 n = stzalloc(sizeof(struct narg));
11343                 n->narg.type = NARG;
11344                 /*n->narg.next = NULL; - stzalloc did it */
11345                 n->narg.text = wordtext;
11346                 n->narg.backquote = backquotelist;
11347                 here->here->nhere.doc = n;
11348                 here = here->next;
11349         }
11350 }
11351
11352
11353 /*
11354  * called by editline -- any expansions to the prompt should be added here.
11355  */
11356 #if ENABLE_ASH_EXPAND_PRMT
11357 static const char *
11358 expandstr(const char *ps)
11359 {
11360         union node n;
11361
11362         /* XXX Fix (char *) cast. */
11363         setinputstring((char *)ps);
11364         readtoken1(pgetc(), PSSYNTAX, nullstr, 0);
11365         popfile();
11366
11367         n.narg.type = NARG;
11368         n.narg.next = NULL;
11369         n.narg.text = wordtext;
11370         n.narg.backquote = backquotelist;
11371
11372         expandarg(&n, NULL, 0);
11373         return stackblock();
11374 }
11375 #endif
11376
11377 /*
11378  * Execute a command or commands contained in a string.
11379  */
11380 static int
11381 evalstring(char *s, int mask)
11382 {
11383         union node *n;
11384         struct stackmark smark;
11385         int skip;
11386
11387         setinputstring(s);
11388         setstackmark(&smark);
11389
11390         skip = 0;
11391         while ((n = parsecmd(0)) != NEOF) {
11392                 evaltree(n, 0);
11393                 popstackmark(&smark);
11394                 skip = evalskip;
11395                 if (skip)
11396                         break;
11397         }
11398         popfile();
11399
11400         skip &= mask;
11401         evalskip = skip;
11402         return skip;
11403 }
11404
11405 /*
11406  * The eval command.
11407  */
11408 static int
11409 evalcmd(int argc ATTRIBUTE_UNUSED, char **argv)
11410 {
11411         char *p;
11412         char *concat;
11413
11414         if (argv[1]) {
11415                 p = argv[1];
11416                 argv += 2;
11417                 if (argv[0]) {
11418                         STARTSTACKSTR(concat);
11419                         for (;;) {
11420                                 concat = stack_putstr(p, concat);
11421                                 p = *argv++;
11422                                 if (p == NULL)
11423                                         break;
11424                                 STPUTC(' ', concat);
11425                         }
11426                         STPUTC('\0', concat);
11427                         p = grabstackstr(concat);
11428                 }
11429                 evalstring(p, ~SKIPEVAL);
11430
11431         }
11432         return exitstatus;
11433 }
11434
11435 /*
11436  * Read and execute commands.  "Top" is nonzero for the top level command
11437  * loop; it turns on prompting if the shell is interactive.
11438  */
11439 static int
11440 cmdloop(int top)
11441 {
11442         union node *n;
11443         struct stackmark smark;
11444         int inter;
11445         int numeof = 0;
11446
11447         TRACE(("cmdloop(%d) called\n", top));
11448         for (;;) {
11449                 int skip;
11450
11451                 setstackmark(&smark);
11452 #if JOBS
11453                 if (doing_jobctl)
11454                         showjobs(stderr, SHOW_CHANGED);
11455 #endif
11456                 inter = 0;
11457                 if (iflag && top) {
11458                         inter++;
11459 #if ENABLE_ASH_MAIL
11460                         chkmail();
11461 #endif
11462                 }
11463                 n = parsecmd(inter);
11464                 /* showtree(n); DEBUG */
11465                 if (n == NEOF) {
11466                         if (!top || numeof >= 50)
11467                                 break;
11468                         if (!stoppedjobs()) {
11469                                 if (!Iflag)
11470                                         break;
11471                                 out2str("\nUse \"exit\" to leave shell.\n");
11472                         }
11473                         numeof++;
11474                 } else if (nflag == 0) {
11475                         /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */
11476                         job_warning >>= 1;
11477                         numeof = 0;
11478                         evaltree(n, 0);
11479                 }
11480                 popstackmark(&smark);
11481                 skip = evalskip;
11482
11483                 if (skip) {
11484                         evalskip = 0;
11485                         return skip & SKIPEVAL;
11486                 }
11487         }
11488         return 0;
11489 }
11490
11491 /*
11492  * Take commands from a file.  To be compatible we should do a path
11493  * search for the file, which is necessary to find sub-commands.
11494  */
11495 static char *
11496 find_dot_file(char *name)
11497 {
11498         char *fullname;
11499         const char *path = pathval();
11500         struct stat statb;
11501
11502         /* don't try this for absolute or relative paths */
11503         if (strchr(name, '/'))
11504                 return name;
11505
11506         while ((fullname = padvance(&path, name)) != NULL) {
11507                 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
11508                         /*
11509                          * Don't bother freeing here, since it will
11510                          * be freed by the caller.
11511                          */
11512                         return fullname;
11513                 }
11514                 stunalloc(fullname);
11515         }
11516
11517         /* not found in the PATH */
11518         ash_msg_and_raise_error("%s: not found", name);
11519         /* NOTREACHED */
11520 }
11521
11522 static int
11523 dotcmd(int argc, char **argv)
11524 {
11525         struct strlist *sp;
11526         volatile struct shparam saveparam;
11527         int status = 0;
11528
11529         for (sp = cmdenviron; sp; sp = sp->next)
11530                 setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED);
11531
11532         if (argv[1]) {        /* That's what SVR2 does */
11533                 char *fullname = find_dot_file(argv[1]);
11534                 argv += 2;
11535                 argc -= 2;
11536                 if (argc) { /* argc > 0, argv[0] != NULL */
11537                         saveparam = shellparam;
11538                         shellparam.malloced = 0;
11539                         shellparam.nparam = argc;
11540                         shellparam.p = argv;
11541                 };
11542
11543                 setinputfile(fullname, INPUT_PUSH_FILE);
11544                 commandname = fullname;
11545                 cmdloop(0);
11546                 popfile();
11547
11548                 if (argc) {
11549                         freeparam(&shellparam);
11550                         shellparam = saveparam;
11551                 };
11552                 status = exitstatus;
11553         }
11554         return status;
11555 }
11556
11557 static int
11558 exitcmd(int argc ATTRIBUTE_UNUSED, char **argv)
11559 {
11560         if (stoppedjobs())
11561                 return 0;
11562         if (argv[1])
11563                 exitstatus = number(argv[1]);
11564         raise_exception(EXEXIT);
11565         /* NOTREACHED */
11566 }
11567
11568 /*
11569  * Read a file containing shell functions.
11570  */
11571 static void
11572 readcmdfile(char *name)
11573 {
11574         setinputfile(name, INPUT_PUSH_FILE);
11575         cmdloop(0);
11576         popfile();
11577 }
11578
11579
11580 /* ============ find_command inplementation */
11581
11582 /*
11583  * Resolve a command name.  If you change this routine, you may have to
11584  * change the shellexec routine as well.
11585  */
11586 static void
11587 find_command(char *name, struct cmdentry *entry, int act, const char *path)
11588 {
11589         struct tblentry *cmdp;
11590         int idx;
11591         int prev;
11592         char *fullname;
11593         struct stat statb;
11594         int e;
11595         int updatetbl;
11596         struct builtincmd *bcmd;
11597
11598         /* If name contains a slash, don't use PATH or hash table */
11599         if (strchr(name, '/') != NULL) {
11600                 entry->u.index = -1;
11601                 if (act & DO_ABS) {
11602                         while (stat(name, &statb) < 0) {
11603 #ifdef SYSV
11604                                 if (errno == EINTR)
11605                                         continue;
11606 #endif
11607                                 entry->cmdtype = CMDUNKNOWN;
11608                                 return;
11609                         }
11610                 }
11611                 entry->cmdtype = CMDNORMAL;
11612                 return;
11613         }
11614
11615 /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */
11616
11617         updatetbl = (path == pathval());
11618         if (!updatetbl) {
11619                 act |= DO_ALTPATH;
11620                 if (strstr(path, "%builtin") != NULL)
11621                         act |= DO_ALTBLTIN;
11622         }
11623
11624         /* If name is in the table, check answer will be ok */
11625         cmdp = cmdlookup(name, 0);
11626         if (cmdp != NULL) {
11627                 int bit;
11628
11629                 switch (cmdp->cmdtype) {
11630                 default:
11631 #if DEBUG
11632                         abort();
11633 #endif
11634                 case CMDNORMAL:
11635                         bit = DO_ALTPATH;
11636                         break;
11637                 case CMDFUNCTION:
11638                         bit = DO_NOFUNC;
11639                         break;
11640                 case CMDBUILTIN:
11641                         bit = DO_ALTBLTIN;
11642                         break;
11643                 }
11644                 if (act & bit) {
11645                         updatetbl = 0;
11646                         cmdp = NULL;
11647                 } else if (cmdp->rehash == 0)
11648                         /* if not invalidated by cd, we're done */
11649                         goto success;
11650         }
11651
11652         /* If %builtin not in path, check for builtin next */
11653         bcmd = find_builtin(name);
11654         if (bcmd) {
11655                 if (IS_BUILTIN_REGULAR(bcmd))
11656                         goto builtin_success;
11657                 if (act & DO_ALTPATH) {
11658                         if (!(act & DO_ALTBLTIN))
11659                                 goto builtin_success;
11660                 } else if (builtinloc <= 0) {
11661                         goto builtin_success;
11662                 }
11663         }
11664
11665 #if ENABLE_FEATURE_SH_STANDALONE
11666         {
11667                 int applet_no = find_applet_by_name(name);
11668                 if (applet_no >= 0) {
11669                         entry->cmdtype = CMDNORMAL;
11670                         entry->u.index = -2 - applet_no;
11671                         return;
11672                 }
11673         }
11674 #endif
11675
11676         /* We have to search path. */
11677         prev = -1;              /* where to start */
11678         if (cmdp && cmdp->rehash) {     /* doing a rehash */
11679                 if (cmdp->cmdtype == CMDBUILTIN)
11680                         prev = builtinloc;
11681                 else
11682                         prev = cmdp->param.index;
11683         }
11684
11685         e = ENOENT;
11686         idx = -1;
11687  loop:
11688         while ((fullname = padvance(&path, name)) != NULL) {
11689                 stunalloc(fullname);
11690                 /* NB: code below will still use fullname
11691                  * despite it being "unallocated" */
11692                 idx++;
11693                 if (pathopt) {
11694                         if (prefix(pathopt, "builtin")) {
11695                                 if (bcmd)
11696                                         goto builtin_success;
11697                                 continue;
11698                         }
11699                         if ((act & DO_NOFUNC)
11700                          || !prefix(pathopt, "func")
11701                         ) {     /* ignore unimplemented options */
11702                                 continue;
11703                         }
11704                 }
11705                 /* if rehash, don't redo absolute path names */
11706                 if (fullname[0] == '/' && idx <= prev) {
11707                         if (idx < prev)
11708                                 continue;
11709                         TRACE(("searchexec \"%s\": no change\n", name));
11710                         goto success;
11711                 }
11712                 while (stat(fullname, &statb) < 0) {
11713 #ifdef SYSV
11714                         if (errno == EINTR)
11715                                 continue;
11716 #endif
11717                         if (errno != ENOENT && errno != ENOTDIR)
11718                                 e = errno;
11719                         goto loop;
11720                 }
11721                 e = EACCES;     /* if we fail, this will be the error */
11722                 if (!S_ISREG(statb.st_mode))
11723                         continue;
11724                 if (pathopt) {          /* this is a %func directory */
11725                         stalloc(strlen(fullname) + 1);
11726                         /* NB: stalloc will return space pointed by fullname
11727                          * (because we don't have any intervening allocations
11728                          * between stunalloc above and this stalloc) */
11729                         readcmdfile(fullname);
11730                         cmdp = cmdlookup(name, 0);
11731                         if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION)
11732                                 ash_msg_and_raise_error("%s not defined in %s", name, fullname);
11733                         stunalloc(fullname);
11734                         goto success;
11735                 }
11736                 TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
11737                 if (!updatetbl) {
11738                         entry->cmdtype = CMDNORMAL;
11739                         entry->u.index = idx;
11740                         return;
11741                 }
11742                 INT_OFF;
11743                 cmdp = cmdlookup(name, 1);
11744                 cmdp->cmdtype = CMDNORMAL;
11745                 cmdp->param.index = idx;
11746                 INT_ON;
11747                 goto success;
11748         }
11749
11750         /* We failed.  If there was an entry for this command, delete it */
11751         if (cmdp && updatetbl)
11752                 delete_cmd_entry();
11753         if (act & DO_ERR)
11754                 ash_msg("%s: %s", name, errmsg(e, "not found"));
11755         entry->cmdtype = CMDUNKNOWN;
11756         return;
11757
11758  builtin_success:
11759         if (!updatetbl) {
11760                 entry->cmdtype = CMDBUILTIN;
11761                 entry->u.cmd = bcmd;
11762                 return;
11763         }
11764         INT_OFF;
11765         cmdp = cmdlookup(name, 1);
11766         cmdp->cmdtype = CMDBUILTIN;
11767         cmdp->param.cmd = bcmd;
11768         INT_ON;
11769  success:
11770         cmdp->rehash = 0;
11771         entry->cmdtype = cmdp->cmdtype;
11772         entry->u = cmdp->param;
11773 }
11774
11775
11776 /* ============ trap.c */
11777
11778 /*
11779  * The trap builtin.
11780  */
11781 static int
11782 trapcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
11783 {
11784         char *action;
11785         char **ap;
11786         int signo;
11787
11788         nextopt(nullstr);
11789         ap = argptr;
11790         if (!*ap) {
11791                 for (signo = 0; signo < NSIG; signo++) {
11792                         if (trap[signo] != NULL) {
11793                                 const char *sn;
11794
11795                                 sn = get_signame(signo);
11796                                 out1fmt("trap -- %s %s\n",
11797                                         single_quote(trap[signo]), sn);
11798                         }
11799                 }
11800                 return 0;
11801         }
11802         if (!ap[1])
11803                 action = NULL;
11804         else
11805                 action = *ap++;
11806         while (*ap) {
11807                 signo = get_signum(*ap);
11808                 if (signo < 0)
11809                         ash_msg_and_raise_error("%s: bad trap", *ap);
11810                 INT_OFF;
11811                 if (action) {
11812                         if (LONE_DASH(action))
11813                                 action = NULL;
11814                         else
11815                                 action = ckstrdup(action);
11816                 }
11817                 free(trap[signo]);
11818                 trap[signo] = action;
11819                 if (signo != 0)
11820                         setsignal(signo);
11821                 INT_ON;
11822                 ap++;
11823         }
11824         return 0;
11825 }
11826
11827
11828 /* ============ Builtins */
11829
11830 #if !ENABLE_FEATURE_SH_EXTRA_QUIET
11831 /*
11832  * Lists available builtins
11833  */
11834 static int
11835 helpcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
11836 {
11837         unsigned col;
11838         unsigned i;
11839
11840         out1fmt("\nBuilt-in commands:\n-------------------\n");
11841         for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) {
11842                 col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '),
11843                                         builtintab[i].name + 1);
11844                 if (col > 60) {
11845                         out1fmt("\n");
11846                         col = 0;
11847                 }
11848         }
11849 #if ENABLE_FEATURE_SH_STANDALONE
11850         {
11851                 const char *a = applet_names;
11852                 while (*a) {
11853                         col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a);
11854                         if (col > 60) {
11855                                 out1fmt("\n");
11856                                 col = 0;
11857                         }
11858                         a += strlen(a) + 1;
11859                 }
11860         }
11861 #endif
11862         out1fmt("\n\n");
11863         return EXIT_SUCCESS;
11864 }
11865 #endif /* FEATURE_SH_EXTRA_QUIET */
11866
11867 /*
11868  * The export and readonly commands.
11869  */
11870 static int
11871 exportcmd(int argc ATTRIBUTE_UNUSED, char **argv)
11872 {
11873         struct var *vp;
11874         char *name;
11875         const char *p;
11876         char **aptr;
11877         int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
11878
11879         if (nextopt("p") != 'p') {
11880                 aptr = argptr;
11881                 name = *aptr;
11882                 if (name) {
11883                         do {
11884                                 p = strchr(name, '=');
11885                                 if (p != NULL) {
11886                                         p++;
11887                                 } else {
11888                                         vp = *findvar(hashvar(name), name);
11889                                         if (vp) {
11890                                                 vp->flags |= flag;
11891                                                 continue;
11892                                         }
11893                                 }
11894                                 setvar(name, p, flag);
11895                         } while ((name = *++aptr) != NULL);
11896                         return 0;
11897                 }
11898         }
11899         showvars(argv[0], flag, 0);
11900         return 0;
11901 }
11902
11903 /*
11904  * Delete a function if it exists.
11905  */
11906 static void
11907 unsetfunc(const char *name)
11908 {
11909         struct tblentry *cmdp;
11910
11911         cmdp = cmdlookup(name, 0);
11912         if (cmdp!= NULL && cmdp->cmdtype == CMDFUNCTION)
11913                 delete_cmd_entry();
11914 }
11915
11916 /*
11917  * The unset builtin command.  We unset the function before we unset the
11918  * variable to allow a function to be unset when there is a readonly variable
11919  * with the same name.
11920  */
11921 static int
11922 unsetcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
11923 {
11924         char **ap;
11925         int i;
11926         int flag = 0;
11927         int ret = 0;
11928
11929         while ((i = nextopt("vf")) != '\0') {
11930                 flag = i;
11931         }
11932
11933         for (ap = argptr; *ap; ap++) {
11934                 if (flag != 'f') {
11935                         i = unsetvar(*ap);
11936                         ret |= i;
11937                         if (!(i & 2))
11938                                 continue;
11939                 }
11940                 if (flag != 'v')
11941                         unsetfunc(*ap);
11942         }
11943         return ret & 1;
11944 }
11945
11946
11947 /*      setmode.c      */
11948
11949 #include <sys/times.h>
11950
11951 static const unsigned char timescmd_str[] ALIGN1 = {
11952         ' ',  offsetof(struct tms, tms_utime),
11953         '\n', offsetof(struct tms, tms_stime),
11954         ' ',  offsetof(struct tms, tms_cutime),
11955         '\n', offsetof(struct tms, tms_cstime),
11956         0
11957 };
11958
11959 static int
11960 timescmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
11961 {
11962         long clk_tck, s, t;
11963         const unsigned char *p;
11964         struct tms buf;
11965
11966         clk_tck = sysconf(_SC_CLK_TCK);
11967         times(&buf);
11968
11969         p = timescmd_str;
11970         do {
11971                 t = *(clock_t *)(((char *) &buf) + p[1]);
11972                 s = t / clk_tck;
11973                 out1fmt("%ldm%ld.%.3lds%c",
11974                         s/60, s%60,
11975                         ((t - s * clk_tck) * 1000) / clk_tck,
11976                         p[0]);
11977         } while (*(p += 2));
11978
11979         return 0;
11980 }
11981
11982 #if ENABLE_ASH_MATH_SUPPORT
11983 static arith_t
11984 dash_arith(const char *s)
11985 {
11986         arith_t result;
11987         int errcode = 0;
11988
11989         INT_OFF;
11990         result = arith(s, &errcode);
11991         if (errcode < 0) {
11992                 if (errcode == -3)
11993                         ash_msg_and_raise_error("exponent less than 0");
11994                 if (errcode == -2)
11995                         ash_msg_and_raise_error("divide by zero");
11996                 if (errcode == -5)
11997                         ash_msg_and_raise_error("expression recursion loop detected");
11998                 raise_error_syntax(s);
11999         }
12000         INT_ON;
12001
12002         return result;
12003 }
12004
12005 /*
12006  *  The let builtin. partial stolen from GNU Bash, the Bourne Again SHell.
12007  *  Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
12008  *
12009  *  Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
12010  */
12011 static int
12012 letcmd(int argc ATTRIBUTE_UNUSED, char **argv)
12013 {
12014         arith_t i;
12015
12016         argv++;
12017         if (!*argv)
12018                 ash_msg_and_raise_error("expression expected");
12019         do {
12020                 i = dash_arith(*argv);
12021         } while (*++argv);
12022
12023         return !i;
12024 }
12025 #endif /* ASH_MATH_SUPPORT */
12026
12027
12028 /* ============ miscbltin.c
12029  *
12030  * Miscellaneous builtins.
12031  */
12032
12033 #undef rflag
12034
12035 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 1
12036 typedef enum __rlimit_resource rlim_t;
12037 #endif
12038
12039 /*
12040  * The read builtin. Options:
12041  *      -r              Do not interpret '\' specially
12042  *      -s              Turn off echo (tty only)
12043  *      -n NCHARS       Read NCHARS max
12044  *      -p PROMPT       Display PROMPT on stderr (if input is from tty)
12045  *      -t SECONDS      Timeout after SECONDS (tty or pipe only)
12046  *      -u FD           Read from given FD instead of fd 0
12047  * This uses unbuffered input, which may be avoidable in some cases.
12048  * TODO: bash also has:
12049  *      -a ARRAY        Read into array[0],[1],etc
12050  *      -d DELIM        End on DELIM char, not newline
12051  *      -e              Use line editing (tty only)
12052  */
12053 static int
12054 readcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
12055 {
12056         static const char *const arg_REPLY[] = { "REPLY", NULL };
12057
12058         char **ap;
12059         int backslash;
12060         char c;
12061         int rflag;
12062         char *prompt;
12063         const char *ifs;
12064         char *p;
12065         int startword;
12066         int status;
12067         int i;
12068         int fd = 0;
12069 #if ENABLE_ASH_READ_NCHARS
12070         int nchars = 0; /* if != 0, -n is in effect */
12071         int silent = 0;
12072         struct termios tty, old_tty;
12073 #endif
12074 #if ENABLE_ASH_READ_TIMEOUT
12075         unsigned end_ms = 0;
12076         unsigned timeout = 0;
12077 #endif
12078
12079         rflag = 0;
12080         prompt = NULL;
12081         while ((i = nextopt("p:u:r"
12082                 USE_ASH_READ_TIMEOUT("t:")
12083                 USE_ASH_READ_NCHARS("n:s")
12084         )) != '\0') {
12085                 switch (i) {
12086                 case 'p':
12087                         prompt = optionarg;
12088                         break;
12089 #if ENABLE_ASH_READ_NCHARS
12090                 case 'n':
12091                         nchars = bb_strtou(optionarg, NULL, 10);
12092                         if (nchars < 0 || errno)
12093                                 ash_msg_and_raise_error("invalid count");
12094                         /* nchars == 0: off (bash 3.2 does this too) */
12095                         break;
12096                 case 's':
12097                         silent = 1;
12098                         break;
12099 #endif
12100 #if ENABLE_ASH_READ_TIMEOUT
12101                 case 't':
12102                         timeout = bb_strtou(optionarg, NULL, 10);
12103                         if (errno || timeout > UINT_MAX / 2048)
12104                                 ash_msg_and_raise_error("invalid timeout");
12105                         timeout *= 1000;
12106 #if 0 /* even bash have no -t N.NNN support */
12107                         ts.tv_sec = bb_strtou(optionarg, &p, 10);
12108                         ts.tv_usec = 0;
12109                         /* EINVAL means number is ok, but not terminated by NUL */
12110                         if (*p == '.' && errno == EINVAL) {
12111                                 char *p2;
12112                                 if (*++p) {
12113                                         int scale;
12114                                         ts.tv_usec = bb_strtou(p, &p2, 10);
12115                                         if (errno)
12116                                                 ash_msg_and_raise_error("invalid timeout");
12117                                         scale = p2 - p;
12118                                         /* normalize to usec */
12119                                         if (scale > 6)
12120                                                 ash_msg_and_raise_error("invalid timeout");
12121                                         while (scale++ < 6)
12122                                                 ts.tv_usec *= 10;
12123                                 }
12124                         } else if (ts.tv_sec < 0 || errno) {
12125                                 ash_msg_and_raise_error("invalid timeout");
12126                         }
12127                         if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */
12128                                 ash_msg_and_raise_error("invalid timeout");
12129                         }
12130 #endif /* if 0 */
12131                         break;
12132 #endif
12133                 case 'r':
12134                         rflag = 1;
12135                         break;
12136                 case 'u':
12137                         fd = bb_strtou(optionarg, NULL, 10);
12138                         if (fd < 0 || errno)
12139                                 ash_msg_and_raise_error("invalid file descriptor");
12140                         break;
12141                 default:
12142                         break;
12143                 }
12144         }
12145         if (prompt && isatty(fd)) {
12146                 out2str(prompt);
12147         }
12148         ap = argptr;
12149         if (*ap == NULL)
12150                 ap = (char**)arg_REPLY;
12151         ifs = bltinlookup("IFS");
12152         if (ifs == NULL)
12153                 ifs = defifs;
12154 #if ENABLE_ASH_READ_NCHARS
12155         tcgetattr(fd, &tty);
12156         old_tty = tty;
12157         if (nchars || silent) {
12158                 if (nchars) {
12159                         tty.c_lflag &= ~ICANON;
12160                         tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
12161                 }
12162                 if (silent) {
12163                         tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
12164                 }
12165                 /* if tcgetattr failed, tcsetattr will fail too.
12166                  * Ignoring, it's harmless. */
12167                 tcsetattr(fd, TCSANOW, &tty);
12168         }
12169 #endif
12170
12171         status = 0;
12172         startword = 1;
12173         backslash = 0;
12174 #if ENABLE_ASH_READ_TIMEOUT
12175         if (timeout) /* NB: ensuring end_ms is nonzero */
12176                 end_ms = ((unsigned)(monotonic_us() / 1000) + timeout) | 1;
12177 #endif
12178         STARTSTACKSTR(p);
12179         do {
12180 #if ENABLE_ASH_READ_TIMEOUT
12181                 if (end_ms) {
12182                         struct pollfd pfd[1];
12183                         pfd[0].fd = fd;
12184                         pfd[0].events = POLLIN;
12185                         timeout = end_ms - (unsigned)(monotonic_us() / 1000);
12186                         if ((int)timeout <= 0 /* already late? */
12187                          || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */
12188                         ) { /* timed out! */
12189 #if ENABLE_ASH_READ_NCHARS
12190                                 tcsetattr(fd, TCSANOW, &old_tty);
12191 #endif
12192                                 return 1;
12193                         }
12194                 }
12195 #endif
12196                 if (nonblock_safe_read(fd, &c, 1) != 1) {
12197                         status = 1;
12198                         break;
12199                 }
12200                 if (c == '\0')
12201                         continue;
12202                 if (backslash) {
12203                         backslash = 0;
12204                         if (c != '\n')
12205                                 goto put;
12206                         continue;
12207                 }
12208                 if (!rflag && c == '\\') {
12209                         backslash++;
12210                         continue;
12211                 }
12212                 if (c == '\n')
12213                         break;
12214                 if (startword && *ifs == ' ' && strchr(ifs, c)) {
12215                         continue;
12216                 }
12217                 startword = 0;
12218                 if (ap[1] != NULL && strchr(ifs, c) != NULL) {
12219                         STACKSTRNUL(p);
12220                         setvar(*ap, stackblock(), 0);
12221                         ap++;
12222                         startword = 1;
12223                         STARTSTACKSTR(p);
12224                 } else {
12225  put:
12226                         STPUTC(c, p);
12227                 }
12228         }
12229 /* end of do {} while: */
12230 #if ENABLE_ASH_READ_NCHARS
12231         while (--nchars);
12232 #else
12233         while (1);
12234 #endif
12235
12236 #if ENABLE_ASH_READ_NCHARS
12237         tcsetattr(fd, TCSANOW, &old_tty);
12238 #endif
12239
12240         STACKSTRNUL(p);
12241         /* Remove trailing blanks */
12242         while ((char *)stackblock() <= --p && strchr(ifs, *p) != NULL)
12243                 *p = '\0';
12244         setvar(*ap, stackblock(), 0);
12245         while (*++ap != NULL)
12246                 setvar(*ap, nullstr, 0);
12247         return status;
12248 }
12249
12250 static int
12251 umaskcmd(int argc ATTRIBUTE_UNUSED, char **argv)
12252 {
12253         static const char permuser[3] ALIGN1 = "ugo";
12254         static const char permmode[3] ALIGN1 = "rwx";
12255         static const short permmask[] ALIGN2 = {
12256                 S_IRUSR, S_IWUSR, S_IXUSR,
12257                 S_IRGRP, S_IWGRP, S_IXGRP,
12258                 S_IROTH, S_IWOTH, S_IXOTH
12259         };
12260
12261         char *ap;
12262         mode_t mask;
12263         int i;
12264         int symbolic_mode = 0;
12265
12266         while (nextopt("S") != '\0') {
12267                 symbolic_mode = 1;
12268         }
12269
12270         INT_OFF;
12271         mask = umask(0);
12272         umask(mask);
12273         INT_ON;
12274
12275         ap = *argptr;
12276         if (ap == NULL) {
12277                 if (symbolic_mode) {
12278                         char buf[18];
12279                         char *p = buf;
12280
12281                         for (i = 0; i < 3; i++) {
12282                                 int j;
12283
12284                                 *p++ = permuser[i];
12285                                 *p++ = '=';
12286                                 for (j = 0; j < 3; j++) {
12287                                         if ((mask & permmask[3 * i + j]) == 0) {
12288                                                 *p++ = permmode[j];
12289                                         }
12290                                 }
12291                                 *p++ = ',';
12292                         }
12293                         *--p = 0;
12294                         puts(buf);
12295                 } else {
12296                         out1fmt("%.4o\n", mask);
12297                 }
12298         } else {
12299                 if (isdigit((unsigned char) *ap)) {
12300                         mask = 0;
12301                         do {
12302                                 if (*ap >= '8' || *ap < '0')
12303                                         ash_msg_and_raise_error(illnum, argv[1]);
12304                                 mask = (mask << 3) + (*ap - '0');
12305                         } while (*++ap != '\0');
12306                         umask(mask);
12307                 } else {
12308                         mask = ~mask & 0777;
12309                         if (!bb_parse_mode(ap, &mask)) {
12310                                 ash_msg_and_raise_error("illegal mode: %s", ap);
12311                         }
12312                         umask(~mask & 0777);
12313                 }
12314         }
12315         return 0;
12316 }
12317
12318 /*
12319  * ulimit builtin
12320  *
12321  * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
12322  * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
12323  * ash by J.T. Conklin.
12324  *
12325  * Public domain.
12326  */
12327
12328 struct limits {
12329         uint8_t cmd;          /* RLIMIT_xxx fit into it */
12330         uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
12331         char    option;
12332 };
12333
12334 static const struct limits limits_tbl[] = {
12335 #ifdef RLIMIT_CPU
12336         { RLIMIT_CPU,        0, 't' },
12337 #endif
12338 #ifdef RLIMIT_FSIZE
12339         { RLIMIT_FSIZE,      9, 'f' },
12340 #endif
12341 #ifdef RLIMIT_DATA
12342         { RLIMIT_DATA,      10, 'd' },
12343 #endif
12344 #ifdef RLIMIT_STACK
12345         { RLIMIT_STACK,     10, 's' },
12346 #endif
12347 #ifdef RLIMIT_CORE
12348         { RLIMIT_CORE,       9, 'c' },
12349 #endif
12350 #ifdef RLIMIT_RSS
12351         { RLIMIT_RSS,       10, 'm' },
12352 #endif
12353 #ifdef RLIMIT_MEMLOCK
12354         { RLIMIT_MEMLOCK,   10, 'l' },
12355 #endif
12356 #ifdef RLIMIT_NPROC
12357         { RLIMIT_NPROC,      0, 'p' },
12358 #endif
12359 #ifdef RLIMIT_NOFILE
12360         { RLIMIT_NOFILE,     0, 'n' },
12361 #endif
12362 #ifdef RLIMIT_AS
12363         { RLIMIT_AS,        10, 'v' },
12364 #endif
12365 #ifdef RLIMIT_LOCKS
12366         { RLIMIT_LOCKS,      0, 'w' },
12367 #endif
12368 };
12369 static const char limits_name[] =
12370 #ifdef RLIMIT_CPU
12371         "time(seconds)" "\0"
12372 #endif
12373 #ifdef RLIMIT_FSIZE
12374         "file(blocks)" "\0"
12375 #endif
12376 #ifdef RLIMIT_DATA
12377         "data(kb)" "\0"
12378 #endif
12379 #ifdef RLIMIT_STACK
12380         "stack(kb)" "\0"
12381 #endif
12382 #ifdef RLIMIT_CORE
12383         "coredump(blocks)" "\0"
12384 #endif
12385 #ifdef RLIMIT_RSS
12386         "memory(kb)" "\0"
12387 #endif
12388 #ifdef RLIMIT_MEMLOCK
12389         "locked memory(kb)" "\0"
12390 #endif
12391 #ifdef RLIMIT_NPROC
12392         "process" "\0"
12393 #endif
12394 #ifdef RLIMIT_NOFILE
12395         "nofiles" "\0"
12396 #endif
12397 #ifdef RLIMIT_AS
12398         "vmemory(kb)" "\0"
12399 #endif
12400 #ifdef RLIMIT_LOCKS
12401         "locks" "\0"
12402 #endif
12403 ;
12404
12405 enum limtype { SOFT = 0x1, HARD = 0x2 };
12406
12407 static void
12408 printlim(enum limtype how, const struct rlimit *limit,
12409                         const struct limits *l)
12410 {
12411         rlim_t val;
12412
12413         val = limit->rlim_max;
12414         if (how & SOFT)
12415                 val = limit->rlim_cur;
12416
12417         if (val == RLIM_INFINITY)
12418                 out1fmt("unlimited\n");
12419         else {
12420                 val >>= l->factor_shift;
12421                 out1fmt("%lld\n", (long long) val);
12422         }
12423 }
12424
12425 static int
12426 ulimitcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
12427 {
12428         int c;
12429         rlim_t val = 0;
12430         enum limtype how = SOFT | HARD;
12431         const struct limits *l;
12432         int set, all = 0;
12433         int optc, what;
12434         struct rlimit limit;
12435
12436         what = 'f';
12437         while ((optc = nextopt("HSa"
12438 #ifdef RLIMIT_CPU
12439                                 "t"
12440 #endif
12441 #ifdef RLIMIT_FSIZE
12442                                 "f"
12443 #endif
12444 #ifdef RLIMIT_DATA
12445                                 "d"
12446 #endif
12447 #ifdef RLIMIT_STACK
12448                                 "s"
12449 #endif
12450 #ifdef RLIMIT_CORE
12451                                 "c"
12452 #endif
12453 #ifdef RLIMIT_RSS
12454                                 "m"
12455 #endif
12456 #ifdef RLIMIT_MEMLOCK
12457                                 "l"
12458 #endif
12459 #ifdef RLIMIT_NPROC
12460                                 "p"
12461 #endif
12462 #ifdef RLIMIT_NOFILE
12463                                 "n"
12464 #endif
12465 #ifdef RLIMIT_AS
12466                                 "v"
12467 #endif
12468 #ifdef RLIMIT_LOCKS
12469                                 "w"
12470 #endif
12471                                         )) != '\0')
12472                 switch (optc) {
12473                 case 'H':
12474                         how = HARD;
12475                         break;
12476                 case 'S':
12477                         how = SOFT;
12478                         break;
12479                 case 'a':
12480                         all = 1;
12481                         break;
12482                 default:
12483                         what = optc;
12484                 }
12485
12486         for (l = limits_tbl; l->option != what; l++)
12487                 continue;
12488
12489         set = *argptr ? 1 : 0;
12490         if (set) {
12491                 char *p = *argptr;
12492
12493                 if (all || argptr[1])
12494                         ash_msg_and_raise_error("too many arguments");
12495                 if (strncmp(p, "unlimited\n", 9) == 0)
12496                         val = RLIM_INFINITY;
12497                 else {
12498                         val = (rlim_t) 0;
12499
12500                         while ((c = *p++) >= '0' && c <= '9') {
12501                                 val = (val * 10) + (long)(c - '0');
12502                                 // val is actually 'unsigned long int' and can't get < 0
12503                                 if (val < (rlim_t) 0)
12504                                         break;
12505                         }
12506                         if (c)
12507                                 ash_msg_and_raise_error("bad number");
12508                         val <<= l->factor_shift;
12509                 }
12510         }
12511         if (all) {
12512                 const char *lname = limits_name;
12513                 for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
12514                         getrlimit(l->cmd, &limit);
12515                         out1fmt("%-20s ", lname);
12516                         lname += strlen(lname) + 1;
12517                         printlim(how, &limit, l);
12518                 }
12519                 return 0;
12520         }
12521
12522         getrlimit(l->cmd, &limit);
12523         if (set) {
12524                 if (how & HARD)
12525                         limit.rlim_max = val;
12526                 if (how & SOFT)
12527                         limit.rlim_cur = val;
12528                 if (setrlimit(l->cmd, &limit) < 0)
12529                         ash_msg_and_raise_error("error setting limit (%m)");
12530         } else {
12531                 printlim(how, &limit, l);
12532         }
12533         return 0;
12534 }
12535
12536
12537 /* ============ Math support */
12538
12539 #if ENABLE_ASH_MATH_SUPPORT
12540
12541 /* Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
12542
12543    Permission is hereby granted, free of charge, to any person obtaining
12544    a copy of this software and associated documentation files (the
12545    "Software"), to deal in the Software without restriction, including
12546    without limitation the rights to use, copy, modify, merge, publish,
12547    distribute, sublicense, and/or sell copies of the Software, and to
12548    permit persons to whom the Software is furnished to do so, subject to
12549    the following conditions:
12550
12551    The above copyright notice and this permission notice shall be
12552    included in all copies or substantial portions of the Software.
12553
12554    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
12555    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12556    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
12557    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
12558    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
12559    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
12560    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12561 */
12562
12563 /* This is my infix parser/evaluator. It is optimized for size, intended
12564  * as a replacement for yacc-based parsers. However, it may well be faster
12565  * than a comparable parser written in yacc. The supported operators are
12566  * listed in #defines below. Parens, order of operations, and error handling
12567  * are supported. This code is thread safe. The exact expression format should
12568  * be that which POSIX specifies for shells. */
12569
12570 /* The code uses a simple two-stack algorithm. See
12571  * http://www.onthenet.com.au/~grahamis/int2008/week02/lect02.html
12572  * for a detailed explanation of the infix-to-postfix algorithm on which
12573  * this is based (this code differs in that it applies operators immediately
12574  * to the stack instead of adding them to a queue to end up with an
12575  * expression). */
12576
12577 /* To use the routine, call it with an expression string and error return
12578  * pointer */
12579
12580 /*
12581  * Aug 24, 2001              Manuel Novoa III
12582  *
12583  * Reduced the generated code size by about 30% (i386) and fixed several bugs.
12584  *
12585  * 1) In arith_apply():
12586  *    a) Cached values of *numptr and &(numptr[-1]).
12587  *    b) Removed redundant test for zero denominator.
12588  *
12589  * 2) In arith():
12590  *    a) Eliminated redundant code for processing operator tokens by moving
12591  *       to a table-based implementation.  Also folded handling of parens
12592  *       into the table.
12593  *    b) Combined all 3 loops which called arith_apply to reduce generated
12594  *       code size at the cost of speed.
12595  *
12596  * 3) The following expressions were treated as valid by the original code:
12597  *       1()  ,    0!  ,    1 ( *3 )   .
12598  *    These bugs have been fixed by internally enclosing the expression in
12599  *    parens and then checking that all binary ops and right parens are
12600  *    preceded by a valid expression (NUM_TOKEN).
12601  *
12602  * Note: It may be desirable to replace Aaron's test for whitespace with
12603  * ctype's isspace() if it is used by another busybox applet or if additional
12604  * whitespace chars should be considered.  Look below the "#include"s for a
12605  * precompiler test.
12606  */
12607
12608 /*
12609  * Aug 26, 2001              Manuel Novoa III
12610  *
12611  * Return 0 for null expressions.  Pointed out by Vladimir Oleynik.
12612  *
12613  * Merge in Aaron's comments previously posted to the busybox list,
12614  * modified slightly to take account of my changes to the code.
12615  *
12616  */
12617
12618 /*
12619  *  (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
12620  *
12621  * - allow access to variable,
12622  *   used recursive find value indirection (c=2*2; a="c"; $((a+=2)) produce 6)
12623  * - realize assign syntax (VAR=expr, +=, *= etc)
12624  * - realize exponentiation (** operator)
12625  * - realize comma separated - expr, expr
12626  * - realise ++expr --expr expr++ expr--
12627  * - realise expr ? expr : expr (but, second expr calculate always)
12628  * - allow hexadecimal and octal numbers
12629  * - was restored loses XOR operator
12630  * - remove one goto label, added three ;-)
12631  * - protect $((num num)) as true zero expr (Manuel`s error)
12632  * - always use special isspace(), see comment from bash ;-)
12633  */
12634
12635 #define arith_isspace(arithval) \
12636         (arithval == ' ' || arithval == '\n' || arithval == '\t')
12637
12638 typedef unsigned char operator;
12639
12640 /* An operator's token id is a bit of a bitfield. The lower 5 bits are the
12641  * precedence, and 3 high bits are an ID unique across operators of that
12642  * precedence. The ID portion is so that multiple operators can have the
12643  * same precedence, ensuring that the leftmost one is evaluated first.
12644  * Consider * and /. */
12645
12646 #define tok_decl(prec,id) (((id)<<5)|(prec))
12647 #define PREC(op) ((op) & 0x1F)
12648
12649 #define TOK_LPAREN tok_decl(0,0)
12650
12651 #define TOK_COMMA tok_decl(1,0)
12652
12653 #define TOK_ASSIGN tok_decl(2,0)
12654 #define TOK_AND_ASSIGN tok_decl(2,1)
12655 #define TOK_OR_ASSIGN tok_decl(2,2)
12656 #define TOK_XOR_ASSIGN tok_decl(2,3)
12657 #define TOK_PLUS_ASSIGN tok_decl(2,4)
12658 #define TOK_MINUS_ASSIGN tok_decl(2,5)
12659 #define TOK_LSHIFT_ASSIGN tok_decl(2,6)
12660 #define TOK_RSHIFT_ASSIGN tok_decl(2,7)
12661
12662 #define TOK_MUL_ASSIGN tok_decl(3,0)
12663 #define TOK_DIV_ASSIGN tok_decl(3,1)
12664 #define TOK_REM_ASSIGN tok_decl(3,2)
12665
12666 /* all assign is right associativity and precedence eq, but (7+3)<<5 > 256 */
12667 #define convert_prec_is_assing(prec) do { if (prec == 3) prec = 2; } while (0)
12668
12669 /* conditional is right associativity too */
12670 #define TOK_CONDITIONAL tok_decl(4,0)
12671 #define TOK_CONDITIONAL_SEP tok_decl(4,1)
12672
12673 #define TOK_OR tok_decl(5,0)
12674
12675 #define TOK_AND tok_decl(6,0)
12676
12677 #define TOK_BOR tok_decl(7,0)
12678
12679 #define TOK_BXOR tok_decl(8,0)
12680
12681 #define TOK_BAND tok_decl(9,0)
12682
12683 #define TOK_EQ tok_decl(10,0)
12684 #define TOK_NE tok_decl(10,1)
12685
12686 #define TOK_LT tok_decl(11,0)
12687 #define TOK_GT tok_decl(11,1)
12688 #define TOK_GE tok_decl(11,2)
12689 #define TOK_LE tok_decl(11,3)
12690
12691 #define TOK_LSHIFT tok_decl(12,0)
12692 #define TOK_RSHIFT tok_decl(12,1)
12693
12694 #define TOK_ADD tok_decl(13,0)
12695 #define TOK_SUB tok_decl(13,1)
12696
12697 #define TOK_MUL tok_decl(14,0)
12698 #define TOK_DIV tok_decl(14,1)
12699 #define TOK_REM tok_decl(14,2)
12700
12701 /* exponent is right associativity */
12702 #define TOK_EXPONENT tok_decl(15,1)
12703
12704 /* For now unary operators. */
12705 #define UNARYPREC 16
12706 #define TOK_BNOT tok_decl(UNARYPREC,0)
12707 #define TOK_NOT tok_decl(UNARYPREC,1)
12708
12709 #define TOK_UMINUS tok_decl(UNARYPREC+1,0)
12710 #define TOK_UPLUS tok_decl(UNARYPREC+1,1)
12711
12712 #define PREC_PRE (UNARYPREC+2)
12713
12714 #define TOK_PRE_INC tok_decl(PREC_PRE, 0)
12715 #define TOK_PRE_DEC tok_decl(PREC_PRE, 1)
12716
12717 #define PREC_POST (UNARYPREC+3)
12718
12719 #define TOK_POST_INC tok_decl(PREC_POST, 0)
12720 #define TOK_POST_DEC tok_decl(PREC_POST, 1)
12721
12722 #define SPEC_PREC (UNARYPREC+4)
12723
12724 #define TOK_NUM tok_decl(SPEC_PREC, 0)
12725 #define TOK_RPAREN tok_decl(SPEC_PREC, 1)
12726
12727 #define NUMPTR (*numstackptr)
12728
12729 static int
12730 tok_have_assign(operator op)
12731 {
12732         operator prec = PREC(op);
12733
12734         convert_prec_is_assing(prec);
12735         return (prec == PREC(TOK_ASSIGN) ||
12736                         prec == PREC_PRE || prec == PREC_POST);
12737 }
12738
12739 static int
12740 is_right_associativity(operator prec)
12741 {
12742         return (prec == PREC(TOK_ASSIGN) || prec == PREC(TOK_EXPONENT)
12743                 || prec == PREC(TOK_CONDITIONAL));
12744 }
12745
12746 typedef struct {
12747         arith_t val;
12748         arith_t contidional_second_val;
12749         char contidional_second_val_initialized;
12750         char *var;      /* if NULL then is regular number,
12751                            else is variable name */
12752 } v_n_t;
12753
12754 typedef struct chk_var_recursive_looped_t {
12755         const char *var;
12756         struct chk_var_recursive_looped_t *next;
12757 } chk_var_recursive_looped_t;
12758
12759 static chk_var_recursive_looped_t *prev_chk_var_recursive;
12760
12761 static int
12762 arith_lookup_val(v_n_t *t)
12763 {
12764         if (t->var) {
12765                 const char * p = lookupvar(t->var);
12766
12767                 if (p) {
12768                         int errcode;
12769
12770                         /* recursive try as expression */
12771                         chk_var_recursive_looped_t *cur;
12772                         chk_var_recursive_looped_t cur_save;
12773
12774                         for (cur = prev_chk_var_recursive; cur; cur = cur->next) {
12775                                 if (strcmp(cur->var, t->var) == 0) {
12776                                         /* expression recursion loop detected */
12777                                         return -5;
12778                                 }
12779                         }
12780                         /* save current lookuped var name */
12781                         cur = prev_chk_var_recursive;
12782                         cur_save.var = t->var;
12783                         cur_save.next = cur;
12784                         prev_chk_var_recursive = &cur_save;
12785
12786                         t->val = arith (p, &errcode);
12787                         /* restore previous ptr after recursiving */
12788                         prev_chk_var_recursive = cur;
12789                         return errcode;
12790                 }
12791                 /* allow undefined var as 0 */
12792                 t->val = 0;
12793         }
12794         return 0;
12795 }
12796
12797 /* "applying" a token means performing it on the top elements on the integer
12798  * stack. For a unary operator it will only change the top element, but a
12799  * binary operator will pop two arguments and push a result */
12800 static int
12801 arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr)
12802 {
12803         v_n_t *numptr_m1;
12804         arith_t numptr_val, rez;
12805         int ret_arith_lookup_val;
12806
12807         /* There is no operator that can work without arguments */
12808         if (NUMPTR == numstack) goto err;
12809         numptr_m1 = NUMPTR - 1;
12810
12811         /* check operand is var with noninteger value */
12812         ret_arith_lookup_val = arith_lookup_val(numptr_m1);
12813         if (ret_arith_lookup_val)
12814                 return ret_arith_lookup_val;
12815
12816         rez = numptr_m1->val;
12817         if (op == TOK_UMINUS)
12818                 rez *= -1;
12819         else if (op == TOK_NOT)
12820                 rez = !rez;
12821         else if (op == TOK_BNOT)
12822                 rez = ~rez;
12823         else if (op == TOK_POST_INC || op == TOK_PRE_INC)
12824                 rez++;
12825         else if (op == TOK_POST_DEC || op == TOK_PRE_DEC)
12826                 rez--;
12827         else if (op != TOK_UPLUS) {
12828                 /* Binary operators */
12829
12830                 /* check and binary operators need two arguments */
12831                 if (numptr_m1 == numstack) goto err;
12832
12833                 /* ... and they pop one */
12834                 --NUMPTR;
12835                 numptr_val = rez;
12836                 if (op == TOK_CONDITIONAL) {
12837                         if (! numptr_m1->contidional_second_val_initialized) {
12838                                 /* protect $((expr1 ? expr2)) without ": expr" */
12839                                 goto err;
12840                         }
12841                         rez = numptr_m1->contidional_second_val;
12842                 } else if (numptr_m1->contidional_second_val_initialized) {
12843                         /* protect $((expr1 : expr2)) without "expr ? " */
12844                         goto err;
12845                 }
12846                 numptr_m1 = NUMPTR - 1;
12847                 if (op != TOK_ASSIGN) {
12848                         /* check operand is var with noninteger value for not '=' */
12849                         ret_arith_lookup_val = arith_lookup_val(numptr_m1);
12850                         if (ret_arith_lookup_val)
12851                                 return ret_arith_lookup_val;
12852                 }
12853                 if (op == TOK_CONDITIONAL) {
12854                         numptr_m1->contidional_second_val = rez;
12855                 }
12856                 rez = numptr_m1->val;
12857                 if (op == TOK_BOR || op == TOK_OR_ASSIGN)
12858                         rez |= numptr_val;
12859                 else if (op == TOK_OR)
12860                         rez = numptr_val || rez;
12861                 else if (op == TOK_BAND || op == TOK_AND_ASSIGN)
12862                         rez &= numptr_val;
12863                 else if (op == TOK_BXOR || op == TOK_XOR_ASSIGN)
12864                         rez ^= numptr_val;
12865                 else if (op == TOK_AND)
12866                         rez = rez && numptr_val;
12867                 else if (op == TOK_EQ)
12868                         rez = (rez == numptr_val);
12869                 else if (op == TOK_NE)
12870                         rez = (rez != numptr_val);
12871                 else if (op == TOK_GE)
12872                         rez = (rez >= numptr_val);
12873                 else if (op == TOK_RSHIFT || op == TOK_RSHIFT_ASSIGN)
12874                         rez >>= numptr_val;
12875                 else if (op == TOK_LSHIFT || op == TOK_LSHIFT_ASSIGN)
12876                         rez <<= numptr_val;
12877                 else if (op == TOK_GT)
12878                         rez = (rez > numptr_val);
12879                 else if (op == TOK_LT)
12880                         rez = (rez < numptr_val);
12881                 else if (op == TOK_LE)
12882                         rez = (rez <= numptr_val);
12883                 else if (op == TOK_MUL || op == TOK_MUL_ASSIGN)
12884                         rez *= numptr_val;
12885                 else if (op == TOK_ADD || op == TOK_PLUS_ASSIGN)
12886                         rez += numptr_val;
12887                 else if (op == TOK_SUB || op == TOK_MINUS_ASSIGN)
12888                         rez -= numptr_val;
12889                 else if (op == TOK_ASSIGN || op == TOK_COMMA)
12890                         rez = numptr_val;
12891                 else if (op == TOK_CONDITIONAL_SEP) {
12892                         if (numptr_m1 == numstack) {
12893                                 /* protect $((expr : expr)) without "expr ? " */
12894                                 goto err;
12895                         }
12896                         numptr_m1->contidional_second_val_initialized = op;
12897                         numptr_m1->contidional_second_val = numptr_val;
12898                 } else if (op == TOK_CONDITIONAL) {
12899                         rez = rez ?
12900                                 numptr_val : numptr_m1->contidional_second_val;
12901                 } else if (op == TOK_EXPONENT) {
12902                         if (numptr_val < 0)
12903                                 return -3;      /* exponent less than 0 */
12904                         else {
12905                                 arith_t c = 1;
12906
12907                                 if (numptr_val)
12908                                         while (numptr_val--)
12909                                                 c *= rez;
12910                                 rez = c;
12911                         }
12912                 } else if (numptr_val==0)          /* zero divisor check */
12913                         return -2;
12914                 else if (op == TOK_DIV || op == TOK_DIV_ASSIGN)
12915                         rez /= numptr_val;
12916                 else if (op == TOK_REM || op == TOK_REM_ASSIGN)
12917                         rez %= numptr_val;
12918         }
12919         if (tok_have_assign(op)) {
12920                 char buf[sizeof(arith_t_type)*3 + 2];
12921
12922                 if (numptr_m1->var == NULL) {
12923                         /* Hmm, 1=2 ? */
12924                         goto err;
12925                 }
12926                 /* save to shell variable */
12927 #if ENABLE_ASH_MATH_SUPPORT_64
12928                 snprintf(buf, sizeof(buf), "%lld", (arith_t_type) rez);
12929 #else
12930                 snprintf(buf, sizeof(buf), "%ld", (arith_t_type) rez);
12931 #endif
12932                 setvar(numptr_m1->var, buf, 0);
12933                 /* after saving, make previous value for v++ or v-- */
12934                 if (op == TOK_POST_INC)
12935                         rez--;
12936                 else if (op == TOK_POST_DEC)
12937                         rez++;
12938         }
12939         numptr_m1->val = rez;
12940         /* protect geting var value, is number now */
12941         numptr_m1->var = NULL;
12942         return 0;
12943  err:
12944         return -1;
12945 }
12946
12947 /* longest must be first */
12948 static const char op_tokens[] ALIGN1 = {
12949         '<','<','=',0, TOK_LSHIFT_ASSIGN,
12950         '>','>','=',0, TOK_RSHIFT_ASSIGN,
12951         '<','<',    0, TOK_LSHIFT,
12952         '>','>',    0, TOK_RSHIFT,
12953         '|','|',    0, TOK_OR,
12954         '&','&',    0, TOK_AND,
12955         '!','=',    0, TOK_NE,
12956         '<','=',    0, TOK_LE,
12957         '>','=',    0, TOK_GE,
12958         '=','=',    0, TOK_EQ,
12959         '|','=',    0, TOK_OR_ASSIGN,
12960         '&','=',    0, TOK_AND_ASSIGN,
12961         '*','=',    0, TOK_MUL_ASSIGN,
12962         '/','=',    0, TOK_DIV_ASSIGN,
12963         '%','=',    0, TOK_REM_ASSIGN,
12964         '+','=',    0, TOK_PLUS_ASSIGN,
12965         '-','=',    0, TOK_MINUS_ASSIGN,
12966         '-','-',    0, TOK_POST_DEC,
12967         '^','=',    0, TOK_XOR_ASSIGN,
12968         '+','+',    0, TOK_POST_INC,
12969         '*','*',    0, TOK_EXPONENT,
12970         '!',        0, TOK_NOT,
12971         '<',        0, TOK_LT,
12972         '>',        0, TOK_GT,
12973         '=',        0, TOK_ASSIGN,
12974         '|',        0, TOK_BOR,
12975         '&',        0, TOK_BAND,
12976         '*',        0, TOK_MUL,
12977         '/',        0, TOK_DIV,
12978         '%',        0, TOK_REM,
12979         '+',        0, TOK_ADD,
12980         '-',        0, TOK_SUB,
12981         '^',        0, TOK_BXOR,
12982         /* uniq */
12983         '~',        0, TOK_BNOT,
12984         ',',        0, TOK_COMMA,
12985         '?',        0, TOK_CONDITIONAL,
12986         ':',        0, TOK_CONDITIONAL_SEP,
12987         ')',        0, TOK_RPAREN,
12988         '(',        0, TOK_LPAREN,
12989         0
12990 };
12991 /* ptr to ")" */
12992 #define endexpression (&op_tokens[sizeof(op_tokens)-7])
12993
12994 static arith_t
12995 arith(const char *expr, int *perrcode)
12996 {
12997         char arithval; /* Current character under analysis */
12998         operator lasttok, op;
12999         operator prec;
13000         operator *stack, *stackptr;
13001         const char *p = endexpression;
13002         int errcode;
13003         v_n_t *numstack, *numstackptr;
13004         unsigned datasizes = strlen(expr) + 2;
13005
13006         /* Stack of integers */
13007         /* The proof that there can be no more than strlen(startbuf)/2+1 integers
13008          * in any given correct or incorrect expression is left as an exercise to
13009          * the reader. */
13010         numstackptr = numstack = alloca((datasizes / 2) * sizeof(numstack[0]));
13011         /* Stack of operator tokens */
13012         stackptr = stack = alloca(datasizes * sizeof(stack[0]));
13013
13014         *stackptr++ = lasttok = TOK_LPAREN;     /* start off with a left paren */
13015         *perrcode = errcode = 0;
13016
13017         while (1) {
13018                 arithval = *expr;
13019                 if (arithval == 0) {
13020                         if (p == endexpression) {
13021                                 /* Null expression. */
13022                                 return 0;
13023                         }
13024
13025                         /* This is only reached after all tokens have been extracted from the
13026                          * input stream. If there are still tokens on the operator stack, they
13027                          * are to be applied in order. At the end, there should be a final
13028                          * result on the integer stack */
13029
13030                         if (expr != endexpression + 1) {
13031                                 /* If we haven't done so already, */
13032                                 /* append a closing right paren */
13033                                 expr = endexpression;
13034                                 /* and let the loop process it. */
13035                                 continue;
13036                         }
13037                         /* At this point, we're done with the expression. */
13038                         if (numstackptr != numstack+1) {
13039                                 /* ... but if there isn't, it's bad */
13040  err:
13041                                 *perrcode = -1;
13042                                 return *perrcode;
13043                         }
13044                         if (numstack->var) {
13045                                 /* expression is $((var)) only, lookup now */
13046                                 errcode = arith_lookup_val(numstack);
13047                         }
13048  ret:
13049                         *perrcode = errcode;
13050                         return numstack->val;
13051                 }
13052
13053                 /* Continue processing the expression. */
13054                 if (arith_isspace(arithval)) {
13055                         /* Skip whitespace */
13056                         goto prologue;
13057                 }
13058                 p = endofname(expr);
13059                 if (p != expr) {
13060                         size_t var_name_size = (p-expr) + 1;  /* trailing zero */
13061
13062                         numstackptr->var = alloca(var_name_size);
13063                         safe_strncpy(numstackptr->var, expr, var_name_size);
13064                         expr = p;
13065  num:
13066                         numstackptr->contidional_second_val_initialized = 0;
13067                         numstackptr++;
13068                         lasttok = TOK_NUM;
13069                         continue;
13070                 }
13071                 if (isdigit(arithval)) {
13072                         numstackptr->var = NULL;
13073 #if ENABLE_ASH_MATH_SUPPORT_64
13074                         numstackptr->val = strtoll(expr, (char **) &expr, 0);
13075 #else
13076                         numstackptr->val = strtol(expr, (char **) &expr, 0);
13077 #endif
13078                         goto num;
13079                 }
13080                 for (p = op_tokens; ; p++) {
13081                         const char *o;
13082
13083                         if (*p == 0) {
13084                                 /* strange operator not found */
13085                                 goto err;
13086                         }
13087                         for (o = expr; *p && *o == *p; p++)
13088                                 o++;
13089                         if (! *p) {
13090                                 /* found */
13091                                 expr = o - 1;
13092                                 break;
13093                         }
13094                         /* skip tail uncompared token */
13095                         while (*p)
13096                                 p++;
13097                         /* skip zero delim */
13098                         p++;
13099                 }
13100                 op = p[1];
13101
13102                 /* post grammar: a++ reduce to num */
13103                 if (lasttok == TOK_POST_INC || lasttok == TOK_POST_DEC)
13104                         lasttok = TOK_NUM;
13105
13106                 /* Plus and minus are binary (not unary) _only_ if the last
13107                  * token was as number, or a right paren (which pretends to be
13108                  * a number, since it evaluates to one). Think about it.
13109                  * It makes sense. */
13110                 if (lasttok != TOK_NUM) {
13111                         switch (op) {
13112                         case TOK_ADD:
13113                                 op = TOK_UPLUS;
13114                                 break;
13115                         case TOK_SUB:
13116                                 op = TOK_UMINUS;
13117                                 break;
13118                         case TOK_POST_INC:
13119                                 op = TOK_PRE_INC;
13120                                 break;
13121                         case TOK_POST_DEC:
13122                                 op = TOK_PRE_DEC;
13123                                 break;
13124                         }
13125                 }
13126                 /* We don't want a unary operator to cause recursive descent on the
13127                  * stack, because there can be many in a row and it could cause an
13128                  * operator to be evaluated before its argument is pushed onto the
13129                  * integer stack. */
13130                 /* But for binary operators, "apply" everything on the operator
13131                  * stack until we find an operator with a lesser priority than the
13132                  * one we have just extracted. */
13133                 /* Left paren is given the lowest priority so it will never be
13134                  * "applied" in this way.
13135                  * if associativity is right and priority eq, applied also skip
13136                  */
13137                 prec = PREC(op);
13138                 if ((prec > 0 && prec < UNARYPREC) || prec == SPEC_PREC) {
13139                         /* not left paren or unary */
13140                         if (lasttok != TOK_NUM) {
13141                                 /* binary op must be preceded by a num */
13142                                 goto err;
13143                         }
13144                         while (stackptr != stack) {
13145                                 if (op == TOK_RPAREN) {
13146                                         /* The algorithm employed here is simple: while we don't
13147                                          * hit an open paren nor the bottom of the stack, pop
13148                                          * tokens and apply them */
13149                                         if (stackptr[-1] == TOK_LPAREN) {
13150                                                 --stackptr;
13151                                                 /* Any operator directly after a */
13152                                                 lasttok = TOK_NUM;
13153                                                 /* close paren should consider itself binary */
13154                                                 goto prologue;
13155                                         }
13156                                 } else {
13157                                         operator prev_prec = PREC(stackptr[-1]);
13158
13159                                         convert_prec_is_assing(prec);
13160                                         convert_prec_is_assing(prev_prec);
13161                                         if (prev_prec < prec)
13162                                                 break;
13163                                         /* check right assoc */
13164                                         if (prev_prec == prec && is_right_associativity(prec))
13165                                                 break;
13166                                 }
13167                                 errcode = arith_apply(*--stackptr, numstack, &numstackptr);
13168                                 if (errcode) goto ret;
13169                         }
13170                         if (op == TOK_RPAREN) {
13171                                 goto err;
13172                         }
13173                 }
13174
13175                 /* Push this operator to the stack and remember it. */
13176                 *stackptr++ = lasttok = op;
13177  prologue:
13178                 ++expr;
13179         } /* while */
13180 }
13181 #endif /* ASH_MATH_SUPPORT */
13182
13183
13184 /* ============ main() and helpers */
13185
13186 /*
13187  * Called to exit the shell.
13188  */
13189 static void exitshell(void) ATTRIBUTE_NORETURN;
13190 static void
13191 exitshell(void)
13192 {
13193         struct jmploc loc;
13194         char *p;
13195         int status;
13196
13197         status = exitstatus;
13198         TRACE(("pid %d, exitshell(%d)\n", getpid(), status));
13199         if (setjmp(loc.loc)) {
13200                 if (exception == EXEXIT)
13201 /* dash bug: it just does _exit(exitstatus) here
13202  * but we have to do setjobctl(0) first!
13203  * (bug is still not fixed in dash-0.5.3 - if you run dash
13204  * under Midnight Commander, on exit from dash MC is backgrounded) */
13205                         status = exitstatus;
13206                 goto out;
13207         }
13208         exception_handler = &loc;
13209         p = trap[0];
13210         if (p) {
13211                 trap[0] = NULL;
13212                 evalstring(p, 0);
13213         }
13214         flush_stdout_stderr();
13215  out:
13216         setjobctl(0);
13217         _exit(status);
13218         /* NOTREACHED */
13219 }
13220
13221 static void
13222 init(void)
13223 {
13224         /* from input.c: */
13225         basepf.nextc = basepf.buf = basebuf;
13226
13227         /* from trap.c: */
13228         signal(SIGCHLD, SIG_DFL);
13229
13230         /* from var.c: */
13231         {
13232                 char **envp;
13233                 char ppid[sizeof(int)*3 + 1];
13234                 const char *p;
13235                 struct stat st1, st2;
13236
13237                 initvar();
13238                 for (envp = environ; envp && *envp; envp++) {
13239                         if (strchr(*envp, '=')) {
13240                                 setvareq(*envp, VEXPORT|VTEXTFIXED);
13241                         }
13242                 }
13243
13244                 snprintf(ppid, sizeof(ppid), "%u", (unsigned) getppid());
13245                 setvar("PPID", ppid, 0);
13246
13247                 p = lookupvar("PWD");
13248                 if (p)
13249                         if (*p != '/' || stat(p, &st1) || stat(".", &st2)
13250                          || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
13251                                 p = '\0';
13252                 setpwd(p, 0);
13253         }
13254 }
13255
13256 /*
13257  * Process the shell command line arguments.
13258  */
13259 static void
13260 procargs(char **argv)
13261 {
13262         int i;
13263         const char *xminusc;
13264         char **xargv;
13265
13266         xargv = argv;
13267         arg0 = xargv[0];
13268         /* if (xargv[0]) - mmm, this is always true! */
13269                 xargv++;
13270         for (i = 0; i < NOPTS; i++)
13271                 optlist[i] = 2;
13272         argptr = xargv;
13273         if (options(1)) {
13274                 /* it already printed err message */
13275                 raise_exception(EXERROR);
13276         }
13277         xargv = argptr;
13278         xminusc = minusc;
13279         if (*xargv == NULL) {
13280                 if (xminusc)
13281                         ash_msg_and_raise_error(bb_msg_requires_arg, "-c");
13282                 sflag = 1;
13283         }
13284         if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
13285                 iflag = 1;
13286         if (mflag == 2)
13287                 mflag = iflag;
13288         for (i = 0; i < NOPTS; i++)
13289                 if (optlist[i] == 2)
13290                         optlist[i] = 0;
13291 #if DEBUG == 2
13292         debug = 1;
13293 #endif
13294         /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
13295         if (xminusc) {
13296                 minusc = *xargv++;
13297                 if (*xargv)
13298                         goto setarg0;
13299         } else if (!sflag) {
13300                 setinputfile(*xargv, 0);
13301  setarg0:
13302                 arg0 = *xargv++;
13303                 commandname = arg0;
13304         }
13305
13306         shellparam.p = xargv;
13307 #if ENABLE_ASH_GETOPTS
13308         shellparam.optind = 1;
13309         shellparam.optoff = -1;
13310 #endif
13311         /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */
13312         while (*xargv) {
13313                 shellparam.nparam++;
13314                 xargv++;
13315         }
13316         optschanged();
13317 }
13318
13319 /*
13320  * Read /etc/profile or .profile.
13321  */
13322 static void
13323 read_profile(const char *name)
13324 {
13325         int skip;
13326
13327         if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0)
13328                 return;
13329         skip = cmdloop(0);
13330         popfile();
13331         if (skip)
13332                 exitshell();
13333 }
13334
13335 /*
13336  * This routine is called when an error or an interrupt occurs in an
13337  * interactive shell and control is returned to the main command loop.
13338  */
13339 static void
13340 reset(void)
13341 {
13342         /* from eval.c: */
13343         evalskip = 0;
13344         loopnest = 0;
13345         /* from input.c: */
13346         parselleft = parsenleft = 0;      /* clear input buffer */
13347         popallfiles();
13348         /* from parser.c: */
13349         tokpushback = 0;
13350         checkkwd = 0;
13351         /* from redir.c: */
13352         clearredir(0);
13353 }
13354
13355 #if PROFILE
13356 static short profile_buf[16384];
13357 extern int etext();
13358 #endif
13359
13360 /*
13361  * Main routine.  We initialize things, parse the arguments, execute
13362  * profiles if we're a login shell, and then call cmdloop to execute
13363  * commands.  The setjmp call sets up the location to jump to when an
13364  * exception occurs.  When an exception occurs the variable "state"
13365  * is used to figure out how far we had gotten.
13366  */
13367 int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
13368 int ash_main(int argc ATTRIBUTE_UNUSED, char **argv)
13369 {
13370         char *shinit;
13371         volatile int state;
13372         struct jmploc jmploc;
13373         struct stackmark smark;
13374
13375         /* Initialize global data */
13376         INIT_G_misc();
13377         INIT_G_memstack();
13378         INIT_G_var();
13379 #if ENABLE_ASH_ALIAS
13380         INIT_G_alias();
13381 #endif
13382         INIT_G_cmdtable();
13383
13384 #if PROFILE
13385         monitor(4, etext, profile_buf, sizeof(profile_buf), 50);
13386 #endif
13387
13388 #if ENABLE_FEATURE_EDITING
13389         line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP);
13390 #endif
13391         state = 0;
13392         if (setjmp(jmploc.loc)) {
13393                 int e;
13394                 int s;
13395
13396                 reset();
13397
13398                 e = exception;
13399                 if (e == EXERROR)
13400                         exitstatus = 2;
13401                 s = state;
13402                 if (e == EXEXIT || s == 0 || iflag == 0 || shlvl)
13403                         exitshell();
13404
13405                 if (e == EXINT) {
13406                         outcslow('\n', stderr);
13407                 }
13408                 popstackmark(&smark);
13409                 FORCE_INT_ON; /* enable interrupts */
13410                 if (s == 1)
13411                         goto state1;
13412                 if (s == 2)
13413                         goto state2;
13414                 if (s == 3)
13415                         goto state3;
13416                 goto state4;
13417         }
13418         exception_handler = &jmploc;
13419 #if DEBUG
13420         opentrace();
13421         trace_puts("Shell args: ");
13422         trace_puts_args(argv);
13423 #endif
13424         rootpid = getpid();
13425
13426 #if ENABLE_ASH_RANDOM_SUPPORT
13427         rseed = rootpid + time(NULL);
13428 #endif
13429         init();
13430         setstackmark(&smark);
13431         procargs(argv);
13432
13433 #if ENABLE_FEATURE_EDITING_SAVEHISTORY
13434         if (iflag) {
13435                 const char *hp = lookupvar("HISTFILE");
13436
13437                 if (hp == NULL) {
13438                         hp = lookupvar("HOME");
13439                         if (hp != NULL) {
13440                                 char *defhp = concat_path_file(hp, ".ash_history");
13441                                 setvar("HISTFILE", defhp, 0);
13442                                 free(defhp);
13443                         }
13444                 }
13445         }
13446 #endif
13447         if (argv[0] && argv[0][0] == '-')
13448                 isloginsh = 1;
13449         if (isloginsh) {
13450                 state = 1;
13451                 read_profile("/etc/profile");
13452  state1:
13453                 state = 2;
13454                 read_profile(".profile");
13455         }
13456  state2:
13457         state = 3;
13458         if (
13459 #ifndef linux
13460          getuid() == geteuid() && getgid() == getegid() &&
13461 #endif
13462          iflag
13463         ) {
13464                 shinit = lookupvar("ENV");
13465                 if (shinit != NULL && *shinit != '\0') {
13466                         read_profile(shinit);
13467                 }
13468         }
13469  state3:
13470         state = 4;
13471         if (minusc)
13472                 evalstring(minusc, 0);
13473
13474         if (sflag || minusc == NULL) {
13475 #if ENABLE_FEATURE_EDITING_SAVEHISTORY
13476                 if (iflag) {
13477                         const char *hp = lookupvar("HISTFILE");
13478
13479                         if (hp != NULL)
13480                                 line_input_state->hist_file = hp;
13481                 }
13482 #endif
13483  state4: /* XXX ??? - why isn't this before the "if" statement */
13484                 cmdloop(1);
13485         }
13486 #if PROFILE
13487         monitor(0);
13488 #endif
13489 #ifdef GPROF
13490         {
13491                 extern void _mcleanup(void);
13492                 _mcleanup();
13493         }
13494 #endif
13495         exitshell();
13496         /* NOTREACHED */
13497 }
13498
13499 #if DEBUG
13500 const char *applet_name = "debug stuff usage";
13501 int main(int argc, char **argv)
13502 {
13503         return ash_main(argc, argv);
13504 }
13505 #endif
13506
13507
13508 /*-
13509  * Copyright (c) 1989, 1991, 1993, 1994
13510  *      The Regents of the University of California.  All rights reserved.
13511  *
13512  * This code is derived from software contributed to Berkeley by
13513  * Kenneth Almquist.
13514  *
13515  * Redistribution and use in source and binary forms, with or without
13516  * modification, are permitted provided that the following conditions
13517  * are met:
13518  * 1. Redistributions of source code must retain the above copyright
13519  *    notice, this list of conditions and the following disclaimer.
13520  * 2. Redistributions in binary form must reproduce the above copyright
13521  *    notice, this list of conditions and the following disclaimer in the
13522  *    documentation and/or other materials provided with the distribution.
13523  * 3. Neither the name of the University nor the names of its contributors
13524  *    may be used to endorse or promote products derived from this software
13525  *    without specific prior written permission.
13526  *
13527  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
13528  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13529  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13530  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
13531  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
13532  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
13533  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13534  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
13535  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
13536  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
13537  * SUCH DAMAGE.
13538  */