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