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